]> git.sesse.net Git - ffmpeg/commitdiff
Merge remote-tracking branch 'qatar/master'
authorMichael Niedermayer <michaelni@gmx.at>
Sat, 19 Nov 2011 00:55:55 +0000 (01:55 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Sat, 19 Nov 2011 01:00:06 +0000 (02:00 +0100)
* qatar/master: (22 commits)
  configure: add check for w32threads to enable it automatically
  rtmp: do not hardcode invoke numbers
  cinepack: return non-generic errors
  fate-lavf-ts: use -mpegts_transport_stream_id option.
  Add an APIchanges entry and a minor bump for avio changes.
  avio: Mark the old interrupt callback mechanism as deprecated
  avplay: Set the new interrupt callback
  avconv: Set new interrupt callbacks for all AVFormatContexts, use avio_open2() everywhere
  cinepak: remove redundant coordinate checks
  cinepak: check strip_size
  cinepak, simplify, use AV_RB24()
  cinepak: simplify, use FFMIN()
  cinepak: Fix division by zero, ask for sample if encoded_buf_size is 0
  applehttp: Fix seeking in streams not starting at DTS=0
  http: Don't use the normal http proxy mechanism for https
  tls: Handle connection via a http proxy
  http: Reorder two code blocks
  http: Add a new protocol for opening connections via http proxies
  http: Split out the non-chunked buffer reading part from http_read
  segafilm: add support for raw videos
  ...

Conflicts:
avconv.c
configure
doc/APIchanges
libavcodec/cinepak.c
libavformat/applehttp.c
libavformat/version.h
tests/lavf-regression.sh
tests/ref/lavf/ts

Merged-by: Michael Niedermayer <michaelni@gmx.at>
19 files changed:
1  2 
avconv.c
configure
doc/APIchanges
ffmpeg.c
ffplay.c
libavcodec/cinepak.c
libavformat/allformats.c
libavformat/applehttp.c
libavformat/avio.c
libavformat/avio.h
libavformat/http.c
libavformat/rtmpproto.c
libavformat/rtpdec.c
libavformat/rtpdec.h
libavformat/rtspdec.c
libavformat/segafilm.c
libavformat/version.h
tests/lavf-regression.sh
tests/ref/lavf/ts

diff --combined avconv.c
index 76d8def806a2cd3b3ff8feda00d0ff5657aee33e,ef41245726dd3a8075881301b60a07266cd4d20c..a019ffaf6f79a8c5caf8027d188c5f45f7a2c586
+++ b/avconv.c
@@@ -1,21 -1,21 +1,21 @@@
  /*
 - * avconv main
 - * Copyright (c) 2000-2011 The libav developers.
 + * ffmpeg main
 + * Copyright (c) 2000-2003 Fabrice Bellard
   *
 - * 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 "libavutil/libm.h"
  #include "libavformat/os_support.h"
  
 +#include "libavformat/ffm.h" // not public API
 +
  #if CONFIG_AVFILTER
 +# include "libavfilter/avcodec.h"
  # include "libavfilter/avfilter.h"
  # include "libavfilter/avfiltergraph.h"
 +# include "libavfilter/buffersink.h"
  # include "libavfilter/vsrc_buffer.h"
  #endif
  
  #include <sys/select.h>
  #endif
  
 +#if HAVE_TERMIOS_H
 +#include <fcntl.h>
 +#include <sys/ioctl.h>
 +#include <sys/time.h>
 +#include <termios.h>
 +#elif HAVE_KBHIT
 +#include <conio.h>
 +#endif
  #include <time.h>
  
  #include "cmdutils.h"
@@@ -109,9 -97,6 +109,9 @@@ typedef struct MetadataMap 
  
  static const OptionDef options[];
  
 +#define MAX_STREAMS 1024    /* arbitrary sanity check value */
 +
 +static int frame_bits_per_raw_sample = 0;
  static int video_discard = 0;
  static int same_quant = 0;
  static int do_deinterlace = 0;
@@@ -123,12 -108,12 +123,12 @@@ static int do_benchmark = 0
  static int do_hex_dump = 0;
  static int do_pkt_dump = 0;
  static int do_pass = 0;
 -static char *pass_logfilename_prefix = NULL;
 +static const char *pass_logfilename_prefix;
  static int video_sync_method= -1;
  static int audio_sync_method= 0;
  static float audio_drift_threshold= 0.1;
  static int copy_ts= 0;
 -static int copy_tb;
 +static int copy_tb= 0;
  static int opt_shortest = 0;
  static char *vstats_filename;
  static FILE *vstats_file;
@@@ -137,8 -122,6 +137,8 @@@ static int audio_volume = 256
  
  static int exit_on_error = 0;
  static int using_stdin = 0;
 +static int run_as_daemon  = 0;
 +static int q_pressed = 0;
  static int64_t video_size = 0;
  static int64_t audio_size = 0;
  static int64_t extra_size = 0;
@@@ -169,6 -152,7 +169,6 @@@ typedef struct InputStream 
      int64_t       next_pts;  /* synthetic pts for cases where pkt.pts
                                  is not defined */
      int64_t       pts;       /* current pts */
 -    PtsCorrectionContext pts_ctx;
      double ts_scale;
      int is_start;            /* is 1 at the start and after a discontinuity */
      int showed_multi_packet_warning;
@@@ -204,7 -188,7 +204,7 @@@ typedef struct OutputStream 
  
      /* video only */
      int video_resample;
 -    AVFrame pict_tmp;      /* temporary image for resampling */
 +    AVFrame resample_frame;              /* temporary frame for image resampling */
      struct SwsContext *img_resample_ctx; /* for image resampling */
      int resample_height;
      int resample_width;
      int copy_initial_nonkeyframes;
  } OutputStream;
  
 +#if HAVE_TERMIOS_H
 +
 +/* init terminal so that we can grab keys */
 +static struct termios oldtty;
 +#endif
  
  typedef struct OutputFile {
      AVFormatContext *ctx;
@@@ -421,8 -400,7 +421,8 @@@ static int configure_video_filters(Inpu
      /** filter graph containing all filters including input & output */
      AVCodecContext *codec = ost->st->codec;
      AVCodecContext *icodec = ist->st->codec;
 -    FFSinkContext ffsink_ctx = { .pix_fmt = codec->pix_fmt };
 +    enum PixelFormat pix_fmts[] = { codec->pix_fmt, PIX_FMT_NONE };
 +    AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
      AVRational sample_aspect_ratio;
      char args[255];
      int ret;
                                         "src", args, NULL, ost->graph);
      if (ret < 0)
          return ret;
 -    ret = avfilter_graph_create_filter(&ost->output_video_filter, &ffsink,
 -                                       "out", NULL, &ffsink_ctx, ost->graph);
 +#if FF_API_OLD_VSINK_API
 +    ret = avfilter_graph_create_filter(&ost->output_video_filter, avfilter_get_by_name("buffersink"),
 +                                       "out", NULL, pix_fmts, ost->graph);
 +#else
 +    buffersink_params->pixel_fmts = pix_fmts;
 +    ret = avfilter_graph_create_filter(&ost->output_video_filter, avfilter_get_by_name("buffersink"),
 +                                       "out", NULL, buffersink_params, ost->graph);
 +#endif
 +    av_freep(&buffersink_params);
      if (ret < 0)
          return ret;
      last_filter = ost->input_video_filter;
      ost->graph->scale_sws_opts = av_strdup(args);
  
      if (ost->avfilter) {
 -        AVFilterInOut *outputs = av_malloc(sizeof(AVFilterInOut));
 -        AVFilterInOut *inputs  = av_malloc(sizeof(AVFilterInOut));
 +        AVFilterInOut *outputs = avfilter_inout_alloc();
 +        AVFilterInOut *inputs  = avfilter_inout_alloc();
  
          outputs->name    = av_strdup("in");
          outputs->filter_ctx = last_filter;
          inputs->pad_idx = 0;
          inputs->next    = NULL;
  
 -        if ((ret = avfilter_graph_parse(ost->graph, ost->avfilter, inputs, outputs, NULL)) < 0)
 +        if ((ret = avfilter_graph_parse(ost->graph, ost->avfilter, &inputs, &outputs, NULL)) < 0)
              return ret;
          av_freep(&ost->avfilter);
      } else {
  
  static void term_exit(void)
  {
 -    av_log(NULL, AV_LOG_QUIET, "");
 +    av_log(NULL, AV_LOG_QUIET, "%s", "");
 +#if HAVE_TERMIOS_H
 +    if(!run_as_daemon)
 +        tcsetattr (0, TCSANOW, &oldtty);
 +#endif
  }
  
  static volatile int received_sigterm = 0;
 -static volatile int received_nb_signals = 0;
  
  static void
  sigterm_handler(int sig)
  {
      received_sigterm = sig;
 -    received_nb_signals++;
 +    q_pressed++;
      term_exit();
  }
  
  static void term_init(void)
  {
 +#if HAVE_TERMIOS_H
 +    if(!run_as_daemon){
 +    struct termios tty;
 +
 +    tcgetattr (0, &tty);
 +    oldtty = tty;
 +    atexit(term_exit);
 +
 +    tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
 +                          |INLCR|IGNCR|ICRNL|IXON);
 +    tty.c_oflag |= OPOST;
 +    tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
 +    tty.c_cflag &= ~(CSIZE|PARENB);
 +    tty.c_cflag |= CS8;
 +    tty.c_cc[VMIN] = 1;
 +    tty.c_cc[VTIME] = 0;
 +
 +    tcsetattr (0, TCSANOW, &tty);
 +    signal(SIGQUIT, sigterm_handler); /* Quit (POSIX).  */
 +    }
 +#endif
 +
      signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).  */
      signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
  #ifdef SIGXCPU
  #endif
  }
  
- static int decode_interrupt_cb(void)
 +/* read a key without blocking */
 +static int read_key(void)
 +{
 +#if HAVE_TERMIOS_H
 +    int n = 1;
 +    unsigned char ch;
 +    struct timeval tv;
 +    fd_set rfds;
 +
 +    if(run_as_daemon)
 +        return -1;
 +
 +    FD_ZERO(&rfds);
 +    FD_SET(0, &rfds);
 +    tv.tv_sec = 0;
 +    tv.tv_usec = 0;
 +    n = select(1, &rfds, NULL, NULL, &tv);
 +    if (n > 0) {
 +        n = read(0, &ch, 1);
 +        if (n == 1)
 +            return ch;
 +
 +        return n;
 +    }
 +#elif HAVE_KBHIT
 +    if(kbhit())
 +        return(getch());
 +#endif
 +    return -1;
 +}
 +
+ static int decode_interrupt_cb(void *ctx)
  {
 -    return received_nb_signals > 1;
 +    q_pressed += read_key() == 'q';
 +    return q_pressed > 1;
  }
  
+ static const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
  void exit_program(int ret)
  {
      int i;
@@@ -676,9 -592,6 +678,9 @@@ static void choose_sample_fmt(AVStream 
                  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, "Convertion 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),
      }
  }
  
 -/**
 - * Update the requested input sample format based on the output sample format.
 - * This is currently only used to request float output from decoders which
 - * support multiple sample formats, one of which is AV_SAMPLE_FMT_FLT.
 - * Ideally this will be removed in the future when decoders do not do format
 - * conversion and only output in their native format.
 - */
 -static void update_sample_fmt(AVCodecContext *dec, AVCodec *dec_codec,
 -                              AVCodecContext *enc)
 -{
 -    /* if sample formats match or a decoder sample format has already been
 -       requested, just return */
 -    if (enc->sample_fmt == dec->sample_fmt ||
 -        dec->request_sample_fmt > AV_SAMPLE_FMT_NONE)
 -        return;
 -
 -    /* if decoder supports more than one output format */
 -    if (dec_codec && dec_codec->sample_fmts &&
 -        dec_codec->sample_fmts[0] != AV_SAMPLE_FMT_NONE &&
 -        dec_codec->sample_fmts[1] != AV_SAMPLE_FMT_NONE) {
 -        const enum AVSampleFormat *p;
 -        int min_dec = -1, min_inc = -1;
 -
 -        /* find a matching sample format in the encoder */
 -        for (p = dec_codec->sample_fmts; *p != AV_SAMPLE_FMT_NONE; p++) {
 -            if (*p == enc->sample_fmt) {
 -                dec->request_sample_fmt = *p;
 -                return;
 -            } else if (*p > enc->sample_fmt) {
 -                min_inc = FFMIN(min_inc, *p - enc->sample_fmt);
 -            } else
 -                min_dec = FFMIN(min_dec, enc->sample_fmt - *p);
 -        }
 -
 -        /* if none match, provide the one that matches quality closest */
 -        dec->request_sample_fmt = min_inc > 0 ? enc->sample_fmt + min_inc :
 -                                  enc->sample_fmt - min_dec;
 -    }
 -}
 -
  static void choose_sample_rate(AVStream *st, AVCodec *codec)
  {
      if(codec && codec->supported_samplerates){
@@@ -816,7 -769,7 +818,7 @@@ need_realloc
          exit_program(1);
      }
  
 -    if (enc->channels != dec->channels || enc->sample_rate != dec->sample_rate)
 +    if (enc->channels != dec->channels)
          ost->audio_resample = 1;
  
      resample_changed = ost->resample_sample_fmt  != dec->sample_fmt ||
              ost->resample_sample_rate == enc->sample_rate) {
              ost->resample = NULL;
              ost->audio_resample = 0;
 -        } else if (ost->audio_resample) {
 +        } else {
              if (dec->sample_fmt != AV_SAMPLE_FMT_S16)
                  av_log(NULL, AV_LOG_WARNING, "Using s16 intermediate sample format for resampling\n");
              ost->resample = av_audio_resample_init(enc->channels,    dec->channels,
@@@ -1130,46 -1083,36 +1132,46 @@@ static void do_video_resample(OutputStr
  {
      int resample_changed = 0;
      AVCodecContext *dec = ist->st->codec;
 +    AVCodecContext *enc = ost->st->codec;
      *out_picture = in_picture;
  
      resample_changed = ost->resample_width   != dec->width  ||
                         ost->resample_height  != dec->height ||
                         ost->resample_pix_fmt != dec->pix_fmt;
  
 +#if !CONFIG_AVFILTER
      if (resample_changed) {
          av_log(NULL, AV_LOG_INFO,
                 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
                 ist->file_index, ist->st->index,
                 ost->resample_width, ost->resample_height, av_get_pix_fmt_name(ost->resample_pix_fmt),
                 dec->width         , dec->height         , av_get_pix_fmt_name(dec->pix_fmt));
 -        if(!ost->video_resample)
 -            ost->video_resample = 1;
 +        ost->resample_width   = dec->width;
 +        ost->resample_height  = dec->height;
 +        ost->resample_pix_fmt = dec->pix_fmt;
      }
  
 -#if !CONFIG_AVFILTER
 +    ost->video_resample = dec->width   != enc->width  ||
 +                          dec->height  != enc->height ||
 +                          dec->pix_fmt != enc->pix_fmt;
 +
      if (ost->video_resample) {
 -        *out_picture = &ost->pict_tmp;
 -        if (resample_changed) {
 +        *out_picture = &ost->resample_frame;
 +        if (!ost->img_resample_ctx || resample_changed) {
 +            /* initialize the destination picture */
 +            if (!ost->resample_frame.data[0]) {
 +                avcodec_get_frame_defaults(&ost->resample_frame);
 +                if (avpicture_alloc((AVPicture *)&ost->resample_frame, enc->pix_fmt,
 +                                    enc->width, enc->height)) {
 +                    fprintf(stderr, "Cannot allocate temp picture, check pix fmt\n");
 +                    exit_program(1);
 +                }
 +            }
              /* initialize a new scaler context */
              sws_freeContext(ost->img_resample_ctx);
 -            ost->img_resample_ctx = sws_getContext(
 -                ist->st->codec->width,
 -                ist->st->codec->height,
 -                ist->st->codec->pix_fmt,
 -                ost->st->codec->width,
 -                ost->st->codec->height,
 -                ost->st->codec->pix_fmt,
 -                ost->sws_flags, NULL, NULL, NULL);
 +            ost->img_resample_ctx = sws_getContext(dec->width, dec->height, dec->pix_fmt,
 +                                                   enc->width, enc->height, enc->pix_fmt,
 +                                                   ost->sws_flags, NULL, NULL, NULL);
              if (ost->img_resample_ctx == NULL) {
                  av_log(NULL, AV_LOG_FATAL, "Cannot get resampling context\n");
                  exit_program(1);
@@@ -1380,11 -1323,9 +1382,11 @@@ static void print_report(OutputFile *ou
      int64_t total_size;
      AVCodecContext *enc;
      int frame_number, vid, i;
 -    double bitrate, ti1, pts;
 +    double bitrate;
 +    int64_t pts = INT64_MAX;
      static int64_t last_time = -1;
      static int qp_histogram[52];
 +    int hours, mins, secs, us;
  
      if (!print_stats && !is_last_report)
          return;
          total_size= avio_tell(oc->pb);
  
      buf[0] = '\0';
 -    ti1 = 1e10;
      vid = 0;
      for(i=0;i<nb_ostreams;i++) {
          float q = -1;
              vid = 1;
          }
          /* compute min output value */
 -        pts = (double)ost->st->pts.val * av_q2d(ost->st->time_base);
 -        if ((pts < ti1) && (pts > 0))
 -            ti1 = pts;
 +        pts = FFMIN(pts, av_rescale_q(ost->st->pts.val,
 +                                      ost->st->time_base, AV_TIME_BASE_Q));
      }
 -    if (ti1 < 0.01)
 -        ti1 = 0.01;
  
 -    bitrate = (double)(total_size * 8) / ti1 / 1000.0;
 +    secs = pts / AV_TIME_BASE;
 +    us = pts % AV_TIME_BASE;
 +    mins = secs / 60;
 +    secs %= 60;
 +    hours = mins / 60;
 +    mins %= 60;
  
 +    bitrate = pts ? total_size * 8 / (pts / 1000.0) : 0;
 +
 +    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
 +             "size=%8.0fkB time=", total_size / 1024.0);
 +    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
 +             "%02d:%02d:%02d.%02d ", hours, mins, secs,
 +             (100 * us) / AV_TIME_BASE);
      snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
 -            "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
 -            (double)total_size / 1024, ti1, bitrate);
 +             "bitrate=%6.1fkbits/s", bitrate);
  
      if (nb_frames_dup || nb_frames_drop)
          snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
@@@ -1699,7 -1633,8 +1701,7 @@@ static int output_packet(InputStream *i
                          av_freep(&decoded_frame);
                          goto discard_packet;
                      }
 -                    ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, decoded_frame->pkt_pts,
 -                                                                 decoded_frame->pkt_dts);
 +                    ist->next_pts = ist->pts = decoded_frame->best_effort_timestamp;
                      if (ist->st->codec->time_base.num != 0) {
                          int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
                          ist->next_pts += ((int64_t)AV_TIME_BASE *
  #if CONFIG_AVFILTER
              if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
                  ost->input_video_filter) {
 -                AVRational sar;
 -                if (ist->st->sample_aspect_ratio.num)
 -                    sar = ist->st->sample_aspect_ratio;
 -                else
 -                    sar = ist->st->codec->sample_aspect_ratio;
 -                av_vsrc_buffer_add_frame(ost->input_video_filter, decoded_frame, ist->pts, sar);
 +                if (!decoded_frame->sample_aspect_ratio.num)
 +                    decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
 +                decoded_frame->pts = ist->pts;
 +
 +                av_vsrc_buffer_add_frame(ost->input_video_filter, decoded_frame, AV_VSRC_BUF_FLAG_OVERWRITE);
                  if (!(filtered_frame = avcodec_alloc_frame())) {
                      ret = AVERROR(ENOMEM);
                      goto fail;
              frame_available = ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
                  !ost->output_video_filter || avfilter_poll_frame(ost->output_video_filter->inputs[0]);
              while (frame_available) {
 -                AVRational ist_pts_tb;
 -                if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter)
 -                    get_filtered_video_frame(ost->output_video_filter, filtered_frame, &ost->picref, &ist_pts_tb);
 -                if (ost->picref)
 -                    ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
 +                if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter) {
 +                    AVRational ist_pts_tb = ost->output_video_filter->inputs[0]->time_base;
 +                    if (av_buffersink_get_buffer_ref(ost->output_video_filter, &ost->picref, 0) < 0)
 +                        goto cont;
 +                    if (ost->picref) {
 +                        avfilter_fill_frame_from_video_buffer_ref(filtered_frame, ost->picref);
 +                        ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
 +                    }
 +                }
  #else
                  filtered_frame = decoded_frame;
  #endif
                      case AVMEDIA_TYPE_VIDEO:
  #if CONFIG_AVFILTER
                          if (ost->picref->video && !ost->frame_aspect_ratio)
 -                            ost->st->codec->sample_aspect_ratio = ost->picref->video->pixel_aspect;
 +                            ost->st->codec->sample_aspect_ratio = ost->picref->video->sample_aspect_ratio;
  #endif
                          do_video_out(os, ost, ist, filtered_frame, &frame_size,
                                       same_quant ? quality : ost->st->codec->global_quality);
                          abort();
                      }
                  } else {
 +                    AVPicture pict;
                      AVPacket opkt;
                      int64_t ost_tb_start_time= av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base);
 -
                      av_init_packet(&opkt);
  
                      if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
                          opkt.size = data_size;
                      }
  
 +                    if (os->oformat->flags & AVFMT_RAWPICTURE) {
 +                        /* store AVPicture in AVPacket, as expected by the output format */
 +                        avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height);
 +                        opkt.data = (uint8_t *)&pict;
 +                        opkt.size = sizeof(AVPicture);
 +                        opkt.flags |= AV_PKT_FLAG_KEY;
 +                    }
                      write_frame(os, &opkt, ost->st->codec, ost->bitstream_filters);
                      ost->st->codec->frame_number++;
                      ost->frame_number++;
@@@ -2003,6 -1928,16 +2005,6 @@@ static int init_input_stream(int ist_in
              return AVERROR(EINVAL);
          }
  
 -        /* update requested sample format for the decoder based on the
 -           corresponding encoder sample format */
 -        for (i = 0; i < nb_output_streams; i++) {
 -            OutputStream *ost = &output_streams[i];
 -            if (ost->source_index == ist_index) {
 -                update_sample_fmt(ist->st->codec, codec, ost->st->codec);
 -                break;
 -            }
 -        }
 -
          if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) {
              snprintf(error, error_len, "Error while opening decoder for input stream #%d:%d",
                      ist->file_index, ist->st->index);
  
      ist->pts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames*AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
      ist->next_pts = AV_NOPTS_VALUE;
 -    init_pts_correction(&ist->pts_ctx);
      ist->is_start = 1;
  
      return 0;
@@@ -2093,23 -2029,13 +2095,23 @@@ static int transcode_init(OutputFile *o
              }
              memcpy(codec->extradata, icodec->extradata, icodec->extradata_size);
              codec->extradata_size= icodec->extradata_size;
 -            if(!copy_tb && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/500){
 -                codec->time_base = icodec->time_base;
 -                codec->time_base.num *= icodec->ticks_per_frame;
 -                av_reduce(&codec->time_base.num, &codec->time_base.den,
 -                          codec->time_base.num, codec->time_base.den, INT_MAX);
 -            }else
 -                codec->time_base = ist->st->time_base;
 +
 +            codec->time_base = ist->st->time_base;
 +            if(!strcmp(os->oformat->name, "avi")) {
 +                if(!copy_tb && av_q2d(icodec->time_base)*icodec->ticks_per_frame > 2*av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/500){
 +                    codec->time_base = icodec->time_base;
 +                    codec->time_base.num *= icodec->ticks_per_frame;
 +                    codec->time_base.den *= 2;
 +                }
 +            } else if(!(os->oformat->flags & AVFMT_VARIABLE_FPS)) {
 +                if(!copy_tb && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/500){
 +                    codec->time_base = icodec->time_base;
 +                    codec->time_base.num *= icodec->ticks_per_frame;
 +                }
 +            }
 +            av_reduce(&codec->time_base.num, &codec->time_base.den,
 +                        codec->time_base.num, codec->time_base.den, INT_MAX);
 +
              switch(codec->codec_type) {
              case AVMEDIA_TYPE_AUDIO:
                  if(audio_volume != 256) {
                  if (codec->sample_fmt == AV_SAMPLE_FMT_NONE)
                      codec->sample_fmt = icodec->sample_fmt;
                  choose_sample_fmt(ost->st, ost->enc);
 -                if (!codec->channels)
 +                if (!codec->channels) {
                      codec->channels = icodec->channels;
 -                codec->channel_layout = icodec->channel_layout;
 +                    codec->channel_layout = icodec->channel_layout;
 +                }
                  if (av_get_channel_layout_nb_channels(codec->channel_layout) != codec->channels)
                      codec->channel_layout = 0;
                  ost->audio_resample = codec->sample_rate != icodec->sample_rate || audio_sync_method > 1;
                                        codec->height  != icodec->height ||
                                        codec->pix_fmt != icodec->pix_fmt;
                  if (ost->video_resample) {
 -#if !CONFIG_AVFILTER
 -                    avcodec_get_frame_defaults(&ost->pict_tmp);
 -                    if(avpicture_alloc((AVPicture*)&ost->pict_tmp, codec->pix_fmt,
 -                                       codec->width, codec->height)) {
 -                        av_log(NULL, AV_LOG_FATAL, "Cannot allocate temp picture, check pix fmt\n");
 -                        exit_program(1);
 -                    }
 -                    ost->img_resample_ctx = sws_getContext(
 -                        icodec->width,
 -                        icodec->height,
 -                        icodec->pix_fmt,
 -                        codec->width,
 -                        codec->height,
 -                        codec->pix_fmt,
 -                        ost->sws_flags, NULL, NULL, NULL);
 -                    if (ost->img_resample_ctx == NULL) {
 -                        av_log(NULL, AV_LOG_FATAL, "Cannot get resampling context\n");
 -                        exit_program(1);
 -                    }
 -#endif
 -                    codec->bits_per_raw_sample= 0;
 +                    codec->bits_per_raw_sample= frame_bits_per_raw_sample;
                  }
  
                  ost->resample_height = icodec->height;
                      ost->frame_rate = ost->enc->supported_framerates[idx];
                  }
                  codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num};
 +                if(   av_q2d(codec->time_base) < 0.001 && video_sync_method
 +                   && (video_sync_method==1 || (video_sync_method<0 && !(os->oformat->flags & AVFMT_VARIABLE_FPS)))){
 +                    av_log(os, AV_LOG_WARNING, "Frame rate very high for a muxer not effciciently supporting it.\n"
 +                                               "Please consider specifiying a lower framerate, a different muxer or -vsync 2\n");
 +                }
  
  #if CONFIG_AVFILTER
                  if (configure_video_filters(ist, ost)) {
                  break;
              }
              /* two pass mode */
 -            if ((codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
 +            if (codec->codec_id != CODEC_ID_H264 &&
 +                (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
                  char logfilename[1024];
                  FILE *f;
  
              }
          }
          if(codec->codec_type == AVMEDIA_TYPE_VIDEO){
 +            /* maximum video buffer size is 6-bytes per pixel, plus DPX header size */
              int size= codec->width * codec->height;
 -            bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 200);
 +            bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 1664);
          }
      }
  
      /* open files and write file headers */
      for (i = 0; i < nb_output_files; i++) {
          os = output_files[i].ctx;
+         os->interrupt_callback = int_cb;
          if (avformat_write_header(os, &output_files[i].opts) < 0) {
              snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?)", i);
              ret = AVERROR(EINVAL);
              goto dump_format;
          }
 -        assert_avoptions(output_files[i].opts);
 +//        assert_avoptions(output_files[i].opts);
          if (strcmp(os->oformat->name, "rtp")) {
              want_sdp = 0;
          }
@@@ -2410,7 -2349,6 +2413,7 @@@ static int transcode(OutputFile *output
      uint8_t *no_packet;
      int no_packet_count=0;
      int64_t timer_start;
 +    int key;
  
      if (!(no_packet = av_mallocz(nb_input_files)))
          exit_program(1);
      if (ret < 0)
          goto fail;
  
 -    av_log(NULL, AV_LOG_INFO, "Press ctrl-c to stop encoding\n");
 +    if (!using_stdin) {
 +        av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");
 +        avio_set_interrupt_cb(decode_interrupt_cb);
 +    }
      term_init();
  
      timer_start = av_gettime();
  
          ipts_min = INT64_MAX;
          opts_min= 1e100;
 +        /* if 'q' pressed, exits */
 +        if (!using_stdin) {
 +            if (q_pressed)
 +                break;
 +            /* read_key() returns 0 on EOF */
 +            key = read_key();
 +            if (key == 'q')
 +                break;
 +            if (key == '+') av_log_set_level(av_log_get_level()+10);
 +            if (key == '-') av_log_set_level(av_log_get_level()-10);
 +            if (key == 's') qp_hist     ^= 1;
 +            if (key == 'h'){
 +                if (do_hex_dump){
 +                    do_hex_dump = do_pkt_dump = 0;
 +                } else if(do_pkt_dump){
 +                    do_hex_dump = 1;
 +                } else
 +                    do_pkt_dump = 1;
 +                av_log_set_level(AV_LOG_DEBUG);
 +            }
 +            if (key == 'd' || key == 'D'){
 +                int debug=0;
 +                if(key == 'D') {
 +                    debug = input_streams[0].st->codec->debug<<1;
 +                    if(!debug) debug = 1;
 +                    while(debug & (FF_DEBUG_DCT_COEFF|FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) //unsupported, would just crash
 +                        debug += debug;
 +                }else
 +                    scanf("%d", &debug);
 +                for(i=0;i<nb_input_streams;i++) {
 +                    input_streams[i].st->codec->debug = debug;
 +                }
 +                for(i=0;i<nb_output_streams;i++) {
 +                    ost = &output_streams[i];
 +                    ost->st->codec->debug = debug;
 +                }
 +                if(debug) av_log_set_level(AV_LOG_DEBUG);
 +                fprintf(stderr,"debug=%d\n", debug);
 +            }
 +            if (key == '?'){
 +                fprintf(stderr, "key    function\n"
 +                                "?      show this help\n"
 +                                "+      increase verbosity\n"
 +                                "-      decrease verbosity\n"
 +                                "D      cycle through available debug modes\n"
 +                                "h      dump packets/hex press to cycle through the 3 states\n"
 +                                "q      quit\n"
 +                                "s      Show QP histogram\n"
 +                );
 +            }
 +        }
  
          /* select the stream that we must read now by looking at the
             smallest output pts */
                  av_fifo_free(ost->fifo); /* works even if fifo is not
                                               initialized but set to zero */
                  av_freep(&ost->st->codec->subtitle_header);
 -                av_free(ost->pict_tmp.data[0]);
 +                av_free(ost->resample_frame.data[0]);
                  av_free(ost->forced_kf_pts);
                  if (ost->video_resample)
                      sws_freeContext(ost->img_resample_ctx);
@@@ -2924,18 -2808,16 +2927,18 @@@ static void add_input_streams(OptionsCo
  
          switch (dec->codec_type) {
          case AVMEDIA_TYPE_AUDIO:
 -            if (o->audio_disable)
 +            if(!ist->dec)
 +                ist->dec = avcodec_find_decoder(dec->codec_id);
 +            if(o->audio_disable)
                  st->discard= AVDISCARD_ALL;
              break;
          case AVMEDIA_TYPE_VIDEO:
 +            if(!ist->dec)
 +                ist->dec = avcodec_find_decoder(dec->codec_id);
              rfps      = ic->streams[i]->r_frame_rate.num;
              rfps_base = ic->streams[i]->r_frame_rate.den;
              if (dec->lowres) {
                  dec->flags |= CODEC_FLAG_EMU_EDGE;
 -                dec->height >>= dec->lowres;
 -                dec->width  >>= dec->lowres;
              }
  
              if (dec->time_base.den != rfps*dec->ticks_per_frame || dec->time_base.num != rfps_base) {
          case AVMEDIA_TYPE_DATA:
              break;
          case AVMEDIA_TYPE_SUBTITLE:
 -            if (o->subtitle_disable)
 +            if(!ist->dec)
 +                ist->dec = avcodec_find_decoder(dec->codec_id);
 +            if(o->subtitle_disable)
                  st->discard = AVDISCARD_ALL;
              break;
          case AVMEDIA_TYPE_ATTACHMENT:
@@@ -3010,7 -2890,7 +3013,7 @@@ static void dump_attachment(AVStream *s
  
      assert_file_overwrite(filename);
  
-     if ((ret = avio_open (&out, filename, AVIO_FLAG_WRITE)) < 0) {
+     if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
          av_log(NULL, AV_LOG_FATAL, "Could not open file %s for writing.\n",
                 filename);
          exit_program(1);
@@@ -3068,6 -2948,7 +3071,7 @@@ static int opt_input_file(OptionsContex
          av_dict_set(&format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
  
      ic->flags |= AVFMT_FLAG_NONBLOCK;
+     ic->interrupt_callback = int_cb;
  
      /* open the input file with generic libav function */
      err = avformat_open_input(&ic, filename, file_iformat, &format_opts);
@@@ -3197,12 -3078,12 +3201,12 @@@ static int get_preset_file_2(const cha
          if (codec_name) {
              snprintf(filename, sizeof(filename), "%s%s/%s-%s.avpreset", base[i],
                       i != 1 ? "" : "/.avconv", codec_name, preset_name);
-             ret = avio_open(s, filename, AVIO_FLAG_READ);
+             ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
          }
          if (ret) {
              snprintf(filename, sizeof(filename), "%s%s/%s.avpreset", base[i],
                       i != 1 ? "" : "/.avconv", preset_name);
-             ret = avio_open(s, filename, AVIO_FLAG_READ);
+             ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
          }
      }
      return ret;
@@@ -3539,7 -3420,7 +3543,7 @@@ static int opt_streamid(OptionsContext 
          exit_program(1);
      }
      *p++ = '\0';
 -    idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, INT_MAX);
 +    idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, MAX_STREAMS-1);
      o->streamid_map = grow_array(o->streamid_map, sizeof(*o->streamid_map), &o->nb_streamid_map, idx+1);
      o->streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);
      return 0;
@@@ -3585,11 -3466,12 +3589,12 @@@ static int copy_chapters(InputFile *ifi
      return 0;
  }
  
 -static int read_avserver_streams(OptionsContext *o, AVFormatContext *s, const char *filename)
 +static int read_ffserver_streams(OptionsContext *o, AVFormatContext *s, const char *filename)
  {
      int i, err;
-     AVFormatContext *ic = NULL;
+     AVFormatContext *ic = avformat_alloc_context();
  
+     ic->interrupt_callback = int_cb;
      err = avformat_open_input(&ic, filename, NULL, NULL);
      if (err < 0)
          return err;
  
          // FIXME: a more elegant solution is needed
          memcpy(st, ic->streams[i], sizeof(AVStream));
 -        st->info = NULL;
 +        st->info = av_malloc(sizeof(*st->info));
 +        memcpy(st->info, ic->streams[i]->info, sizeof(*st->info));
          avcodec_copy_context(st->codec, ic->streams[i]->codec);
  
          if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && !ost->stream_copy)
@@@ -3631,19 -3512,36 +3636,20 @@@ static void opt_output_file(void *optct
      if (!strcmp(filename, "-"))
          filename = "pipe:";
  
 -    oc = avformat_alloc_context();
 +    err = avformat_alloc_output_context2(&oc, NULL, o->format, filename);
      if (!oc) {
 -        print_error(filename, AVERROR(ENOMEM));
 +        print_error(filename, err);
          exit_program(1);
      }
  
 -    if (o->format) {
 -        file_oformat = av_guess_format(o->format, NULL, NULL);
 -        if (!file_oformat) {
 -            av_log(NULL, AV_LOG_FATAL, "Requested output format '%s' is not a suitable output format\n", o->format);
 -            exit_program(1);
 -        }
 -    } else {
 -        file_oformat = av_guess_format(NULL, filename, NULL);
 -        if (!file_oformat) {
 -            av_log(NULL, AV_LOG_FATAL, "Unable to find a suitable output format for '%s'\n",
 -                   filename);
 -            exit_program(1);
 -        }
 -    }
 -
 -    oc->oformat = file_oformat;
 +    file_oformat= oc->oformat;
+     oc->interrupt_callback = int_cb;
 -    av_strlcpy(oc->filename, filename, sizeof(oc->filename));
  
      if (!strcmp(file_oformat->name, "ffm") &&
          av_strstart(filename, "http:", NULL)) {
 -        /* special case for files sent to avserver: we get the stream
 -           parameters from avserver */
 -        int err = read_avserver_streams(o, oc, filename);
 +        /* special case for files sent to ffserver: we get the stream
 +           parameters from ffserver */
 +        int err = read_ffserver_streams(o, oc, filename);
          if (err < 0) {
              print_error(filename, err);
              exit_program(1);
          const char *p;
          int64_t len;
  
-         if ((err = avio_open(&pb, o->attachments[i], AVIO_FLAG_READ)) < 0) {
+         if ((err = avio_open2(&pb, o->attachments[i], AVIO_FLAG_READ, &int_cb, NULL)) < 0) {
              av_log(NULL, AV_LOG_FATAL, "Could not open attachment file %s.\n",
                     o->attachments[i]);
              exit_program(1);
          assert_file_overwrite(filename);
  
          /* open the file */
-         if ((err = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE)) < 0) {
+         if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
+                               &oc->interrupt_callback,
+                               &output_files[nb_output_files - 1].opts)) < 0) {
              print_error(filename, err);
              exit_program(1);
          }
          av_dict_set(&output_files[nb_output_files - 1].opts, "preload", buf, 0);
      }
      oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
 -    oc->flags |= AVFMT_FLAG_NONBLOCK;
  
      /* copy chapters */
      if (o->chapters_input_file >= nb_input_files) {
@@@ -3970,7 -3871,7 +3978,7 @@@ static void show_usage(void
      printf("\n");
  }
  
 -static void show_help(void)
 +static int opt_help(const char *opt, const char *arg)
  {
      int flags = AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM;
      av_log_set_callback(log_callback_help);
      show_help_children(avcodec_get_class(), flags);
      show_help_children(avformat_get_class(), flags);
      show_help_children(sws_get_class(), flags);
 +
 +    return 0;
  }
  
  static int opt_target(OptionsContext *o, const char *opt, const char *arg)
@@@ -4175,20 -4074,6 +4183,20 @@@ static int opt_data_frames(OptionsConte
      return parse_option(o, "frames:d", arg, options);
  }
  
 +static void log_callback_null(void* ptr, int level, const char* fmt, va_list vl)
 +{
 +}
 +
 +static int opt_passlogfile(const char *opt, const char *arg)
 +{
 +    pass_logfilename_prefix = arg;
 +#if CONFIG_LIBX264_ENCODER
 +    return opt_default("passlogfile", arg);
 +#else
 +    return 0;
 +#endif
 +}
 +
  static int opt_video_tag(OptionsContext *o, const char *opt, const char *arg)
  {
      return parse_option(o, "tag:v", arg, options);
@@@ -4265,7 -4150,6 +4273,7 @@@ static const OptionDef options[] = 
      { "s", HAS_ARG | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_sizes)}, "set frame size (WxH or abbreviation)", "size" },
      { "aspect", HAS_ARG | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_aspect_ratios)}, "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
      { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_pix_fmts)}, "set pixel format", "format" },
 +    { "bits_per_raw_sample", OPT_INT | HAS_ARG | OPT_VIDEO, {(void*)&frame_bits_per_raw_sample}, "set the number of bits per raw sample", "number" },
      { "vn", OPT_BOOL | OPT_VIDEO | OPT_OFFSET, {.off = OFFSET(video_disable)}, "disable video" },
      { "vdt", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)&video_discard}, "discard threshold", "n" },
      { "rc_override", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(rc_overrides)}, "rate control override for specific intervals", "override" },
      { "same_quant", OPT_BOOL | OPT_VIDEO, {(void*)&same_quant},
        "use same quantizer as source (implies VBR)" },
      { "pass", HAS_ARG | OPT_VIDEO, {(void*)opt_pass}, "select the pass number (1 or 2)", "n" },
 -    { "passlogfile", HAS_ARG | OPT_STRING | OPT_VIDEO, {(void*)&pass_logfilename_prefix}, "select two pass log file name prefix", "prefix" },
 +    { "passlogfile", HAS_ARG | OPT_VIDEO, {(void*)&opt_passlogfile}, "select two pass log file name prefix", "prefix" },
      { "deinterlace", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_deinterlace},
        "deinterlace pictures" },
      { "vstats", OPT_EXPERT | OPT_VIDEO, {(void*)&opt_vstats}, "dump video coding statistics to file" },
@@@ -4333,13 -4217,6 +4341,13 @@@ int main(int argc, char **argv
      av_log_set_flags(AV_LOG_SKIP_REPEATED);
      parse_loglevel(argc, argv, options);
  
 +    if(argc>1 && !strcmp(argv[1], "-d")){
 +        run_as_daemon=1;
 +        av_log_set_callback(log_callback_null);
 +        argc--;
 +        argv++;
 +    }
 +
      avcodec_register_all();
  #if CONFIG_AVDEVICE
      avdevice_register_all();
      av_register_all();
      avformat_network_init();
  
- #if HAVE_ISATTY
-     if(isatty(STDIN_FILENO))
-         avio_set_interrupt_cb(decode_interrupt_cb);
- #endif
--    show_banner();
  
      /* parse options */
      parse_options(&o, argc, argv, options, opt_output_file);
diff --combined configure
index 2daa079b573161f40a5e76715232ac59d470280a,61bb4dfb54770b868b14e836b746683c3fb1b087..d0376bb391f5f443f4f5f0b4472f85a787eaf82e
+++ b/configure
@@@ -1,6 -1,6 +1,6 @@@
  #!/bin/sh
  #
 -# Libav configure script
 +# FFmpeg configure script
  #
  # Copyright (c) 2000-2002 Fabrice Bellard
  # Copyright (c) 2005-2008 Diego Biurrun
@@@ -44,9 -44,9 +44,9 @@@ if test "$E1" != 0 || test "$E2" = 0; t
      echo "No compatible shell script interpreter found."
      echo "This configure script requires a POSIX-compatible shell"
      echo "such as bash or ksh."
 -    echo "THIS IS NOT A BUG IN LIBAV, DO NOT REPORT IT AS SUCH."
 +    echo "THIS IS NOT A BUG IN FFMPEG, DO NOT REPORT IT AS SUCH."
      echo "Instead, install a working POSIX-compatible shell."
 -    echo "Disabling this configure test will create a broken Libav."
 +    echo "Disabling this configure test will create a broken FFmpeg."
      if test "$BASH_VERSION" = '2.04.0(1)-release'; then
          echo "This bash version ($BASH_VERSION) is broken on your platform."
          echo "Upgrade to a later version if available."
@@@ -65,7 -65,7 +65,7 @@@ Standard options
    --disable-logging        do not log configure debug information
    --prefix=PREFIX          install in PREFIX [$prefix]
    --bindir=DIR             install binaries in DIR [PREFIX/bin]
 -  --datadir=DIR            install data files in DIR [PREFIX/share/avconv]
 +  --datadir=DIR            install data files in DIR [PREFIX/share/ffmpeg]
    --libdir=DIR             install libs in DIR [PREFIX/lib]
    --shlibdir=DIR           install shared libs in DIR [PREFIX/lib]
    --incdir=DIR             install includes in DIR [PREFIX/include]
@@@ -82,19 -82,17 +82,19 @@@ Configuration options
    --disable-doc            do not build documentation
    --disable-ffmpeg         disable ffmpeg build
    --disable-avconv         disable avconv build
 -  --disable-avplay         disable avplay build
 -  --disable-avprobe        disable avprobe build
 -  --disable-avserver       disable avserver build
 +  --disable-ffplay         disable ffplay build
 +  --disable-ffprobe        disable ffprobe build
 +  --disable-ffserver       disable ffserver build
    --disable-avdevice       disable libavdevice build
    --disable-avcodec        disable libavcodec build
    --disable-avformat       disable libavformat build
 +  --disable-swresample     disable libswresample build
    --disable-swscale        disable libswscale build
    --disable-postproc       disable libpostproc build
    --disable-avfilter       disable video filter support [no]
    --disable-pthreads       disable pthreads [auto]
    --disable-w32threads     disable Win32 threads [auto]
 +  --disable-os2threads     disable OS/2 threads [auto]
    --enable-x11grab         enable X11 grabbing [no]
    --disable-network        disable network support [no]
    --enable-gray            enable full grayscale support (slower color)
    --disable-lpc            disable LPC code
    --disable-mdct           disable MDCT code
    --disable-rdft           disable RDFT code
 -  --enable-vaapi           enable VAAPI code
 -  --enable-vda             enable VDA code
 -  --enable-vdpau           enable VDPAU code
 +  --enable-vaapi           enable VAAPI code [autodetect]
 +  --enable-vda             enable VDA code [autodetect]
 +  --enable-vdpau           enable VDPAU code [autodetect]
    --disable-dxva2          disable DXVA2 code
 +  --disable-vda            disable VDA code
    --enable-runtime-cpudetect detect cpu capabilities at runtime (bigger binary)
    --enable-hardcoded-tables use hardcoded tables instead of runtime generation
    --enable-memalign-hack   emulate memalign, interferes with memory debuggers
  External library support:
    --enable-avisynth        enable reading of AVISynth script files [no]
    --enable-bzlib           enable bzlib [autodetect]
 +  --enable-libcelt         enable CELT decoding via libcelt [no]
    --enable-frei0r          enable frei0r video filtering
 +  --enable-libaacplus      enable AAC+ encoding via libaacplus [no]
    --enable-gnutls          enable gnutls [no]
    --enable-libopencore-amrnb enable AMR-NB de/encoding via libopencore-amrnb [no]
    --enable-libopencore-amrwb enable AMR-WB decoding via libopencore-amrwb [no]
    --enable-libfaac         enable FAAC support via libfaac [no]
    --enable-libfreetype     enable libfreetype [no]
    --enable-libgsm          enable GSM support via libgsm [no]
 +  --enable-libmodplug      enable ModPlug via libmodplug [no]
    --enable-libmp3lame      enable MP3 encoding via libmp3lame [no]
    --enable-libnut          enable NUT (de)muxing via libnut,
                             native (de)muxer exists [no]
 -  --enable-libopenjpeg     enable JPEG 2000 decoding via OpenJPEG [no]
 +  --enable-libopenjpeg     enable JPEG 2000 encoding/decoding via OpenJPEG [no]
    --enable-librtmp         enable RTMP[E] support via librtmp [no]
    --enable-libschroedinger enable Dirac support via libschroedinger [no]
    --enable-libspeex        enable Speex support via libspeex [no]
 +  --enable-libstagefright-h264  enable H.264 decoding via libstagefright [no]
    --enable-libtheora       enable Theora encoding via libtheora [no]
 +  --enable-libutvideo      enable Ut Video decoding via libutvideo [no]
 +  --enable-libv4l2         enable libv4l2/v4l-utils [no]
    --enable-libvo-aacenc    enable AAC encoding via libvo-aacenc [no]
    --enable-libvo-amrwbenc  enable AMR-WB encoding via libvo-amrwbenc [no]
    --enable-libvorbis       enable Vorbis encoding via libvorbis,
    --enable-libxavs         enable AVS encoding via xavs [no]
    --enable-libxvid         enable Xvid encoding via xvidcore,
                             native MPEG-4/Xvid encoder exists [no]
 +  --enable-openal          enable OpenAL 1.1 capture support [no]
    --enable-mlib            enable Sun medialib [no]
    --enable-openssl         enable openssl [no]
    --enable-zlib            enable zlib [autodetect]
@@@ -217,19 -207,16 +217,19 @@@ Advanced options (experts only)
    --ar=AR                  use archive tool AR [$ar_default]
    --as=AS                  use assembler AS [$as_default]
    --cc=CC                  use C compiler CC [$cc_default]
 +  --cxx=CXX                use C compiler CXX [$cxx_default]
    --ld=LD                  use linker LD
    --host-cc=HOSTCC         use host C compiler HOSTCC
    --host-cflags=HCFLAGS    use HCFLAGS when compiling for host
    --host-ldflags=HLDFLAGS  use HLDFLAGS when linking for host
    --host-libs=HLIBS        use libs HLIBS when linking for host
    --extra-cflags=ECFLAGS   add ECFLAGS to CFLAGS [$CFLAGS]
 +  --extra-cxxflags=ECFLAGS add ECFLAGS to CXXFLAGS [$CXXFLAGS]
    --extra-ldflags=ELDFLAGS add ELDFLAGS to LDFLAGS [$LDFLAGS]
    --extra-libs=ELIBS       add ELIBS [$ELIBS]
    --extra-version=STRING   version string suffix []
    --build-suffix=SUFFIX    library name suffix []
 +  --progs-suffix=SUFFIX    program name suffix []
    --arch=ARCH              select architecture [$arch]
    --cpu=CPU                select the minimum required CPU (affects
                             instruction selection, may crash on older CPUs)
    --disable-symver         disable symbol versioning
    --optflags               override optimization-related compiler flags
  
 -Developer options (useful when working on Libav itself):
 +Developer options (useful when working on FFmpeg itself):
    --disable-debug          disable debugging symbols
    --enable-debug=LEVEL     set the debug level [$debuglevel]
    --disable-optimizations  disable compiler optimizations
    --enable-extra-warnings  enable more compiler warnings
 +  --disable-stripping      disable stripping of executables and shared libraries
    --samples=PATH           location of test samples for FATE, if not set use
                             \$FATE_SAMPLES at make invocation time.
  
@@@ -299,7 -285,7 +299,7 @@@ die()
  
  If you think configure made a mistake, make sure you are using the latest
  version from Git.  If the latest version fails, report the problem to the
 -libav-user@libav.org mailing list or IRC #libav on irc.freenode.net.
 +ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
  EOF
      if disabled logging; then
          cat <<EOF
@@@ -596,10 -582,6 +596,10 @@@ add_cflags()
      append CFLAGS $($filter_cflags "$@")
  }
  
 +add_cxxflags(){
 +    append CXXFLAGS $($filter_cflags "$@")
 +}
 +
  add_asflags(){
      append ASFLAGS $($filter_asflags "$@")
  }
@@@ -624,13 -606,6 +624,13 @@@ check_cc()
      check_cmd $cc $CPPFLAGS $CFLAGS "$@" -c -o $TMPO $TMPC
  }
  
 +check_cxx(){
 +    log check_cxx "$@"
 +    cat > $TMPCPP
 +    log_file $TMPCPP
 +    check_cmd $cxx $CPPFLAGS $CFLAGS $CXXFLAGS "$@" -c -o $TMPO $TMPCPP
 +}
 +
  check_cpp(){
      log check_cpp "$@"
      cat > $TMPC
@@@ -666,14 -641,12 +666,14 @@@ check_yasm()
  
  check_ld(){
      log check_ld "$@"
 +    type=$1
 +    shift 1
      flags=''
      libs=''
      for f; do
          test "${f}" = "${f#-l}" && flags="$flags $f" || libs="$libs $f"
      done
 -    check_cc $($filter_cflags $flags) || return
 +    check_$type $($filter_cflags $flags) || return
      check_cmd $ld $LDFLAGS $flags -o $TMPE $TMPO $libs $extralibs
  }
  
@@@ -693,17 -666,9 +693,17 @@@ int x
  EOF
  }
  
 +check_cxxflags(){
 +    log check_cxxflags "$@"
 +    set -- $($filter_cflags "$@")
 +    check_cxx "$@" <<EOF && append CXXFLAGS "$@"
 +int x;
 +EOF
 +}
 +
  test_ldflags(){
      log test_ldflags "$@"
 -    check_ld "$@" <<EOF
 +    check_ld "cc" "$@" <<EOF
  int main(void){ return 0; }
  EOF
  }
@@@ -729,7 -694,7 +729,7 @@@ check_func()
      func=$1
      shift
      disable $func
 -    check_ld "$@" <<EOF && enable $func
 +    check_ld "cc" "$@" <<EOF && enable $func
  extern int $func();
  int main(void){ $func(); }
  EOF
@@@ -740,10 -705,10 +740,10 @@@ check_mathfunc()
      func=$1
      shift
      disable $func
 -    check_ld "$@" <<EOF && enable $func
 +    check_ld "cc" "$@" <<EOF && enable $func
  #include <math.h>
  float foo(float f) { return $func(f); }
 -int main(void){ return 0; }
 +int main(void){ return (int) foo; }
  EOF
  }
  
@@@ -760,26 -725,7 +760,26 @@@ check_func_headers()
              echo "long check_$func(void) { return (long) $func; }"
          done
          echo "int main(void) { return 0; }"
 -    } | check_ld "$@" && enable $funcs && enable_safe $headers
 +    } | check_ld "cc" "$@" && enable $funcs && enable_safe $headers
 +}
 +
 +check_class_headers_cpp(){
 +    log check_class_headers_cpp "$@"
 +    headers=$1
 +    classes=$2
 +    shift 2
 +    {
 +        for hdr in $headers; do
 +            echo "#include <$hdr>"
 +        done
 +        echo "int main(void) { "
 +        i=1
 +        for class in $classes; do
 +            echo "$class obj$i;"
 +            i=$(expr $i + 1)
 +        done
 +        echo "return 0; }"
 +    } | check_ld "cxx" "$@" && enable $funcs && enable_safe $headers
  }
  
  check_cpp_condition(){
@@@ -811,21 -757,13 +811,21 @@@ check_lib2()
      check_func_headers "$headers" "$funcs" "$@" && add_extralibs "$@"
  }
  
 +check_lib_cpp(){
 +    log check_lib_cpp "$@"
 +    headers="$1"
 +    classes="$2"
 +    shift 2
 +    check_class_headers_cpp "$headers" "$classes" "$@" && add_extralibs "$@"
 +}
 +
  check_pkg_config(){
      log check_pkg_config "$@"
      pkg="$1"
      headers="$2"
      funcs="$3"
      shift 3
 -    $pkg_config --exists $pkg || return
 +    $pkg_config --exists $pkg 2>/dev/null || return
      pkg_cflags=$($pkg_config --cflags $pkg)
      pkg_libs=$($pkg_config --libs $pkg)
      check_func_headers "$headers" "$funcs" $pkg_cflags $pkg_libs "$@" &&
  }
  
  check_exec(){
 -    check_ld "$@" && { enabled cross_compile || $TMPE >> $logfile 2>&1; }
 +    check_ld "cc" "$@" && { enabled cross_compile || $TMPE >> $logfile 2>&1; }
  }
  
  check_exec_crash(){
  static void sighandler(int sig){
      raise(SIGTERM);
  }
 +int func(void){
 +    $code
 +}
  int main(void){
      signal(SIGILL, sighandler);
      signal(SIGFPE, sighandler);
  #ifdef SIGBUS
      signal(SIGBUS, sighandler);
  #endif
 -    { $code }
 +    return func();
  }
  EOF
  }
@@@ -917,14 -852,6 +917,14 @@@ require2()
      check_lib2 "$headers" $func "$@" || die "ERROR: $name not found"
  }
  
 +require_cpp(){
 +    name="$1"
 +    headers="$2"
 +    classes="$3"
 +    shift 3
 +    check_lib_cpp "$headers" "$classes" "$@" || die "ERROR: $name not found"
 +}
 +
  require_pkg_config(){
      pkg="$1"
      check_pkg_config "$@" || die "ERROR: $pkg not found"
@@@ -985,7 -912,6 +985,7 @@@ CONFIG_LIST=
      avformat
      avisynth
      bzlib
 +    crystalhd
      dct
      doc
      dwt
      fastdiv
      ffmpeg
      avconv
 -    avplay
 -    avprobe
 -    avserver
 +    ffplay
 +    ffprobe
 +    ffserver
      fft
      frei0r
      gnutls
      h264pred
      hardcoded_tables
      huffman
 +    libaacplus
      libcdio
 +    libcelt
      libdc1394
      libdirac
      libfaac
      libfreetype
      libgsm
 +    libmodplug
      libmp3lame
      libnut
      libopencore_amrnb
      librtmp
      libschroedinger
      libspeex
 +    libstagefright_h264
      libtheora
 +    libutvideo
 +    libv4l2
      libvo_aacenc
      libvo_amrwbenc
      libvorbis
      mpegaudiodsp
      network
      nonfree
 +    openal
      openssl
      pic
      postproc
      small
      sram
      static
 +    swresample
      swscale
      swscale_alpha
      thumb
  THREADS_LIST='
      pthreads
      w32threads
 +    os2threads
  '
  
  ARCH_LIST='
@@@ -1136,7 -1053,6 +1136,7 @@@ HAVE_LIST=
      attribute_may_alias
      attribute_packed
      bswap
 +    cbrtf
      closesocket
      cmov
      dcbzl
      inet_aton
      inline_asm
      isatty
 +    kbhit
      ldbrx
 -    libdc1394_1
 -    libdc1394_2
      llrint
      llrintf
      local_aligned_16
      memalign
      mkstemp
      mmap
 +    PeekNamedPipe
      posix_memalign
      round
      roundf
      setrlimit
      strerror_r
      strptime
 -    strtok_r
      struct_addrinfo
      struct_ipv6_mreq
      struct_sockaddr_in6
      sys_select_h
      sys_soundcard_h
      sys_videoio_h
 +    termios_h
      threads
      trunc
      truncf
@@@ -1244,7 -1160,6 +1244,7 @@@ CMDLINE_SELECT=
      extra_warnings
      logging
      optimizations
 +    stripping
      symver
      yasm
  "
@@@ -1265,11 -1180,9 +1265,11 @@@ CMDLINE_SET=
      arch
      as
      build_suffix
 +    progs_suffix
      cc
      cpu
      cross_prefix
 +    cxx
      dep_cc
      extra_version
      host_cc
      host_ldflags
      host_libs
      host_os
 +    install
      ld
      logfile
      malloc_prefix
      optflags
      pkg_config
      samples
 +    strip
      sysinclude
      sysroot
      target_exec
  
  CMDLINE_APPEND="
      extra_cflags
 +    extra_cxxflags
  "
  
  # code dependency declarations
@@@ -1372,7 -1282,6 +1372,7 @@@ flac_decoder_select="golomb
  flac_encoder_select="golomb lpc"
  flashsv_decoder_select="zlib"
  flashsv_encoder_select="zlib"
 +flashsv2_encoder_select="zlib"
  flashsv2_decoder_select="zlib"
  flv_decoder_select="h263_decoder"
  flv_encoder_select="h263_encoder"
@@@ -1384,11 -1293,9 +1384,11 @@@ h263_vaapi_hwaccel_select="vaapi h263_d
  h263i_decoder_select="h263_decoder"
  h263p_encoder_select="h263_encoder"
  h264_decoder_select="golomb h264dsp h264pred"
 +h264_crystalhd_decoder_select="crystalhd h264_mp4toannexb_bsf h264_parser"
  h264_dxva2_hwaccel_deps="dxva2api_h"
  h264_dxva2_hwaccel_select="dxva2 h264_decoder"
  h264_vaapi_hwaccel_select="vaapi h264_decoder"
 +h264_vda_hwaccel_deps="VideoDecodeAcceleration_VDADecoder_h pthreads"
  h264_vda_hwaccel_select="vda h264_decoder"
  h264_vdpau_decoder_select="vdpau h264_decoder"
  imc_decoder_select="fft mdct sinewin"
@@@ -1414,18 -1321,13 +1414,18 @@@ mpeg4_decoder_select="h263_decoder mpeg
  mpeg4_encoder_select="h263_encoder"
  mpeg_vdpau_decoder_select="vdpau mpegvideo_decoder"
  mpeg1_vdpau_decoder_select="vdpau mpeg1video_decoder"
 +mpeg1_vdpau_hwaccel_select="vdpau mpeg1video_decoder"
 +mpeg2_crystalhd_decoder_select="crystalhd"
  mpeg2_dxva2_hwaccel_deps="dxva2api_h"
  mpeg2_dxva2_hwaccel_select="dxva2 mpeg2video_decoder"
 +mpeg2_vdpau_hwaccel_select="vdpau mpeg2video_decoder"
  mpeg2_vaapi_hwaccel_select="vaapi mpeg2video_decoder"
 +mpeg4_crystalhd_decoder_select="crystalhd"
  mpeg4_vaapi_hwaccel_select="vaapi mpeg4_decoder"
  mpeg4_vdpau_decoder_select="vdpau mpeg4_decoder"
  mpeg_xvmc_decoder_deps="X11_extensions_XvMClib_h"
  mpeg_xvmc_decoder_select="mpegvideo_decoder"
 +msmpeg4_crystalhd_decoder_select="crystalhd"
  msmpeg4v1_decoder_select="h263_decoder"
  msmpeg4v1_encoder_select="h263_encoder"
  msmpeg4v2_decoder_select="h263_decoder"
@@@ -1449,9 -1351,6 +1449,9 @@@ shorten_decoder_select="golomb
  sipr_decoder_select="lsp"
  snow_decoder_select="dwt"
  snow_encoder_select="aandct dwt"
 +sonic_decoder_select="golomb"
 +sonic_encoder_select="golomb"
 +sonic_ls_encoder_select="golomb"
  svq1_encoder_select="aandct"
  svq3_decoder_select="golomb h264dsp h264pred"
  svq3_decoder_suggest="zlib"
@@@ -1462,8 -1361,7 +1462,8 @@@ truehd_decoder_select="mlp_decoder
  tscc_decoder_select="zlib"
  twinvq_decoder_select="mdct lsp sinewin"
  vc1_decoder_select="h263_decoder"
 -vc1_dxva2_hwaccel_deps="dxva2api_h DXVA_PictureParameters_wDecodedPictureIndex"
 +vc1_crystalhd_decoder_select="crystalhd"
 +vc1_dxva2_hwaccel_deps="dxva2api_h"
  vc1_dxva2_hwaccel_select="dxva2 vc1_decoder"
  vc1_vaapi_hwaccel_select="vaapi vc1_decoder"
  vc1_vdpau_decoder_select="vdpau vc1_decoder"
@@@ -1485,7 -1383,6 +1485,7 @@@ wmv1_encoder_select="h263_encoder
  wmv2_decoder_select="h263_decoder"
  wmv2_encoder_select="h263_encoder"
  wmv3_decoder_select="vc1_decoder"
 +wmv3_crystalhd_decoder_select="crystalhd"
  wmv3_dxva2_hwaccel_select="vc1_dxva2_hwaccel"
  wmv3_vaapi_hwaccel_select="vc1_vaapi_hwaccel"
  wmv3_vdpau_decoder_select="vc1_vdpau_decoder"
@@@ -1495,7 -1392,6 +1495,7 @@@ zlib_encoder_select="zlib
  zmbv_decoder_select="zlib"
  zmbv_encoder_select="zlib"
  
 +crystalhd_deps="libcrystalhd_libcrystalhd_if_h"
  vaapi_deps="va_va_h"
  vda_deps="VideoDecodeAcceleration_VDADecoder_h pthreads"
  vdpau_deps="vdpau_vdpau_h vdpau_vdpau_x11_h"
  h264_parser_select="golomb h264dsp h264pred"
  
  # external libraries
 +libaacplus_encoder_deps="libaacplus"
 +libcelt_decoder_deps="libcelt"
  libdirac_decoder_deps="libdirac !libschroedinger"
  libdirac_encoder_deps="libdirac"
  libfaac_encoder_deps="libfaac"
@@@ -1513,18 -1407,15 +1513,18 @@@ libgsm_decoder_deps="libgsm
  libgsm_encoder_deps="libgsm"
  libgsm_ms_decoder_deps="libgsm"
  libgsm_ms_encoder_deps="libgsm"
 +libmodplug_demuxer_deps="libmodplug"
  libmp3lame_encoder_deps="libmp3lame"
  libopencore_amrnb_decoder_deps="libopencore_amrnb"
  libopencore_amrnb_encoder_deps="libopencore_amrnb"
  libopencore_amrwb_decoder_deps="libopencore_amrwb"
  libopenjpeg_decoder_deps="libopenjpeg"
 +libopenjpeg_encoder_deps="libopenjpeg"
  libschroedinger_decoder_deps="libschroedinger"
  libschroedinger_encoder_deps="libschroedinger"
  libspeex_decoder_deps="libspeex"
  libspeex_encoder_deps="libspeex"
 +libstagefright_h264_decoder_deps="libstagefright_h264"
  libtheora_encoder_deps="libtheora"
  libvo_aacenc_encoder_deps="libvo_aacenc"
  libvo_amrwbenc_encoder_deps="libvo_amrwbenc"
@@@ -1534,7 -1425,6 +1534,7 @@@ libvpx_encoder_deps="libvpx
  libx264_encoder_deps="libx264"
  libxavs_encoder_deps="libxavs"
  libxvid_encoder_deps="libxvid"
 +libutvideo_decoder_deps="libutvideo gpl"
  
  # demuxers / muxers
  ac3_demuxer_select="ac3_parser"
@@@ -1571,20 -1461,14 +1571,20 @@@ w64_demuxer_deps="wav_demuxer
  alsa_indev_deps="alsa_asoundlib_h snd_pcm_htimestamp"
  alsa_outdev_deps="alsa_asoundlib_h"
  bktr_indev_deps_any="dev_bktr_ioctl_bt848_h machine_ioctl_bt848_h dev_video_bktr_ioctl_bt848_h dev_ic_bt8xx_h"
 +dshow_indev_deps="IBaseFilter"
 +dshow_indev_extralibs="-lpsapi -lole32 -lstrmiids -luuid"
  dv1394_indev_deps="dv1394 dv_demuxer"
  fbdev_indev_deps="linux_fb_h"
 -jack_indev_deps="jack_jack_h"
 +jack_indev_deps="jack_jack_h sem_timedwait"
 +lavfi_indev_deps="avfilter"
  libcdio_indev_deps="libcdio"
  libdc1394_indev_deps="libdc1394"
 +libv4l2_indev_deps="libv4l2"
 +openal_indev_deps="openal"
  oss_indev_deps_any="soundcard_h sys_soundcard_h"
  oss_outdev_deps_any="soundcard_h sys_soundcard_h"
  pulse_indev_deps="libpulse"
 +sdl_outdev_deps="sdl"
  sndio_indev_deps="sndio_h"
  sndio_outdev_deps="sndio_h"
  v4l_indev_deps="linux_videodev_h"
@@@ -1609,19 -1493,14 +1609,19 @@@ tls_protocol_select="tcp_protocol
  udp_protocol_deps="network"
  
  # filters
 +amovie_filter_deps="avcodec avformat"
  blackframe_filter_deps="gpl"
  boxblur_filter_deps="gpl"
  cropdetect_filter_deps="gpl"
  delogo_filter_deps="gpl"
  drawtext_filter_deps="libfreetype"
 -frei0r_filter_deps="frei0r dlopen strtok_r"
 -frei0r_src_filter_deps="frei0r dlopen strtok_r"
 +frei0r_filter_deps="frei0r dlopen"
 +frei0r_src_filter_deps="frei0r dlopen"
  hqdn3d_filter_deps="gpl"
 +movie_filter_deps="avcodec avformat"
 +mp_filter_deps="gpl avcodec"
 +mptestsrc_filter_deps="gpl"
 +negate_filter_deps="lut_filter"
  ocv_filter_deps="libopencv"
  scale_filter_deps="swscale"
  yadif_filter_deps="gpl"
@@@ -1632,15 -1511,15 +1632,15 @@@ avformat_deps="avcodec
  postproc_deps="gpl"
  
  # programs
 -ffmpeg_deps="avcodec avformat swscale"
 -ffmpeg_select="buffer_filter"
 +ffmpeg_deps="avcodec avformat swscale swresample"
 +ffmpeg_select="buffer_filter buffersink_filter"
  avconv_deps="avcodec avformat swscale"
  avconv_select="buffer_filter"
 -avplay_deps="avcodec avformat swscale sdl"
 -avplay_select="rdft"
 -avprobe_deps="avcodec avformat"
 -avserver_deps="avformat ffm_muxer fork rtp_protocol rtsp_demuxer"
 -avserver_extralibs='$ldl'
 +ffplay_deps="avcodec avformat swscale sdl"
 +ffplay_select="buffersink_filter rdft"
 +ffprobe_deps="avcodec avformat"
 +ffserver_deps="avformat ffm_muxer fork rtp_protocol rtsp_demuxer"
 +ffserver_extralibs='$ldl'
  
  doc_deps="texi2html"
  
@@@ -1745,7 -1624,7 +1745,7 @@@ logfile="config.log
  # installation paths
  prefix_default="/usr/local"
  bindir_default='${prefix}/bin'
 -datadir_default='${prefix}/share/avconv'
 +datadir_default='${prefix}/share/ffmpeg'
  incdir_default='${prefix}/include'
  libdir_default='${prefix}/lib'
  mandir_default='${prefix}/share/man'
@@@ -1754,16 -1633,13 +1754,16 @@@ shlibdir_default="$libdir_default
  # toolchain
  ar_default="ar"
  cc_default="gcc"
 +cxx_default="g++"
  cc_version=\"unknown\"
  host_cc_default="gcc"
 +install="install"
  ln_s="ln -sf"
  nm_default="nm"
  objformat="elf"
  pkg_config_default=pkg-config
  ranlib="ranlib"
 +strip_default="strip"
  yasmexe="yasm"
  nogas=":"
  
@@@ -1789,22 -1665,20 +1789,22 @@@ enable do
  enable fastdiv
  enable ffmpeg
  enable avconv
 -enable avplay
 -enable avprobe
 -enable avserver
 +enable ffplay
 +enable ffprobe
 +enable ffserver
  enable network
  enable optimizations
  enable postproc
  enable protocols
  enable static
 +enable stripping
 +enable swresample
  enable swscale
  enable swscale_alpha
  
  # build settings
  SHFLAGS='-shared -Wl,-soname,$$(@F)'
 -AVSERVERLDFLAGS=-Wl,-E
 +FFSERVERLDFLAGS=-Wl,-E
  LIBPREF="lib"
  LIBSUF=".a"
  FULLNAME='$(NAME)$(BUILDSUF)'
@@@ -1820,7 -1694,6 +1820,7 @@@ SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJ
  
  AS_O='-o $@'
  CC_O='-o $@'
 +CXX_O='-o $@'
  
  host_cflags='-D_ISOC99_SOURCE -O3 -g'
  host_libs='-lm'
@@@ -1847,7 -1720,7 +1847,7 @@@ for v in "$@"; d
      r=${v#*=}
      l=${v%"$r"}
      r=$(sh_quote "$r")
 -    LIBAV_CONFIGURATION="${LIBAV_CONFIGURATION# } ${l}${r}"
 +    FFMPEG_CONFIGURATION="${FFMPEG_CONFIGURATION# } ${l}${r}"
  done
  
  find_things(){
@@@ -1959,7 -1832,7 +1959,7 @@@ don
  
  disabled logging && logfile=/dev/null
  
 -echo "# $0 $LIBAV_CONFIGURATION" > $logfile
 +echo "# $0 $FFMPEG_CONFIGURATION" > $logfile
  set >> $logfile
  
  test -n "$cross_prefix" && enable cross_compile
@@@ -1973,15 -1846,13 +1973,15 @@@ set_default arch target_o
  
  ar_default="${cross_prefix}${ar_default}"
  cc_default="${cross_prefix}${cc_default}"
 +cxx_default="${cross_prefix}${cxx_default}"
  nm_default="${cross_prefix}${nm_default}"
  pkg_config_default="${cross_prefix}${pkg_config_default}"
  ranlib="${cross_prefix}${ranlib}"
 +strip_default="${cross_prefix}${strip_default}"
  
  sysinclude_default="${sysroot}/usr/include"
  
 -set_default cc nm pkg_config sysinclude
 +set_default cc cxx nm pkg_config strip sysinclude
  enabled cross_compile || host_cc_default=$cc
  set_default host_cc
  
@@@ -2023,7 -1894,6 +2023,7 @@@ tmpfile()
  trap 'rm -f -- $TMPFILES' EXIT
  
  tmpfile TMPC  .c
 +tmpfile TMPCPP .cpp
  tmpfile TMPE  $EXESUF
  tmpfile TMPH  .h
  tmpfile TMPO  .o
@@@ -2241,11 -2111,9 +2241,11 @@@ test -n "$cc_type" && enable $cc_type |
  set_default ar as dep_cc ld
  
  test -n "$CC_DEPFLAGS" || CCDEP=$DEPEND_CMD
 +test -n "$CXX_DEPFLAGS" || CXXDEP=$DEPEND_CMD
  test -n "$AS_DEPFLAGS" || ASDEP=$DEPEND_CMD
  
  add_cflags $extra_cflags
 +add_cxxflags $extra_cxxflags
  add_asflags $extra_cflags
  
  if test -n "$sysroot"; then
@@@ -2300,8 -2168,12 +2300,8 @@@ case "$arch" i
          arch="parisc"
          subarch="parisc64"
      ;;
 -    "Power Macintosh"|ppc|powerpc)
 -        arch="ppc"
 -    ;;
 -    ppc64|powerpc64)
 +    "Power Macintosh"|ppc|powerpc|ppc64|powerpc64)
          arch="ppc"
 -        subarch="ppc64"
      ;;
      s390|s390x)
          arch="s390"
          arch="sparc"
          subarch="sparc64"
      ;;
 -    i[3-6]86|i86pc|BePC|x86pc|x86_64|amd64)
 +    i[3-6]86|i86pc|BePC|x86pc|x86_64|x86_32|amd64)
          arch="x86"
      ;;
  esac
@@@ -2476,7 -2348,6 +2476,7 @@@ if test "$?" != 0; the
  fi
  
  add_cppflags -D_ISOC99_SOURCE
 +add_cxxflags -D__STDC_CONSTANT_MACROS
  check_cflags -std=c99
  check_cc -D_FILE_OFFSET_BITS=64 <<EOF && add_cppflags -D_FILE_OFFSET_BITS=64
  #include <stdlib.h>
              spic=$shared
          fi
      ;;
 +    ppc)
 +        check_cc <<EOF && subarch="ppc64"
 +        int test[(int)sizeof(char*) - 7];
 +EOF
 +    ;;
  esac
  
  enable $subarch
@@@ -2519,7 -2385,7 +2519,7 @@@ case $target_os i
          host_libs=
          ;;
      sunos)
 -        AVSERVERLDFLAGS=""
 +        FFSERVERLDFLAGS=""
          SHFLAGS='-shared -Wl,-h,$$(@F)'
          enabled x86 && SHFLAGS="-mimpure-text $SHFLAGS"
          network_extralibs="-lsocket -lnsl"
      openbsd)
          enable malloc_aligned
          # On OpenBSD 4.5. the compiler does not use PIC unless
 -        # explicitly using -fPIC. Libav builds fine without PIC,
 +        # explicitly using -fPIC. FFmpeg builds fine without PIC,
          # however the generated executable will not do anything
          # (simply quits with exit-code 1, no crash, no output).
          # Thus explicitly enable PIC here.
          ;;
      bsd/os)
          add_extralibs -lpoll -lgnugetopt
 +        strip="strip -d"
          ;;
      darwin)
          enable malloc_aligned
          enabled ppc && add_asflags -force_cpusubtype_ALL
          SHFLAGS='-dynamiclib -Wl,-single_module -Wl,-install_name,$(SHLIBDIR)/$(SLIBNAME),-current_version,$(LIBVERSION),-compatibility_version,$(LIBMAJOR)'
          enabled x86_32 && append SHFLAGS -Wl,-read_only_relocs,suppress
 +        strip="${strip} -x"
          add_ldflags -Wl,-dynamic,-search_paths_first
          SLIBSUF=".dylib"
          SLIBNAME_WITH_VERSION='$(SLIBPREF)$(FULLNAME).$(LIBVERSION)$(SLIBSUF)'
          SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME).$(LIBMAJOR)$(SLIBSUF)'
 -        AVSERVERLDFLAGS=-Wl,-bind_at_load
 +        FFSERVERLDFLAGS=-Wl,-bind_at_load
          objformat="macho"
          enabled x86_64 && objformat="macho64"
          enabled_any pic shared ||
              disable network
          else
              target_os=mingw32
-             enable_weak w32threads
          fi
          LIBTARGET=i386
          if enabled x86_64; then
              enable malloc_aligned
 -            LIBTARGET="i386:x86-64"
 +            LIBTARGET=x64
          elif enabled arm; then
              LIBTARGET=arm-wince
          fi
          SLIBSUF=".dll"
          SLIBNAME_WITH_VERSION='$(SLIBPREF)$(FULLNAME)-$(LIBVERSION)$(SLIBSUF)'
          SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
 -        SLIB_EXTRA_CMD=-'$(DLLTOOL) -m $(LIBTARGET) -d $$(@:$(SLIBSUF)=.def) -l $(SUBDIR)$(SLIBNAME:$(SLIBSUF)=.lib) -D $(SLIBNAME_WITH_MAJOR)'
 +        SLIB_EXTRA_CMD='-lib.exe /machine:$(LIBTARGET) /def:$$(@:$(SLIBSUF)=.def) /out:$(SUBDIR)$(SLIBNAME:$(SLIBSUF)=.lib)'
          SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
          SLIB_INSTALL_LINKS=
          SLIB_INSTALL_EXTRA_SHLIB='$(SLIBNAME:$(SLIBSUF)=.lib)'
          SLIB_INSTALL_EXTRA_LIB='lib$(SLIBNAME:$(SLIBSUF)=.dll.a) $(SLIBNAME_WITH_MAJOR:$(SLIBSUF)=.def)'
          SHFLAGS='-shared -Wl,--output-def,$$(@:$(SLIBSUF)=.def) -Wl,--out-implib,$(SUBDIR)lib$(SLIBNAME:$(SLIBSUF)=.dll.a) -Wl,--enable-runtime-pseudo-reloc -Wl,--enable-auto-image-base'
          objformat="win32"
 -        dlltool="${cross_prefix}dlltool"
          enable dos_paths
          check_cflags -fno-common
          check_cpp_condition _mingw.h "defined (__MINGW64_VERSION_MAJOR) || (__MINGW32_MAJOR_VERSION > 3) \
          ranlib="echo ignoring ranlib"
          ;;
      os/2*)
 +        strip="lxlite -CS"
          ln_s="cp -f"
          objformat="aout"
          add_cppflags -D_GNU_SOURCE
          add_ldflags -Zomf -Zbin-files -Zargs-wild -Zmap
          SHFLAGS='$(SUBDIR)$(NAME).def -Zdll -Zomf'
 -        AVSERVERLDFLAGS=""
 +        FFSERVERLDFLAGS=""
          LIBSUF="_s.a"
          SLIBPREF=""
          SLIBSUF=".dll"
            emximp -o $(SUBDIR)$(LIBPREF)$(NAME)_dll.lib $(SUBDIR)$(NAME).def;'
          SLIB_INSTALL_EXTRA_LIB='$(LIBPREF)$(NAME)_dll.a $(LIBPREF)$(NAME)_dll.lib'
          enable dos_paths
 +        enable_weak os2threads
          ;;
      gnu/kfreebsd)
          add_cppflags -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -D_BSD_SOURCE
          ;;
  esac
  
 -echo "config:$arch:$subarch:$cpu:$target_os:$cc_ident:$LIBAV_CONFIGURATION" >config.fate
 +echo "config:$arch:$subarch:$cpu:$target_os:$cc_ident:$FFMPEG_CONFIGURATION" >config.fate
  
  check_cpp_condition stdlib.h "defined(__PIC__) || defined(__pic__) || defined(PIC)" && enable pic
  
@@@ -2719,7 -2581,6 +2718,7 @@@ die_license_disabled gpl libxav
  die_license_disabled gpl libxvid
  die_license_disabled gpl x11grab
  
 +die_license_disabled nonfree libaacplus
  die_license_disabled nonfree libfaac
  die_license_disabled nonfree openssl
  
@@@ -2785,7 -2646,7 +2784,7 @@@ elif enabled arm; the
      elif ! check_cpp_condition stddef.h "defined __ARM_PCS || defined __SOFTFP__"; then
          case "${cross_prefix:-$cc}" in
              *hardfloat*)         enable vfp_args;   fpabi=vfp ;;
 -            *) check_ld <<EOF && enable vfp_args && fpabi=vfp || fpabi=soft ;;
 +            *) check_ld "cc" <<EOF && enable vfp_args && fpabi=vfp || fpabi=soft ;;
  __asm__ (".eabi_attribute 28, 1");
  int main(void) { return 0; }
  EOF
@@@ -2966,8 -2827,7 +2965,8 @@@ check_func  ${malloc_prefix}posix_memal
  check_func  setrlimit
  check_func  strerror_r
  check_func  strptime
 -check_func  strtok_r
 +check_func_headers conio.h kbhit
 +check_func_headers windows.h PeekNamedPipe
  check_func_headers io.h setmode
  check_func_headers lzo/lzo1x.h lzo1x_999_compress
  check_lib2 "windows.h psapi.h" GetProcessMemoryInfo -lpsapi
@@@ -2976,32 -2836,28 +2975,36 @@@ check_func_headers windows.h MapViewOfF
  check_func_headers windows.h VirtualAlloc
  
  check_header dlfcn.h
 -check_header dxva2api.h
 +check_header dxva2api.h -D_WIN32_WINNT=0x0600
 +check_header libcrystalhd/libcrystalhd_if.h
  check_header malloc.h
  check_header poll.h
  check_header sys/mman.h
  check_header sys/resource.h
  check_header sys/select.h
 +check_header termios.h
  check_header vdpau/vdpau.h
  check_header vdpau/vdpau_x11.h
  check_header X11/extensions/XvMClib.h
  
 -check_struct dxva2api.h DXVA_PictureParameters wDecodedPictureIndex
 -
  disabled  zlib || check_lib   zlib.h      zlibVersion -lz   || disable  zlib
  disabled bzlib || check_lib2 bzlib.h BZ2_bzlibVersion -lbz2 || disable bzlib
  
 +# check for VDA header
 +if ! disabled vda; then
 +    if check_header VideoDecodeAcceleration/VDADecoder.h; then
 +        enable vda
 +        add_ldflags -framework CoreFoundation -framework VideoDecodeAcceleration -framework QuartzCore
 +    fi
 +fi
 +
+ if ! disabled w32threads && ! enabled pthreads; then
+     check_func _beginthreadex && enable w32threads
+ fi
  # check for some common methods of building with pthread support
  # do this before the optional library checks as some of them require pthreads
 -if ! disabled pthreads && ! enabled w32threads; then
 +if ! disabled pthreads && ! enabled w32threads && ! enabled os2threads; then
      enable pthreads
      if check_func pthread_create; then
          :
@@@ -3027,10 -2883,8 +3030,10 @@@ for thread in $THREADS_LIST; d
  done
  
  check_lib math.h sin -lm && LIBM="-lm"
 +disabled crystalhd || check_lib libcrystalhd/libcrystalhd_if.h DtsCrystalHDVersion -lcrystalhd || disable crystalhd
  enabled vaapi && require vaapi va/va.h vaInitialize -lva
  
 +check_mathfunc cbrtf
  check_mathfunc exp2
  check_mathfunc exp2f
  check_mathfunc llrint
@@@ -3046,10 -2900,7 +3049,10 @@@ check_mathfunc trunc
  
  # these are off by default, so fail if requested and not available
  enabled avisynth   && require2 vfw32 "windows.h vfw.h" AVIFileInit -lavifil32
 +enabled libcelt    && require libcelt celt/celt.h celt_decode -lcelt0
  enabled frei0r     && { check_header frei0r.h || die "ERROR: frei0r.h header not found"; }
 +enabled libaacplus && require  "libaacplus >= 2.0.0" aacplus.h aacplusEncOpen -laacplus
 +enabled libdc1394  && require_pkg_config libdc1394-2 dc1394/dc1394.h dc1394_new
  enabled gnutls     && require_pkg_config gnutls gnutls/gnutls.h gnutls_global_init
  enabled libdirac   && require_pkg_config dirac                          \
      "libdirac_decoder/dirac_parser.h libdirac_encoder/dirac_encoder.h"  \
  enabled libfaac    && require2 libfaac "stdint.h faac.h" faacEncGetVersion -lfaac
  enabled libfreetype && require_pkg_config freetype2 "ft2build.h freetype/freetype.h" FT_Init_FreeType
  enabled libgsm     && require  libgsm gsm/gsm.h gsm_create -lgsm
 +enabled libmodplug && require  libmodplug libmodplug/modplug.h ModPlug_Load -lmodplug
  enabled libmp3lame && require  "libmp3lame >= 3.98.3" lame/lame.h lame_set_VBR_quality -lmp3lame
  enabled libnut     && require  libnut libnut.h nut_demuxer_init -lnut
  enabled libopencore_amrnb  && require libopencore_amrnb opencore-amrnb/interf_dec.h Decoder_Interface_init -lopencore-amrnb
  enabled libopencore_amrwb  && require libopencore_amrwb opencore-amrwb/dec_if.h D_IF_init -lopencore-amrwb
 -enabled libopencv  && require_pkg_config opencv opencv/cv.h cvCreateImageHeader
 +enabled libopencv  && require_pkg_config opencv opencv/cxcore.h cvCreateImageHeader
  enabled libopenjpeg && require libopenjpeg openjpeg.h opj_version -lopenjpeg
  enabled libpulse && require_pkg_config libpulse-simple pulse/simple.h pa_simple_new
  enabled librtmp    && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket
  enabled libschroedinger && require_pkg_config schroedinger-1.0 schroedinger/schro.h schro_init
  enabled libspeex   && require  libspeex speex/speex.h speex_decoder_init -lspeex
 +enabled libstagefright_h264  && require_cpp libstagefright_h264 "binder/ProcessState.h media/stagefright/MetaData.h
 +    media/stagefright/MediaBufferGroup.h media/stagefright/MediaDebug.h media/stagefright/MediaDefs.h
 +    media/stagefright/OMXClient.h media/stagefright/OMXCodec.h" android::OMXClient -lstagefright -lmedia -lutils -lbinder
  enabled libtheora  && require  libtheora theora/theoraenc.h th_info_init -ltheoraenc -ltheoradec -logg
 +enabled libutvideo    && require_cpp utvideo "stdint.h stdlib.h utvideo/utvideo.h utvideo/Codec.h" 'CCodec*' -lutvideo -lstdc++
 +enabled libv4l2    && require_pkg_config libv4l2 libv4l2.h v4l2_ioctl
  enabled libvo_aacenc && require libvo_aacenc vo-aacenc/voAAC.h voGetAACEncAPI -lvo-aacenc
  enabled libvo_amrwbenc && require libvo_amrwbenc vo-amrwbenc/enc_if.h E_IF_init -lvo-amrwbenc
  enabled libvorbis  && require  libvorbis vorbis/vorbisenc.h vorbis_info_init -lvorbisenc -lvorbis -logg
  enabled libvpx     && {
      enabled libvpx_decoder && { check_lib2 "vpx/vpx_decoder.h vpx/vp8dx.h" vpx_codec_dec_init_ver -lvpx ||
                                  die "ERROR: libvpx decoder version must be >=0.9.1"; }
 -    enabled libvpx_encoder && { check_lib2 "vpx/vpx_encoder.h vpx/vp8cx.h" vpx_codec_enc_init_ver -lvpx ||
 -                                die "ERROR: libvpx encoder version must be >=0.9.1"; } }
 +    enabled libvpx_encoder && { check_lib2 "vpx/vpx_encoder.h vpx/vp8cx.h" "vpx_codec_enc_init_ver VPX_CQ" -lvpx ||
 +                                die "ERROR: libvpx encoder version must be >=0.9.6"; } }
  enabled libx264    && require  libx264 x264.h x264_encoder_encode -lx264 &&
                        { check_cpp_condition x264.h "X264_BUILD >= 118" ||
                          die "ERROR: libx264 version must be >= 0.118."; }
  enabled libxavs    && require  libxavs xavs.h xavs_encoder_encode -lxavs
  enabled libxvid    && require  libxvid xvid.h xvid_global -lxvidcore
 +enabled openal     && { { for al_libs in "${OPENAL_LIBS}" "-lopenal" "-lOpenAL32"; do
 +                        check_lib 'AL/al.h' alGetError "${al_libs}" && break; done } ||
 +                        die "ERROR: openal not found"; } &&
 +                      { check_cpp_condition "AL/al.h" "defined(AL_VERSION_1_1)" ||
 +                        die "ERROR: openal version must be 1.1 or compatible"; }
  enabled mlib       && require  mediaLib mlib_types.h mlib_VectorSub_S16_U8_Mod -lmlib
  enabled openssl    && { check_lib openssl/ssl.h SSL_library_init -lssl -lcrypto ||
                          check_lib openssl/ssl.h SSL_library_init -lssl32 -leay32 ||
                          check_lib openssl/ssl.h SSL_library_init -lssl -lcrypto -lws2_32 -lgdi32 ||
                          die "ERROR: openssl not found"; }
  
 -# libdc1394 check
 -if enabled libdc1394; then
 -    { check_lib dc1394/dc1394.h dc1394_new -ldc1394 -lraw1394 &&
 -        enable libdc1394_2; } ||
 -    { check_lib libdc1394/dc1394_control.h dc1394_create_handle -ldc1394_control -lraw1394 &&
 -        enable libdc1394_1; } ||
 -    die "ERROR: No version of libdc1394 found "
 -fi
 -
 +SDL_CONFIG="${cross_prefix}sdl-config"
  if check_pkg_config sdl SDL_version.h SDL_Linked_Version; then
      check_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) >= 0x010201" $sdl_cflags &&
      enable sdl &&
      check_struct SDL.h SDL_VideoInfo current_w $sdl_cflags && enable sdl_video_size
 +else
 +  if "${SDL_CONFIG}" --version > /dev/null 2>&1; then
 +    sdl_cflags=$("${SDL_CONFIG}" --cflags)
 +    sdl_libs=$("${SDL_CONFIG}" --libs)
 +    check_func_headers SDL_version.h SDL_Linked_Version $sdl_cflags $sdl_libs &&
 +    check_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) >= 0x010201" $sdl_cflags &&
 +    enable sdl &&
 +    check_struct SDL.h SDL_VideoInfo current_w $sdl_cflags && enable sdl_video_size
 +  fi
  fi
 +enabled sdl && add_cflags $sdl_cflags && add_extralibs $sdl_libs
  
  texi2html -version > /dev/null 2>&1 && enable texi2html || disable texi2html
  
@@@ -3127,8 -2965,6 +3130,8 @@@ check_func_headers "windows.h vfw.h" ca
  # w32api 3.12 had it defined wrong
  check_cpp_condition vfw.h "WM_CAP_DRIVER_CONNECT > WM_USER" && enable vfwcap_defines
  
 +check_type "dshow.h" IBaseFilter
 +
  # check for ioctl_meteor.h, ioctl_bt848.h and alternatives
  { check_header dev/bktr/ioctl_meteor.h &&
    check_header dev/bktr/ioctl_bt848.h; } ||
  check_header dev/ic/bt8xx.h
  
  check_header sndio.h
 -check_header sys/soundcard.h
 +if check_struct sys/soundcard.h audio_buf_info bytes; then
 +    enable_safe sys/soundcard.h
 +else
 +    check_cc -D__BSD_VISIBLE -D__XSI_VISIBLE <<EOF && add_cppflags -D__BSD_VISIBLE -D__XSI_VISIBLE && enable_safe sys/soundcard.h
 +    #include <sys/soundcard.h>
 +    audio_buf_info abc;
 +EOF
 +fi
  check_header soundcard.h
  
  enabled_any alsa_indev alsa_outdev && check_lib2 alsa/asoundlib.h snd_pcm_htimestamp -lasound
  
 -enabled jack_indev && check_lib2 jack/jack.h jack_client_open -ljack
 +enabled jack_indev && check_lib2 jack/jack.h jack_client_open -ljack && check_func sem_timedwait
  
  enabled_any sndio_indev sndio_outdev && check_lib2 sndio.h sio_open -lsndio
  
@@@ -3166,11 -2995,9 +3169,11 @@@ check_func XOpenDisplay -lX1
  check_func XShmCreateImage -lX11 -lXext &&
  check_func XFixesGetCursorImage -lX11 -lXext -lXfixes
  
 -# check for VDA header
 -if ! disabled vda && check_header VideoDecodeAcceleration/VDADecoder.h; then
 -    enable vda && add_ldflags -framework CoreFoundation -framework VideoDecodeAcceleration -framework QuartzCore
 +if ! disabled vaapi; then
 +    check_lib va/va.h vaInitialize -lva && {
 +        check_cpp_condition va/va_version.h "VA_CHECK_VERSION(0,32,0)" ||
 +        warn "Please upgrade to VA-API >= 0.32 if you would like full VA-API support.";
 +    } || disable vaapi
  fi
  
  if ! disabled vdpau && enabled vdpau_vdpau_h; then
@@@ -3197,13 -3024,12 +3200,13 @@@ check_cflags -Wwrite-string
  check_cflags -Wtype-limits
  check_cflags -Wundef
  check_cflags -Wmissing-prototypes
 +check_cflags -Wno-pointer-to-int-cast
  check_cflags -Wstrict-prototypes
  enabled extra_warnings && check_cflags -Winline
  
  # add some linker flags
  check_ldflags -Wl,--warn-common
 -check_ldflags -Wl,-rpath-link=libpostproc:libswscale:libavfilter:libavdevice:libavformat:libavcodec:libavutil
 +check_ldflags -Wl,-rpath-link=libpostproc:libswresample:libswscale:libavfilter:libavdevice:libavformat:libavcodec:libavutil
  test_ldflags -Wl,-Bsymbolic && append SHFLAGS -Wl,-Bsymbolic
  
  echo "X{};" > $TMPV
  fi
  check_cflags -fno-math-errno
  check_cflags -fno-signed-zeros
 +check_cc -mno-red-zone <<EOF && noredzone_flags="-mno-red-zone"
 +int x;
 +EOF
 +
  
  if enabled icc; then
      # Just warnings, no remarks
@@@ -3312,11 -3134,6 +3315,11 @@@ check_deps $CONFIG_LIST       
  
  enabled asm || { arch=c; disable $ARCH_LIST $ARCH_EXT_LIST; }
  
 +if test $target_os = "haiku"; then
 +    disable memalign
 +    disable posix_memalign
 +fi
 +
  ! enabled_any memalign posix_memalign malloc_aligned &&
      enabled_any $need_memalign && enable memalign_hack
  
@@@ -3327,9 -3144,6 +3330,9 @@@ echo "ARCH                      $arch (
  if test "$build_suffix" != ""; then
      echo "build suffix              $build_suffix"
  fi
 +if test "$progs_suffix" != ""; then
 +    echo "progs suffix              $progs_suffix"
 +fi
  if test "$extra_version" != ""; then
      echo "version string suffix     $extra_version"
  fi
@@@ -3369,7 -3183,6 +3372,7 @@@ if enabled sparc; the
      echo "VIS enabled               ${vis-no}"
  fi
  echo "debug symbols             ${debug-no}"
 +echo "strip symbols             ${stripping-no}"
  echo "optimize for size         ${small-no}"
  echo "optimizations             ${optimizations-no}"
  echo "static                    ${static-no}"
@@@ -3384,16 -3197,13 +3387,16 @@@ echo "libdxva2 enabled          ${dxva2
  echo "libva enabled             ${vaapi-no}"
  echo "libvdpau enabled          ${vdpau-no}"
  echo "AVISynth enabled          ${avisynth-no}"
 +echo "libcelt enabled           ${libcelt-no}"
  echo "frei0r enabled            ${frei0r-no}"
  echo "gnutls enabled            ${gnutls-no}"
  echo "libcdio support           ${libcdio-no}"
  echo "libdc1394 support         ${libdc1394-no}"
  echo "libdirac enabled          ${libdirac-no}"
  echo "libfaac enabled           ${libfaac-no}"
 +echo "libaacplus enabled        ${libaacplus-no}"
  echo "libgsm enabled            ${libgsm-no}"
 +echo "libmodplug enabled        ${libmodplug-no}"
  echo "libmp3lame enabled        ${libmp3lame-no}"
  echo "libnut enabled            ${libnut-no}"
  echo "libopencore-amrnb support ${libopencore_amrnb-no}"
@@@ -3404,10 -3214,7 +3407,10 @@@ echo "libpulse enabled          ${libpu
  echo "librtmp enabled           ${librtmp-no}"
  echo "libschroedinger enabled   ${libschroedinger-no}"
  echo "libspeex enabled          ${libspeex-no}"
 +echo "libstagefright-h264 enabled    ${libstagefright_h264-no}"
  echo "libtheora enabled         ${libtheora-no}"
 +echo "libutvideo enabled        ${libutvideo-no}"
 +echo "libv4l2 enabled           ${libv4l2-no}"
  echo "libvo-aacenc support      ${libvo_aacenc-no}"
  echo "libvo-amrwbenc support    ${libvo_amrwbenc-no}"
  echo "libvorbis enabled         ${libvorbis-no}"
@@@ -3415,7 -3222,6 +3418,7 @@@ echo "libvpx enabled            ${libvp
  echo "libx264 enabled           ${libx264-no}"
  echo "libxavs enabled           ${libxavs-no}"
  echo "libxvid enabled           ${libxvid-no}"
 +echo "openal enabled            ${openal-no}"
  echo "openssl enabled           ${openssl-no}"
  echo "zlib enabled              ${zlib-no}"
  echo "bzlib enabled             ${bzlib-no}"
@@@ -3445,15 -3251,11 +3448,15 @@@ echo "Creating config.mak and config.h.
  
  test -e Makefile || $ln_s "$source_path/Makefile" .
  
 +enabled stripping || strip="echo skipping strip"
 +
  config_files="$TMPH config.mak"
  
  cat > config.mak <<EOF
  # Automatically generated by configure - do not modify!
 -LIBAV_CONFIGURATION=$LIBAV_CONFIGURATION
 +ifndef FFMPEG_CONFIG_MAK
 +FFMPEG_CONFIG_MAK=1
 +FFMPEG_CONFIGURATION=$FFMPEG_CONFIGURATION
  prefix=$prefix
  LIBDIR=\$(DESTDIR)$libdir
  SHLIBDIR=\$(DESTDIR)$shlibdir
@@@ -3462,13 -3264,9 +3465,13 @@@ BINDIR=\$(DESTDIR)$bindi
  DATADIR=\$(DESTDIR)$datadir
  MANDIR=\$(DESTDIR)$mandir
  SRC_PATH=$source_path
 +ifndef MAIN_MAKEFILE
 +SRC_PATH:=\$(SRC_PATH:.%=..%)
 +endif
  CC_IDENT=$cc_ident
  ARCH=$arch
  CC=$cc
 +CXX=$cxx
  AS=$as
  LD=$ld
  DEPCC=$dep_cc
@@@ -3476,22 -3274,18 +3479,22 @@@ YASM=$yasmex
  YASMDEP=$yasmexe
  AR=$ar
  RANLIB=$ranlib
 +CP=cp -p
  LN_S=$ln_s
 +STRIP=$strip
  CPPFLAGS=$CPPFLAGS
  CFLAGS=$CFLAGS
 +CXXFLAGS=$CXXFLAGS
  ASFLAGS=$ASFLAGS
  AS_O=$CC_O
  CC_O=$CC_O
 -DLLTOOL=$dlltool
 +CXX_O=$CXX_O
  LDFLAGS=$LDFLAGS
 -AVSERVERLDFLAGS=$AVSERVERLDFLAGS
 +FFSERVERLDFLAGS=$FFSERVERLDFLAGS
  SHFLAGS=$SHFLAGS
  YASMFLAGS=$YASMFLAGS
  BUILDSUF=$build_suffix
 +PROGSSUF=$progs_suffix
  FULLNAME=$FULLNAME
  LIBPREF=$LIBPREF
  LIBSUF=$LIBSUF
@@@ -3502,7 -3296,6 +3505,7 @@@ EXESUF=$EXESU
  EXTRA_VERSION=$extra_version
  DEPFLAGS=$DEPFLAGS
  CCDEP=$CCDEP
 +CXXDEP=$CXXDEP
  ASDEP=$ASDEP
  CC_DEPFLAGS=$CC_DEPFLAGS
  AS_DEPFLAGS=$AS_DEPFLAGS
@@@ -3517,7 -3310,7 +3520,7 @@@ SDL_LIBS=$sdl_lib
  SDL_CFLAGS=$sdl_cflags
  LIB_INSTALL_EXTRA_CMD=$LIB_INSTALL_EXTRA_CMD
  EXTRALIBS=$extralibs
 -INSTALL=install
 +INSTALL=$install
  LIBTARGET=${LIBTARGET}
  SLIBNAME=${SLIBNAME}
  SLIBNAME_WITH_VERSION=${SLIBNAME_WITH_VERSION}
@@@ -3529,7 -3322,6 +3532,7 @@@ SLIB_INSTALL_LINKS=${SLIB_INSTALL_LINKS
  SLIB_INSTALL_EXTRA_LIB=${SLIB_INSTALL_EXTRA_LIB}
  SLIB_INSTALL_EXTRA_SHLIB=${SLIB_INSTALL_EXTRA_SHLIB}
  SAMPLES:=${samples:-\$(FATE_SAMPLES)}
 +NOREDZONE_FLAGS=$noredzone_flags
  EOF
  
  get_version(){
  }
  
  get_version LIBSWSCALE  libswscale/swscale.h
 +get_version LIBSWRESAMPLE libswresample/swresample.h
  get_version LIBPOSTPROC libpostproc/postprocess.h
  get_version LIBAVCODEC  libavcodec/version.h
  get_version LIBAVDEVICE libavdevice/avdevice.h
@@@ -3553,11 -3344,10 +3556,11 @@@ get_version LIBAVFILTER libavfilter/avf
  
  cat > $TMPH <<EOF
  /* Automatically generated by configure - do not modify! */
 -#ifndef LIBAV_CONFIG_H
 -#define LIBAV_CONFIG_H
 -#define LIBAV_CONFIGURATION "$(c_escape $LIBAV_CONFIGURATION)"
 -#define LIBAV_LICENSE "$(c_escape $license)"
 +#ifndef FFMPEG_CONFIG_H
 +#define FFMPEG_CONFIG_H
 +#define FFMPEG_CONFIGURATION "$(c_escape $FFMPEG_CONFIGURATION)"
 +#define FFMPEG_LICENSE "$(c_escape $license)"
 +#define FFMPEG_DATADIR "$(eval c_escape $datadir)"
  #define AVCONV_DATADIR "$(eval c_escape $datadir)"
  #define CC_TYPE "$cc_type"
  #define CC_VERSION $cc_version
@@@ -3608,8 -3398,7 +3611,8 @@@ LAVFI_TESTS=$(print_enabled  -n _test $
  SEEK_TESTS=$(print_enabled   -n _test $SEEK_TESTS)
  EOF
  
 -echo "#endif /* LIBAV_CONFIG_H */" >> $TMPH
 +echo "#endif /* FFMPEG_CONFIG_H */" >> $TMPH
 +echo "endif # FFMPEG_CONFIG_MAK" >> config.mak
  
  # Do not overwrite an unchanged config.h to avoid superfluous rebuilds.
  cp_if_changed $TMPH config.h
@@@ -3674,11 -3463,10 +3677,11 @@@ Cflags: -I\${includedir
  EOF
  }
  
 -pkgconfig_generate libavutil "Libav utility library" "$LIBAVUTIL_VERSION" "$LIBM"
 -pkgconfig_generate libavcodec "Libav codec library" "$LIBAVCODEC_VERSION" "$extralibs" "libavutil = $LIBAVUTIL_VERSION"
 -pkgconfig_generate libavformat "Libav container format library" "$LIBAVFORMAT_VERSION" "$extralibs" "libavcodec = $LIBAVCODEC_VERSION"
 -pkgconfig_generate libavdevice "Libav device handling library" "$LIBAVDEVICE_VERSION" "$extralibs" "libavformat = $LIBAVFORMAT_VERSION"
 -pkgconfig_generate libavfilter "Libav video filtering library" "$LIBAVFILTER_VERSION" "$extralibs"
 -pkgconfig_generate libpostproc "Libav postprocessing library" "$LIBPOSTPROC_VERSION" "" "libavutil = $LIBAVUTIL_VERSION"
 -pkgconfig_generate libswscale "Libav image rescaling library" "$LIBSWSCALE_VERSION" "$LIBM" "libavutil = $LIBAVUTIL_VERSION"
 +pkgconfig_generate libavutil "FFmpeg utility library" "$LIBAVUTIL_VERSION" "$LIBM"
 +pkgconfig_generate libavcodec "FFmpeg codec library" "$LIBAVCODEC_VERSION" "$extralibs" "libavutil = $LIBAVUTIL_VERSION"
 +pkgconfig_generate libavformat "FFmpeg container format library" "$LIBAVFORMAT_VERSION" "$extralibs" "libavcodec = $LIBAVCODEC_VERSION"
 +pkgconfig_generate libavdevice "FFmpeg device handling library" "$LIBAVDEVICE_VERSION" "$extralibs" "libavformat = $LIBAVFORMAT_VERSION"
 +pkgconfig_generate libavfilter "FFmpeg video filtering library" "$LIBAVFILTER_VERSION" "$extralibs"
 +pkgconfig_generate libpostproc "FFmpeg postprocessing library" "$LIBPOSTPROC_VERSION" "" "libavutil = $LIBAVUTIL_VERSION"
 +pkgconfig_generate libswscale "FFmpeg image rescaling library" "$LIBSWSCALE_VERSION" "$LIBM" "libavutil = $LIBAVUTIL_VERSION"
 +pkgconfig_generate libswresample "FFmpeg audio rescaling library" "$LIBSWRESAMPLE_VERSION" "$LIBM" "libavutil = $LIBAVUTIL_VERSION"
diff --combined doc/APIchanges
index 03f7d460d2943d1f441e2ce6f85d83178c040334,f1913bd6deddaa0ad0f078bb1bede4c66a1147e2..2ce14618fa783d44edd831cb0903e85ac8d66fb7
@@@ -13,12 -13,16 +13,22 @@@ libavutil:   2011-04-1
  
  API changes, most recent first:
  
 +2011-11-03 - 96949da - lavu 51.23.0
 +  Add av_strcasecmp() and av_strncasecmp() to avstring.h.
 +
 +2011-10-20 - b35e9e1 - lavu 51.22.0
 +  Add av_strtok() to avstring.h.
 +
+ 2011-11-13 - lavf 53.15.0
+   New interrupt callback API, allowing per-AVFormatContext/AVIOContext
+   interrupt callbacks.
+   6aa0b98 Add AVIOInterruptCB struct and the interrupt_callback field to
+           AVFormatContext.
+   1dee0ac Add avio_open2() with additional parameters. Those are
+           an interrupt callback and an options AVDictionary.
+           This will allow passing AVOptions to protocols after lavf
+           54.0.
  2011-11-xx - xxxxxxx - lavu 51.16.0
    Add av_timegm()
  
    - 641c7af new functions - av_opt_child_next, av_opt_child_class_next
      and av_opt_find2()
  
 -2011-09-03 - fb4ca26 - lavc 53.10.0
 -                       lavf 53.6.0
 +2011-09-22 - a70e787 - lavu 51.17.0
 +  Add av_x_if_null().
 +
 +2011-09-18 - 645cebb - lavc 53.16.0
 +  Add showall flag2
 +
 +2011-09-16 - ea8de10 - lavfi 2.42.0
 +  Add avfilter_all_channel_layouts.
 +
 +2011-09-16 - 9899037 - lavfi 2.41.0
 +  Rename avfilter_all_* function names to avfilter_make_all_*.
 +
 +  In particular, apply the renames:
 +  avfilter_all_formats         -> avfilter_make_all_formats
 +  avfilter_all_channel_layouts -> avfilter_make_all_channel_layouts
 +  avfilter_all_packing_formats -> avfilter_make_all_packing_formats
 +
 +2011-09-12 - 4381bdd - lavfi 2.40.0
 +  Change AVFilterBufferRefAudioProps.sample_rate type from uint32_t to int.
 +
 +2011-09-12 - 2c03174 - lavfi 2.40.0
 +  Simplify signature for avfilter_get_audio_buffer(), make it
 +  consistent with avfilter_get_video_buffer().
 +
 +2011-09-06 - 4f7dfe1 - lavfi 2.39.0
 +  Rename libavfilter/vsink_buffer.h to libavfilter/buffersink.h.
 +
 +2011-09-06 - c4415f6 - lavfi 2.38.0
 +  Unify video and audio sink API.
 +
 +  In particular, add av_buffersink_get_buffer_ref(), deprecate
 +  av_vsink_buffer_get_video_buffer_ref() and change the value for the
 +  opaque field passed to the abuffersink init function.
 +
 +2011-09-04 - 61e2e29 - lavu 51.16.0
 +  Add av_asprintf().
 +
 +2011-08-22 - dacd827 - lavf 53.10.0
 +  Add av_find_program_from_stream().
 +
 +2011-08-20 - 69e2c1a - lavu 51.13.0
 +  Add av_get_media_type_string().
 +
 +2011-09-03 - fb4ca26 - lavc 53.13.0
 +                       lavf 53.11.0
                         lsws  2.1.0
    Add {avcodec,avformat,sws}_get_class().
  
 -2011-09-03 - c11fb82 - lavu 51.10.0
 +2011-08-03 - c11fb82 - lavu 51.15.0
    Add AV_OPT_SEARCH_FAKE_OBJ flag for av_opt_find() function.
  
 -2011-08-26 - f2011ed - lavu 51.9.0
 +2011-08-14 - 323b930 - lavu 51.12.0
    Add av_fifo_peek2(), deprecate av_fifo_peek().
  
 -2011-08-16 - 48f9e45 - lavf 53.4.0
 +2011-08-16 - 48f9e45 - lavf 53.8.0
    Add avformat_query_codec().
  
 -2011-08-16 - bca06e7 - lavc 53.8.0
 +2011-08-16 - bca06e7 - lavc 53.11.0
    Add avcodec_get_type().
  
 -2011-08-06 - 2f63440 - lavf 53.4.0
 +2011-08-06 - 2f63440 - lavf 53.7.0
    Add error_recognition to AVFormatContext.
  
 -2011-08-02 - 9d39cbf - lavc 53.7.1
 +2011-08-02 - 9d39cbf - lavc 53.9.1
    Add AV_PKT_FLAG_CORRUPT AVPacket flag.
  
 -2011-07-10 - a67c061 - lavf 53.3.0
 +2011-07-16 - b57df29 - lavfi 2.27.0
 +  Add audio packing negotiation fields and helper functions.
 +
 +  In particular, add AVFilterPacking enum, planar, in_packings and
 +  out_packings fields to AVFilterLink, and the functions:
 +  avfilter_set_common_packing_formats()
 +  avfilter_all_packing_formats()
 +
 +2011-07-10 - a67c061 - lavf 53.6.0
    Add avformat_find_stream_info(), deprecate av_find_stream_info().
  
 -2011-07-10 - 0b950fe - lavc 53.6.0
 +2011-07-10 - 0b950fe - lavc 53.8.0
    Add avcodec_open2(), deprecate avcodec_open().
  
 +2011-07-01 - b442ca6 - lavf 53.5.0 - avformat.h
 +  Add function av_get_output_timestamp().
 +
 +2011-06-28 - 5129336 - lavu 51.11.0 - avutil.h
 +  Define the AV_PICTURE_TYPE_NONE value in AVPictureType enum.
 +
 +2011-06-19 - fd2c0a5 - lavfi 2.23.0 - avfilter.h
 +  Add layout negotiation fields and helper functions.
 +
 +  In particular, add in_chlayouts and out_chlayouts to AVFilterLink,
 +  and the functions:
 +  avfilter_set_common_sample_formats()
 +  avfilter_set_common_channel_layouts()
 +  avfilter_all_channel_layouts()
 +
 +2011-06-19 - 527ca39 - lavfi 2.22.0 - AVFilterFormats
 +  Change type of AVFilterFormats.formats from int * to int64_t *,
 +  and update formats handling API accordingly.
 +
 +  avfilter_make_format_list() still takes a int32_t array and converts
 +  it to int64_t. A new function, avfilter_make_format64_list(), that
 +  takes int64_t arrays has been added.
 +
 +2011-06-19 - 44f669e - lavfi 2.21.0 - vsink_buffer.h
 +  Add video sink buffer and vsink_buffer.h public header.
 +
 +2011-06-12 - 9fdf772 - lavfi 2.18.0 - avcodec.h
 +  Add avfilter_get_video_buffer_ref_from_frame() function in
 +  libavfilter/avcodec.h.
 +
 +2011-06-12 - c535494 - lavfi 2.17.0 - avfiltergraph.h
 +  Add avfilter_inout_alloc() and avfilter_inout_free() functions.
 +
 +2011-06-12 - 6119b23 - lavfi 2.16.0 - avfilter_graph_parse()
 +  Change avfilter_graph_parse() signature.
 +
  2011-06-23 - 67e9ae1 - lavu 51.8.0 - attributes.h
    Add av_printf_format().
  
  2011-06-10 - cb7c11c - lavu 51.6.0 - opt.h
    Add av_opt_flag_is_set().
  
 -2011-06-08 - d9f80ea - lavu 51.5.0 - AVMetadata
 +2011-06-10 - c381960 - lavfi 2.15.0 - avfilter_get_audio_buffer_ref_from_arrays
 +  Add avfilter_get_audio_buffer_ref_from_arrays() to avfilter.h.
 +
 +2011-06-09 - d9f80ea - lavu 51.8.0 - AVMetadata
    Move AVMetadata from lavf to lavu and rename it to
    AVDictionary -- new installed header dict.h.
    All av_metadata_* functions renamed to av_dict_*.
  
 -2011-06-07 - a6703fa - lavu 51.4.0 - av_get_bytes_per_sample()
 +2011-06-07 - a6703fa - lavu 51.8.0 - av_get_bytes_per_sample()
    Add av_get_bytes_per_sample() in libavutil/samplefmt.h.
    Deprecate av_get_bits_per_sample_fmt().
  
 -2011-06-05 - b39b062 - lavu 51.3.0 - opt.h
 +2011-06-05 - b39b062 - lavu 51.8.0 - opt.h
    Add av_opt_free convenience function.
  
 -2011-05-28 - 0420bd7 - lavu 51.2.0 - pixdesc.h
 +2011-06-06 - 95a0242 - lavfi 2.14.0 - AVFilterBufferRefAudioProps
 +  Remove AVFilterBufferRefAudioProps.size, and use nb_samples in
 +  avfilter_get_audio_buffer() and avfilter_default_get_audio_buffer() in
 +  place of size.
 +
 +2011-06-06 - 0bc2cca - lavu 51.6.0 - av_samples_alloc()
 +  Switch nb_channels and nb_samples parameters order in
 +  av_samples_alloc().
 +
 +2011-06-06 - e1c7414 - lavu 51.5.0 - av_samples_*
 +  Change the data layout created by av_samples_fill_arrays() and
 +  av_samples_alloc().
 +
 +2011-06-06 - 27bcf55 - lavfi 2.13.0 - vsrc_buffer.h
 +  Make av_vsrc_buffer_add_video_buffer_ref() accepts an additional
 +  flags parameter in input.
 +
 +2011-06-03 - e977ca2 - lavfi 2.12.0 - avfilter_link_free()
 +  Add avfilter_link_free() function.
 +
 +2011-06-02 - 5ad38d9 - lavu 51.4.0 - av_force_cpu_flags()
 +  Add av_cpu_flags() in libavutil/cpu.h.
 +
 +2011-05-28 - e71f260 - lavu 51.3.0 - pixdesc.h
    Add av_get_pix_fmt_name() in libavutil/pixdesc.h, and deprecate
    avcodec_get_pix_fmt_name() in libavcodec/avcodec.h in its favor.
  
 -2011-05-25 - 30315a8 - lavf 53.1.0 - avformat.h
 +2011-05-25 - 30315a8 - lavf 53.3.0 - avformat.h
    Add fps_probe_size to AVFormatContext.
  
 -2011-05-18 - 64150ff - lavc 53.4.0 - AVCodecContext.request_sample_fmt
 +2011-05-22 - 5ecdfd0 - lavf 53.2.0 - avformat.h
 +  Introduce avformat_alloc_output_context2() and deprecate
 +  avformat_alloc_output_context().
 +
 +2011-05-22 - 83db719 - lavfi 2.10.0 - vsrc_buffer.h
 +  Make libavfilter/vsrc_buffer.h public.
 +
 +2011-05-19 - c000a9f - lavfi 2.8.0 - avcodec.h
 +  Add av_vsrc_buffer_add_frame() to libavfilter/avcodec.h.
 +
 +2011-05-14 - 9fdf772 - lavfi 2.6.0 - avcodec.h
 +  Add avfilter_get_video_buffer_ref_from_frame() to libavfilter/avcodec.h.
 +
 +2011-05-18 - 64150ff - lavc 53.7.0 - AVCodecContext.request_sample_fmt
    Add request_sample_fmt field to AVCodecContext.
  
 -2011-05-10 - 188dea1 - lavc 53.3.0 - avcodec.h
 +2011-05-10 - 188dea1 - lavc 53.6.0 - avcodec.h
    Deprecate AVLPCType and the following fields in
    AVCodecContext: lpc_coeff_precision, prediction_order_method,
    min_partition_order, max_partition_order, lpc_type, lpc_passes.
    Corresponding FLAC encoder options should be used instead.
  
 +2011-05-07 - 9fdf772 - lavfi 2.5.0 - avcodec.h
 +  Add libavfilter/avcodec.h header and avfilter_copy_frame_props()
 +  function.
 +
 +2011-05-07 - 18ded93 - lavc 53.5.0 - AVFrame
 +  Add format field to AVFrame.
 +
 +2011-05-07 - 22333a6 - lavc 53.4.0 - AVFrame
 +  Add width and height fields to AVFrame.
 +
 +2011-05-01 - 35fe66a - lavfi 2.4.0 - avfilter.h
 +  Rename AVFilterBufferRefVideoProps.pixel_aspect to
 +  sample_aspect_ratio.
 +
 +2011-05-01 - 77e9dee - lavc 53.3.0 - AVFrame
 +  Add a sample_aspect_ratio field to AVFrame.
 +
 +2011-05-01 - 1ba5727 - lavc 53.2.0 - AVFrame
 +  Add a pkt_pos field to AVFrame.
 +
 +2011-04-29 - 35ceaa7 - lavu 51.2.0 - mem.h
 +  Add av_dynarray_add function for adding
 +  an element to a dynamic array.
 +
  2011-04-26 - bebe72f - lavu 51.1.0 - avutil.h
    Add AVPictureType enum and av_get_picture_type_char(), deprecate
    FF_*_TYPE defines and av_get_pict_type_char() defined in
      333e894 deprecate url_open_protocol
      e230705 deprecate url_poll and URLPollEntry
  
 -2011-04-10 - lavu  50.40.0 - pixfmt.h
 -  Add PIX_FMT_BGR48LE and PIX_FMT_BGR48BE pixel formats
 -
  2011-04-08 - lavf 52.106.0 - avformat.h
    Minor avformat.h cleanup:
      a9bf9d8 deprecate av_guess_image2_codec
      d9d86e0 rename url_fprintf -> avio_printf
      59f65d9 deprecate url_setbufsize
      3e68b3b deprecate url_ferror
 -    66e5b1d deprecate url_feof
      e8bb2e2 deprecate url_fget_max_packet_size
      76aa876 rename url_fsize -> avio_size
      e519753 deprecate url_fgetc
  2011-03-25 - 34b47d7 - lavc 52.115.0 - AVCodecContext.audio_service_type
    Add audio_service_type field to AVCodecContext.
  
 +2011-03-17 - e309fdc - lavu 50.40.0 - pixfmt.h
 +  Add PIX_FMT_BGR48LE and PIX_FMT_BGR48BE pixel formats
 +
  2011-03-02 - 863c471 - lavf  52.103.0 - av_pkt_dump2, av_pkt_dump_log2
    Add new functions av_pkt_dump2, av_pkt_dump_log2 that uses the
    source stream timebase for outputting timestamps. Deprecate
  2011-02-02 - dfd2a00 - lavu 50.37.0 - log.h
    Make av_dlog public.
  
 +2011-01-31 - 7b3ea55 - lavfi 1.76.0 - vsrc_buffer
 +  Add sample_aspect_ratio fields to vsrc_buffer arguments
 +
 +2011-01-31 - 910b5b8 - lavfi 1.75.0 - AVFilterLink sample_aspect_ratio
 +  Add sample_aspect_ratio field to AVFilterLink.
 +
  2011-01-15 - r26374 - lavfi 1.74.0 - AVFilterBufferRefAudioProps
    Rename AVFilterBufferRefAudioProps.samples_nb to nb_samples.
  
diff --combined ffmpeg.c
index 9895fb39703646c1418248ffded0889a5bc3706c,c731cec53b4b5d0f8ae0de84ba76fa5a64e3b613..507c35dba4ec281ffcef3c5d76cd0641a5b9ba6f
+++ b/ffmpeg.c
@@@ -2,20 -2,20 +2,20 @@@
   * ffmpeg main
   * Copyright (c) 2000-2003 Fabrice Bellard
   *
 - * 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 "libavutil/avstring.h"
  #include "libavutil/libm.h"
  #include "libavformat/os_support.h"
 +#include "libswresample/swresample.h"
 +
 +#include "libavformat/ffm.h" // not public API
  
  #if CONFIG_AVFILTER
 +# include "libavfilter/avcodec.h"
  # include "libavfilter/avfilter.h"
  # include "libavfilter/avfiltergraph.h"
 +# include "libavfilter/buffersink.h"
  # include "libavfilter/vsrc_buffer.h"
  #endif
  
  #include <sys/select.h>
  #endif
  
 +#if HAVE_TERMIOS_H
 +#include <fcntl.h>
 +#include <sys/ioctl.h>
 +#include <sys/time.h>
 +#include <termios.h>
 +#elif HAVE_KBHIT
 +#include <conio.h>
 +#endif
  #include <time.h>
  
  #include "cmdutils.h"
@@@ -92,18 -79,12 +92,18 @@@ const int program_birth_year = 2000
  
  /* select an input stream for an output stream */
  typedef struct StreamMap {
 +    int disabled;           /** 1 is this mapping is disabled by a negative map */
      int file_index;
      int stream_index;
      int sync_file_index;
      int sync_stream_index;
  } StreamMap;
  
 +typedef struct {
 +    int  file_idx,  stream_idx,  channel_idx; // input
 +    int ofile_idx, ostream_idx;               // output
 +} AudioChannelMap;
 +
  /**
   * select an input file for an output file
   */
@@@ -113,92 -94,151 +113,92 @@@ typedef struct MetadataMap 
      int  index;     ///< stream/chapter/program number
  } MetadataMap;
  
 -typedef struct ChapterMap {
 -    int in_file;
 -    int out_file;
 -} ChapterMap;
 -
  static const OptionDef options[];
  
 -#define MAX_FILES 100
 -
 -static const char *last_asked_format = NULL;
 -static double *ts_scale;
 -static int  nb_ts_scale;
 -
 -static AVFormatContext *output_files[MAX_FILES];
 -static AVDictionary *output_opts[MAX_FILES];
 -static int nb_output_files = 0;
 -
 -static StreamMap *stream_maps = NULL;
 -static int nb_stream_maps;
 -
 -/* first item specifies output metadata, second is input */
 -static MetadataMap (*meta_data_maps)[2] = NULL;
 -static int nb_meta_data_maps;
 -static int metadata_global_autocopy   = 1;
 -static int metadata_streams_autocopy  = 1;
 -static int metadata_chapters_autocopy = 1;
 -
 -static ChapterMap *chapter_maps = NULL;
 -static int nb_chapter_maps;
 -
 -/* indexed by output file stream index */
 -static int *streamid_map = NULL;
 -static int nb_streamid_map = 0;
 -
 -static int frame_width  = 0;
 -static int frame_height = 0;
 -static float frame_aspect_ratio = 0;
 -static enum PixelFormat frame_pix_fmt = PIX_FMT_NONE;
 -static enum AVSampleFormat audio_sample_fmt = AV_SAMPLE_FMT_NONE;
 -static int max_frames[4] = {INT_MAX, INT_MAX, INT_MAX, INT_MAX};
 -static AVRational frame_rate;
 -static float video_qscale = 0;
 -static uint16_t *intra_matrix = NULL;
 -static uint16_t *inter_matrix = NULL;
 -static const char *video_rc_override_string=NULL;
 -static int video_disable = 0;
 +#define MAX_STREAMS 1024    /* arbitrary sanity check value */
 +
 +static int frame_bits_per_raw_sample = 0;
  static int video_discard = 0;
 -static char *video_codec_name = NULL;
 -static unsigned int video_codec_tag = 0;
 -static char *video_language = NULL;
 -static int same_quality = 0;
 +static int same_quant = 0;
  static int do_deinterlace = 0;
 -static int top_field_first = -1;
 -static int me_threshold = 0;
  static int intra_dc_precision = 8;
  static int loop_input = 0;
  static int loop_output = AVFMT_NOOUTPUTLOOP;
  static int qp_hist = 0;
 -#if CONFIG_AVFILTER
 -static char *vfilters = NULL;
 -#endif
 -
  static int intra_only = 0;
 -static int audio_sample_rate = 0;
 -#define QSCALE_NONE -99999
 -static float audio_qscale = QSCALE_NONE;
 -static int audio_disable = 0;
 -static int audio_channels = 0;
 -static char  *audio_codec_name = NULL;
 -static unsigned int audio_codec_tag = 0;
 -static char *audio_language = NULL;
 -
 -static int subtitle_disable = 0;
 -static char *subtitle_codec_name = NULL;
 -static char *subtitle_language = NULL;
 -static unsigned int subtitle_codec_tag = 0;
 -
 -static int data_disable = 0;
 -static char *data_codec_name = NULL;
 -static unsigned int data_codec_tag = 0;
 -
 -static float mux_preload= 0.5;
 -static float mux_max_delay= 0.7;
 -
 -static int64_t recording_time = INT64_MAX;
 -static int64_t start_time = 0;
 -static int64_t input_ts_offset = 0;
 +static const char *video_codec_name    = NULL;
 +static const char *audio_codec_name    = NULL;
 +static const char *subtitle_codec_name = NULL;
 +
  static int file_overwrite = 0;
 -static AVDictionary *metadata;
  static int do_benchmark = 0;
  static int do_hex_dump = 0;
  static int do_pkt_dump = 0;
  static int do_psnr = 0;
  static int do_pass = 0;
 -static char *pass_logfilename_prefix = NULL;
 -static int audio_stream_copy = 0;
 -static int video_stream_copy = 0;
 -static int subtitle_stream_copy = 0;
 -static int data_stream_copy = 0;
 +static const char *pass_logfilename_prefix;
  static int video_sync_method= -1;
  static int audio_sync_method= 0;
  static float audio_drift_threshold= 0.1;
  static int copy_ts= 0;
 -static int copy_tb;
 +static int copy_tb= 0;
  static int opt_shortest = 0;
  static char *vstats_filename;
  static FILE *vstats_file;
 -static int opt_programid = 0;
 -static int copy_initial_nonkeyframes = 0;
 -
 -static int rate_emu = 0;
  
  static int audio_volume = 256;
  
  static int exit_on_error = 0;
  static int using_stdin = 0;
 -static int verbose = 1;
 -static int thread_count= 1;
 +static int run_as_daemon  = 0;
 +static volatile int received_nb_signals = 0;
  static int64_t video_size = 0;
  static int64_t audio_size = 0;
  static int64_t extra_size = 0;
  static int nb_frames_dup = 0;
  static int nb_frames_drop = 0;
  static int input_sync;
 -static uint64_t limit_filesize = 0;
 -static int force_fps = 0;
 -static char *forced_key_frames = NULL;
  
  static float dts_delta_threshold = 10;
  
 -static int64_t timer_start;
 +static int print_stats = 1;
  
  static uint8_t *audio_buf;
  static uint8_t *audio_out;
  static unsigned int allocated_audio_out_size, allocated_audio_buf_size;
  
 -static short *samples;
 -
 -static AVBitStreamFilterContext *video_bitstream_filters=NULL;
 -static AVBitStreamFilterContext *audio_bitstream_filters=NULL;
 -static AVBitStreamFilterContext *subtitle_bitstream_filters=NULL;
 +static void *samples;
 +static uint8_t *input_tmp= NULL;
  
  #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
  
 -struct InputStream;
 +typedef struct InputStream {
 +    int file_index;
 +    AVStream *st;
 +    int discard;             /* true if stream data should be discarded */
 +    int decoding_needed;     /* true if the packets must be decoded in 'raw_fifo' */
 +    AVCodec *dec;
 +
 +    int64_t       start;     /* time when read started */
 +    int64_t       next_pts;  /* synthetic pts for cases where pkt.pts
 +                                is not defined */
 +    int64_t       pts;       /* current pts */
 +    double ts_scale;
 +    int is_start;            /* is 1 at the start and after a discontinuity */
 +    int showed_multi_packet_warning;
 +    AVDictionary *opts;
 +} InputStream;
 +
 +typedef struct InputFile {
 +    AVFormatContext *ctx;
 +    int eof_reached;      /* true if eof reached */
 +    int ist_index;        /* index of first stream in input_streams */
 +    int buffer_size;      /* current total buffer size */
 +    int64_t ts_offset;
 +    int nb_streams;       /* number of stream that ffmpeg is aware of; may be different
 +                             from ctx.nb_streams if new streams appear during av_read_frame() */
 +    int rate_emu;
 +} InputFile;
  
  typedef struct OutputStream {
      int file_index;          /* file index */
      int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ //FIXME look at frame_number
      AVBitStreamFilterContext *bitstream_filters;
      AVCodec *enc;
 +    int64_t max_frames;
  
      /* video only */
      int video_resample;
 -    AVFrame pict_tmp;      /* temporary image for resampling */
 +    AVFrame resample_frame;              /* temporary frame for image resampling */
      struct SwsContext *img_resample_ctx; /* for image resampling */
      int resample_height;
      int resample_width;
      int resample_pix_fmt;
      AVRational frame_rate;
 +    int force_fps;
 +    int top_field_first;
  
      float frame_aspect_ratio;
  
  
      /* audio only */
      int audio_resample;
 -    ReSampleContext *resample; /* for audio resampling */
 +    int audio_channels_map[SWR_CH_MAX];  ///< list of the channels id to pick from the source stream
 +    int audio_channels_mapped;           ///< number of channels in audio_channels_map
      int resample_sample_fmt;
      int resample_channels;
      int resample_sample_rate;
 -    int reformat_pair;
 -    AVAudioConvert *reformat_ctx;
 +    float rematrix_volume;
      AVFifoBuffer *fifo;     /* for compression: one audio fifo per codec */
      FILE *logfile;
  
 +    struct SwrContext *swr;
 +
  #if CONFIG_AVFILTER
      AVFilterContext *output_video_filter;
      AVFilterContext *input_video_filter;
      AVFilterGraph *graph;
  #endif
  
 -   int sws_flags;
 -   AVDictionary *opts;
 +    int64_t sws_flags;
 +    AVDictionary *opts;
 +    int is_past_recording_time;
 +    int stream_copy;
 +    const char *attachment_filename;
 +    int copy_initial_nonkeyframes;
  } OutputStream;
  
 -static OutputStream **output_streams_for_file[MAX_FILES] = { NULL };
 -static int nb_output_streams_for_file[MAX_FILES] = { 0 };
  
 -typedef struct InputStream {
 -    int file_index;
 -    AVStream *st;
 -    int discard;             /* true if stream data should be discarded */
 -    int decoding_needed;     /* true if the packets must be decoded in 'raw_fifo' */
 -    AVCodec *dec;
 +#if HAVE_TERMIOS_H
  
 -    int64_t       start;     /* time when read started */
 -    int64_t       next_pts;  /* synthetic pts for cases where pkt.pts
 -                                is not defined */
 -    int64_t       pts;       /* current pts */
 -    PtsCorrectionContext pts_ctx;
 -    double ts_scale;
 -    int is_start;            /* is 1 at the start and after a discontinuity */
 -    int showed_multi_packet_warning;
 -    int is_past_recording_time;
 -    AVDictionary *opts;
 -} InputStream;
 +/* init terminal so that we can grab keys */
 +static struct termios oldtty;
 +#endif
  
 -typedef struct InputFile {
 +typedef struct OutputFile {
      AVFormatContext *ctx;
 -    int eof_reached;      /* true if eof reached */
 -    int ist_index;        /* index of first stream in ist_table */
 -    int buffer_size;      /* current total buffer size */
 -    int64_t ts_offset;
 -    int nb_streams;       /* nb streams we are aware of */
 -} InputFile;
 +    AVDictionary *opts;
 +    int ost_index;       /* index of the first stream in output_streams */
 +    int64_t recording_time; /* desired length of the resulting file in microseconds */
 +    int64_t start_time;     /* start time in microseconds */
 +    uint64_t limit_filesize;
 +} OutputFile;
  
  static InputStream *input_streams = NULL;
  static int         nb_input_streams = 0;
  static InputFile   *input_files   = NULL;
  static int         nb_input_files   = 0;
  
 +static OutputStream *output_streams = NULL;
 +static int        nb_output_streams = 0;
 +static OutputFile   *output_files   = NULL;
 +static int        nb_output_files   = 0;
 +
 +typedef struct OptionsContext {
 +    /* input/output options */
 +    int64_t start_time;
 +    const char *format;
 +
 +    SpecifierOpt *codec_names;
 +    int        nb_codec_names;
 +    SpecifierOpt *audio_channels;
 +    int        nb_audio_channels;
 +    SpecifierOpt *audio_sample_rate;
 +    int        nb_audio_sample_rate;
 +    SpecifierOpt *rematrix_volume;
 +    int        nb_rematrix_volume;
 +    SpecifierOpt *frame_rates;
 +    int        nb_frame_rates;
 +    SpecifierOpt *frame_sizes;
 +    int        nb_frame_sizes;
 +    SpecifierOpt *frame_pix_fmts;
 +    int        nb_frame_pix_fmts;
 +
 +    /* input options */
 +    int64_t input_ts_offset;
 +    int rate_emu;
 +
 +    SpecifierOpt *ts_scale;
 +    int        nb_ts_scale;
 +    SpecifierOpt *dump_attachment;
 +    int        nb_dump_attachment;
 +
 +    /* output options */
 +    StreamMap *stream_maps;
 +    int     nb_stream_maps;
 +    AudioChannelMap *audio_channel_maps; ///< one info entry per -map_channel
 +    int           nb_audio_channel_maps; ///< number of (valid) -map_channel settings
 +    /* first item specifies output metadata, second is input */
 +    MetadataMap (*meta_data_maps)[2];
 +    int nb_meta_data_maps;
 +    int metadata_global_manual;
 +    int metadata_streams_manual;
 +    int metadata_chapters_manual;
 +    const char **attachments;
 +    int       nb_attachments;
 +
 +    int chapters_input_file;
 +
 +    int64_t recording_time;
 +    uint64_t limit_filesize;
 +    float mux_preload;
 +    float mux_max_delay;
 +
 +    int video_disable;
 +    int audio_disable;
 +    int subtitle_disable;
 +    int data_disable;
 +
 +    /* indexed by output file stream index */
 +    int   *streamid_map;
 +    int nb_streamid_map;
 +
 +    SpecifierOpt *metadata;
 +    int        nb_metadata;
 +    SpecifierOpt *max_frames;
 +    int        nb_max_frames;
 +    SpecifierOpt *bitstream_filters;
 +    int        nb_bitstream_filters;
 +    SpecifierOpt *codec_tags;
 +    int        nb_codec_tags;
 +    SpecifierOpt *sample_fmts;
 +    int        nb_sample_fmts;
 +    SpecifierOpt *qscale;
 +    int        nb_qscale;
 +    SpecifierOpt *forced_key_frames;
 +    int        nb_forced_key_frames;
 +    SpecifierOpt *force_fps;
 +    int        nb_force_fps;
 +    SpecifierOpt *frame_aspect_ratios;
 +    int        nb_frame_aspect_ratios;
 +    SpecifierOpt *rc_overrides;
 +    int        nb_rc_overrides;
 +    SpecifierOpt *intra_matrices;
 +    int        nb_intra_matrices;
 +    SpecifierOpt *inter_matrices;
 +    int        nb_inter_matrices;
 +    SpecifierOpt *top_field_first;
 +    int        nb_top_field_first;
 +    SpecifierOpt *presets;
 +    int        nb_presets;
 +    SpecifierOpt *copy_initial_nonkeyframes;
 +    int        nb_copy_initial_nonkeyframes;
 +#if CONFIG_AVFILTER
 +    SpecifierOpt *filters;
 +    int        nb_filters;
 +#endif
 +} OptionsContext;
 +
 +#define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
 +{\
 +    int i, ret;\
 +    for (i = 0; i < o->nb_ ## name; i++) {\
 +        char *spec = o->name[i].specifier;\
 +        if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0)\
 +            outvar = o->name[i].u.type;\
 +        else if (ret < 0)\
 +            exit_program(1);\
 +    }\
 +}
 +
 +static void reset_options(OptionsContext *o, int is_input)
 +{
 +    const OptionDef *po = options;
 +    OptionsContext bak= *o;
 +
 +    /* all OPT_SPEC and OPT_STRING can be freed in generic way */
 +    while (po->name) {
 +        void *dst = (uint8_t*)o + po->u.off;
 +
 +        if (po->flags & OPT_SPEC) {
 +            SpecifierOpt **so = dst;
 +            int i, *count = (int*)(so + 1);
 +            for (i = 0; i < *count; i++) {
 +                av_freep(&(*so)[i].specifier);
 +                if (po->flags & OPT_STRING)
 +                    av_freep(&(*so)[i].u.str);
 +            }
 +            av_freep(so);
 +            *count = 0;
 +        } else if (po->flags & OPT_OFFSET && po->flags & OPT_STRING)
 +            av_freep(dst);
 +        po++;
 +    }
 +
 +    av_freep(&o->stream_maps);
 +    av_freep(&o->audio_channel_maps);
 +    av_freep(&o->meta_data_maps);
 +    av_freep(&o->streamid_map);
 +
 +    memset(o, 0, sizeof(*o));
 +
 +    if(is_input) o->recording_time = bak.recording_time;
 +    else         o->recording_time = INT64_MAX;
 +    o->mux_max_delay  = 0.7;
 +    o->limit_filesize = UINT64_MAX;
 +    o->chapters_input_file = INT_MAX;
 +
 +    uninit_opts();
 +    init_opts();
 +}
 +
  #if CONFIG_AVFILTER
  
  static int configure_video_filters(InputStream *ist, OutputStream *ost)
      /** filter graph containing all filters including input & output */
      AVCodecContext *codec = ost->st->codec;
      AVCodecContext *icodec = ist->st->codec;
 -    FFSinkContext ffsink_ctx = { .pix_fmt = codec->pix_fmt };
 +    enum PixelFormat pix_fmts[] = { codec->pix_fmt, PIX_FMT_NONE };
 +    AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
      AVRational sample_aspect_ratio;
      char args[255];
      int ret;
                                         "src", args, NULL, ost->graph);
      if (ret < 0)
          return ret;
 -    ret = avfilter_graph_create_filter(&ost->output_video_filter, &ffsink,
 -                                       "out", NULL, &ffsink_ctx, ost->graph);
 +#if FF_API_OLD_VSINK_API
 +    ret = avfilter_graph_create_filter(&ost->output_video_filter, avfilter_get_by_name("buffersink"),
 +                                       "out", NULL, pix_fmts, ost->graph);
 +#else
 +    buffersink_params->pixel_fmts = pix_fmts;
 +    ret = avfilter_graph_create_filter(&ost->output_video_filter, avfilter_get_by_name("buffersink"),
 +                                       "out", NULL, buffersink_params, ost->graph);
 +#endif
 +    av_freep(&buffersink_params);
      if (ret < 0)
          return ret;
      last_filter = ost->input_video_filter;
          snprintf(args, 255, "%d:%d:flags=0x%X",
                   codec->width,
                   codec->height,
 -                 ost->sws_flags);
 +                 (unsigned)ost->sws_flags);
          if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
                                                  NULL, args, NULL, ost->graph)) < 0)
              return ret;
          last_filter = filter;
      }
  
 -    snprintf(args, sizeof(args), "flags=0x%X", ost->sws_flags);
 +    snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
      ost->graph->scale_sws_opts = av_strdup(args);
  
      if (ost->avfilter) {
 -        AVFilterInOut *outputs = av_malloc(sizeof(AVFilterInOut));
 -        AVFilterInOut *inputs  = av_malloc(sizeof(AVFilterInOut));
 +        AVFilterInOut *outputs = avfilter_inout_alloc();
 +        AVFilterInOut *inputs  = avfilter_inout_alloc();
  
          outputs->name    = av_strdup("in");
          outputs->filter_ctx = last_filter;
          inputs->pad_idx = 0;
          inputs->next    = NULL;
  
 -        if ((ret = avfilter_graph_parse(ost->graph, ost->avfilter, inputs, outputs, NULL)) < 0)
 +        if ((ret = avfilter_graph_parse(ost->graph, ost->avfilter, &inputs, &outputs, NULL)) < 0)
              return ret;
          av_freep(&ost->avfilter);
      } else {
      codec->width  = ost->output_video_filter->inputs[0]->w;
      codec->height = ost->output_video_filter->inputs[0]->h;
      codec->sample_aspect_ratio = ost->st->sample_aspect_ratio =
 -        ost->frame_aspect_ratio ? // overriden by the -aspect cli option
 +        ost->frame_aspect_ratio ? // overridden by the -aspect cli option
          av_d2q(ost->frame_aspect_ratio*codec->height/codec->width, 255) :
          ost->output_video_filter->inputs[0]->sample_aspect_ratio;
  
  
  static void term_exit(void)
  {
 -    av_log(NULL, AV_LOG_QUIET, "");
 +    av_log(NULL, AV_LOG_QUIET, "%s", "");
 +#if HAVE_TERMIOS_H
 +    if(!run_as_daemon)
 +        tcsetattr (0, TCSANOW, &oldtty);
 +#endif
  }
  
  static volatile int received_sigterm = 0;
 -static volatile int received_nb_signals = 0;
  
 -static void
 -sigterm_handler(int sig)
 +static void sigterm_handler(int sig)
  {
      received_sigterm = sig;
      received_nb_signals++;
  
  static void term_init(void)
  {
 +#if HAVE_TERMIOS_H
 +    if(!run_as_daemon){
 +    struct termios tty;
 +
 +    tcgetattr (0, &tty);
 +    oldtty = tty;
 +    atexit(term_exit);
 +
 +    tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
 +                          |INLCR|IGNCR|ICRNL|IXON);
 +    tty.c_oflag |= OPOST;
 +    tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
 +    tty.c_cflag &= ~(CSIZE|PARENB);
 +    tty.c_cflag |= CS8;
 +    tty.c_cc[VMIN] = 1;
 +    tty.c_cc[VTIME] = 0;
 +
 +    tcsetattr (0, TCSANOW, &tty);
 +    signal(SIGQUIT, sigterm_handler); /* Quit (POSIX).  */
 +    }
 +#endif
 +    avformat_network_deinit();
 +
      signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).  */
      signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
  #ifdef SIGXCPU
  #endif
  }
  
 -static int decode_interrupt_cb(void)
 +/* read a key without blocking */
 +static int read_key(void)
 +{
 +    unsigned char ch;
 +#if HAVE_TERMIOS_H
 +    int n = 1;
 +    struct timeval tv;
 +    fd_set rfds;
 +
 +    FD_ZERO(&rfds);
 +    FD_SET(0, &rfds);
 +    tv.tv_sec = 0;
 +    tv.tv_usec = 0;
 +    n = select(1, &rfds, NULL, NULL, &tv);
 +    if (n > 0) {
 +        n = read(0, &ch, 1);
 +        if (n == 1)
 +            return ch;
 +
 +        return n;
 +    }
 +#elif HAVE_KBHIT
 +#    if HAVE_PEEKNAMEDPIPE
 +    static int is_pipe;
 +    static HANDLE input_handle;
 +    DWORD dw, nchars;
 +    if(!input_handle){
 +        input_handle = GetStdHandle(STD_INPUT_HANDLE);
 +        is_pipe = !GetConsoleMode(input_handle, &dw);
 +    }
 +
 +    if (stdin->_cnt > 0) {
 +        read(0, &ch, 1);
 +        return ch;
 +    }
 +    if (is_pipe) {
 +        /* When running under a GUI, you will end here. */
 +        if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL))
 +            return -1;
 +        //Read it
 +        if(nchars != 0) {
 +            read(0, &ch, 1);
 +            return ch;
 +        }else{
 +            return -1;
 +        }
 +    }
 +#    endif
 +    if(kbhit())
 +        return(getch());
 +#endif
 +    return -1;
 +}
 +
- static int decode_interrupt_cb(void)
++static int decode_interrupt_cb(void *ctx)
  {
      return received_nb_signals > 1;
  }
  
 -void exit_program(int ret)
++static const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
++
 +void av_noreturn exit_program(int ret)
  {
      int i;
  
      /* close files */
      for(i=0;i<nb_output_files;i++) {
 -        AVFormatContext *s = output_files[i];
 +        AVFormatContext *s = output_files[i].ctx;
          if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
              avio_close(s->pb);
          avformat_free_context(s);
 -        av_free(output_streams_for_file[i]);
 -        av_dict_free(&output_opts[i]);
 +        av_dict_free(&output_files[i].opts);
      }
      for(i=0;i<nb_input_files;i++) {
          av_close_input_file(input_files[i].ctx);
      for (i = 0; i < nb_input_streams; i++)
          av_dict_free(&input_streams[i].opts);
  
 -    av_free(intra_matrix);
 -    av_free(inter_matrix);
 -
      if (vstats_file)
          fclose(vstats_file);
      av_free(vstats_filename);
  
 -    av_free(streamid_map);
 -    av_free(stream_maps);
 -    av_free(meta_data_maps);
 -
      av_freep(&input_streams);
      av_freep(&input_files);
 -
 -    av_free(video_codec_name);
 -    av_free(audio_codec_name);
 -    av_free(subtitle_codec_name);
 -    av_free(data_codec_name);
 +    av_freep(&output_streams);
 +    av_freep(&output_files);
  
      uninit_opts();
      av_free(audio_buf);
  #endif
      avformat_network_deinit();
  
 +    av_freep(&input_tmp);
 +
      if (received_sigterm) {
 -        fprintf(stderr,
 -            "Received signal %d: terminating.\n",
 -            (int) received_sigterm);
 +        av_log(NULL, AV_LOG_INFO, "Received signal %d: terminating.\n",
 +               (int) received_sigterm);
          exit (255);
      }
  
@@@ -693,7 -509,7 +695,7 @@@ static void assert_avoptions(AVDictiona
  {
      AVDictionaryEntry *t;
      if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
 -        av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
 +        av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
          exit_program(1);
      }
  }
@@@ -704,12 -520,12 +706,12 @@@ static void assert_codec_experimental(A
      AVCodec *codec;
      if (c->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
          c->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
 -        av_log(NULL, AV_LOG_ERROR, "%s '%s' is experimental and might produce bad "
 +        av_log(NULL, AV_LOG_FATAL, "%s '%s' is experimental and might produce bad "
                  "results.\nAdd '-strict experimental' if you want to use it.\n",
                  codec_string, c->codec->name);
          codec = encoder ? avcodec_find_encoder(c->codec->id) : avcodec_find_decoder(c->codec->id);
          if (!(codec->capabilities & CODEC_CAP_EXPERIMENTAL))
 -            av_log(NULL, AV_LOG_ERROR, "Or use the non experimental %s '%s'.\n",
 +            av_log(NULL, AV_LOG_FATAL, "Or use the non experimental %s '%s'.\n",
                     codec_string, codec->name);
          exit_program(1);
      }
@@@ -724,9 -540,6 +726,9 @@@ static void choose_sample_fmt(AVStream 
                  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),
      }
  }
  
 -/**
 - * Update the requested input sample format based on the output sample format.
 - * This is currently only used to request float output from decoders which
 - * support multiple sample formats, one of which is AV_SAMPLE_FMT_FLT.
 - * Ideally this will be removed in the future when decoders do not do format
 - * conversion and only output in their native format.
 - */
 -static void update_sample_fmt(AVCodecContext *dec, AVCodec *dec_codec,
 -                              AVCodecContext *enc)
 -{
 -    /* if sample formats match or a decoder sample format has already been
 -       requested, just return */
 -    if (enc->sample_fmt == dec->sample_fmt ||
 -        dec->request_sample_fmt > AV_SAMPLE_FMT_NONE)
 -        return;
 -
 -    /* if decoder supports more than one output format */
 -    if (dec_codec && dec_codec->sample_fmts &&
 -        dec_codec->sample_fmts[0] != AV_SAMPLE_FMT_NONE &&
 -        dec_codec->sample_fmts[1] != AV_SAMPLE_FMT_NONE) {
 -        const enum AVSampleFormat *p;
 -        int min_dec = -1, min_inc = -1;
 -
 -        /* find a matching sample format in the encoder */
 -        for (p = dec_codec->sample_fmts; *p != AV_SAMPLE_FMT_NONE; p++) {
 -            if (*p == enc->sample_fmt) {
 -                dec->request_sample_fmt = *p;
 -                return;
 -            } else if (*p > enc->sample_fmt) {
 -                min_inc = FFMIN(min_inc, *p - enc->sample_fmt);
 -            } else
 -                min_dec = FFMIN(min_dec, enc->sample_fmt - *p);
 -        }
 -
 -        /* if none match, provide the one that matches quality closest */
 -        dec->request_sample_fmt = min_inc > 0 ? enc->sample_fmt + min_inc :
 -                                  enc->sample_fmt - min_dec;
 -    }
 -}
 -
  static void choose_sample_rate(AVStream *st, AVCodec *codec)
  {
      if(codec && codec->supported_samplerates){
@@@ -784,15 -637,89 +786,15 @@@ static void choose_pixel_fmt(AVStream *
      }
  }
  
 -static OutputStream *new_output_stream(AVFormatContext *oc, int file_idx, AVCodec *codec)
 -{
 -    OutputStream *ost;
 -    AVStream *st = av_new_stream(oc, oc->nb_streams < nb_streamid_map ? streamid_map[oc->nb_streams] : 0);
 -    int idx      = oc->nb_streams - 1;
 -
 -    if (!st) {
 -        av_log(NULL, AV_LOG_ERROR, "Could not alloc stream.\n");
 -        exit_program(1);
 -    }
 -
 -    output_streams_for_file[file_idx] =
 -        grow_array(output_streams_for_file[file_idx],
 -                   sizeof(*output_streams_for_file[file_idx]),
 -                   &nb_output_streams_for_file[file_idx],
 -                   oc->nb_streams);
 -    ost = output_streams_for_file[file_idx][idx] =
 -        av_mallocz(sizeof(OutputStream));
 -    if (!ost) {
 -        fprintf(stderr, "Could not alloc output stream\n");
 -        exit_program(1);
 -    }
 -    ost->file_index = file_idx;
 -    ost->index = idx;
 -    ost->st    = st;
 -    ost->enc   = codec;
 -    if (codec)
 -        ost->opts  = filter_codec_opts(codec_opts, codec->id, oc, st);
 -
 -    avcodec_get_context_defaults3(st->codec, codec);
 -
 -    ost->sws_flags = av_get_int(sws_opts, "sws_flags", NULL);
 -    return ost;
 -}
 -
 -static int read_avserver_streams(AVFormatContext *s, const char *filename)
 -{
 -    int i, err;
 -    AVFormatContext *ic = NULL;
 -
 -    err = avformat_open_input(&ic, filename, NULL, NULL);
 -    if (err < 0)
 -        return err;
 -    /* copy stream format */
 -    for(i=0;i<ic->nb_streams;i++) {
 -        AVStream *st;
 -        OutputStream *ost;
 -        AVCodec *codec;
 -
 -        codec = avcodec_find_encoder(ic->streams[i]->codec->codec_id);
 -        ost   = new_output_stream(s, nb_output_files, codec);
 -        st    = ost->st;
 -
 -        // FIXME: a more elegant solution is needed
 -        memcpy(st, ic->streams[i], sizeof(AVStream));
 -        st->info = NULL;
 -        avcodec_copy_context(st->codec, ic->streams[i]->codec);
 -
 -        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
 -            if (audio_stream_copy) {
 -                st->stream_copy = 1;
 -            } else
 -                choose_sample_fmt(st, codec);
 -        } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
 -            if (video_stream_copy) {
 -                st->stream_copy = 1;
 -            } else
 -                choose_pixel_fmt(st, codec);
 -        }
 -    }
 -
 -    av_close_input_file(ic);
 -    return 0;
 -}
 -
 -static double
 -get_sync_ipts(const OutputStream *ost)
 +static double get_sync_ipts(const OutputStream *ost)
  {
      const InputStream *ist = ost->sync_ist;
 -    return (double)(ist->pts - start_time)/AV_TIME_BASE;
 +    OutputFile *of = &output_files[ost->file_index];
 +    return (double)(ist->pts - of->start_time)/AV_TIME_BASE;
  }
  
 -static void write_frame(AVFormatContext *s, AVPacket *pkt, AVCodecContext *avctx, AVBitStreamFilterContext *bsfc){
 +static void write_frame(AVFormatContext *s, AVPacket *pkt, AVCodecContext *avctx, AVBitStreamFilterContext *bsfc)
 +{
      int ret;
  
      while(bsfc){
              av_free_packet(pkt);
              new_pkt.destruct= av_destruct_packet;
          } else if(a<0){
 -            fprintf(stderr, "%s failed for stream %d, codec %s",
 -                    bsfc->filter->name, pkt->stream_index,
 -                    avctx->codec ? avctx->codec->name : "copy");
 +            av_log(NULL, AV_LOG_ERROR, "%s failed for stream %d, codec %s",
 +                   bsfc->filter->name, pkt->stream_index,
 +                   avctx->codec ? avctx->codec->name : "copy");
              print_error("", a);
              if (exit_on_error)
                  exit_program(1);
      }
  }
  
 -#define MAX_AUDIO_PACKET_SIZE (128 * 1024)
 -
  static void do_audio_out(AVFormatContext *s,
                           OutputStream *ost,
                           InputStream *ist,
@@@ -853,65 -782,52 +855,65 @@@ need_realloc
      audio_out_size += FF_MIN_BUFFER_SIZE;
  
      if(audio_out_size > INT_MAX || audio_buf_size > INT_MAX){
 -        fprintf(stderr, "Buffer sizes too large\n");
 +        av_log(NULL, AV_LOG_FATAL, "Buffer sizes too large\n");
          exit_program(1);
      }
  
      av_fast_malloc(&audio_buf, &allocated_audio_buf_size, audio_buf_size);
      av_fast_malloc(&audio_out, &allocated_audio_out_size, audio_out_size);
      if (!audio_buf || !audio_out){
 -        fprintf(stderr, "Out of memory in do_audio_out\n");
 +        av_log(NULL, AV_LOG_FATAL, "Out of memory in do_audio_out\n");
          exit_program(1);
      }
  
 -    if (enc->channels != dec->channels || enc->sample_rate != dec->sample_rate)
 +    if (enc->channels != dec->channels
 +     || enc->sample_fmt != dec->sample_fmt)
          ost->audio_resample = 1;
  
      resample_changed = ost->resample_sample_fmt  != dec->sample_fmt ||
                         ost->resample_channels    != dec->channels   ||
                         ost->resample_sample_rate != dec->sample_rate;
  
 -    if ((ost->audio_resample && !ost->resample) || resample_changed) {
 +    if ((ost->audio_resample && !ost->swr) || resample_changed || ost->audio_channels_mapped) {
          if (resample_changed) {
 -            av_log(NULL, AV_LOG_INFO, "Input stream #%d.%d frame changed from rate:%d fmt:%s ch:%d to rate:%d fmt:%s ch:%d\n",
 +            av_log(NULL, AV_LOG_INFO, "Input stream #%d:%d frame changed from rate:%d fmt:%s ch:%d to rate:%d fmt:%s ch:%d\n",
                     ist->file_index, ist->st->index,
                     ost->resample_sample_rate, av_get_sample_fmt_name(ost->resample_sample_fmt), ost->resample_channels,
                     dec->sample_rate, av_get_sample_fmt_name(dec->sample_fmt), dec->channels);
              ost->resample_sample_fmt  = dec->sample_fmt;
              ost->resample_channels    = dec->channels;
              ost->resample_sample_rate = dec->sample_rate;
 -            if (ost->resample)
 -                audio_resample_close(ost->resample);
 +            swr_free(&ost->swr);
          }
          /* if audio_sync_method is >1 the resampler is needed for audio drift compensation */
 -        if (audio_sync_method <= 1 &&
 +        if (audio_sync_method <= 1 && !ost->audio_channels_mapped &&
              ost->resample_sample_fmt  == enc->sample_fmt &&
              ost->resample_channels    == enc->channels   &&
              ost->resample_sample_rate == enc->sample_rate) {
 -            ost->resample = NULL;
 +            //ost->swr = NULL;
              ost->audio_resample = 0;
 -        } else if (ost->audio_resample) {
 -            if (dec->sample_fmt != AV_SAMPLE_FMT_S16)
 -                fprintf(stderr, "Warning, using s16 intermediate sample format for resampling\n");
 -            ost->resample = av_audio_resample_init(enc->channels,    dec->channels,
 -                                                   enc->sample_rate, dec->sample_rate,
 -                                                   enc->sample_fmt,  dec->sample_fmt,
 -                                                   16, 10, 0, 0.8);
 -            if (!ost->resample) {
 -                fprintf(stderr, "Can not resample %d channels @ %d Hz to %d channels @ %d Hz\n",
 +        } else {
 +            ost->swr = swr_alloc_set_opts(ost->swr,
 +                                          enc->channel_layout, enc->sample_fmt, enc->sample_rate,
 +                                          dec->channel_layout, dec->sample_fmt, dec->sample_rate,
 +                                          0, NULL);
 +            if (ost->audio_channels_mapped)
 +                swr_set_channel_mapping(ost->swr, ost->audio_channels_map);
 +            av_opt_set_double(ost->swr, "rmvol", ost->rematrix_volume, 0);
 +            if (ost->audio_channels_mapped) {
 +                av_opt_set_int(ost->swr, "icl", av_get_default_channel_layout(ost->audio_channels_mapped), 0);
 +                av_opt_set_int(ost->swr, "uch", ost->audio_channels_mapped, 0);
 +            }
 +            av_opt_set_int(ost->swr, "ich", dec->channels, 0);
 +            av_opt_set_int(ost->swr, "och", enc->channels, 0);
 +            if(audio_sync_method>1) av_opt_set_int(ost->swr, "flags", SWR_FLAG_RESAMPLE, 0);
 +            if(ost->swr && swr_init(ost->swr) < 0){
 +                av_log(NULL, AV_LOG_FATAL, "swr_init() failed\n");
 +                swr_free(&ost->swr);
 +            }
 +
 +            if (!ost->swr) {
 +                av_log(NULL, AV_LOG_FATAL, "Can not resample %d channels @ %d Hz to %d channels @ %d Hz\n",
                          dec->channels, dec->sample_rate,
                          enc->channels, enc->sample_rate);
                  exit_program(1);
          }
      }
  
 -#define MAKE_SFMT_PAIR(a,b) ((a)+AV_SAMPLE_FMT_NB*(b))
 -    if (!ost->audio_resample && dec->sample_fmt!=enc->sample_fmt &&
 -        MAKE_SFMT_PAIR(enc->sample_fmt,dec->sample_fmt)!=ost->reformat_pair) {
 -        if (ost->reformat_ctx)
 -            av_audio_convert_free(ost->reformat_ctx);
 -        ost->reformat_ctx = av_audio_convert_alloc(enc->sample_fmt, 1,
 -                                                   dec->sample_fmt, 1, NULL, 0);
 -        if (!ost->reformat_ctx) {
 -            fprintf(stderr, "Cannot convert %s sample format to %s sample format\n",
 -                av_get_sample_fmt_name(dec->sample_fmt),
 -                av_get_sample_fmt_name(enc->sample_fmt));
 -            exit_program(1);
 -        }
 -        ost->reformat_pair=MAKE_SFMT_PAIR(enc->sample_fmt,dec->sample_fmt);
 -    }
 +    av_assert0(ost->audio_resample || dec->sample_fmt==enc->sample_fmt);
  
      if(audio_sync_method){
          double delta = get_sync_ipts(ost) * enc->sample_rate - ost->sync_opts
                      byte_delta= FFMAX(byte_delta, -size);
                      size += byte_delta;
                      buf  -= byte_delta;
 -                    if(verbose > 2)
 -                        fprintf(stderr, "discarding %d audio samples\n", (int)-delta);
 +                    av_log(NULL, AV_LOG_VERBOSE, "discarding %d audio samples\n", (int)-delta);
                      if(!size)
                          return;
                      ist->is_start=0;
                  }else{
 -                    static uint8_t *input_tmp= NULL;
                      input_tmp= av_realloc(input_tmp, byte_delta + size);
  
                      if(byte_delta > allocated_for_size - size){
                      memcpy(input_tmp + byte_delta, buf, size);
                      buf= input_tmp;
                      size += byte_delta;
 -                    if(verbose > 2)
 -                        fprintf(stderr, "adding %d audio samples of silence\n", (int)delta);
 +                    av_log(NULL, AV_LOG_VERBOSE, "adding %d audio samples of silence\n", (int)delta);
                  }
              }else if(audio_sync_method>1){
                  int comp= av_clip(delta, -audio_sync_method, audio_sync_method);
                  av_assert0(ost->audio_resample);
 -                if(verbose > 2)
 -                    fprintf(stderr, "compensating audio timestamp drift:%f compensation:%d in:%d\n", delta, comp, enc->sample_rate);
 +                av_log(NULL, AV_LOG_VERBOSE, "compensating audio timestamp drift:%f compensation:%d in:%d\n",
 +                       delta, comp, enc->sample_rate);
  //                fprintf(stderr, "drift:%f len:%d opts:%"PRId64" ipts:%"PRId64" fifo:%d\n", delta, -1, ost->sync_opts, (int64_t)(get_sync_ipts(ost) * enc->sample_rate), av_fifo_size(ost->fifo)/(ost->st->codec->channels * 2));
 -                av_resample_compensate(*(struct AVResampleContext**)ost->resample, comp, enc->sample_rate);
 +                swr_compensate(ost->swr, comp, enc->sample_rate);
              }
          }
      }else
  
      if (ost->audio_resample) {
          buftmp = audio_buf;
 -        size_out = audio_resample(ost->resample,
 -                                  (short *)buftmp, (short *)buf,
 -                                  size / (dec->channels * isize));
 +        size_out = swr_convert(ost->swr, (      uint8_t*[]){buftmp}, audio_buf_size / (enc->channels * osize),
 +                                         (const uint8_t*[]){buf   }, size / (dec->channels * isize));
          size_out = size_out * enc->channels * osize;
      } else {
          buftmp = buf;
          size_out = size;
      }
  
 -    if (!ost->audio_resample && dec->sample_fmt!=enc->sample_fmt) {
 -        const void *ibuf[6]= {buftmp};
 -        void *obuf[6]= {audio_buf};
 -        int istride[6]= {isize};
 -        int ostride[6]= {osize};
 -        int len= size_out/istride[0];
 -        if (av_audio_convert(ost->reformat_ctx, obuf, ostride, ibuf, istride, len)<0) {
 -            printf("av_audio_convert() failed\n");
 -            if (exit_on_error)
 -                exit_program(1);
 -            return;
 -        }
 -        buftmp = audio_buf;
 -        size_out = len*osize;
 -    }
 +    av_assert0(ost->audio_resample || dec->sample_fmt==enc->sample_fmt);
  
      /* now encode as many frames as possible */
      if (enc->frame_size > 1) {
          /* output resampled raw samples */
          if (av_fifo_realloc2(ost->fifo, av_fifo_size(ost->fifo) + size_out) < 0) {
 -            fprintf(stderr, "av_fifo_realloc2() failed\n");
 +            av_log(NULL, AV_LOG_FATAL, "av_fifo_realloc2() failed\n");
              exit_program(1);
          }
          av_fifo_generic_write(ost->fifo, buftmp, size_out, NULL);
              ret = avcodec_encode_audio(enc, audio_out, audio_out_size,
                                         (short *)audio_buf);
              if (ret < 0) {
 -                fprintf(stderr, "Audio encoding failed\n");
 +                av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
                  exit_program(1);
              }
              audio_size += ret;
              size_out = size_out*coded_bps/8;
  
          if(size_out > audio_out_size){
 -            fprintf(stderr, "Internal error, buffer size too small\n");
 +            av_log(NULL, AV_LOG_FATAL, "Internal error, buffer size too small\n");
              exit_program(1);
          }
  
          ret = avcodec_encode_audio(enc, audio_out, size_out,
                                     (short *)buftmp);
          if (ret < 0) {
 -            fprintf(stderr, "Audio encoding failed\n");
 +            av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
              exit_program(1);
          }
          audio_size += ret;
@@@ -1074,7 -1022,7 +1076,7 @@@ static void pre_process_video_frame(Inp
          if(avpicture_deinterlace(picture2, picture,
                                   dec->pix_fmt, dec->width, dec->height) < 0) {
              /* if error, do not deinterlace */
 -            fprintf(stderr, "Deinterlacing failed\n");
 +            av_log(NULL, AV_LOG_WARNING, "Deinterlacing failed\n");
              av_free(buf);
              buf = NULL;
              picture2 = picture;
      *bufp = buf;
  }
  
 -/* we begin to correct av delay at this threshold */
 -#define AV_DELAY_MAX 0.100
 -
  static void do_subtitle_out(AVFormatContext *s,
                              OutputStream *ost,
                              InputStream *ist,
      AVPacket pkt;
  
      if (pts == AV_NOPTS_VALUE) {
 -        fprintf(stderr, "Subtitle packets must have a pts\n");
 +        av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
          if (exit_on_error)
              exit_program(1);
          return;
          subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
                                                      subtitle_out_max_size, sub);
          if (subtitle_out_size < 0) {
 -            fprintf(stderr, "Subtitle encoding failed\n");
 +            av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
              exit_program(1);
          }
  
  static int bit_buffer_size= 1024*256;
  static uint8_t *bit_buffer= NULL;
  
 +static void do_video_resample(OutputStream *ost,
 +                              InputStream *ist,
 +                              AVFrame *in_picture,
 +                              AVFrame **out_picture)
 +{
 +#if CONFIG_AVFILTER
 +    *out_picture = in_picture;
 +#else
 +    AVCodecContext *dec = ist->st->codec;
 +    AVCodecContext *enc = ost->st->codec;
 +    int resample_changed = ost->resample_width   != dec->width  ||
 +                           ost->resample_height  != dec->height ||
 +                           ost->resample_pix_fmt != dec->pix_fmt;
 +
 +    *out_picture = in_picture;
 +    if (resample_changed) {
 +        av_log(NULL, AV_LOG_INFO,
 +               "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
 +               ist->file_index, ist->st->index,
 +               ost->resample_width, ost->resample_height, av_get_pix_fmt_name(ost->resample_pix_fmt),
 +               dec->width         , dec->height         , av_get_pix_fmt_name(dec->pix_fmt));
 +        ost->resample_width   = dec->width;
 +        ost->resample_height  = dec->height;
 +        ost->resample_pix_fmt = dec->pix_fmt;
 +    }
 +
 +    ost->video_resample = dec->width   != enc->width  ||
 +                          dec->height  != enc->height ||
 +                          dec->pix_fmt != enc->pix_fmt;
 +
 +    if (ost->video_resample) {
 +        *out_picture = &ost->resample_frame;
 +        if (!ost->img_resample_ctx || resample_changed) {
 +            /* initialize the destination picture */
 +            if (!ost->resample_frame.data[0]) {
 +                avcodec_get_frame_defaults(&ost->resample_frame);
 +                if (avpicture_alloc((AVPicture *)&ost->resample_frame, enc->pix_fmt,
 +                                    enc->width, enc->height)) {
 +                    av_log(NULL, AV_LOG_FATAL, "Cannot allocate temp picture, check pix fmt\n");
 +                    exit_program(1);
 +                }
 +            }
 +            /* initialize a new scaler context */
 +            sws_freeContext(ost->img_resample_ctx);
 +            ost->img_resample_ctx = sws_getContext(dec->width, dec->height, dec->pix_fmt,
 +                                                   enc->width, enc->height, enc->pix_fmt,
 +                                                   ost->sws_flags, NULL, NULL, NULL);
 +            if (ost->img_resample_ctx == NULL) {
 +                av_log(NULL, AV_LOG_FATAL, "Cannot get resampling context\n");
 +                exit_program(1);
 +            }
 +        }
 +        sws_scale(ost->img_resample_ctx, in_picture->data, in_picture->linesize,
 +              0, ost->resample_height, (*out_picture)->data, (*out_picture)->linesize);
 +    }
 +#endif
 +}
 +
 +
  static void do_video_out(AVFormatContext *s,
                           OutputStream *ost,
                           InputStream *ist,
                           AVFrame *in_picture,
                           int *frame_size, float quality)
  {
 -    int nb_frames, i, ret, resample_changed;
 -    AVFrame *final_picture, *formatted_picture;
 -    AVCodecContext *enc, *dec;
 +    int nb_frames, i, ret, format_video_sync;
 +    AVFrame *final_picture;
 +    AVCodecContext *enc;
      double sync_ipts;
 +    double duration = 0;
  
      enc = ost->st->codec;
 -    dec = ist->st->codec;
 +
 +    if (ist->st->start_time != AV_NOPTS_VALUE && ist->st->first_dts != AV_NOPTS_VALUE) {
 +        duration = FFMAX(av_q2d(ist->st->time_base), av_q2d(ist->st->codec->time_base));
 +        if(ist->st->avg_frame_rate.num)
 +            duration= FFMAX(duration, 1/av_q2d(ist->st->avg_frame_rate));
 +
 +        duration /= av_q2d(enc->time_base);
 +    }
  
      sync_ipts = get_sync_ipts(ost) / av_q2d(enc->time_base);
  
  
      *frame_size = 0;
  
 -    if(video_sync_method){
 -        double vdelta = sync_ipts - ost->sync_opts;
 +    format_video_sync = video_sync_method;
 +    if (format_video_sync < 0)
 +        format_video_sync = (s->oformat->flags & AVFMT_VARIABLE_FPS) ? 2 : 1;
 +
 +    if (format_video_sync) {
 +        double vdelta = sync_ipts - ost->sync_opts + duration;
          //FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
          if (vdelta < -1.1)
              nb_frames = 0;
 -        else if (video_sync_method == 2 || (video_sync_method<0 && (s->oformat->flags & AVFMT_VARIABLE_FPS))){
 +        else if (format_video_sync == 2) {
              if(vdelta<=-0.6){
                  nb_frames=0;
              }else if(vdelta>0.6)
  //fprintf(stderr, "vdelta:%f, ost->sync_opts:%"PRId64", ost->sync_ipts:%f nb_frames:%d\n", vdelta, ost->sync_opts, get_sync_ipts(ost), nb_frames);
          if (nb_frames == 0){
              ++nb_frames_drop;
 -            if (verbose>2)
 -                fprintf(stderr, "*** drop!\n");
 +            av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n");
          }else if (nb_frames > 1) {
              nb_frames_dup += nb_frames - 1;
 -            if (verbose>2)
 -                fprintf(stderr, "*** %d dup!\n", nb_frames-1);
 +            av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", nb_frames-1);
          }
      }else
          ost->sync_opts= lrintf(sync_ipts);
  
 -    nb_frames= FFMIN(nb_frames, max_frames[AVMEDIA_TYPE_VIDEO] - ost->frame_number);
 +    nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number);
      if (nb_frames <= 0)
          return;
  
 -    formatted_picture = in_picture;
 -    final_picture = formatted_picture;
 -
 -    resample_changed = ost->resample_width   != dec->width  ||
 -                       ost->resample_height  != dec->height ||
 -                       ost->resample_pix_fmt != dec->pix_fmt;
 -
 -    if (resample_changed) {
 -        av_log(NULL, AV_LOG_INFO,
 -               "Input stream #%d.%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
 -               ist->file_index, ist->st->index,
 -               ost->resample_width, ost->resample_height, av_get_pix_fmt_name(ost->resample_pix_fmt),
 -               dec->width         , dec->height         , av_get_pix_fmt_name(dec->pix_fmt));
 -        if(!ost->video_resample)
 -            exit_program(1);
 -    }
 -
 -#if !CONFIG_AVFILTER
 -    if (ost->video_resample) {
 -        final_picture = &ost->pict_tmp;
 -        if (resample_changed) {
 -            /* initialize a new scaler context */
 -            sws_freeContext(ost->img_resample_ctx);
 -            ost->img_resample_ctx = sws_getContext(
 -                ist->st->codec->width,
 -                ist->st->codec->height,
 -                ist->st->codec->pix_fmt,
 -                ost->st->codec->width,
 -                ost->st->codec->height,
 -                ost->st->codec->pix_fmt,
 -                ost->sws_flags, NULL, NULL, NULL);
 -            if (ost->img_resample_ctx == NULL) {
 -                fprintf(stderr, "Cannot get resampling context\n");
 -                exit_program(1);
 -            }
 -        }
 -        sws_scale(ost->img_resample_ctx, formatted_picture->data, formatted_picture->linesize,
 -              0, ost->resample_height, final_picture->data, final_picture->linesize);
 -    }
 -#endif
 +    do_video_resample(ost, ist, in_picture, &final_picture);
  
      /* duplicates frame if needed */
      for(i=0;i<nb_frames;i++) {
  
          if (s->oformat->flags & AVFMT_RAWPICTURE) {
              /* raw pictures are written as AVPicture structure to
 -               avoid any copies. We support temorarily the older
 +               avoid any copies. We support temporarily the older
                 method. */
 -            AVFrame* old_frame = enc->coded_frame;
 -            enc->coded_frame = dec->coded_frame; //FIXME/XXX remove this hack
 +            enc->coded_frame->interlaced_frame = in_picture->interlaced_frame;
 +            enc->coded_frame->top_field_first  = in_picture->top_field_first;
              pkt.data= (uint8_t *)final_picture;
              pkt.size=  sizeof(AVPicture);
              pkt.pts= av_rescale_q(ost->sync_opts, enc->time_base, ost->st->time_base);
              pkt.flags |= AV_PKT_FLAG_KEY;
  
              write_frame(s, &pkt, ost->st->codec, ost->bitstream_filters);
 -            enc->coded_frame = old_frame;
          } else {
              AVFrame big_picture;
  
                 settings */
              big_picture.interlaced_frame = in_picture->interlaced_frame;
              if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)) {
 -                if(top_field_first == -1)
 +                if (ost->top_field_first == -1)
                      big_picture.top_field_first = in_picture->top_field_first;
                  else
 -                    big_picture.top_field_first = top_field_first;
 +                    big_picture.top_field_first = !!ost->top_field_first;
              }
  
 -            /* handles sameq here. This is not correct because it may
 +            /* handles same_quant here. This is not correct because it may
                 not be a global option */
              big_picture.quality = quality;
 -            if(!me_threshold)
 +            if (!enc->me_threshold)
                  big_picture.pict_type = 0;
  //            big_picture.pts = AV_NOPTS_VALUE;
              big_picture.pts= ost->sync_opts;
                                         bit_buffer, bit_buffer_size,
                                         &big_picture);
              if (ret < 0) {
 -                fprintf(stderr, "Video encoding failed\n");
 +                av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
                  exit_program(1);
              }
  
      }
  }
  
 -static double psnr(double d){
 +static double psnr(double d)
 +{
      return -10.0*log(d)/log(10.0);
  }
  
@@@ -1398,9 -1319,9 +1400,9 @@@ static void do_video_stats(AVFormatCont
      }
  }
  
 -static void print_report(AVFormatContext **output_files,
 -                         OutputStream **ost_table, int nb_ostreams,
 -                         int is_last_report)
 +static void print_report(OutputFile *output_files,
 +                         OutputStream *ost_table, int nb_ostreams,
 +                         int is_last_report, int64_t timer_start, int64_t cur_time)
  {
      char buf[1024];
      OutputStream *ost;
      int64_t total_size;
      AVCodecContext *enc;
      int frame_number, vid, i;
 -    double bitrate, ti1, pts;
 +    double bitrate;
 +    int64_t pts = INT64_MAX;
      static int64_t last_time = -1;
      static int qp_histogram[52];
 +    int hours, mins, secs, us;
 +
 +    if (!print_stats && !is_last_report)
 +        return;
  
      if (!is_last_report) {
 -        int64_t cur_time;
 -        /* display the report every 0.5 seconds */
 -        cur_time = av_gettime();
          if (last_time == -1) {
              last_time = cur_time;
              return;
      }
  
  
 -    oc = output_files[0];
 +    oc = output_files[0].ctx;
  
      total_size = avio_size(oc->pb);
      if(total_size<0) // FIXME improve avio_size() so it works with non seekable output too
          total_size= avio_tell(oc->pb);
  
      buf[0] = '\0';
 -    ti1 = 1e10;
      vid = 0;
      for(i=0;i<nb_ostreams;i++) {
          float q = -1;
 -        ost = ost_table[i];
 +        ost = &ost_table[i];
          enc = ost->st->codec;
 -        if (!ost->st->stream_copy && enc->coded_frame)
 +        if (!ost->stream_copy && enc->coded_frame)
              q = enc->coded_frame->quality/(float)FF_QP2LAMBDA;
          if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
              snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q);
          }
          if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
 -            float t = (av_gettime()-timer_start) / 1000000.0;
 +            float t = (cur_time-timer_start) / 1000000.0;
  
              frame_number = ost->frame_number;
              snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3d q=%3.1f ",
              vid = 1;
          }
          /* compute min output value */
 -        pts = (double)ost->st->pts.val * av_q2d(ost->st->time_base);
 -        if ((pts < ti1) && (pts > 0))
 -            ti1 = pts;
 +        pts = FFMIN(pts, av_rescale_q(ost->st->pts.val,
 +                                      ost->st->time_base, AV_TIME_BASE_Q));
      }
 -    if (ti1 < 0.01)
 -        ti1 = 0.01;
  
 -    if (verbose > 0 || is_last_report) {
 -        bitrate = (double)(total_size * 8) / ti1 / 1000.0;
 +    secs = pts / AV_TIME_BASE;
 +    us = pts % AV_TIME_BASE;
 +    mins = secs / 60;
 +    secs %= 60;
 +    hours = mins / 60;
 +    mins %= 60;
  
 -        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
 -            "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
 -            (double)total_size / 1024, ti1, bitrate);
 +    bitrate = pts ? total_size * 8 / (pts / 1000.0) : 0;
  
 -        if (nb_frames_dup || nb_frames_drop)
 -          snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
 -                  nb_frames_dup, nb_frames_drop);
 +    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
 +             "size=%8.0fkB time=", total_size / 1024.0);
 +    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
 +             "%02d:%02d:%02d.%02d ", hours, mins, secs,
 +             (100 * us) / AV_TIME_BASE);
 +    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
 +             "bitrate=%6.1fkbits/s", bitrate);
  
 -        if (verbose >= 0)
 -            fprintf(stderr, "%s    \r", buf);
 +    if (nb_frames_dup || nb_frames_drop)
 +        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
 +                nb_frames_dup, nb_frames_drop);
  
 -        fflush(stderr);
 -    }
 +    av_log(NULL, AV_LOG_INFO, "%s    \r", buf);
 +
 +    fflush(stderr);
  
 -    if (is_last_report && verbose >= 0){
 +    if (is_last_report{
          int64_t raw= audio_size + video_size + extra_size;
 -        fprintf(stderr, "\n");
 -        fprintf(stderr, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
 -                video_size/1024.0,
 -                audio_size/1024.0,
 -                extra_size/1024.0,
 -                100.0*(total_size - raw)/raw
 +        av_log(NULL, AV_LOG_INFO, "\n");
 +        av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
 +               video_size/1024.0,
 +               audio_size/1024.0,
 +               extra_size/1024.0,
 +               100.0*(total_size - raw)/raw
          );
      }
  }
  
 -static void generate_silence(uint8_tbuf, enum AVSampleFormat sample_fmt, size_t size)
 +static void generate_silence(uint8_t *buf, enum AVSampleFormat sample_fmt, size_t size)
  {
      int fill_char = 0x00;
      if (sample_fmt == AV_SAMPLE_FMT_U8)
      memset(buf, fill_char, size);
  }
  
 -/* pkt = NULL means EOF (needed to flush decoder buffers) */
 -static int output_packet(InputStream *ist, int ist_index,
 -                         OutputStream **ost_table, int nb_ostreams,
 -                         const AVPacket *pkt)
 +static void flush_encoders(OutputStream *ost_table, int nb_ostreams)
  {
 -    AVFormatContext *os;
 -    OutputStream *ost;
 -    int ret, i;
 -    int got_output;
 -    AVFrame picture;
 -    void *buffer_to_free = NULL;
 -    static unsigned int samples_size= 0;
 -    AVSubtitle subtitle, *subtitle_to_free;
 -    int64_t pkt_pts = AV_NOPTS_VALUE;
 -#if CONFIG_AVFILTER
 -    int frame_available;
 -#endif
 -    float quality;
 +    int i, ret;
  
 -    AVPacket avpkt;
 -    int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt);
 +    for (i = 0; i < nb_ostreams; i++) {
 +        OutputStream   *ost = &ost_table[i];
 +        AVCodecContext *enc = ost->st->codec;
 +        AVFormatContext *os = output_files[ost->file_index].ctx;
  
 -    if(ist->next_pts == AV_NOPTS_VALUE)
 -        ist->next_pts= ist->pts;
 +        if (!ost->encoding_needed)
 +            continue;
  
 -    if (pkt == NULL) {
 -        /* EOF handling */
 -        av_init_packet(&avpkt);
 -        avpkt.data = NULL;
 -        avpkt.size = 0;
 -        goto handle_eof;
 -    } else {
 -        avpkt = *pkt;
 -    }
 +        if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <=1)
 +            continue;
 +        if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE))
 +            continue;
 +
 +        for(;;) {
 +            AVPacket pkt;
 +            int fifo_bytes;
 +            av_init_packet(&pkt);
 +            pkt.stream_index= ost->index;
 +
 +            switch (ost->st->codec->codec_type) {
 +            case AVMEDIA_TYPE_AUDIO:
 +                fifo_bytes = av_fifo_size(ost->fifo);
 +                ret = 0;
 +                /* encode any samples remaining in fifo */
 +                if (fifo_bytes > 0) {
 +                    int osize = av_get_bytes_per_sample(enc->sample_fmt);
 +                    int fs_tmp = enc->frame_size;
 +
 +                    av_fifo_generic_read(ost->fifo, audio_buf, fifo_bytes, NULL);
 +                    if (enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
 +                        enc->frame_size = fifo_bytes / (osize * enc->channels);
 +                    } else { /* pad */
 +                        int frame_bytes = enc->frame_size*osize*enc->channels;
 +                        if (allocated_audio_buf_size < frame_bytes)
 +                            exit_program(1);
 +                        generate_silence(audio_buf+fifo_bytes, enc->sample_fmt, frame_bytes - fifo_bytes);
 +                    }
 +
 +                    ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, (short *)audio_buf);
 +                    pkt.duration = av_rescale((int64_t)enc->frame_size*ost->st->time_base.den,
 +                                              ost->st->time_base.num, enc->sample_rate);
 +                    enc->frame_size = fs_tmp;
 +                }
 +                if (ret <= 0) {
 +                    ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL);
 +                }
 +                if (ret < 0) {
 +                    av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
 +                    exit_program(1);
 +                }
 +                audio_size += ret;
 +                pkt.flags |= AV_PKT_FLAG_KEY;
 +                break;
 +            case AVMEDIA_TYPE_VIDEO:
 +                ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL);
 +                if (ret < 0) {
 +                    av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
 +                    exit_program(1);
 +                }
 +                video_size += ret;
 +                if(enc->coded_frame && enc->coded_frame->key_frame)
 +                    pkt.flags |= AV_PKT_FLAG_KEY;
 +                if (ost->logfile && enc->stats_out) {
 +                    fprintf(ost->logfile, "%s", enc->stats_out);
 +                }
 +                break;
 +            default:
 +                ret=-1;
 +            }
 +
 +            if (ret <= 0)
 +                break;
 +            pkt.data = bit_buffer;
 +            pkt.size = ret;
 +            if (enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
 +                pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
 +            write_frame(os, &pkt, ost->st->codec, ost->bitstream_filters);
 +        }
 +    }
 +}
  
 -    if(pkt->dts != AV_NOPTS_VALUE)
 -        ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
 +/* pkt = NULL means EOF (needed to flush decoder buffers) */
 +static int output_packet(InputStream *ist, int ist_index,
 +                         OutputStream *ost_table, int nb_ostreams,
 +                         const AVPacket *pkt)
 +{
 +    AVFormatContext *os;
 +    OutputStream *ost;
 +    int ret = 0, i;
 +    int got_output;
 +    void *buffer_to_free = NULL;
 +    static unsigned int samples_size= 0;
 +    AVSubtitle subtitle, *subtitle_to_free;
 +    int64_t pkt_dts = AV_NOPTS_VALUE;
 +    int64_t pkt_pts = AV_NOPTS_VALUE;
 +#if CONFIG_AVFILTER
 +    int frame_available;
 +#endif
 +    float quality = 0;
 +
 +    AVPacket avpkt;
 +    int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt);
 +
 +    if(ist->next_pts == AV_NOPTS_VALUE)
 +        ist->next_pts= ist->pts;
 +
 +    if (pkt == NULL) {
 +        /* EOF handling */
 +        av_init_packet(&avpkt);
 +        avpkt.data = NULL;
 +        avpkt.size = 0;
 +        goto handle_eof;
 +    } else {
 +        avpkt = *pkt;
 +    }
 +
 +    if(pkt->dts != AV_NOPTS_VALUE){
 +        if(ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed)
 +            ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
 +        pkt_dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
 +    }
      if(pkt->pts != AV_NOPTS_VALUE)
          pkt_pts = av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
  
      while (avpkt.size > 0 || (!pkt && got_output)) {
          uint8_t *data_buf, *decoded_data_buf;
          int data_size, decoded_data_size;
 +        AVFrame *decoded_frame, *filtered_frame;
      handle_eof:
          ist->pts= ist->next_pts;
  
 -        if(avpkt.size && avpkt.size != pkt->size &&
 -           ((!ist->showed_multi_packet_warning && verbose>0) || verbose>1)){
 -            fprintf(stderr, "Multiple frames in a packet from stream %d\n", pkt->stream_index);
 +        if(avpkt.size && avpkt.size != pkt->size)
 +            av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,
 +                   "Multiple frames in a packet from stream %d\n", pkt->stream_index);
              ist->showed_multi_packet_warning=1;
 -        }
  
          /* decode the packet if needed */
 +        decoded_frame    = filtered_frame = NULL;
          decoded_data_buf = NULL; /* fail safe */
          decoded_data_size= 0;
          data_buf  = avpkt.data;
          if (ist->decoding_needed) {
              switch(ist->st->codec->codec_type) {
              case AVMEDIA_TYPE_AUDIO:{
 -                if(pkt && samples_size < FFMAX(pkt->size*sizeof(*samples), AVCODEC_MAX_AUDIO_FRAME_SIZE)) {
 -                    samples_size = FFMAX(pkt->size*sizeof(*samples), AVCODEC_MAX_AUDIO_FRAME_SIZE);
 +                if(pkt && samples_size < FFMAX(pkt->size * bps, AVCODEC_MAX_AUDIO_FRAME_SIZE)) {
 +                    samples_size = FFMAX(pkt->size * bps, AVCODEC_MAX_AUDIO_FRAME_SIZE);
                      av_free(samples);
                      samples= av_malloc(samples_size);
                  }
                      (ist->st->codec->sample_rate * ist->st->codec->channels);
                  break;}
              case AVMEDIA_TYPE_VIDEO:
 -                    decoded_data_size = (ist->st->codec->width * ist->st->codec->height * 3) / 2;
 -                    /* XXX: allocate picture correctly */
 -                    avcodec_get_frame_defaults(&picture);
 +                    if (!(decoded_frame = avcodec_alloc_frame()))
 +                        return AVERROR(ENOMEM);
                      avpkt.pts = pkt_pts;
 -                    avpkt.dts = ist->pts;
 +                    avpkt.dts = pkt_dts;
                      pkt_pts = AV_NOPTS_VALUE;
 +                    if(pkt_dts != AV_NOPTS_VALUE && ist->st->codec->time_base.num != 0) {
 +                        int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
 +                        pkt_dts += ((int64_t)AV_TIME_BASE *
 +                                          ist->st->codec->time_base.num * ticks) /
 +                            ist->st->codec->time_base.den;
 +                    }else
 +                        pkt_dts = AV_NOPTS_VALUE;
  
                      ret = avcodec_decode_video2(ist->st->codec,
 -                                                &picture, &got_output, &avpkt);
 -                    quality = same_quality ? picture.quality : 0;
 +                                                decoded_frame, &got_output, &avpkt);
 +                    quality = same_quant ? decoded_frame->quality : 0;
                      if (ret < 0)
 -                        return ret;
 +                        goto fail;
                      if (!got_output) {
                          /* no picture yet */
 +                        av_freep(&decoded_frame);
                          goto discard_packet;
                      }
 -                    ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, picture.pkt_pts, picture.pkt_dts);
 +
 +                    if(decoded_frame->best_effort_timestamp != AV_NOPTS_VALUE)
 +                        ist->next_pts = ist->pts = decoded_frame->best_effort_timestamp;
 +
                      if (ist->st->codec->time_base.num != 0) {
                          int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
                          ist->next_pts += ((int64_t)AV_TIME_BASE *
                      }
                      avpkt.size = 0;
                      buffer_to_free = NULL;
 -                    pre_process_video_frame(ist, (AVPicture *)&picture, &buffer_to_free);
 +                    pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
                      break;
              case AVMEDIA_TYPE_SUBTITLE:
                  ret = avcodec_decode_subtitle2(ist->st->codec,
                  }
                  break;
              }
 -            ret = avpkt.size;
              avpkt.size = 0;
          }
  
  #if CONFIG_AVFILTER
 -        if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
 -            for (i = 0; i < nb_ostreams; i++) {
 -                ost = ost_table[i];
 +        if(ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
 +        for(i=0;i<nb_ostreams;i++) {
 +            OutputFile *of = &output_files[ost_table[i].file_index];
 +            if (of->start_time == 0 || ist->pts >= of->start_time) {
 +                ost = &ost_table[i];
                  if (ost->input_video_filter && ost->source_index == ist_index) {
 -                    AVRational sar;
 -                    if (ist->st->sample_aspect_ratio.num)
 -                        sar = ist->st->sample_aspect_ratio;
 -                    else
 -                        sar = ist->st->codec->sample_aspect_ratio;
 -                    // add it to be filtered
 -                    av_vsrc_buffer_add_frame(ost->input_video_filter, &picture,
 -                                             ist->pts,
 -                                             sar);
 +                    if (!decoded_frame->sample_aspect_ratio.num)
 +                        decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
 +                    decoded_frame->pts = ist->pts;
 +
 +                    av_vsrc_buffer_add_frame(ost->input_video_filter, decoded_frame, AV_VSRC_BUF_FLAG_OVERWRITE);
                  }
              }
          }
          // preprocess audio (volume)
          if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
              if (audio_volume != 256) {
 -                short *volp;
 -                volp = samples;
 -                for(i=0;i<(decoded_data_size / sizeof(short));i++) {
 -                    int v = ((*volp) * audio_volume + 128) >> 8;
 -                    if (v < -32768) v = -32768;
 -                    if (v >  32767) v = 32767;
 -                    *volp++ = v;
 +                switch (ist->st->codec->sample_fmt) {
 +                case AV_SAMPLE_FMT_U8:
 +                {
 +                    uint8_t *volp = samples;
 +                    for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
 +                        int v = (((*volp - 128) * audio_volume + 128) >> 8) + 128;
 +                        *volp++ = av_clip_uint8(v);
 +                    }
 +                    break;
 +                }
 +                case AV_SAMPLE_FMT_S16:
 +                {
 +                    int16_t *volp = samples;
 +                    for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
 +                        int v = ((*volp) * audio_volume + 128) >> 8;
 +                        *volp++ = av_clip_int16(v);
 +                    }
 +                    break;
 +                }
 +                case AV_SAMPLE_FMT_S32:
 +                {
 +                    int32_t *volp = samples;
 +                    for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
 +                        int64_t v = (((int64_t)*volp * audio_volume + 128) >> 8);
 +                        *volp++ = av_clipl_int32(v);
 +                    }
 +                    break;
 +                }
 +                case AV_SAMPLE_FMT_FLT:
 +                {
 +                    float *volp = samples;
 +                    float scale = audio_volume / 256.f;
 +                    for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
 +                        *volp++ *= scale;
 +                    }
 +                    break;
 +                }
 +                case AV_SAMPLE_FMT_DBL:
 +                {
 +                    double *volp = samples;
 +                    double scale = audio_volume / 256.;
 +                    for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
 +                        *volp++ *= scale;
 +                    }
 +                    break;
 +                }
 +                default:
 +                    av_log(NULL, AV_LOG_FATAL,
 +                           "Audio volume adjustment on sample format %s is not supported.\n",
 +                           av_get_sample_fmt_name(ist->st->codec->sample_fmt));
 +                    exit_program(1);
                  }
              }
          }
  
          /* frame rate emulation */
 -        if (rate_emu) {
 +        if (input_files[ist->file_index].rate_emu) {
              int64_t pts = av_rescale(ist->pts, 1000000, AV_TIME_BASE);
              int64_t now = av_gettime() - ist->start;
              if (pts > now)
          }
          /* if output time reached then transcode raw format,
             encode packets and output them */
 -        if (start_time == 0 || ist->pts >= start_time)
 -            for(i=0;i<nb_ostreams;i++) {
 -                int frame_size;
 +        for (i = 0; i < nb_ostreams; i++) {
 +            OutputFile *of = &output_files[ost_table[i].file_index];
 +            int frame_size;
 +
 +            ost = &ost_table[i];
 +            if (ost->source_index != ist_index)
 +                continue;
 +
 +            if (of->start_time && ist->pts < of->start_time)
 +                continue;
 +
 +            if (of->recording_time != INT64_MAX &&
 +                av_compare_ts(ist->pts, AV_TIME_BASE_Q, of->recording_time + of->start_time,
 +                              (AVRational){1, 1000000}) >= 0) {
 +                ost->is_past_recording_time = 1;
 +                continue;
 +            }
  
 -                ost = ost_table[i];
 -                if (ost->source_index == ist_index) {
  #if CONFIG_AVFILTER
 -                frame_available = ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
 -                    !ost->output_video_filter || avfilter_poll_frame(ost->output_video_filter->inputs[0]);
 -                while (frame_available) {
 -                    AVRational ist_pts_tb;
 -                    if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter)
 -                        get_filtered_video_frame(ost->output_video_filter, &picture, &ost->picref, &ist_pts_tb);
 -                    if (ost->picref)
 +            frame_available = ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
 +                !ost->output_video_filter || avfilter_poll_frame(ost->output_video_filter->inputs[0]);
 +            while (frame_available) {
 +                if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter) {
 +                    AVRational ist_pts_tb = ost->output_video_filter->inputs[0]->time_base;
 +                    if (av_buffersink_get_buffer_ref(ost->output_video_filter, &ost->picref, 0) < 0)
 +                        goto cont;
 +                    if (!filtered_frame && !(filtered_frame = avcodec_alloc_frame())) {
 +                        ret = AVERROR(ENOMEM);
 +                        goto fail;
 +                    }
 +                    *filtered_frame= *decoded_frame; //for me_threshold
 +                    if (ost->picref) {
 +                        avfilter_fill_frame_from_video_buffer_ref(filtered_frame, ost->picref);
                          ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
 +                    }
 +                }
 +#else
 +                filtered_frame = decoded_frame;
  #endif
 -                    os = output_files[ost->file_index];
 -
 -                    /* set the input output pts pairs */
 -                    //ost->sync_ipts = (double)(ist->pts + input_files[ist->file_index].ts_offset - start_time)/ AV_TIME_BASE;
 -
 -                    if (ost->encoding_needed) {
 -                        av_assert0(ist->decoding_needed);
 -                        switch(ost->st->codec->codec_type) {
 -                        case AVMEDIA_TYPE_AUDIO:
 -                            do_audio_out(os, ost, ist, decoded_data_buf, decoded_data_size);
 -                            break;
 -                        case AVMEDIA_TYPE_VIDEO:
 +                os = output_files[ost->file_index].ctx;
 +
 +                /* set the input output pts pairs */
 +                //ost->sync_ipts = (double)(ist->pts + input_files[ist->file_index].ts_offset - start_time)/ AV_TIME_BASE;
 +
 +                if (ost->encoding_needed) {
 +                    av_assert0(ist->decoding_needed);
 +                    switch(ost->st->codec->codec_type) {
 +                    case AVMEDIA_TYPE_AUDIO:
 +                        do_audio_out(os, ost, ist, decoded_data_buf, decoded_data_size);
 +                        break;
 +                    case AVMEDIA_TYPE_VIDEO:
  #if CONFIG_AVFILTER
 -                            if (ost->picref->video && !ost->frame_aspect_ratio)
 -                                ost->st->codec->sample_aspect_ratio = ost->picref->video->pixel_aspect;
 +                        if (ost->picref->video && !ost->frame_aspect_ratio)
 +                            ost->st->codec->sample_aspect_ratio = ost->picref->video->sample_aspect_ratio;
  #endif
 -                            do_video_out(os, ost, ist, &picture, &frame_size,
 -                                         same_quality ? quality : ost->st->codec->global_quality);
 -                            if (vstats_filename && frame_size)
 -                                do_video_stats(os, ost, frame_size);
 -                            break;
 -                        case AVMEDIA_TYPE_SUBTITLE:
 -                            do_subtitle_out(os, ost, ist, &subtitle,
 -                                            pkt->pts);
 -                            break;
 -                        default:
 -                            abort();
 -                        }
 -                    } else {
 -                        AVFrame avframe; //FIXME/XXX remove this
 -                        AVPacket opkt;
 -                        int64_t ost_tb_start_time= av_rescale_q(start_time, AV_TIME_BASE_Q, ost->st->time_base);
 -
 -                        av_init_packet(&opkt);
 +                        do_video_out(os, ost, ist, filtered_frame, &frame_size,
 +                                     same_quant ? quality : ost->st->codec->global_quality);
 +                        if (vstats_filename && frame_size)
 +                            do_video_stats(os, ost, frame_size);
 +                        break;
 +                    case AVMEDIA_TYPE_SUBTITLE:
 +                        do_subtitle_out(os, ost, ist, &subtitle,
 +                                        pkt->pts);
 +                        break;
 +                    default:
 +                        abort();
 +                    }
 +                } else {
 +                    AVPicture pict;
 +                    AVPacket opkt;
 +                    int64_t ost_tb_start_time= av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base);
 +                    av_init_packet(&opkt);
  
 -                        if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) && !copy_initial_nonkeyframes)
 +                    if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
 +                        !ost->copy_initial_nonkeyframes)
  #if !CONFIG_AVFILTER
 -                            continue;
 +                        continue;
  #else
 -                            goto cont;
 +                        goto cont;
  #endif
  
 -                        /* no reencoding needed : output the packet directly */
 -                        /* force the input stream PTS */
 +                    /* no reencoding needed : output the packet directly */
 +                    /* force the input stream PTS */
  
 -                        avcodec_get_frame_defaults(&avframe);
 -                        ost->st->codec->coded_frame= &avframe;
 -                        avframe.key_frame = pkt->flags & AV_PKT_FLAG_KEY;
 +                    if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
 +                        audio_size += data_size;
 +                    else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
 +                        video_size += data_size;
 +                        ost->sync_opts++;
 +                    }
  
 -                        if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
 -                            audio_size += data_size;
 -                        else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
 -                            video_size += data_size;
 -                            ost->sync_opts++;
 -                        }
 +                    opkt.stream_index= ost->index;
 +                    if(pkt->pts != AV_NOPTS_VALUE)
 +                        opkt.pts= av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;
 +                    else
 +                        opkt.pts= AV_NOPTS_VALUE;
  
 -                        opkt.stream_index= ost->index;
 -                        if(pkt->pts != AV_NOPTS_VALUE)
 -                            opkt.pts= av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;
 -                        else
 -                            opkt.pts= AV_NOPTS_VALUE;
 -
 -                        if (pkt->dts == AV_NOPTS_VALUE)
 -                            opkt.dts = av_rescale_q(ist->pts, AV_TIME_BASE_Q, ost->st->time_base);
 -                        else
 -                            opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);
 -                        opkt.dts -= ost_tb_start_time;
 -
 -                        opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base);
 -                        opkt.flags= pkt->flags;
 -
 -                        //FIXME remove the following 2 lines they shall be replaced by the bitstream filters
 -                        if(   ost->st->codec->codec_id != CODEC_ID_H264
 -                           && ost->st->codec->codec_id != CODEC_ID_MPEG1VIDEO
 -                           && ost->st->codec->codec_id != CODEC_ID_MPEG2VIDEO
 -                           ) {
 -                            if(av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, data_buf, data_size, pkt->flags & AV_PKT_FLAG_KEY))
 -                                opkt.destruct= av_destruct_packet;
 -                        } else {
 -                            opkt.data = data_buf;
 -                            opkt.size = data_size;
 -                        }
 +                    if (pkt->dts == AV_NOPTS_VALUE)
 +                        opkt.dts = av_rescale_q(ist->pts, AV_TIME_BASE_Q, ost->st->time_base);
 +                    else
 +                        opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);
 +                    opkt.dts -= ost_tb_start_time;
 +
 +                    opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base);
 +                    opkt.flags= pkt->flags;
 +
 +                    //FIXME remove the following 2 lines they shall be replaced by the bitstream filters
 +                    if(   ost->st->codec->codec_id != CODEC_ID_H264
 +                       && ost->st->codec->codec_id != CODEC_ID_MPEG1VIDEO
 +                       && ost->st->codec->codec_id != CODEC_ID_MPEG2VIDEO
 +                       ) {
 +                        if(av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, data_buf, data_size, pkt->flags & AV_PKT_FLAG_KEY))
 +                            opkt.destruct= av_destruct_packet;
 +                    } else {
 +                        opkt.data = data_buf;
 +                        opkt.size = data_size;
 +                    }
  
 -                        write_frame(os, &opkt, ost->st->codec, ost->bitstream_filters);
 -                        ost->st->codec->frame_number++;
 -                        ost->frame_number++;
 -                        av_free_packet(&opkt);
 +                    if (os->oformat->flags & AVFMT_RAWPICTURE) {
 +                        /* store AVPicture in AVPacket, as expected by the output format */
 +                        avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height);
 +                        opkt.data = (uint8_t *)&pict;
 +                        opkt.size = sizeof(AVPicture);
 +                        opkt.flags |= AV_PKT_FLAG_KEY;
                      }
 -#if CONFIG_AVFILTER
 -                    cont:
 -                    frame_available = (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
 -                                       ost->output_video_filter && avfilter_poll_frame(ost->output_video_filter->inputs[0]);
 -                    if (ost->picref)
 -                        avfilter_unref_buffer(ost->picref);
 -                }
 -#endif
 +                    write_frame(os, &opkt, ost->st->codec, ost->bitstream_filters);
 +                    ost->st->codec->frame_number++;
 +                    ost->frame_number++;
 +                    av_free_packet(&opkt);
                  }
 +#if CONFIG_AVFILTER
 +                cont:
 +                frame_available = (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
 +                                   ost->output_video_filter && avfilter_poll_frame(ost->output_video_filter->inputs[0]);
 +                avfilter_unref_buffer(ost->picref);
              }
 +            av_freep(&filtered_frame);
 +#endif
 +        }
  
 +fail:
          av_free(buffer_to_free);
          /* XXX: allocate the subtitles in the codec ? */
          if (subtitle_to_free) {
              avsubtitle_free(subtitle_to_free);
              subtitle_to_free = NULL;
          }
 +        av_freep(&decoded_frame);
 +        if (ret < 0)
 +            return ret;
      }
   discard_packet:
 -    if (pkt == NULL) {
 -        /* EOF handling */
 -
 -        for(i=0;i<nb_ostreams;i++) {
 -            ost = ost_table[i];
 -            if (ost->source_index == ist_index) {
 -                AVCodecContext *enc= ost->st->codec;
 -                os = output_files[ost->file_index];
 -
 -                if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <=1)
 -                    continue;
 -                if(ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE))
 -                    continue;
 -
 -                if (ost->encoding_needed) {
 -                    for(;;) {
 -                        AVPacket pkt;
 -                        int fifo_bytes;
 -                        av_init_packet(&pkt);
 -                        pkt.stream_index= ost->index;
 -
 -                        switch(ost->st->codec->codec_type) {
 -                        case AVMEDIA_TYPE_AUDIO:
 -                            fifo_bytes = av_fifo_size(ost->fifo);
 -                            ret = 0;
 -                            /* encode any samples remaining in fifo */
 -                            if (fifo_bytes > 0) {
 -                                int osize = av_get_bytes_per_sample(enc->sample_fmt);
 -                                int fs_tmp = enc->frame_size;
 -
 -                                av_fifo_generic_read(ost->fifo, audio_buf, fifo_bytes, NULL);
 -                                if (enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
 -                                    enc->frame_size = fifo_bytes / (osize * enc->channels);
 -                                } else { /* pad */
 -                                    int frame_bytes = enc->frame_size*osize*enc->channels;
 -                                    if (allocated_audio_buf_size < frame_bytes)
 -                                        exit_program(1);
 -                                    generate_silence(audio_buf+fifo_bytes, enc->sample_fmt, frame_bytes - fifo_bytes);
 -                                }
 -
 -                                ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, (short *)audio_buf);
 -                                pkt.duration = av_rescale((int64_t)enc->frame_size*ost->st->time_base.den,
 -                                                          ost->st->time_base.num, enc->sample_rate);
 -                                enc->frame_size = fs_tmp;
 -                            }
 -                            if(ret <= 0) {
 -                                ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL);
 -                            }
 -                            if (ret < 0) {
 -                                fprintf(stderr, "Audio encoding failed\n");
 -                                exit_program(1);
 -                            }
 -                            audio_size += ret;
 -                            pkt.flags |= AV_PKT_FLAG_KEY;
 -                            break;
 -                        case AVMEDIA_TYPE_VIDEO:
 -                            ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL);
 -                            if (ret < 0) {
 -                                fprintf(stderr, "Video encoding failed\n");
 -                                exit_program(1);
 -                            }
 -                            video_size += ret;
 -                            if(enc->coded_frame && enc->coded_frame->key_frame)
 -                                pkt.flags |= AV_PKT_FLAG_KEY;
 -                            if (ost->logfile && enc->stats_out) {
 -                                fprintf(ost->logfile, "%s", enc->stats_out);
 -                            }
 -                            break;
 -                        default:
 -                            ret=-1;
 -                        }
 -
 -                        if(ret<=0)
 -                            break;
 -                        pkt.data= bit_buffer;
 -                        pkt.size= ret;
 -                        if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
 -                            pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
 -                        write_frame(os, &pkt, ost->st->codec, ost->bitstream_filters);
 -                    }
 -                }
 -            }
 -        }
 -    }
  
      return 0;
  }
  
 -static void print_sdp(AVFormatContext **avc, int n)
 +static void print_sdp(OutputFile *output_files, int n)
  {
      char sdp[2048];
 +    int i;
 +    AVFormatContext **avc = av_malloc(sizeof(*avc)*n);
 +
 +    if (!avc)
 +        exit_program(1);
 +    for (i = 0; i < n; i++)
 +        avc[i] = output_files[i].ctx;
  
      av_sdp_create(avc, n, sdp, sizeof(sdp));
      printf("SDP:\n%s\n", sdp);
      fflush(stdout);
 +    av_freep(&avc);
  }
  
 -static int copy_chapters(int infile, int outfile)
 +static int init_input_stream(int ist_index, OutputStream *output_streams, int nb_output_streams,
 +                             char *error, int error_len)
  {
 -    AVFormatContext *is = input_files[infile].ctx;
 -    AVFormatContext *os = output_files[outfile];
 -    int i;
 -
 -    for (i = 0; i < is->nb_chapters; i++) {
 -        AVChapter *in_ch = is->chapters[i], *out_ch;
 -        int64_t ts_off   = av_rescale_q(start_time - input_files[infile].ts_offset,
 -                                      AV_TIME_BASE_Q, in_ch->time_base);
 -        int64_t rt       = (recording_time == INT64_MAX) ? INT64_MAX :
 -                           av_rescale_q(recording_time, AV_TIME_BASE_Q, in_ch->time_base);
 -
 -
 -        if (in_ch->end < ts_off)
 -            continue;
 -        if (rt != INT64_MAX && in_ch->start > rt + ts_off)
 -            break;
 -
 -        out_ch = av_mallocz(sizeof(AVChapter));
 -        if (!out_ch)
 -            return AVERROR(ENOMEM);
 -
 -        out_ch->id        = in_ch->id;
 -        out_ch->time_base = in_ch->time_base;
 -        out_ch->start     = FFMAX(0,  in_ch->start - ts_off);
 -        out_ch->end       = FFMIN(rt, in_ch->end   - ts_off);
 -
 -        if (metadata_chapters_autocopy)
 -            av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
 -
 -        os->nb_chapters++;
 -        os->chapters = av_realloc(os->chapters, sizeof(AVChapter)*os->nb_chapters);
 -        if (!os->chapters)
 -            return AVERROR(ENOMEM);
 -        os->chapters[os->nb_chapters - 1] = out_ch;
 +    InputStream *ist = &input_streams[ist_index];
 +    if (ist->decoding_needed) {
 +        AVCodec *codec = ist->dec;
 +        if (!codec) {
 +            snprintf(error, error_len, "Decoder (codec %s) not found for input stream #%d:%d",
 +                    avcodec_get_name(ist->st->codec->codec_id), ist->file_index, ist->st->index);
 +            return AVERROR(EINVAL);
 +        }
 +        if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) {
 +            snprintf(error, error_len, "Error while opening decoder for input stream #%d:%d",
 +                    ist->file_index, ist->st->index);
 +            return AVERROR(EINVAL);
 +        }
 +        assert_codec_experimental(ist->st->codec, 0);
 +        assert_avoptions(ist->opts);
      }
 -    return 0;
 -}
  
 -static void parse_forced_key_frames(char *kf, OutputStream *ost,
 -                                    AVCodecContext *avctx)
 -{
 -    char *p;
 -    int n = 1, i;
 -    int64_t t;
 +    ist->pts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames*AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
 +    ist->next_pts = AV_NOPTS_VALUE;
 +    ist->is_start = 1;
  
 -    for (p = kf; *p; p++)
 -        if (*p == ',')
 -            n++;
 -    ost->forced_kf_count = n;
 -    ost->forced_kf_pts = av_malloc(sizeof(*ost->forced_kf_pts) * n);
 -    if (!ost->forced_kf_pts) {
 -        av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
 -        exit_program(1);
 -    }
 -    for (i = 0; i < n; i++) {
 -        p = i ? strchr(p, ',') + 1 : kf;
 -        t = parse_time_or_die("force_key_frames", p, 1);
 -        ost->forced_kf_pts[i] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
 -    }
 +    return 0;
  }
  
 -/*
 - * The following code is the main loop of the file converter
 - */
 -static int transcode(AVFormatContext **output_files,
 -                     int nb_output_files,
 -                     InputFile *input_files,
 -                     int nb_input_files,
 -                     StreamMap *stream_maps, int nb_stream_maps)
 +static int transcode_init(OutputFile *output_files, int nb_output_files,
 +                          InputFile  *input_files,  int nb_input_files)
  {
 -    int ret = 0, i, j, k, n, nb_ostreams = 0;
 -    AVFormatContext *is, *os;
 +    int ret = 0, i, j, k;
 +    AVFormatContext *os;
      AVCodecContext *codec, *icodec;
 -    OutputStream *ost, **ost_table = NULL;
 +    OutputStream *ost;
      InputStream *ist;
      char error[1024];
      int want_sdp = 1;
 -    uint8_t no_packet[MAX_FILES]={0};
 -    int no_packet_count=0;
  
 -    if (rate_emu)
 -        for (i = 0; i < nb_input_streams; i++)
 -            input_streams[i].start = av_gettime();
 +    /* init framerate emulation */
 +    for (i = 0; i < nb_input_files; i++) {
 +        InputFile *ifile = &input_files[i];
 +        if (ifile->rate_emu)
 +            for (j = 0; j < ifile->nb_streams; j++)
 +                input_streams[j + ifile->ist_index].start = av_gettime();
 +    }
  
      /* output stream init */
 -    nb_ostreams = 0;
      for(i=0;i<nb_output_files;i++) {
 -        os = output_files[i];
 +        os = output_files[i].ctx;
          if (!os->nb_streams && !(os->oformat->flags & AVFMT_NOSTREAMS)) {
 -            av_dump_format(output_files[i], i, output_files[i]->filename, 1);
 -            fprintf(stderr, "Output file #%d does not contain any stream\n", i);
 -            ret = AVERROR(EINVAL);
 -            goto fail;
 -        }
 -        nb_ostreams += os->nb_streams;
 -    }
 -    if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
 -        fprintf(stderr, "Number of stream maps must match number of output streams\n");
 -        ret = AVERROR(EINVAL);
 -        goto fail;
 -    }
 -
 -    /* Sanity check the mapping args -- do the input files & streams exist? */
 -    for(i=0;i<nb_stream_maps;i++) {
 -        int fi = stream_maps[i].file_index;
 -        int si = stream_maps[i].stream_index;
 -
 -        if (fi < 0 || fi > nb_input_files - 1 ||
 -            si < 0 || si > input_files[fi].nb_streams - 1) {
 -            fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si);
 -            ret = AVERROR(EINVAL);
 -            goto fail;
 -        }
 -        fi = stream_maps[i].sync_file_index;
 -        si = stream_maps[i].sync_stream_index;
 -        if (fi < 0 || fi > nb_input_files - 1 ||
 -            si < 0 || si > input_files[fi].nb_streams - 1) {
 -            fprintf(stderr,"Could not find sync stream #%d.%d\n", fi, si);
 -            ret = AVERROR(EINVAL);
 -            goto fail;
 -        }
 -    }
 -
 -    ost_table = av_mallocz(sizeof(OutputStream *) * nb_ostreams);
 -    if (!ost_table)
 -        goto fail;
 -    n = 0;
 -    for(k=0;k<nb_output_files;k++) {
 -        os = output_files[k];
 -        for(i=0;i<os->nb_streams;i++,n++) {
 -            int found;
 -            ost = ost_table[n] = output_streams_for_file[k][i];
 -            if (nb_stream_maps > 0) {
 -                ost->source_index = input_files[stream_maps[n].file_index].ist_index +
 -                    stream_maps[n].stream_index;
 -
 -                /* Sanity check that the stream types match */
 -                if (input_streams[ost->source_index].st->codec->codec_type != ost->st->codec->codec_type) {
 -                    int i= ost->file_index;
 -                    av_dump_format(output_files[i], i, output_files[i]->filename, 1);
 -                    fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n",
 -                        stream_maps[n].file_index, stream_maps[n].stream_index,
 -                        ost->file_index, ost->index);
 -                    exit_program(1);
 -                }
 -
 -            } else {
 -                int best_nb_frames=-1;
 -                /* get corresponding input stream index : we select the first one with the right type */
 -                found = 0;
 -                for (j = 0; j < nb_input_streams; j++) {
 -                    int skip=0;
 -                    ist = &input_streams[j];
 -                    if(opt_programid){
 -                        int pi,si;
 -                        AVFormatContext *f = input_files[ist->file_index].ctx;
 -                        skip=1;
 -                        for(pi=0; pi<f->nb_programs; pi++){
 -                            AVProgram *p= f->programs[pi];
 -                            if(p->id == opt_programid)
 -                                for(si=0; si<p->nb_stream_indexes; si++){
 -                                    if(f->streams[ p->stream_index[si] ] == ist->st)
 -                                        skip=0;
 -                                }
 -                        }
 -                    }
 -                    if (ist->discard && ist->st->discard != AVDISCARD_ALL && !skip &&
 -                        ist->st->codec->codec_type == ost->st->codec->codec_type) {
 -                        if(best_nb_frames < ist->st->codec_info_nb_frames){
 -                            best_nb_frames= ist->st->codec_info_nb_frames;
 -                            ost->source_index = j;
 -                            found = 1;
 -                        }
 -                    }
 -                }
 -
 -                if (!found) {
 -                    if(! opt_programid) {
 -                        /* try again and reuse existing stream */
 -                        for (j = 0; j < nb_input_streams; j++) {
 -                            ist = &input_streams[j];
 -                            if (   ist->st->codec->codec_type == ost->st->codec->codec_type
 -                                && ist->st->discard != AVDISCARD_ALL) {
 -                                ost->source_index = j;
 -                                found = 1;
 -                            }
 -                        }
 -                    }
 -                    if (!found) {
 -                        int i= ost->file_index;
 -                        av_dump_format(output_files[i], i, output_files[i]->filename, 1);
 -                        fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
 -                                ost->file_index, ost->index);
 -                        exit_program(1);
 -                    }
 -                }
 -            }
 -            ist = &input_streams[ost->source_index];
 -            ist->discard = 0;
 -            ost->sync_ist = (nb_stream_maps > 0) ?
 -                &input_streams[input_files[stream_maps[n].sync_file_index].ist_index +
 -                         stream_maps[n].sync_stream_index] : ist;
 +            av_dump_format(os, i, os->filename, 1);
 +            av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", i);
 +            return AVERROR(EINVAL);
          }
      }
  
      /* for each output stream, we compute the right encoding parameters */
 -    for(i=0;i<nb_ostreams;i++) {
 -        ost = ost_table[i];
 -        os = output_files[ost->file_index];
 +    for (i = 0; i < nb_output_streams; i++) {
 +        ost = &output_streams[i];
 +        os = output_files[ost->file_index].ctx;
          ist = &input_streams[ost->source_index];
  
 +        if (ost->attachment_filename)
 +            continue;
 +
          codec = ost->st->codec;
          icodec = ist->st->codec;
  
 -        if (metadata_streams_autocopy)
 -            av_dict_copy(&ost->st->metadata, ist->st->metadata,
 -                         AV_DICT_DONT_OVERWRITE);
 -
          ost->st->disposition = ist->st->disposition;
          codec->bits_per_raw_sample= icodec->bits_per_raw_sample;
          codec->chroma_sample_location = icodec->chroma_sample_location;
  
 -        if (ost->st->stream_copy) {
 +        if (ost->stream_copy) {
              uint64_t extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
  
 -            if (extra_size > INT_MAX)
 -                goto fail;
 +            if (extra_size > INT_MAX) {
 +                return AVERROR(EINVAL);
 +            }
  
              /* if stream_copy is selected, no need to decode or encode */
              codec->codec_id = icodec->codec_id;
              codec->rc_max_rate    = icodec->rc_max_rate;
              codec->rc_buffer_size = icodec->rc_buffer_size;
              codec->extradata= av_mallocz(extra_size);
 -            if (!codec->extradata)
 -                goto fail;
 +            if (!codec->extradata) {
 +                return AVERROR(ENOMEM);
 +            }
              memcpy(codec->extradata, icodec->extradata, icodec->extradata_size);
              codec->extradata_size= icodec->extradata_size;
 -            if(!copy_tb && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/500){
 -                codec->time_base = icodec->time_base;
 -                codec->time_base.num *= icodec->ticks_per_frame;
 -                av_reduce(&codec->time_base.num, &codec->time_base.den,
 -                          codec->time_base.num, codec->time_base.den, INT_MAX);
 -            }else
 -                codec->time_base = ist->st->time_base;
 +
 +            codec->time_base = ist->st->time_base;
 +            if(!strcmp(os->oformat->name, "avi")) {
 +                if(!copy_tb && av_q2d(icodec->time_base)*icodec->ticks_per_frame > 2*av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/500){
 +                    codec->time_base = icodec->time_base;
 +                    codec->time_base.num *= icodec->ticks_per_frame;
 +                    codec->time_base.den *= 2;
 +                }
 +            } else if(!(os->oformat->flags & AVFMT_VARIABLE_FPS)) {
 +                if(!copy_tb && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/500){
 +                    codec->time_base = icodec->time_base;
 +                    codec->time_base.num *= icodec->ticks_per_frame;
 +                }
 +            }
 +            av_reduce(&codec->time_base.num, &codec->time_base.den,
 +                        codec->time_base.num, codec->time_base.den, INT_MAX);
 +
              switch(codec->codec_type) {
              case AVMEDIA_TYPE_AUDIO:
                  if(audio_volume != 256) {
 -                    fprintf(stderr,"-acodec copy and -vol are incompatible (frames are not decoded)\n");
 +                    av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
                      exit_program(1);
                  }
                  codec->channel_layout = icodec->channel_layout;
                  codec->frame_size = icodec->frame_size;
                  codec->audio_service_type = icodec->audio_service_type;
                  codec->block_align= icodec->block_align;
 -                if(codec->block_align == 1 && codec->codec_id == CODEC_ID_MP3)
 -                    codec->block_align= 0;
 -                if(codec->codec_id == CODEC_ID_AC3)
 -                    codec->block_align= 0;
                  break;
              case AVMEDIA_TYPE_VIDEO:
                  codec->pix_fmt = icodec->pix_fmt;
                          ist->st->codec->sample_aspect_ratio.num ?
                          ist->st->codec->sample_aspect_ratio : (AVRational){0, 1};
                  }
 +                ost->st->avg_frame_rate = ist->st->avg_frame_rate;
                  break;
              case AVMEDIA_TYPE_SUBTITLE:
                  codec->width = icodec->width;
                  codec->height = icodec->height;
                  break;
              case AVMEDIA_TYPE_DATA:
 +            case AVMEDIA_TYPE_ATTACHMENT:
                  break;
              default:
                  abort();
          } else {
              if (!ost->enc)
                  ost->enc = avcodec_find_encoder(ost->st->codec->codec_id);
 +            ist->decoding_needed = 1;
 +            ost->encoding_needed = 1;
              switch(codec->codec_type) {
              case AVMEDIA_TYPE_AUDIO:
                  ost->fifo= av_fifo_alloc(1024);
 -                if(!ost->fifo)
 -                    goto fail;
 -                ost->reformat_pair = MAKE_SFMT_PAIR(AV_SAMPLE_FMT_NONE,AV_SAMPLE_FMT_NONE);
 -                if (!codec->sample_rate) {
 -                    codec->sample_rate = icodec->sample_rate;
 -                    if (icodec->lowres)
 -                        codec->sample_rate >>= icodec->lowres;
 +                if (!ost->fifo) {
 +                    return AVERROR(ENOMEM);
                  }
 +                if (!codec->sample_rate)
 +                    codec->sample_rate = icodec->sample_rate;
                  choose_sample_rate(ost->st, ost->enc);
                  codec->time_base = (AVRational){1, codec->sample_rate};
                  if (codec->sample_fmt == AV_SAMPLE_FMT_NONE)
                      codec->sample_fmt = icodec->sample_fmt;
                  choose_sample_fmt(ost->st, ost->enc);
 -                if (!codec->channels)
 +                if (ost->audio_channels_mapped) {
 +                    /* the requested output channel is set to the number of
 +                     * -map_channel only if no -ac are specified */
 +                    if (!codec->channels) {
 +                        codec->channels       = ost->audio_channels_mapped;
 +                        codec->channel_layout = av_get_default_channel_layout(codec->channels);
 +                        if (!codec->channel_layout) {
 +                            av_log(NULL, AV_LOG_FATAL, "Unable to find an appropriate channel layout for requested number of channel\n");
 +                            exit_program(1);
 +                        }
 +                    }
 +                    /* fill unused channel mapping with -1 (which means a muted
 +                     * channel in case the number of output channels is bigger
 +                     * than the number of mapped channel) */
 +                    for (j = ost->audio_channels_mapped; j < FF_ARRAY_ELEMS(ost->audio_channels_map); j++)
 +                        ost->audio_channels_map[j] = -1;
 +                } else if (!codec->channels) {
                      codec->channels = icodec->channels;
 -                codec->channel_layout = icodec->channel_layout;
 +                    codec->channel_layout = icodec->channel_layout;
 +                }
                  if (av_get_channel_layout_nb_channels(codec->channel_layout) != codec->channels)
                      codec->channel_layout = 0;
                  ost->audio_resample = codec->sample_rate != icodec->sample_rate || audio_sync_method > 1;
 +                ost->audio_resample |=    codec->sample_fmt     != icodec->sample_fmt
 +                                       || codec->channel_layout != icodec->channel_layout;
                  icodec->request_channels = codec->channels;
 -                ist->decoding_needed = 1;
 -                ost->encoding_needed = 1;
                  ost->resample_sample_fmt  = icodec->sample_fmt;
                  ost->resample_sample_rate = icodec->sample_rate;
                  ost->resample_channels    = icodec->channels;
                  choose_pixel_fmt(ost->st, ost->enc);
  
                  if (ost->st->codec->pix_fmt == PIX_FMT_NONE) {
 -                    fprintf(stderr, "Video pixel format is unknown, stream cannot be encoded\n");
 +                    av_log(NULL, AV_LOG_FATAL, "Video pixel format is unknown, stream cannot be encoded\n");
                      exit_program(1);
                  }
  
                                        codec->height  != icodec->height ||
                                        codec->pix_fmt != icodec->pix_fmt;
                  if (ost->video_resample) {
 -#if !CONFIG_AVFILTER
 -                    avcodec_get_frame_defaults(&ost->pict_tmp);
 -                    if(avpicture_alloc((AVPicture*)&ost->pict_tmp, codec->pix_fmt,
 -                                       codec->width, codec->height)) {
 -                        fprintf(stderr, "Cannot allocate temp picture, check pix fmt\n");
 -                        exit_program(1);
 -                    }
 -                    ost->img_resample_ctx = sws_getContext(
 -                        icodec->width,
 -                        icodec->height,
 -                        icodec->pix_fmt,
 -                        codec->width,
 -                        codec->height,
 -                        codec->pix_fmt,
 -                        ost->sws_flags, NULL, NULL, NULL);
 -                    if (ost->img_resample_ctx == NULL) {
 -                        fprintf(stderr, "Cannot get resampling context\n");
 -                        exit_program(1);
 -                    }
 -#endif
 -                    codec->bits_per_raw_sample= 0;
 +                    codec->bits_per_raw_sample= frame_bits_per_raw_sample;
                  }
  
                  ost->resample_height = icodec->height;
                  ost->resample_width  = icodec->width;
                  ost->resample_pix_fmt= icodec->pix_fmt;
 -                ost->encoding_needed = 1;
 -                ist->decoding_needed = 1;
  
                  if (!ost->frame_rate.num)
                      ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25,1};
 -                if (ost->enc && ost->enc->supported_framerates && !force_fps) {
 +                if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) {
                      int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
                      ost->frame_rate = ost->enc->supported_framerates[idx];
                  }
                  codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num};
 +                if(   av_q2d(codec->time_base) < 0.001 && video_sync_method
 +                   && (video_sync_method==1 || (video_sync_method<0 && !(os->oformat->flags & AVFMT_VARIABLE_FPS)))){
 +                    av_log(os, AV_LOG_WARNING, "Frame rate very high for a muxer not effciciently supporting it.\n"
 +                                               "Please consider specifiying a lower framerate, a different muxer or -vsync 2\n");
 +                }
 +                for (j = 0; j < ost->forced_kf_count; j++)
 +                    ost->forced_kf_pts[j] = av_rescale_q(ost->forced_kf_pts[j],
 +                                                         AV_TIME_BASE_Q,
 +                                                         codec->time_base);
  
  #if CONFIG_AVFILTER
                  if (configure_video_filters(ist, ost)) {
 -                    fprintf(stderr, "Error opening filters!\n");
 +                    av_log(NULL, AV_LOG_FATAL, "Error opening filters!\n");
                      exit(1);
                  }
  #endif
                  break;
              case AVMEDIA_TYPE_SUBTITLE:
 -                ost->encoding_needed = 1;
 -                ist->decoding_needed = 1;
                  break;
              default:
                  abort();
                  break;
              }
              /* two pass mode */
 -            if (ost->encoding_needed &&
 +            if (codec->codec_id != CODEC_ID_H264 &&
                  (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
                  char logfilename[1024];
                  FILE *f;
                  snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
                           pass_logfilename_prefix ? pass_logfilename_prefix : DEFAULT_PASS_LOGFILENAME_PREFIX,
                           i);
 -                if (codec->flags & CODEC_FLAG_PASS1) {
 -                    f = fopen(logfilename, "wb");
 -                    if (!f) {
 -                        fprintf(stderr, "Cannot write log file '%s' for pass-1 encoding: %s\n", logfilename, strerror(errno));
 -                        exit_program(1);
 -                    }
 -                    ost->logfile = f;
 -                } else {
 +                if (codec->flags & CODEC_FLAG_PASS2) {
                      char  *logbuffer;
                      size_t logbuffer_size;
                      if (cmdutils_read_file(logfilename, &logbuffer, &logbuffer_size) < 0) {
 -                        fprintf(stderr, "Error reading log file '%s' for pass-2 encoding\n", logfilename);
 +                        av_log(NULL, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
 +                               logfilename);
                          exit_program(1);
                      }
                      codec->stats_in = logbuffer;
                  }
 +                if (codec->flags & CODEC_FLAG_PASS1) {
 +                    f = fopen(logfilename, "wb");
 +                    if (!f) {
 +                        av_log(NULL, AV_LOG_FATAL, "Cannot write log file '%s' for pass-1 encoding: %s\n",
 +                               logfilename, strerror(errno));
 +                        exit_program(1);
 +                    }
 +                    ost->logfile = f;
 +                }
              }
          }
          if(codec->codec_type == AVMEDIA_TYPE_VIDEO){
 +            /* maximum video buffer size is 6-bytes per pixel, plus DPX header size (1664)*/
              int size= codec->width * codec->height;
 -            bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 200);
 +            bit_buffer_size= FFMAX(bit_buffer_size, 7*size + 10000);
          }
      }
  
      if (!bit_buffer)
          bit_buffer = av_malloc(bit_buffer_size);
      if (!bit_buffer) {
 -        fprintf(stderr, "Cannot allocate %d bytes output buffer\n",
 -                bit_buffer_size);
 -        ret = AVERROR(ENOMEM);
 -        goto fail;
 +        av_log(NULL, AV_LOG_ERROR, "Cannot allocate %d bytes output buffer\n",
 +               bit_buffer_size);
 +        return AVERROR(ENOMEM);
      }
  
      /* open each encoder */
 -    for(i=0;i<nb_ostreams;i++) {
 -        ost = ost_table[i];
 +    for (i = 0; i < nb_output_streams; i++) {
 +        ost = &output_streams[i];
          if (ost->encoding_needed) {
              AVCodec *codec = ost->enc;
              AVCodecContext *dec = input_streams[ost->source_index].st->codec;
              if (!codec) {
 -                snprintf(error, sizeof(error), "Encoder (codec id %d) not found for output stream #%d.%d",
 -                         ost->st->codec->codec_id, ost->file_index, ost->index);
 +                snprintf(error, sizeof(error), "Encoder (codec %s) not found for output stream #%d:%d",
 +                         avcodec_get_name(ost->st->codec->codec_id), ost->file_index, ost->index);
                  ret = AVERROR(EINVAL);
                  goto dump_format;
              }
                  ost->st->codec->subtitle_header_size = dec->subtitle_header_size;
              }
              if (avcodec_open2(ost->st->codec, codec, &ost->opts) < 0) {
 -                snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height",
 +                snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
                          ost->file_index, ost->index);
                  ret = AVERROR(EINVAL);
                  goto dump_format;
              assert_avoptions(ost->opts);
              if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
                  av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
 -                                             "It takes bits/s as argument, not kbits/s\n");
 +                                             " It takes bits/s as argument, not kbits/s\n");
              extra_size += ost->st->codec->extradata_size;
 -        }
 -    }
 -
 -    /* open each decoder */
 -    for (i = 0; i < nb_input_streams; i++) {
 -        ist = &input_streams[i];
 -        if (ist->decoding_needed) {
 -            AVCodec *codec = ist->dec;
 -            if (!codec)
 -                codec = avcodec_find_decoder(ist->st->codec->codec_id);
 -            if (!codec) {
 -                snprintf(error, sizeof(error), "Decoder (codec id %d) not found for input stream #%d.%d",
 -                        ist->st->codec->codec_id, ist->file_index, ist->st->index);
 -                ret = AVERROR(EINVAL);
 -                goto dump_format;
 -            }
 -
 -            /* update requested sample format for the decoder based on the
 -               corresponding encoder sample format */
 -            for (j = 0; j < nb_ostreams; j++) {
 -                ost = ost_table[j];
 -                if (ost->source_index == i) {
 -                    update_sample_fmt(ist->st->codec, codec, ost->st->codec);
 -                    break;
 -                }
 -            }
 -
 -            if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) {
 -                snprintf(error, sizeof(error), "Error while opening decoder for input stream #%d.%d",
 -                        ist->file_index, ist->st->index);
 -                ret = AVERROR(EINVAL);
 -                goto dump_format;
 -            }
 -            assert_codec_experimental(ist->st->codec, 0);
 -            assert_avoptions(ost->opts);
 -        }
 -    }
 -
 -    /* init pts */
 -    for (i = 0; i < nb_input_streams; i++) {
 -        AVStream *st;
 -        ist = &input_streams[i];
 -        st= ist->st;
 -        ist->pts = st->avg_frame_rate.num ? - st->codec->has_b_frames*AV_TIME_BASE / av_q2d(st->avg_frame_rate) : 0;
 -        ist->next_pts = AV_NOPTS_VALUE;
 -        init_pts_correction(&ist->pts_ctx);
 -        ist->is_start = 1;
 -    }
 -
 -    /* set meta data information from input file if required */
 -    for (i=0;i<nb_meta_data_maps;i++) {
 -        AVFormatContext *files[2];
 -        AVDictionary    **meta[2];
 -        int j;
 -
 -#define METADATA_CHECK_INDEX(index, nb_elems, desc)\
 -        if ((index) < 0 || (index) >= (nb_elems)) {\
 -            snprintf(error, sizeof(error), "Invalid %s index %d while processing metadata maps\n",\
 -                     (desc), (index));\
 -            ret = AVERROR(EINVAL);\
 -            goto dump_format;\
 -        }
 -
 -        int out_file_index = meta_data_maps[i][0].file;
 -        int in_file_index = meta_data_maps[i][1].file;
 -        if (in_file_index < 0 || out_file_index < 0)
 -            continue;
 -        METADATA_CHECK_INDEX(out_file_index, nb_output_files, "output file")
 -        METADATA_CHECK_INDEX(in_file_index, nb_input_files, "input file")
 -
 -        files[0] = output_files[out_file_index];
 -        files[1] = input_files[in_file_index].ctx;
 -
 -        for (j = 0; j < 2; j++) {
 -            MetadataMap *map = &meta_data_maps[i][j];
  
 -            switch (map->type) {
 -            case 'g':
 -                meta[j] = &files[j]->metadata;
 -                break;
 -            case 's':
 -                METADATA_CHECK_INDEX(map->index, files[j]->nb_streams, "stream")
 -                meta[j] = &files[j]->streams[map->index]->metadata;
 -                break;
 -            case 'c':
 -                METADATA_CHECK_INDEX(map->index, files[j]->nb_chapters, "chapter")
 -                meta[j] = &files[j]->chapters[map->index]->metadata;
 -                break;
 -            case 'p':
 -                METADATA_CHECK_INDEX(map->index, files[j]->nb_programs, "program")
 -                meta[j] = &files[j]->programs[map->index]->metadata;
 -                break;
 -            }
 +            if (ost->st->codec->me_threshold)
 +                input_streams[ost->source_index].st->codec->debug |= FF_DEBUG_MV;
          }
 -
 -        av_dict_copy(meta[0], *meta[1], AV_DICT_DONT_OVERWRITE);
      }
  
 -    /* copy global metadata by default */
 -    if (metadata_global_autocopy) {
 -
 -        for (i = 0; i < nb_output_files; i++)
 -            av_dict_copy(&output_files[i]->metadata, input_files[0].ctx->metadata,
 -                         AV_DICT_DONT_OVERWRITE);
 -    }
 -
 -    /* copy chapters according to chapter maps */
 -    for (i = 0; i < nb_chapter_maps; i++) {
 -        int infile  = chapter_maps[i].in_file;
 -        int outfile = chapter_maps[i].out_file;
 -
 -        if (infile < 0 || outfile < 0)
 -            continue;
 -        if (infile >= nb_input_files) {
 -            snprintf(error, sizeof(error), "Invalid input file index %d in chapter mapping.\n", infile);
 -            ret = AVERROR(EINVAL);
 -            goto dump_format;
 -        }
 -        if (outfile >= nb_output_files) {
 -            snprintf(error, sizeof(error), "Invalid output file index %d in chapter mapping.\n",outfile);
 -            ret = AVERROR(EINVAL);
 +    /* init input streams */
 +    for (i = 0; i < nb_input_streams; i++)
 +        if ((ret = init_input_stream(i, output_streams, nb_output_streams, error, sizeof(error))) < 0)
              goto dump_format;
 -        }
 -        copy_chapters(infile, outfile);
 -    }
  
 -    /* copy chapters from the first input file that has them*/
 -    if (!nb_chapter_maps)
 -        for (i = 0; i < nb_input_files; i++) {
 -            if (!input_files[i].ctx->nb_chapters)
 -                continue;
 +    /* discard unused programs */
 +    for (i = 0; i < nb_input_files; i++) {
 +        InputFile *ifile = &input_files[i];
 +        for (j = 0; j < ifile->ctx->nb_programs; j++) {
 +            AVProgram *p = ifile->ctx->programs[j];
 +            int discard  = AVDISCARD_ALL;
  
 -            for (j = 0; j < nb_output_files; j++)
 -                if ((ret = copy_chapters(i, j)) < 0)
 -                    goto dump_format;
 -            break;
 +            for (k = 0; k < p->nb_stream_indexes; k++)
 +                if (!input_streams[ifile->ist_index + p->stream_index[k]].discard) {
 +                    discard = AVDISCARD_DEFAULT;
 +                    break;
 +                }
 +            p->discard = discard;
          }
 +    }
  
      /* open files and write file headers */
 -    for(i=0;i<nb_output_files;i++) {
 -        os = output_files[i];
 -        if (avformat_write_header(os, &output_opts[i]) < 0) {
 +    for (i = 0; i < nb_output_files; i++) {
 +        os = output_files[i].ctx;
++        os->interrupt_callback = int_cb;
 +        if (avformat_write_header(os, &output_files[i].opts) < 0) {
              snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?)", i);
              ret = AVERROR(EINVAL);
              goto dump_format;
          }
 -        assert_avoptions(output_opts[i]);
 -        if (strcmp(output_files[i]->oformat->name, "rtp")) {
 +//        assert_avoptions(output_files[i].opts);
 +        if (strcmp(os->oformat->name, "rtp")) {
              want_sdp = 0;
          }
      }
      /* dump the file output parameters - cannot be done before in case
         of stream copy */
      for(i=0;i<nb_output_files;i++) {
 -        av_dump_format(output_files[i], i, output_files[i]->filename, 1);
 +        av_dump_format(output_files[i].ctx, i, output_files[i].ctx->filename, 1);
      }
  
      /* dump the stream mapping */
 -    if (verbose >= 0) {
 -        fprintf(stderr, "Stream mapping:\n");
 -        for(i=0;i<nb_ostreams;i++) {
 -            ost = ost_table[i];
 -            fprintf(stderr, "  Stream #%d.%d -> #%d.%d",
 -                    input_streams[ost->source_index].file_index,
 -                    input_streams[ost->source_index].st->index,
 -                    ost->file_index,
 -                    ost->index);
 -            if (ost->sync_ist != &input_streams[ost->source_index])
 -                fprintf(stderr, " [sync #%d.%d]",
 -                        ost->sync_ist->file_index,
 -                        ost->sync_ist->st->index);
 -            fprintf(stderr, "\n");
 +    av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
 +    for (i = 0; i < nb_output_streams; i++) {
 +        ost = &output_streams[i];
 +
 +        if (ost->attachment_filename) {
 +            /* an attached file */
 +            av_log(NULL, AV_LOG_INFO, "  File %s -> Stream #%d:%d\n",
 +                   ost->attachment_filename, ost->file_index, ost->index);
 +            continue;
 +        }
 +        av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d -> #%d:%d",
 +               input_streams[ost->source_index].file_index,
 +               input_streams[ost->source_index].st->index,
 +               ost->file_index,
 +               ost->index);
 +        if (ost->audio_channels_mapped) {
 +            av_log(NULL, AV_LOG_INFO, " [ch:");
 +            for (j = 0; j < ost->audio_channels_mapped; j++)
 +                if (ost->audio_channels_map[j] == -1)
 +                    av_log(NULL, AV_LOG_INFO, " M");
 +                else
 +                    av_log(NULL, AV_LOG_INFO, " %d", ost->audio_channels_map[j]);
 +            av_log(NULL, AV_LOG_INFO, "]");
          }
 +        if (ost->sync_ist != &input_streams[ost->source_index])
 +            av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]",
 +                   ost->sync_ist->file_index,
 +                   ost->sync_ist->st->index);
 +        if (ost->stream_copy)
 +            av_log(NULL, AV_LOG_INFO, " (copy)");
 +        else
 +            av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index].dec ?
 +                   input_streams[ost->source_index].dec->name : "?",
 +                   ost->enc ? ost->enc->name : "?");
 +        av_log(NULL, AV_LOG_INFO, "\n");
      }
  
      if (ret) {
 -        fprintf(stderr, "%s\n", error);
 -        goto fail;
 +        av_log(NULL, AV_LOG_ERROR, "%s\n", error);
 +        return ret;
      }
  
      if (want_sdp) {
          print_sdp(output_files, nb_output_files);
      }
  
 -    if (verbose >= 0)
 -        fprintf(stderr, "Press ctrl-c to stop encoding\n");
 -    term_init();
 +    return 0;
 +}
 +
 +/*
 + * The following code is the main loop of the file converter
 + */
 +static int transcode(OutputFile *output_files, int nb_output_files,
 +                     InputFile  *input_files,  int nb_input_files)
 +{
 +    int ret, i;
 +    AVFormatContext *is, *os;
 +    OutputStream *ost;
 +    InputStream *ist;
 +    uint8_t *no_packet;
 +    int no_packet_count=0;
 +    int64_t timer_start;
 +    int key;
 +
 +    if (!(no_packet = av_mallocz(nb_input_files)))
 +        exit_program(1);
 +
 +    ret = transcode_init(output_files, nb_output_files, input_files, nb_input_files);
 +    if (ret < 0)
 +        goto fail;
 +
 +    if (!using_stdin) {
 +        av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");
-         avio_set_interrupt_cb(decode_interrupt_cb);
 +    }
  
      timer_start = av_gettime();
  
      for(; received_sigterm == 0;) {
          int file_index, ist_index;
          AVPacket pkt;
 -        double ipts_min;
 +        int64_t ipts_min;
          double opts_min;
 +        int64_t cur_time= av_gettime();
  
 -    redo:
 -        ipts_min= 1e100;
 +        ipts_min= INT64_MAX;
          opts_min= 1e100;
 +        /* if 'q' pressed, exits */
 +        if (!using_stdin) {
 +            static int64_t last_time;
 +            if (received_nb_signals)
 +                break;
 +            /* read_key() returns 0 on EOF */
 +            if(cur_time - last_time >= 100000 && !run_as_daemon){
 +                key =  read_key();
 +                last_time = cur_time;
 +            }else
 +                key = -1;
 +            if (key == 'q')
 +                break;
 +            if (key == '+') av_log_set_level(av_log_get_level()+10);
 +            if (key == '-') av_log_set_level(av_log_get_level()-10);
 +            if (key == 's') qp_hist     ^= 1;
 +            if (key == 'h'){
 +                if (do_hex_dump){
 +                    do_hex_dump = do_pkt_dump = 0;
 +                } else if(do_pkt_dump){
 +                    do_hex_dump = 1;
 +                } else
 +                    do_pkt_dump = 1;
 +                av_log_set_level(AV_LOG_DEBUG);
 +            }
 +#if CONFIG_AVFILTER
 +            if (key == 'c' || key == 'C'){
 +                char buf[4096], target[64], command[256], arg[256] = {0};
 +                double time;
 +                int k, n = 0;
 +                fprintf(stderr, "\nEnter command: <target> <time> <command>[ <argument>]\n");
 +                i = 0;
 +                while ((k = read_key()) != '\n' && k != '\r' && i < sizeof(buf)-1)
 +                    if (k > 0)
 +                        buf[i++] = k;
 +                buf[i] = 0;
 +                if (k > 0 &&
 +                    (n = sscanf(buf, "%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
 +                    av_log(NULL, AV_LOG_DEBUG, "Processing command target:%s time:%f command:%s arg:%s",
 +                           target, time, command, arg);
 +                    for (i = 0; i < nb_output_streams; i++) {
 +                        ost = &output_streams[i];
 +                        if (ost->graph) {
 +                            if (time < 0) {
 +                                ret = avfilter_graph_send_command(ost->graph, target, command, arg, buf, sizeof(buf),
 +                                                                  key == 'c' ? AVFILTER_CMD_FLAG_ONE : 0);
 +                                fprintf(stderr, "Command reply for stream %d: ret:%d res:%s\n", i, ret, buf);
 +                            } else {
 +                                ret = avfilter_graph_queue_command(ost->graph, target, command, arg, 0, time);
 +                            }
 +                        }
 +                    }
 +                } else {
 +                    av_log(NULL, AV_LOG_ERROR,
 +                           "Parse error, at least 3 arguments were expected, "
 +                           "only %d given in string '%s'\n", n, buf);
 +                }
 +            }
 +#endif
 +            if (key == 'd' || key == 'D'){
 +                int debug=0;
 +                if(key == 'D') {
 +                    debug = input_streams[0].st->codec->debug<<1;
 +                    if(!debug) debug = 1;
 +                    while(debug & (FF_DEBUG_DCT_COEFF|FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) //unsupported, would just crash
 +                        debug += debug;
 +                }else
 +                    if(scanf("%d", &debug)!=1)
 +                        fprintf(stderr,"error parsing debug value\n");
 +                for(i=0;i<nb_input_streams;i++) {
 +                    input_streams[i].st->codec->debug = debug;
 +                }
 +                for(i=0;i<nb_output_streams;i++) {
 +                    ost = &output_streams[i];
 +                    ost->st->codec->debug = debug;
 +                }
 +                if(debug) av_log_set_level(AV_LOG_DEBUG);
 +                fprintf(stderr,"debug=%d\n", debug);
 +            }
 +            if (key == '?'){
 +                fprintf(stderr, "key    function\n"
 +                                "?      show this help\n"
 +                                "+      increase verbosity\n"
 +                                "-      decrease verbosity\n"
 +                                "c      Send command to filtergraph\n"
 +                                "D      cycle through available debug modes\n"
 +                                "h      dump packets/hex press to cycle through the 3 states\n"
 +                                "q      quit\n"
 +                                "s      Show QP histogram\n"
 +                );
 +            }
 +        }
  
          /* select the stream that we must read now by looking at the
             smallest output pts */
          file_index = -1;
 -        for(i=0;i<nb_ostreams;i++) {
 -            double ipts, opts;
 -            ost = ost_table[i];
 -            os = output_files[ost->file_index];
 +        for (i = 0; i < nb_output_streams; i++) {
 +            OutputFile *of;
 +            int64_t ipts;
 +            double  opts;
 +            ost = &output_streams[i];
 +            of = &output_files[ost->file_index];
 +            os = output_files[ost->file_index].ctx;
              ist = &input_streams[ost->source_index];
 -            if(ist->is_past_recording_time || no_packet[ist->file_index])
 +            if (ost->is_past_recording_time || no_packet[ist->file_index] ||
 +                (os->pb && avio_tell(os->pb) >= of->limit_filesize))
                  continue;
 -                opts = ost->st->pts.val * av_q2d(ost->st->time_base);
 -            ipts = (double)ist->pts;
 +            opts = ost->st->pts.val * av_q2d(ost->st->time_base);
 +            ipts = ist->pts;
              if (!input_files[ist->file_index].eof_reached){
                  if(ipts < ipts_min) {
                      ipts_min = ipts;
                      if(!input_sync) file_index = ist->file_index;
                  }
              }
 -            if(ost->frame_number >= max_frames[ost->st->codec->codec_type]){
 -                file_index= -1;
 -                break;
 +            if (ost->frame_number >= ost->max_frames) {
 +                int j;
 +                for (j = 0; j < of->ctx->nb_streams; j++)
 +                    output_streams[of->ost_index + j].is_past_recording_time = 1;
 +                continue;
              }
          }
          /* if none, if is finished */
          if (file_index < 0) {
              if(no_packet_count){
                  no_packet_count=0;
 -                memset(no_packet, 0, sizeof(no_packet));
 +                memset(no_packet, 0, nb_input_files);
                  usleep(10000);
                  continue;
              }
              break;
          }
  
 -        /* finish if limit size exhausted */
 -        if (limit_filesize != 0 && limit_filesize <= avio_tell(output_files[0]->pb))
 -            break;
 -
          /* read a frame from it and output it in the fifo */
          is = input_files[file_index].ctx;
          ret= av_read_frame(is, &pkt);
          }
  
          no_packet_count=0;
 -        memset(no_packet, 0, sizeof(no_packet));
 +        memset(no_packet, 0, nb_input_files);
  
          if (do_pkt_dump) {
              av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump,
          if (pkt.pts != AV_NOPTS_VALUE)
              pkt.pts += av_rescale_q(input_files[ist->file_index].ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
  
 -        if (ist->ts_scale) {
 -            if(pkt.pts != AV_NOPTS_VALUE)
 -                pkt.pts *= ist->ts_scale;
 -            if(pkt.dts != AV_NOPTS_VALUE)
 -                pkt.dts *= ist->ts_scale;
 -        }
 +        if(pkt.pts != AV_NOPTS_VALUE)
 +            pkt.pts *= ist->ts_scale;
 +        if(pkt.dts != AV_NOPTS_VALUE)
 +            pkt.dts *= ist->ts_scale;
  
  //        fprintf(stderr, "next:%"PRId64" dts:%"PRId64" off:%"PRId64" %d\n", ist->next_pts, pkt.dts, input_files[ist->file_index].ts_offset, ist->st->codec->codec_type);
          if (pkt.dts != AV_NOPTS_VALUE && ist->next_pts != AV_NOPTS_VALUE
              && (is->iformat->flags & AVFMT_TS_DISCONT)) {
              int64_t pkt_dts= av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
              int64_t delta= pkt_dts - ist->next_pts;
 -            if((FFABS(delta) > 1LL*dts_delta_threshold*AV_TIME_BASE || pkt_dts+1<ist->pts)&& !copy_ts){
 +            if((delta < -1LL*dts_delta_threshold*AV_TIME_BASE ||
 +                (delta > 1LL*dts_delta_threshold*AV_TIME_BASE &&
 +                 ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) ||
 +                pkt_dts+1<ist->pts)&& !copy_ts){
                  input_files[ist->file_index].ts_offset -= delta;
 -                if (verbose > 2)
 -                    fprintf(stderr, "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
 -                            delta, input_files[ist->file_index].ts_offset);
 +                av_log(NULL, AV_LOG_DEBUG, "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
 +                       delta, input_files[ist->file_index].ts_offset);
                  pkt.dts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
                  if(pkt.pts != AV_NOPTS_VALUE)
                      pkt.pts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
              }
          }
  
 -        /* finish if recording time exhausted */
 -        if (recording_time != INT64_MAX &&
 -            av_compare_ts(pkt.pts, ist->st->time_base, recording_time + start_time, (AVRational){1, 1000000}) >= 0) {
 -            ist->is_past_recording_time = 1;
 -            goto discard_packet;
 -        }
 -
          //fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->st->index, pkt.size);
 -        if (output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt) < 0) {
 +        if (output_packet(ist, ist_index, output_streams, nb_output_streams, &pkt) < 0) {
  
 -            if (verbose >= 0)
 -                fprintf(stderr, "Error while decoding stream #%d.%d\n",
 -                        ist->file_index, ist->st->index);
 +            av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d\n",
 +                   ist->file_index, ist->st->index);
              if (exit_on_error)
                  exit_program(1);
              av_free_packet(&pkt);
 -            goto redo;
 +            continue;
          }
  
      discard_packet:
          av_free_packet(&pkt);
  
          /* dump report by using the output first video and audio streams */
 -        print_report(output_files, ost_table, nb_ostreams, 0);
 +        print_report(output_files, output_streams, nb_output_streams, 0, timer_start, cur_time);
      }
  
      /* at the end of stream, we must flush the decoder buffers */
      for (i = 0; i < nb_input_streams; i++) {
          ist = &input_streams[i];
          if (ist->decoding_needed) {
 -            output_packet(ist, i, ost_table, nb_ostreams, NULL);
 +            output_packet(ist, i, output_streams, nb_output_streams, NULL);
          }
      }
 +    flush_encoders(output_streams, nb_output_streams);
  
      term_exit();
  
      /* write the trailer if needed and close file */
      for(i=0;i<nb_output_files;i++) {
 -        os = output_files[i];
 +        os = output_files[i].ctx;
          av_write_trailer(os);
      }
  
      /* dump report by using the first video and audio streams */
 -    print_report(output_files, ost_table, nb_ostreams, 1);
 +    print_report(output_files, output_streams, nb_output_streams, 1, timer_start, av_gettime());
  
      /* close each encoder */
 -    for(i=0;i<nb_ostreams;i++) {
 -        ost = ost_table[i];
 +    for (i = 0; i < nb_output_streams; i++) {
 +        ost = &output_streams[i];
          if (ost->encoding_needed) {
              av_freep(&ost->st->codec->stats_in);
              avcodec_close(ost->st->codec);
  
   fail:
      av_freep(&bit_buffer);
 +    av_freep(&no_packet);
  
 -    if (ost_table) {
 -        for(i=0;i<nb_ostreams;i++) {
 -            ost = ost_table[i];
 +    if (output_streams) {
 +        for (i = 0; i < nb_output_streams; i++) {
 +            ost = &output_streams[i];
              if (ost) {
 -                if (ost->st->stream_copy)
 +                if (ost->stream_copy)
                      av_freep(&ost->st->codec->extradata);
                  if (ost->logfile) {
                      fclose(ost->logfile);
                  av_fifo_free(ost->fifo); /* works even if fifo is not
                                               initialized but set to zero */
                  av_freep(&ost->st->codec->subtitle_header);
 -                av_free(ost->pict_tmp.data[0]);
 +                av_free(ost->resample_frame.data[0]);
                  av_free(ost->forced_kf_pts);
                  if (ost->video_resample)
                      sws_freeContext(ost->img_resample_ctx);
 -                if (ost->resample)
 -                    audio_resample_close(ost->resample);
 -                if (ost->reformat_ctx)
 -                    av_audio_convert_free(ost->reformat_ctx);
 +                swr_free(&ost->swr);
                  av_dict_free(&ost->opts);
 -                av_free(ost);
              }
          }
 -        av_free(ost_table);
      }
      return ret;
  }
  
 -static int opt_format(const char *opt, const char *arg)
 -{
 -    last_asked_format = arg;
 -    return 0;
 -}
 -
 -static int opt_video_rc_override_string(const char *opt, const char *arg)
 -{
 -    video_rc_override_string = arg;
 -    return 0;
 -}
 -
 -static int opt_me_threshold(const char *opt, const char *arg)
 -{
 -    me_threshold = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
 -    return 0;
 -}
 -
 -static int opt_verbose(const char *opt, const char *arg)
 -{
 -    verbose = parse_number_or_die(opt, arg, OPT_INT64, -10, 10);
 -    return 0;
 -}
 -
 -static int opt_frame_rate(const char *opt, const char *arg)
 -{
 -    if (av_parse_video_rate(&frame_rate, arg) < 0) {
 -        fprintf(stderr, "Incorrect value for %s: %s\n", opt, arg);
 -        exit_program(1);
 -    }
 -    return 0;
 -}
 -
  static int opt_frame_crop(const char *opt, const char *arg)
  {
 -    fprintf(stderr, "Option '%s' has been removed, use the crop filter instead\n", opt);
 +    av_log(NULL, AV_LOG_FATAL, "Option '%s' has been removed, use the crop filter instead\n", opt);
      return AVERROR(EINVAL);
  }
  
 -static int opt_frame_size(const char *opt, const char *arg)
 +static int opt_pad(const char *opt, const char *arg)
  {
 -    if (av_parse_video_size(&frame_width, &frame_height, arg) < 0) {
 -        fprintf(stderr, "Incorrect frame size\n");
 -        return AVERROR(EINVAL);
 -    }
 -    return 0;
 -}
 -
 -static int opt_pad(const char *opt, const char *arg) {
 -    fprintf(stderr, "Option '%s' has been removed, use the pad filter instead\n", opt);
 +    av_log(NULL, AV_LOG_FATAL, "Option '%s' has been removed, use the pad filter instead\n", opt);
      return -1;
  }
  
 -static int opt_frame_pix_fmt(const char *opt, const char *arg)
 -{
 -    if (strcmp(arg, "list")) {
 -        frame_pix_fmt = av_get_pix_fmt(arg);
 -        if (frame_pix_fmt == PIX_FMT_NONE) {
 -            fprintf(stderr, "Unknown pixel format requested: %s\n", arg);
 -            return AVERROR(EINVAL);
 -        }
 -    } else {
 -        show_pix_fmts();
 -        exit_program(0);
 -    }
 -    return 0;
 -}
 -
 -static int opt_frame_aspect_ratio(const char *opt, const char *arg)
 +static double parse_frame_aspect_ratio(const char *arg)
  {
      int x = 0, y = 0;
      double ar = 0;
          ar = strtod(arg, NULL);
  
      if (!ar) {
 -        fprintf(stderr, "Incorrect aspect ratio specification.\n");
 -        return AVERROR(EINVAL);
 -    }
 -    frame_aspect_ratio = ar;
 -    return 0;
 -}
 -
 -static int opt_metadata(const char *opt, const char *arg)
 -{
 -    char *mid= strchr(arg, '=');
 -
 -    if(!mid){
 -        fprintf(stderr, "Missing =\n");
 +        av_log(NULL, AV_LOG_FATAL, "Incorrect aspect ratio specification.\n");
          exit_program(1);
      }
 -    *mid++= 0;
 -
 -    av_dict_set(&metadata, arg, mid, 0);
 -
 -    return 0;
 +    return ar;
  }
  
 -static int opt_qscale(const char *opt, const char *arg)
 +static int opt_video_channel(const char *opt, const char *arg)
  {
 -    video_qscale = parse_number_or_die(opt, arg, OPT_FLOAT, 0, 255);
 -    if (video_qscale == 0) {
 -        fprintf(stderr, "qscale must be > 0.0 and <= 255\n");
 -        return AVERROR(EINVAL);
 -    }
 -    return 0;
 +    av_log(NULL, AV_LOG_WARNING, "This option is deprecated, use -channel.\n");
 +    return opt_default("channel", arg);
  }
  
 -static int opt_top_field_first(const char *opt, const char *arg)
 +static int opt_video_standard(const char *opt, const char *arg)
  {
 -    top_field_first = parse_number_or_die(opt, arg, OPT_INT, 0, 1);
 -    return 0;
 +    av_log(NULL, AV_LOG_WARNING, "This option is deprecated, use -standard.\n");
 +    return opt_default("standard", arg);
  }
  
 -static int opt_thread_count(const char *opt, const char *arg)
 +static int opt_audio_codec(OptionsContext *o, const char *opt, const char *arg)
  {
 -    thread_count= parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
 -#if !HAVE_THREADS
 -    if (verbose >= 0)
 -        fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
 -#endif
 -    return 0;
 +    audio_codec_name = arg;
 +    return parse_option(o, "codec:a", arg, options);
  }
  
 -static int opt_audio_sample_fmt(const char *opt, const char *arg)
 +static int opt_video_codec(OptionsContext *o, const char *opt, const char *arg)
  {
 -    if (strcmp(arg, "list")) {
 -        audio_sample_fmt = av_get_sample_fmt(arg);
 -        if (audio_sample_fmt == AV_SAMPLE_FMT_NONE) {
 -            av_log(NULL, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
 -            return AVERROR(EINVAL);
 -        }
 -    } else {
 -        int i;
 -        char fmt_str[128];
 -        for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
 -            printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
 -        exit_program(0);
 -    }
 -    return 0;
 +    video_codec_name = arg;
 +    return parse_option(o, "codec:v", arg, options);
  }
  
 -static int opt_audio_rate(const char *opt, const char *arg)
 +static int opt_subtitle_codec(OptionsContext *o, const char *opt, const char *arg)
  {
 -    audio_sample_rate = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
 -    return 0;
 +    subtitle_codec_name = arg;
 +    return parse_option(o, "codec:s", arg, options);
  }
  
 -static int opt_audio_channels(const char *opt, const char *arg)
 +static int opt_data_codec(OptionsContext *o, const char *opt, const char *arg)
  {
 -    audio_channels = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
 -    return 0;
 +    return parse_option(o, "codec:d", arg, options);
  }
  
 -static int opt_video_channel(const char *opt, const char *arg)
 +static int opt_map(OptionsContext *o, const char *opt, const char *arg)
  {
 -    av_log(NULL, AV_LOG_WARNING, "This option is deprecated, use -channel.\n");
 -    opt_default("channel", arg);
 +    StreamMap *m = NULL;
 +    int i, negative = 0, file_idx;
 +    int sync_file_idx = -1, sync_stream_idx;
 +    char *p, *sync;
 +    char *map;
 +
 +    if (*arg == '-') {
 +        negative = 1;
 +        arg++;
 +    }
 +    map = av_strdup(arg);
 +
 +    /* parse sync stream first, just pick first matching stream */
 +    if (sync = strchr(map, ',')) {
 +        *sync = 0;
 +        sync_file_idx = strtol(sync + 1, &sync, 0);
 +        if (sync_file_idx >= nb_input_files || sync_file_idx < 0) {
 +            av_log(NULL, AV_LOG_FATAL, "Invalid sync file index: %d.\n", sync_file_idx);
 +            exit_program(1);
 +        }
 +        if (*sync)
 +            sync++;
 +        for (i = 0; i < input_files[sync_file_idx].nb_streams; i++)
 +            if (check_stream_specifier(input_files[sync_file_idx].ctx,
 +                                       input_files[sync_file_idx].ctx->streams[i], sync) == 1) {
 +                sync_stream_idx = i;
 +                break;
 +            }
 +        if (i == input_files[sync_file_idx].nb_streams) {
 +            av_log(NULL, AV_LOG_FATAL, "Sync stream specification in map %s does not "
 +                                       "match any streams.\n", arg);
 +            exit_program(1);
 +        }
 +    }
 +
 +
 +    file_idx = strtol(map, &p, 0);
 +    if (file_idx >= nb_input_files || file_idx < 0) {
 +        av_log(NULL, AV_LOG_FATAL, "Invalid input file index: %d.\n", file_idx);
 +        exit_program(1);
 +    }
 +    if (negative)
 +        /* disable some already defined maps */
 +        for (i = 0; i < o->nb_stream_maps; i++) {
 +            m = &o->stream_maps[i];
 +            if (file_idx == m->file_index &&
 +                check_stream_specifier(input_files[m->file_index].ctx,
 +                                       input_files[m->file_index].ctx->streams[m->stream_index],
 +                                       *p == ':' ? p + 1 : p) > 0)
 +                m->disabled = 1;
 +        }
 +    else
 +        for (i = 0; i < input_files[file_idx].nb_streams; i++) {
 +            if (check_stream_specifier(input_files[file_idx].ctx, input_files[file_idx].ctx->streams[i],
 +                        *p == ':' ? p + 1 : p) <= 0)
 +                continue;
 +            o->stream_maps = grow_array(o->stream_maps, sizeof(*o->stream_maps),
 +                                        &o->nb_stream_maps, o->nb_stream_maps + 1);
 +            m = &o->stream_maps[o->nb_stream_maps - 1];
 +
 +            m->file_index   = file_idx;
 +            m->stream_index = i;
 +
 +            if (sync_file_idx >= 0) {
 +                m->sync_file_index   = sync_file_idx;
 +                m->sync_stream_index = sync_stream_idx;
 +            } else {
 +                m->sync_file_index   = file_idx;
 +                m->sync_stream_index = i;
 +            }
 +        }
 +
 +    if (!m) {
 +        av_log(NULL, AV_LOG_FATAL, "Stream map '%s' matches no streams.\n", arg);
 +        exit_program(1);
 +    }
 +
 +    av_freep(&map);
      return 0;
  }
  
 -static int opt_video_standard(const char *opt, const char *arg)
 +static int opt_attach(OptionsContext *o, const char *opt, const char *arg)
  {
 -    av_log(NULL, AV_LOG_WARNING, "This option is deprecated, use -standard.\n");
 -    opt_default("standard", arg);
 +    o->attachments = grow_array(o->attachments, sizeof(*o->attachments),
 +                                &o->nb_attachments, o->nb_attachments + 1);
 +    o->attachments[o->nb_attachments - 1] = arg;
      return 0;
  }
  
 -static int opt_codec(int *pstream_copy, char **pcodec_name,
 -                      int codec_type, const char *arg)
 +static int opt_map_channel(OptionsContext *o, const char *opt, const char *arg)
  {
 -    av_freep(pcodec_name);
 -    if (!strcmp(arg, "copy")) {
 -        *pstream_copy = 1;
 -    } else {
 -        *pcodec_name = av_strdup(arg);
 +    int n;
 +    AVStream *st;
 +    AudioChannelMap *m;
 +
 +    o->audio_channel_maps =
 +        grow_array(o->audio_channel_maps, sizeof(*o->audio_channel_maps),
 +                   &o->nb_audio_channel_maps, o->nb_audio_channel_maps + 1);
 +    m = &o->audio_channel_maps[o->nb_audio_channel_maps - 1];
 +
 +    /* muted channel syntax */
 +    n = sscanf(arg, "%d:%d.%d", &m->channel_idx, &m->ofile_idx, &m->ostream_idx);
 +    if ((n == 1 || n == 3) && m->channel_idx == -1) {
 +        m->file_idx = m->stream_idx = -1;
 +        if (n == 1)
 +            m->ofile_idx = m->ostream_idx = -1;
 +        return 0;
      }
 -    return 0;
 -}
 -
 -static int opt_audio_codec(const char *opt, const char *arg)
 -{
 -    return opt_codec(&audio_stream_copy, &audio_codec_name, AVMEDIA_TYPE_AUDIO, arg);
 -}
 -
 -static int opt_video_codec(const char *opt, const char *arg)
 -{
 -    return opt_codec(&video_stream_copy, &video_codec_name, AVMEDIA_TYPE_VIDEO, arg);
 -}
 -
 -static int opt_subtitle_codec(const char *opt, const char *arg)
 -{
 -    return opt_codec(&subtitle_stream_copy, &subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, arg);
 -}
 -
 -static int opt_data_codec(const char *opt, const char *arg)
 -{
 -    return opt_codec(&data_stream_copy, &data_codec_name, AVMEDIA_TYPE_DATA, arg);
 -}
 -
 -static int opt_codec_tag(const char *opt, const char *arg)
 -{
 -    char *tail;
 -    uint32_t *codec_tag;
  
 -    codec_tag = !strcmp(opt, "atag") ? &audio_codec_tag :
 -                !strcmp(opt, "vtag") ? &video_codec_tag :
 -                !strcmp(opt, "stag") ? &subtitle_codec_tag : NULL;
 -    if (!codec_tag)
 -        return -1;
 +    /* normal syntax */
 +    n = sscanf(arg, "%d.%d.%d:%d.%d",
 +               &m->file_idx,  &m->stream_idx, &m->channel_idx,
 +               &m->ofile_idx, &m->ostream_idx);
  
 -    *codec_tag = strtol(arg, &tail, 0);
 -    if (!tail || *tail)
 -        *codec_tag = AV_RL32(arg);
 -
 -    return 0;
 -}
 -
 -static int opt_map(const char *opt, const char *arg)
 -{
 -    StreamMap *m;
 -    char *p;
 +    if (n != 3 && n != 5) {
 +        av_log(NULL, AV_LOG_FATAL, "Syntax error, mapchan usage: "
 +               "[file.stream.channel|-1][:syncfile:syncstream]\n");
 +        exit_program(1);
 +    }
  
 -    stream_maps = grow_array(stream_maps, sizeof(*stream_maps), &nb_stream_maps, nb_stream_maps + 1);
 -    m = &stream_maps[nb_stream_maps-1];
 +    if (n != 5) // only file.stream.channel specified
 +        m->ofile_idx = m->ostream_idx = -1;
  
 -    m->file_index = strtol(arg, &p, 0);
 -    if (*p)
 -        p++;
 -
 -    m->stream_index = strtol(p, &p, 0);
 -    if (*p) {
 -        p++;
 -        m->sync_file_index = strtol(p, &p, 0);
 -        if (*p)
 -            p++;
 -        m->sync_stream_index = strtol(p, &p, 0);
 -    } else {
 -        m->sync_file_index = m->file_index;
 -        m->sync_stream_index = m->stream_index;
 +    /* check input */
 +    if (m->file_idx < 0 || m->file_idx >= nb_input_files) {
 +        av_log(NULL, AV_LOG_FATAL, "mapchan: invalid input file index: %d\n",
 +               m->file_idx);
 +        exit_program(1);
 +    }
 +    if (m->stream_idx < 0 ||
 +        m->stream_idx >= input_files[m->file_idx].nb_streams) {
 +        av_log(NULL, AV_LOG_FATAL, "mapchan: invalid input file stream index #%d.%d\n",
 +               m->file_idx, m->stream_idx);
 +        exit_program(1);
 +    }
 +    st = input_files[m->file_idx].ctx->streams[m->stream_idx];
 +    if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
 +        av_log(NULL, AV_LOG_FATAL, "mapchan: stream #%d.%d is not an audio stream.\n",
 +               m->file_idx, m->stream_idx);
 +        exit_program(1);
 +    }
 +    if (m->channel_idx < 0 || m->channel_idx >= st->codec->channels) {
 +        av_log(NULL, AV_LOG_FATAL, "mapchan: invalid audio channel #%d.%d.%d\n",
 +               m->file_idx, m->stream_idx, m->channel_idx);
 +        exit_program(1);
      }
      return 0;
  }
  
 -static void parse_meta_type(char *arg, char *type, int *index, char **endptr)
 +static void parse_meta_type(char *arg, char *type, int *index)
  {
 -    *endptr = arg;
 -    if (*arg == ',') {
 -        *type = *(++arg);
 +    if (*arg) {
 +        *type = *arg;
          switch (*arg) {
          case 'g':
              break;
          case 's':
          case 'c':
          case 'p':
 -            *index = strtol(++arg, endptr, 0);
 +            if (*(++arg) == ':')
 +                *index = strtol(++arg, NULL, 0);
              break;
          default:
 -            fprintf(stderr, "Invalid metadata type %c.\n", *arg);
 +            av_log(NULL, AV_LOG_FATAL, "Invalid metadata type %c.\n", *arg);
              exit_program(1);
          }
      } else
          *type = 'g';
  }
  
 -static int opt_map_metadata(const char *opt, const char *arg)
 +static int opt_map_metadata(OptionsContext *o, const char *opt, const char *arg)
  {
      MetadataMap *m, *m1;
      char *p;
  
 -    meta_data_maps = grow_array(meta_data_maps, sizeof(*meta_data_maps),
 -                                &nb_meta_data_maps, nb_meta_data_maps + 1);
 +    o->meta_data_maps = grow_array(o->meta_data_maps, sizeof(*o->meta_data_maps),
 +                                   &o->nb_meta_data_maps, o->nb_meta_data_maps + 1);
  
 -    m = &meta_data_maps[nb_meta_data_maps - 1][0];
 +    m = &o->meta_data_maps[o->nb_meta_data_maps - 1][1];
      m->file = strtol(arg, &p, 0);
 -    parse_meta_type(p, &m->type, &m->index, &p);
 -    if (*p)
 -        p++;
 +    parse_meta_type(*p ? p + 1 : p, &m->type, &m->index);
  
 -    m1 = &meta_data_maps[nb_meta_data_maps - 1][1];
 -    m1->file = strtol(p, &p, 0);
 -    parse_meta_type(p, &m1->type, &m1->index, &p);
 +    m1 = &o->meta_data_maps[o->nb_meta_data_maps - 1][0];
 +    if (p = strchr(opt, ':'))
 +        parse_meta_type(p + 1, &m1->type, &m1->index);
 +    else
 +        m1->type = 'g';
  
      if (m->type == 'g' || m1->type == 'g')
 -        metadata_global_autocopy = 0;
 +        o->metadata_global_manual = 1;
      if (m->type == 's' || m1->type == 's')
 -        metadata_streams_autocopy = 0;
 +        o->metadata_streams_manual = 1;
      if (m->type == 'c' || m1->type == 'c')
 -        metadata_chapters_autocopy = 0;
 +        o->metadata_chapters_manual = 1;
  
      return 0;
  }
  
 -static int opt_map_meta_data(const char *opt, const char *arg)
 +static int opt_map_meta_data(OptionsContext *o, const char *opt, const char *arg)
  {
 -    fprintf(stderr, "-map_meta_data is deprecated and will be removed soon. "
 +    av_log(NULL, AV_LOG_WARNING, "-map_meta_data is deprecated and will be removed soon. "
                      "Use -map_metadata instead.\n");
 -    return opt_map_metadata(opt, arg);
 +    return opt_map_metadata(o, opt, arg);
  }
  
 -static int opt_map_chapters(const char *opt, const char *arg)
 +static int opt_recording_timestamp(OptionsContext *o, const char *opt, const char *arg)
  {
 -    ChapterMap *c;
 -    char *p;
 -
 -    chapter_maps = grow_array(chapter_maps, sizeof(*chapter_maps), &nb_chapter_maps,
 -                              nb_chapter_maps + 1);
 -    c = &chapter_maps[nb_chapter_maps - 1];
 -    c->out_file = strtol(arg, &p, 0);
 -    if (*p)
 -        p++;
 +    char buf[128];
 +    int64_t recording_timestamp = parse_time_or_die(opt, arg, 0) / 1E6;
 +    struct tm time = *gmtime((time_t*)&recording_timestamp);
 +    strftime(buf, sizeof(buf), "creation_time=%FT%T%z", &time);
 +    parse_option(o, "metadata", buf, options);
  
 -    c->in_file = strtol(p, &p, 0);
 +    av_log(NULL, AV_LOG_WARNING, "%s is deprecated, set the 'creation_time' metadata "
 +                                 "tag instead.\n", opt);
      return 0;
  }
  
 -static int opt_input_ts_scale(const char *opt, const char *arg)
 +static AVCodec *find_codec_or_die(const char *name, enum AVMediaType type, int encoder)
  {
 -    unsigned int stream;
 -    double scale;
 -    char *p;
 -
 -    stream = strtol(arg, &p, 0);
 -    if (*p)
 -        p++;
 -    scale= strtod(p, &p);
 +    const char *codec_string = encoder ? "encoder" : "decoder";
 +    AVCodec *codec;
  
 -    ts_scale = grow_array(ts_scale, sizeof(*ts_scale), &nb_ts_scale, stream + 1);
 -    ts_scale[stream] = scale;
 -    return 0;
 +    codec = encoder ?
 +        avcodec_find_encoder_by_name(name) :
 +        avcodec_find_decoder_by_name(name);
 +    if(!codec) {
 +        av_log(NULL, AV_LOG_FATAL, "Unknown %s '%s'\n", codec_string, name);
 +        exit_program(1);
 +    }
 +    if(codec->type != type) {
 +        av_log(NULL, AV_LOG_FATAL, "Invalid %s type '%s'\n", codec_string, name);
 +        exit_program(1);
 +    }
 +    return codec;
  }
  
 -static int opt_recording_time(const char *opt, const char *arg)
 +static AVCodec *choose_decoder(OptionsContext *o, AVFormatContext *s, AVStream *st)
  {
 -    recording_time = parse_time_or_die(opt, arg, 1);
 -    return 0;
 -}
 +    char *codec_name = NULL;
  
 -static int opt_start_time(const char *opt, const char *arg)
 -{
 -    start_time = parse_time_or_die(opt, arg, 1);
 -    return 0;
 +    MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
 +    if (codec_name) {
 +        AVCodec *codec = find_codec_or_die(codec_name, st->codec->codec_type, 0);
 +        st->codec->codec_id = codec->id;
 +        return codec;
 +    } else
 +        return avcodec_find_decoder(st->codec->codec_id);
  }
  
 -static int opt_recording_timestamp(const char *opt, const char *arg)
 +/**
 + * Add all the streams from the given input file to the global
 + * list of input streams.
 + */
 +static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
  {
 -    char buf[128];
 -    int64_t recording_timestamp = parse_time_or_die(opt, arg, 0) / 1E6;
 -    struct tm time = *gmtime((time_t*)&recording_timestamp);
 -    strftime(buf, sizeof(buf), "creation_time=%FT%T%z", &time);
 -    opt_metadata("metadata", buf);
 +    int i, rfps, rfps_base;
 +    char *next, *codec_tag = NULL;
  
 -    av_log(NULL, AV_LOG_WARNING, "%s is deprecated, set the 'creation_time' metadata "
 -                                 "tag instead.\n", opt);
 -    return 0;
 +    for (i = 0; i < ic->nb_streams; i++) {
 +        AVStream *st = ic->streams[i];
 +        AVCodecContext *dec = st->codec;
 +        InputStream *ist;
 +
 +        input_streams = grow_array(input_streams, sizeof(*input_streams), &nb_input_streams, nb_input_streams + 1);
 +        ist = &input_streams[nb_input_streams - 1];
 +        ist->st = st;
 +        ist->file_index = nb_input_files;
 +        ist->discard = 1;
 +        ist->opts = filter_codec_opts(codec_opts, ist->st->codec->codec_id, ic, st);
 +
 +        ist->ts_scale = 1.0;
 +        MATCH_PER_STREAM_OPT(ts_scale, dbl, ist->ts_scale, ic, st);
 +
 +        MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
 +        if (codec_tag) {
 +            uint32_t tag = strtol(codec_tag, &next, 0);
 +            if (*next)
 +                tag = AV_RL32(codec_tag);
 +            st->codec->codec_tag = tag;
 +        }
 +
 +        ist->dec = choose_decoder(o, ic, st);
 +
 +        switch (dec->codec_type) {
 +        case AVMEDIA_TYPE_AUDIO:
 +            if(!ist->dec)
 +                ist->dec = avcodec_find_decoder(dec->codec_id);
 +            if(o->audio_disable)
 +                st->discard= AVDISCARD_ALL;
 +            break;
 +        case AVMEDIA_TYPE_VIDEO:
 +            if(!ist->dec)
 +                ist->dec = avcodec_find_decoder(dec->codec_id);
 +            rfps      = ic->streams[i]->r_frame_rate.num;
 +            rfps_base = ic->streams[i]->r_frame_rate.den;
 +            if (dec->lowres) {
 +                dec->flags |= CODEC_FLAG_EMU_EDGE;
 +            }
 +
 +            if (dec->time_base.den != rfps*dec->ticks_per_frame || dec->time_base.num != rfps_base) {
 +
 +                av_log(NULL, AV_LOG_INFO,"\nSeems stream %d codec frame rate differs from container frame rate: %2.2f (%d/%d) -> %2.2f (%d/%d)\n",
 +                       i, (float)dec->time_base.den / dec->time_base.num, dec->time_base.den, dec->time_base.num,
 +                       (float)rfps / rfps_base, rfps, rfps_base);
 +            }
 +
 +            if (o->video_disable)
 +                st->discard= AVDISCARD_ALL;
 +            else if(video_discard)
 +                st->discard= video_discard;
 +            break;
 +        case AVMEDIA_TYPE_DATA:
 +            break;
 +        case AVMEDIA_TYPE_SUBTITLE:
 +            if(!ist->dec)
 +                ist->dec = avcodec_find_decoder(dec->codec_id);
 +            if(o->subtitle_disable)
 +                st->discard = AVDISCARD_ALL;
 +            break;
 +        case AVMEDIA_TYPE_ATTACHMENT:
 +        case AVMEDIA_TYPE_UNKNOWN:
 +            break;
 +        default:
 +            abort();
 +        }
 +    }
  }
  
 -static int opt_input_ts_offset(const char *opt, const char *arg)
 +static void assert_file_overwrite(const char *filename)
  {
 -    input_ts_offset = parse_time_or_die(opt, arg, 1);
 -    return 0;
 +    if (!file_overwrite &&
 +        (strchr(filename, ':') == NULL || filename[1] == ':' ||
 +         av_strstart(filename, "file:", NULL))) {
 +        if (avio_check(filename, 0) == 0) {
 +            if (!using_stdin) {
 +                fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
 +                fflush(stderr);
 +                term_exit();
 +                if (!read_yesno()) {
 +                    av_log(0, AV_LOG_FATAL, "Not overwriting - exiting\n");
 +                    exit_program(1);
 +                }
 +                term_init();
 +            }
 +            else {
 +                av_log(0, AV_LOG_FATAL, "File '%s' already exists. Exiting.\n", filename);
 +                exit_program(1);
 +            }
 +        }
 +    }
  }
  
 -static enum CodecID find_codec_or_die(const char *name, int type, int encoder)
 +static void dump_attachment(AVStream *st, const char *filename)
  {
 -    const char *codec_string = encoder ? "encoder" : "decoder";
 -    AVCodec *codec;
 +    int ret;
 +    AVIOContext *out = NULL;
 +    AVDictionaryEntry *e;
  
 -    if(!name)
 -        return CODEC_ID_NONE;
 -    codec = encoder ?
 -        avcodec_find_encoder_by_name(name) :
 -        avcodec_find_decoder_by_name(name);
 -    if(!codec) {
 -        fprintf(stderr, "Unknown %s '%s'\n", codec_string, name);
 +    if (!st->codec->extradata_size) {
 +        av_log(NULL, AV_LOG_WARNING, "No extradata to dump in stream #%d:%d.\n",
 +               nb_input_files - 1, st->index);
 +        return;
 +    }
 +    if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
 +        filename = e->value;
 +    if (!*filename) {
 +        av_log(NULL, AV_LOG_FATAL, "No filename specified and no 'filename' tag"
 +               "in stream #%d:%d.\n", nb_input_files - 1, st->index);
          exit_program(1);
      }
 -    if(codec->type != type) {
 -        fprintf(stderr, "Invalid %s type '%s'\n", codec_string, name);
 +
 +    assert_file_overwrite(filename);
 +
-     if ((ret = avio_open (&out, filename, AVIO_FLAG_WRITE)) < 0) {
++    if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
 +        av_log(NULL, AV_LOG_FATAL, "Could not open file %s for writing.\n",
 +               filename);
          exit_program(1);
      }
 -    return codec->id;
 +
 +    avio_write(out, st->codec->extradata, st->codec->extradata_size);
 +    avio_flush(out);
 +    avio_close(out);
  }
  
 -static int opt_input_file(const char *opt, const char *filename)
 +static int opt_input_file(OptionsContext *o, const char *opt, const char *filename)
  {
      AVFormatContext *ic;
      AVInputFormat *file_iformat = NULL;
 -    int err, i, ret, rfps, rfps_base;
 +    int err, i, ret;
      int64_t timestamp;
      uint8_t buf[128];
      AVDictionary **opts;
      int orig_nb_streams;                     // number of streams before avformat_find_stream_info
  
 -    if (last_asked_format) {
 -        if (!(file_iformat = av_find_input_format(last_asked_format))) {
 -            fprintf(stderr, "Unknown input format: '%s'\n", last_asked_format);
 +    if (o->format) {
 +        if (!(file_iformat = av_find_input_format(o->format))) {
 +            av_log(NULL, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
              exit_program(1);
          }
 -        last_asked_format = NULL;
      }
  
      if (!strcmp(filename, "-"))
          print_error(filename, AVERROR(ENOMEM));
          exit_program(1);
      }
 -    if (audio_sample_rate) {
 -        snprintf(buf, sizeof(buf), "%d", audio_sample_rate);
 +    if (o->nb_audio_sample_rate) {
 +        snprintf(buf, sizeof(buf), "%d", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i);
          av_dict_set(&format_opts, "sample_rate", buf, 0);
      }
 -    if (audio_channels) {
 -        snprintf(buf, sizeof(buf), "%d", audio_channels);
 +    if (o->nb_audio_channels) {
 +        snprintf(buf, sizeof(buf), "%d", o->audio_channels[o->nb_audio_channels - 1].u.i);
          av_dict_set(&format_opts, "channels", buf, 0);
      }
 -    if (frame_rate.num) {
 -        snprintf(buf, sizeof(buf), "%d/%d", frame_rate.num, frame_rate.den);
 -        av_dict_set(&format_opts, "framerate", buf, 0);
 +    if (o->nb_frame_rates) {
 +        av_dict_set(&format_opts, "framerate", o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
      }
 -    if (frame_width && frame_height) {
 -        snprintf(buf, sizeof(buf), "%dx%d", frame_width, frame_height);
 -        av_dict_set(&format_opts, "video_size", buf, 0);
 +    if (o->nb_frame_sizes) {
 +        av_dict_set(&format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
      }
 -    if (frame_pix_fmt != PIX_FMT_NONE)
 -        av_dict_set(&format_opts, "pixel_format", av_get_pix_fmt_name(frame_pix_fmt), 0);
 -
 -    ic->video_codec_id   =
 -        find_codec_or_die(video_codec_name   , AVMEDIA_TYPE_VIDEO   , 0);
 -    ic->audio_codec_id   =
 -        find_codec_or_die(audio_codec_name   , AVMEDIA_TYPE_AUDIO   , 0);
 -    ic->subtitle_codec_id=
 -        find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0);
 +    if (o->nb_frame_pix_fmts)
 +        av_dict_set(&format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
 +
 +    ic->video_codec_id   = video_codec_name ?
 +        find_codec_or_die(video_codec_name   , AVMEDIA_TYPE_VIDEO   , 0)->id : CODEC_ID_NONE;
 +    ic->audio_codec_id   = audio_codec_name ?
 +        find_codec_or_die(audio_codec_name   , AVMEDIA_TYPE_AUDIO   , 0)->id : CODEC_ID_NONE;
 +    ic->subtitle_codec_id= subtitle_codec_name ?
 +        find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0)->id : CODEC_ID_NONE;
      ic->flags |= AVFMT_FLAG_NONBLOCK;
++    ic->interrupt_callback = int_cb;
 +
 +    if (loop_input) {
 +        av_log(NULL, AV_LOG_WARNING, "-loop_input is deprecated, use -loop 1\n");
 +        ic->loop_input = loop_input;
 +    }
  
      /* open the input file with generic libav function */
      err = avformat_open_input(&ic, filename, file_iformat, &format_opts);
      }
      assert_avoptions(format_opts);
  
 -    if(opt_programid) {
 -        int i, j;
 -        int found=0;
 -        for(i=0; i<ic->nb_streams; i++){
 -            ic->streams[i]->discard= AVDISCARD_ALL;
 -        }
 -        for(i=0; i<ic->nb_programs; i++){
 -            AVProgram *p= ic->programs[i];
 -            if(p->id != opt_programid){
 -                p->discard = AVDISCARD_ALL;
 -            }else{
 -                found=1;
 -                for(j=0; j<p->nb_stream_indexes; j++){
 -                    ic->streams[p->stream_index[j]]->discard= AVDISCARD_DEFAULT;
 -                }
 -            }
 -        }
 -        if(!found){
 -            fprintf(stderr, "Specified program id not found\n");
 -            exit_program(1);
 -        }
 -        opt_programid=0;
 -    }
 -
 -    if (loop_input) {
 -        av_log(NULL, AV_LOG_WARNING, "-loop_input is deprecated, use -loop 1\n");
 -        ic->loop_input = loop_input;
 -    }
 +    /* apply forced codec ids */
 +    for (i = 0; i < ic->nb_streams; i++)
 +        choose_decoder(o, ic, ic->streams[i]);
  
      /* Set AVCodecContext options for avformat_find_stream_info */
      opts = setup_find_stream_info_opts(ic, codec_opts);
      /* If not enough info to get the stream parameters, we decode the
         first frames to get it. (used in mpeg case for example) */
      ret = avformat_find_stream_info(ic, opts);
 -    if (ret < 0 && verbose >= 0) {
 -        fprintf(stderr, "%s: could not find codec parameters\n", filename);
 +    if (ret < 0) {
 +        av_log(NULL, AV_LOG_FATAL, "%s: could not find codec parameters\n", filename);
          av_close_input_file(ic);
          exit_program(1);
      }
  
 -    timestamp = start_time;
 +    timestamp = o->start_time;
      /* add the stream start time */
      if (ic->start_time != AV_NOPTS_VALUE)
          timestamp += ic->start_time;
  
      /* if seeking requested, we execute it */
 -    if (start_time != 0) {
 +    if (o->start_time != 0) {
          ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);
          if (ret < 0) {
 -            fprintf(stderr, "%s: could not seek to position %0.3f\n",
 -                    filename, (double)timestamp / AV_TIME_BASE);
 +            av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
 +                   filename, (double)timestamp / AV_TIME_BASE);
          }
 -        /* reset seek info */
 -        start_time = 0;
      }
  
      /* update the current parameters so that they match the one of the input stream */
 -    for(i=0;i<ic->nb_streams;i++) {
 -        AVStream *st = ic->streams[i];
 -        AVCodecContext *dec = st->codec;
 -        InputStream *ist;
 -
 -        dec->thread_count = thread_count;
 -
 -        input_streams = grow_array(input_streams, sizeof(*input_streams), &nb_input_streams, nb_input_streams + 1);
 -        ist = &input_streams[nb_input_streams - 1];
 -        ist->st = st;
 -        ist->file_index = nb_input_files;
 -        ist->discard = 1;
 -        ist->opts = filter_codec_opts(codec_opts, ist->st->codec->codec_id, ic, st);
 -
 -        if (i < nb_ts_scale)
 -            ist->ts_scale = ts_scale[i];
 -
 -        switch (dec->codec_type) {
 -        case AVMEDIA_TYPE_AUDIO:
 -            ist->dec = avcodec_find_decoder_by_name(audio_codec_name);
 -            if(audio_disable)
 -                st->discard= AVDISCARD_ALL;
 -            break;
 -        case AVMEDIA_TYPE_VIDEO:
 -            ist->dec = avcodec_find_decoder_by_name(video_codec_name);
 -            rfps      = ic->streams[i]->r_frame_rate.num;
 -            rfps_base = ic->streams[i]->r_frame_rate.den;
 -            if (dec->lowres) {
 -                dec->flags |= CODEC_FLAG_EMU_EDGE;
 -                dec->height >>= dec->lowres;
 -                dec->width  >>= dec->lowres;
 -            }
 -            if(me_threshold)
 -                dec->debug |= FF_DEBUG_MV;
 -
 -            if (dec->time_base.den != rfps*dec->ticks_per_frame || dec->time_base.num != rfps_base) {
 -
 -                if (verbose >= 0)
 -                    fprintf(stderr,"\nSeems stream %d codec frame rate differs from container frame rate: %2.2f (%d/%d) -> %2.2f (%d/%d)\n",
 -                            i, (float)dec->time_base.den / dec->time_base.num, dec->time_base.den, dec->time_base.num,
 -
 -                    (float)rfps / rfps_base, rfps, rfps_base);
 -            }
 -
 -            if(video_disable)
 -                st->discard= AVDISCARD_ALL;
 -            else if(video_discard)
 -                st->discard= video_discard;
 -            break;
 -        case AVMEDIA_TYPE_DATA:
 -            break;
 -        case AVMEDIA_TYPE_SUBTITLE:
 -            ist->dec = avcodec_find_decoder_by_name(subtitle_codec_name);
 -            if(subtitle_disable)
 -                st->discard = AVDISCARD_ALL;
 -            break;
 -        case AVMEDIA_TYPE_ATTACHMENT:
 -        case AVMEDIA_TYPE_UNKNOWN:
 -            break;
 -        default:
 -            abort();
 -        }
 -    }
 +    add_input_streams(o, ic);
  
      /* dump the file content */
 -    if (verbose >= 0)
 -        av_dump_format(ic, nb_input_files, filename, 0);
 +    av_dump_format(ic, nb_input_files, filename, 0);
  
      input_files = grow_array(input_files, sizeof(*input_files), &nb_input_files, nb_input_files + 1);
      input_files[nb_input_files - 1].ctx        = ic;
      input_files[nb_input_files - 1].ist_index  = nb_input_streams - ic->nb_streams;
 -    input_files[nb_input_files - 1].ts_offset  = input_ts_offset - (copy_ts ? 0 : timestamp);
 +    input_files[nb_input_files - 1].ts_offset  = o->input_ts_offset - (copy_ts ? 0 : timestamp);
      input_files[nb_input_files - 1].nb_streams = ic->nb_streams;
 +    input_files[nb_input_files - 1].rate_emu   = o->rate_emu;
  
 -    frame_rate    = (AVRational){0, 0};
 -    frame_pix_fmt = PIX_FMT_NONE;
 -    frame_height = 0;
 -    frame_width  = 0;
 -    audio_sample_rate = 0;
 -    audio_channels    = 0;
 -    audio_sample_fmt  = AV_SAMPLE_FMT_NONE;
 -    av_freep(&ts_scale);
 -    nb_ts_scale = 0;
 +    for (i = 0; i < o->nb_dump_attachment; i++) {
 +        int j;
 +
 +        for (j = 0; j < ic->nb_streams; j++) {
 +            AVStream *st = ic->streams[j];
 +
 +            if (check_stream_specifier(ic, st, o->dump_attachment[i].specifier) == 1)
 +                dump_attachment(st, o->dump_attachment[i].u.str);
 +        }
 +    }
  
      for (i = 0; i < orig_nb_streams; i++)
          av_dict_free(&opts[i]);
      av_freep(&opts);
 -    av_freep(&video_codec_name);
 -    av_freep(&audio_codec_name);
 -    av_freep(&subtitle_codec_name);
 -    uninit_opts();
 -    init_opts();
 +
 +    reset_options(o, 1);
      return 0;
  }
  
 -static void check_inputs(int *has_video_ptr,
 -                         int *has_audio_ptr,
 -                         int *has_subtitle_ptr,
 -                         int *has_data_ptr)
 +static void parse_forced_key_frames(char *kf, OutputStream *ost)
  {
 -    int has_video, has_audio, has_subtitle, has_data, i, j;
 -    AVFormatContext *ic;
 +    char *p;
 +    int n = 1, i;
  
 -    has_video = 0;
 -    has_audio = 0;
 -    has_subtitle = 0;
 -    has_data = 0;
 +    for (p = kf; *p; p++)
 +        if (*p == ',')
 +            n++;
 +    ost->forced_kf_count = n;
 +    ost->forced_kf_pts = av_malloc(sizeof(*ost->forced_kf_pts) * n);
 +    if (!ost->forced_kf_pts) {
 +        av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
 +        exit_program(1);
 +    }
 +    for (i = 0; i < n; i++) {
 +        p = i ? strchr(p, ',') + 1 : kf;
 +        ost->forced_kf_pts[i] = parse_time_or_die("force_key_frames", p, 1);
 +    }
 +}
  
 -    for(j=0;j<nb_input_files;j++) {
 -        ic = input_files[j].ctx;
 -        for(i=0;i<ic->nb_streams;i++) {
 -            AVCodecContext *enc = ic->streams[i]->codec;
 -            switch(enc->codec_type) {
 -            case AVMEDIA_TYPE_AUDIO:
 -                has_audio = 1;
 -                break;
 -            case AVMEDIA_TYPE_VIDEO:
 -                has_video = 1;
 -                break;
 -            case AVMEDIA_TYPE_SUBTITLE:
 -                has_subtitle = 1;
 -                break;
 -            case AVMEDIA_TYPE_DATA:
 -            case AVMEDIA_TYPE_ATTACHMENT:
 -            case AVMEDIA_TYPE_UNKNOWN:
 -                has_data = 1;
 -                break;
 -            default:
 -                abort();
 -            }
 +static uint8_t *get_line(AVIOContext *s)
 +{
 +    AVIOContext *line;
 +    uint8_t *buf;
 +    char c;
 +
 +    if (avio_open_dyn_buf(&line) < 0) {
 +        av_log(NULL, AV_LOG_FATAL, "Could not alloc buffer for reading preset.\n");
 +        exit_program(1);
 +    }
 +
 +    while ((c = avio_r8(s)) && c != '\n')
 +        avio_w8(line, c);
 +    avio_w8(line, 0);
 +    avio_close_dyn_buf(line, &buf);
 +
 +    return buf;
 +}
 +
 +static int get_preset_file_2(const char *preset_name, const char *codec_name, AVIOContext **s)
 +{
 +    int i, ret = 1;
 +    char filename[1000];
 +    const char *base[3] = { getenv("AVCONV_DATADIR"),
 +                            getenv("HOME"),
 +                            AVCONV_DATADIR,
 +                            };
 +
 +    for (i = 0; i < FF_ARRAY_ELEMS(base) && ret; i++) {
 +        if (!base[i])
 +            continue;
 +        if (codec_name) {
 +            snprintf(filename, sizeof(filename), "%s%s/%s-%s.avpreset", base[i],
 +                     i != 1 ? "" : "/.avconv", codec_name, preset_name);
-             ret = avio_open(s, filename, AVIO_FLAG_READ);
++            ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
 +        }
 +        if (ret) {
 +            snprintf(filename, sizeof(filename), "%s%s/%s.avpreset", base[i],
 +                     i != 1 ? "" : "/.avconv", preset_name);
-             ret = avio_open(s, filename, AVIO_FLAG_READ);
++            ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
          }
      }
 -    *has_video_ptr = has_video;
 -    *has_audio_ptr = has_audio;
 -    *has_subtitle_ptr = has_subtitle;
 -    *has_data_ptr = has_data;
 +    return ret;
  }
  
 -static void new_video_stream(AVFormatContext *oc, int file_idx)
 +static void choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *ost)
 +{
 +    char *codec_name = NULL;
 +
 +    MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st);
 +    if (!codec_name) {
 +        ost->st->codec->codec_id = av_guess_codec(s->oformat, NULL, s->filename,
 +                                                  NULL, ost->st->codec->codec_type);
 +        ost->enc = avcodec_find_encoder(ost->st->codec->codec_id);
 +    } else if (!strcmp(codec_name, "copy"))
 +        ost->stream_copy = 1;
 +    else {
 +        ost->enc = find_codec_or_die(codec_name, ost->st->codec->codec_type, 1);
 +        ost->st->codec->codec_id = ost->enc->id;
 +    }
 +}
 +
 +static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type)
  {
 -    AVStream *st;
      OutputStream *ost;
 -    AVCodecContext *video_enc;
 -    enum CodecID codec_id = CODEC_ID_NONE;
 -    AVCodec *codec= NULL;
 +    AVStream *st = avformat_new_stream(oc, NULL);
 +    int idx      = oc->nb_streams - 1, ret = 0;
 +    char *bsf = NULL, *next, *codec_tag = NULL;
 +    AVBitStreamFilterContext *bsfc, *bsfc_prev = NULL;
 +    double qscale = -1;
 +    char *buf = NULL, *arg = NULL, *preset = NULL;
 +    AVIOContext *s = NULL;
  
 -    if(!video_stream_copy){
 -        if (video_codec_name) {
 -            codec_id = find_codec_or_die(video_codec_name, AVMEDIA_TYPE_VIDEO, 1);
 -            codec = avcodec_find_encoder_by_name(video_codec_name);
 -        } else {
 -            codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_VIDEO);
 -            codec = avcodec_find_encoder(codec_id);
 -        }
 +    if (!st) {
 +        av_log(NULL, AV_LOG_FATAL, "Could not alloc stream.\n");
 +        exit_program(1);
      }
  
 -    ost = new_output_stream(oc, file_idx, codec);
 -    st  = ost->st;
 -    if (!video_stream_copy) {
 -        ost->frame_aspect_ratio = frame_aspect_ratio;
 -        frame_aspect_ratio = 0;
 -#if CONFIG_AVFILTER
 -        ost->avfilter= vfilters;
 -        vfilters = NULL;
 -#endif
 +    if (oc->nb_streams - 1 < o->nb_streamid_map)
 +        st->id = o->streamid_map[oc->nb_streams - 1];
 +
 +    output_streams = grow_array(output_streams, sizeof(*output_streams), &nb_output_streams,
 +                                nb_output_streams + 1);
 +    ost = &output_streams[nb_output_streams - 1];
 +    ost->file_index = nb_output_files;
 +    ost->index = idx;
 +    ost->st    = st;
 +    st->codec->codec_type = type;
 +    choose_encoder(o, oc, ost);
 +    if (ost->enc) {
 +        ost->opts  = filter_codec_opts(codec_opts, ost->enc->id, oc, st);
 +    }
 +
 +    avcodec_get_context_defaults3(st->codec, ost->enc);
 +    st->codec->codec_type = type; // XXX hack, avcodec_get_context_defaults2() sets type to unknown for stream copy
 +
 +    MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);
 +    if (preset && (!(ret = get_preset_file_2(preset, ost->enc->name, &s)))) {
 +        do  {
 +            buf = get_line(s);
 +            if (!buf[0] || buf[0] == '#') {
 +                av_free(buf);
 +                continue;
 +            }
 +            if (!(arg = strchr(buf, '='))) {
 +                av_log(NULL, AV_LOG_FATAL, "Invalid line found in the preset file.\n");
 +                exit_program(1);
 +            }
 +            *arg++ = 0;
 +            av_dict_set(&ost->opts, buf, arg, AV_DICT_DONT_OVERWRITE);
 +            av_free(buf);
 +        } while (!s->eof_reached);
 +        avio_close(s);
 +    }
 +    if (ret) {
 +        av_log(NULL, AV_LOG_FATAL,
 +               "Preset %s specified for stream %d:%d, but could not be opened.\n",
 +               preset, ost->file_index, ost->index);
 +        exit_program(1);
      }
  
 -    ost->bitstream_filters = video_bitstream_filters;
 -    video_bitstream_filters= NULL;
 +    ost->max_frames = INT64_MAX;
 +    MATCH_PER_STREAM_OPT(max_frames, i64, ost->max_frames, oc, st);
  
 -    st->codec->thread_count= thread_count;
 +    MATCH_PER_STREAM_OPT(bitstream_filters, str, bsf, oc, st);
 +    while (bsf) {
 +        if (next = strchr(bsf, ','))
 +            *next++ = 0;
 +        if (!(bsfc = av_bitstream_filter_init(bsf))) {
 +            av_log(NULL, AV_LOG_FATAL, "Unknown bitstream filter %s\n", bsf);
 +            exit_program(1);
 +        }
 +        if (bsfc_prev)
 +            bsfc_prev->next = bsfc;
 +        else
 +            ost->bitstream_filters = bsfc;
  
 -    video_enc = st->codec;
 +        bsfc_prev = bsfc;
 +        bsf       = next;
 +    }
  
 -    if(video_codec_tag)
 -        video_enc->codec_tag= video_codec_tag;
 +    MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st);
 +    if (codec_tag) {
 +        uint32_t tag = strtol(codec_tag, &next, 0);
 +        if (*next)
 +            tag = AV_RL32(codec_tag);
 +        st->codec->codec_tag = tag;
 +    }
  
 -    if(oc->oformat->flags & AVFMT_GLOBALHEADER) {
 -        video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
 +    MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st);
 +    if (qscale >= 0 || same_quant) {
 +        st->codec->flags |= CODEC_FLAG_QSCALE;
 +        st->codec->global_quality = FF_QP2LAMBDA * qscale;
      }
  
 -    video_enc->codec_type = AVMEDIA_TYPE_VIDEO;
 -    if (video_stream_copy) {
 -        st->stream_copy = 1;
 -        video_enc->sample_aspect_ratio =
 -        st->sample_aspect_ratio = av_d2q(frame_aspect_ratio*frame_height/frame_width, 255);
 -    } else {
 -        const char *p;
 +    if (oc->oformat->flags & AVFMT_GLOBALHEADER)
 +        st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
 +
 +    av_opt_get_int(sws_opts, "sws_flags", 0, &ost->sws_flags);
 +    return ost;
 +}
 +
 +static void parse_matrix_coeffs(uint16_t *dest, const char *str)
 +{
 +    int i;
 +    const char *p = str;
 +    for(i = 0;; i++) {
 +        dest[i] = atoi(p);
 +        if(i == 63)
 +            break;
 +        p = strchr(p, ',');
 +        if(!p) {
 +            av_log(NULL, AV_LOG_FATAL, "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
 +            exit_program(1);
 +        }
 +        p++;
 +    }
 +}
 +
 +static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc)
 +{
 +    AVStream *st;
 +    OutputStream *ost;
 +    AVCodecContext *video_enc;
 +
 +    ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO);
 +    st  = ost->st;
 +    video_enc = st->codec;
 +
 +    if (!ost->stream_copy) {
 +        const char *p = NULL;
 +        char *forced_key_frames = NULL, *frame_rate = NULL, *frame_size = NULL;
 +        char *frame_aspect_ratio = NULL, *frame_pix_fmt = NULL;
 +        char *intra_matrix = NULL, *inter_matrix = NULL, *filters = NULL;
          int i;
  
 -        if (frame_rate.num)
 -            ost->frame_rate = frame_rate;
 -        video_enc->codec_id = codec_id;
 +        MATCH_PER_STREAM_OPT(frame_rates, str, frame_rate, oc, st);
 +        if (frame_rate && av_parse_video_rate(&ost->frame_rate, frame_rate) < 0) {
 +            av_log(NULL, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);
 +            exit_program(1);
 +        }
  
 -        video_enc->width = frame_width;
 -        video_enc->height = frame_height;
 -        video_enc->pix_fmt = frame_pix_fmt;
 +        MATCH_PER_STREAM_OPT(frame_sizes, str, frame_size, oc, st);
 +        if (frame_size && av_parse_video_size(&video_enc->width, &video_enc->height, frame_size) < 0) {
 +            av_log(NULL, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
 +            exit_program(1);
 +        }
 +
 +        MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);
 +        if (frame_aspect_ratio)
 +            ost->frame_aspect_ratio = parse_frame_aspect_ratio(frame_aspect_ratio);
 +
 +        video_enc->bits_per_raw_sample = frame_bits_per_raw_sample;
 +        MATCH_PER_STREAM_OPT(frame_pix_fmts, str, frame_pix_fmt, oc, st);
 +        if (frame_pix_fmt && (video_enc->pix_fmt = av_get_pix_fmt(frame_pix_fmt)) == PIX_FMT_NONE) {
 +            av_log(NULL, AV_LOG_FATAL, "Unknown pixel format requested: %s.\n", frame_pix_fmt);
 +            exit_program(1);
 +        }
          st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
  
          if (intra_only)
              video_enc->gop_size = 0;
 -        if (video_qscale || same_quality) {
 -            video_enc->flags |= CODEC_FLAG_QSCALE;
 -            video_enc->global_quality = FF_QP2LAMBDA * video_qscale;
 +        MATCH_PER_STREAM_OPT(intra_matrices, str, intra_matrix, oc, st);
 +        if (intra_matrix) {
 +            if (!(video_enc->intra_matrix = av_mallocz(sizeof(*video_enc->intra_matrix) * 64))) {
 +                av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for intra matrix.\n");
 +                exit_program(1);
 +            }
 +            parse_matrix_coeffs(video_enc->intra_matrix, intra_matrix);
 +        }
 +        MATCH_PER_STREAM_OPT(inter_matrices, str, inter_matrix, oc, st);
 +        if (inter_matrix) {
 +            if (!(video_enc->inter_matrix = av_mallocz(sizeof(*video_enc->inter_matrix) * 64))) {
 +                av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for inter matrix.\n");
 +                exit_program(1);
 +            }
 +            parse_matrix_coeffs(video_enc->inter_matrix, inter_matrix);
          }
  
 -        if(intra_matrix)
 -            video_enc->intra_matrix = intra_matrix;
 -        if(inter_matrix)
 -            video_enc->inter_matrix = inter_matrix;
 -
 -        p= video_rc_override_string;
 +        MATCH_PER_STREAM_OPT(rc_overrides, str, p, oc, st);
          for(i=0; p; i++){
              int start, end, q;
              int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
              if(e!=3){
 -                fprintf(stderr, "error parsing rc_override\n");
 +                av_log(NULL, AV_LOG_FATAL, "error parsing rc_override\n");
                  exit_program(1);
              }
 +            /* FIXME realloc failure */
              video_enc->rc_override=
                  av_realloc(video_enc->rc_override,
                             sizeof(RcOverride)*(i+1));
          video_enc->rc_override_count=i;
          if (!video_enc->rc_initial_buffer_occupancy)
              video_enc->rc_initial_buffer_occupancy = video_enc->rc_buffer_size*3/4;
 -        video_enc->me_threshold= me_threshold;
          video_enc->intra_dc_precision= intra_dc_precision - 8;
  
          if (do_psnr)
  
          /* two pass mode */
          if (do_pass) {
 -            if (do_pass == 1) {
 +            if (do_pass & 1) {
                  video_enc->flags |= CODEC_FLAG_PASS1;
 -            } else {
 +            }
 +            if (do_pass & 2) {
                  video_enc->flags |= CODEC_FLAG_PASS2;
              }
          }
  
 +        MATCH_PER_STREAM_OPT(forced_key_frames, str, forced_key_frames, oc, st);
          if (forced_key_frames)
 -            parse_forced_key_frames(forced_key_frames, ost, video_enc);
 -    }
 -    if (video_language) {
 -        av_dict_set(&st->metadata, "language", video_language, 0);
 -        av_freep(&video_language);
 +            parse_forced_key_frames(forced_key_frames, ost);
 +
 +        MATCH_PER_STREAM_OPT(force_fps, i, ost->force_fps, oc, st);
 +
 +        ost->top_field_first = -1;
 +        MATCH_PER_STREAM_OPT(top_field_first, i, ost->top_field_first, oc, st);
 +
 +        MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, ost->copy_initial_nonkeyframes, oc ,st);
 +
 +#if CONFIG_AVFILTER
 +        MATCH_PER_STREAM_OPT(filters, str, filters, oc, st);
 +        if (filters)
 +            ost->avfilter = av_strdup(filters);
 +#endif
      }
  
 -    /* reset some key parameters */
 -    video_disable = 0;
 -    av_freep(&video_codec_name);
 -    av_freep(&forced_key_frames);
 -    video_stream_copy = 0;
 -    frame_pix_fmt = PIX_FMT_NONE;
 +    return ost;
  }
  
 -static void new_audio_stream(AVFormatContext *oc, int file_idx)
 +static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc)
  {
 +    int n;
      AVStream *st;
      OutputStream *ost;
 -    AVCodec *codec= NULL;
      AVCodecContext *audio_enc;
 -    enum CodecID codec_id = CODEC_ID_NONE;
  
 -    if(!audio_stream_copy){
 -        if (audio_codec_name) {
 -            codec_id = find_codec_or_die(audio_codec_name, AVMEDIA_TYPE_AUDIO, 1);
 -            codec = avcodec_find_encoder_by_name(audio_codec_name);
 -        } else {
 -            codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_AUDIO);
 -            codec = avcodec_find_encoder(codec_id);
 -        }
 -    }
 -    ost = new_output_stream(oc, file_idx, codec);
 +    ost = new_output_stream(o, oc, AVMEDIA_TYPE_AUDIO);
      st  = ost->st;
  
 -    ost->bitstream_filters = audio_bitstream_filters;
 -    audio_bitstream_filters= NULL;
 -
 -    st->codec->thread_count= thread_count;
 -
      audio_enc = st->codec;
      audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
  
 -    if(audio_codec_tag)
 -        audio_enc->codec_tag= audio_codec_tag;
 +    if (!ost->stream_copy) {
 +        char *sample_fmt = NULL;
 +
 +        MATCH_PER_STREAM_OPT(audio_channels, i, audio_enc->channels, oc, st);
 +
 +        MATCH_PER_STREAM_OPT(sample_fmts, str, sample_fmt, oc, st);
 +        if (sample_fmt &&
 +            (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
 +            av_log(NULL, AV_LOG_FATAL, "Invalid sample format '%s'\n", sample_fmt);
 +            exit_program(1);
 +        }
 +
 +        MATCH_PER_STREAM_OPT(audio_sample_rate, i, audio_enc->sample_rate, oc, st);
  
 -    if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
 -        audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
 +        ost->rematrix_volume=1.0;
 +        MATCH_PER_STREAM_OPT(rematrix_volume, f, ost->rematrix_volume, oc, st);
      }
 -    if (audio_stream_copy) {
 -        st->stream_copy = 1;
 -    } else {
 -        audio_enc->codec_id = codec_id;
  
 -        if (audio_qscale > QSCALE_NONE) {
 -            audio_enc->flags |= CODEC_FLAG_QSCALE;
 -            audio_enc->global_quality = FF_QP2LAMBDA * audio_qscale;
 +    /* check for channel mapping for this audio stream */
 +    for (n = 0; n < o->nb_audio_channel_maps; n++) {
 +        AudioChannelMap *map = &o->audio_channel_maps[n];
 +        InputStream *ist = &input_streams[ost->source_index];
 +        if ((map->channel_idx == -1 || (ist->file_index == map->file_idx && ist->st->index == map->stream_idx)) &&
 +            (map->ofile_idx   == -1 || ost->file_index == map->ofile_idx) &&
 +            (map->ostream_idx == -1 || ost->st->index  == map->ostream_idx)) {
 +            if (ost->audio_channels_mapped < FF_ARRAY_ELEMS(ost->audio_channels_map))
 +                ost->audio_channels_map[ost->audio_channels_mapped++] = map->channel_idx;
 +            else
 +                av_log(NULL, AV_LOG_FATAL, "Max channel mapping for output %d.%d reached\n",
 +                       ost->file_index, ost->st->index);
          }
 -        if (audio_channels)
 -            audio_enc->channels = audio_channels;
 -        if (audio_sample_fmt != AV_SAMPLE_FMT_NONE)
 -            audio_enc->sample_fmt = audio_sample_fmt;
 -        if (audio_sample_rate)
 -            audio_enc->sample_rate = audio_sample_rate;
 -    }
 -    if (audio_language) {
 -        av_dict_set(&st->metadata, "language", audio_language, 0);
 -        av_freep(&audio_language);
 -    }
 -
 -    /* reset some key parameters */
 -    audio_disable = 0;
 -    av_freep(&audio_codec_name);
 -    audio_stream_copy = 0;
 +    }
 +
 +    return ost;
  }
  
 -static void new_data_stream(AVFormatContext *oc, int file_idx)
 +static OutputStream *new_data_stream(OptionsContext *o, AVFormatContext *oc)
  {
 -    AVStream *st;
      OutputStream *ost;
 -    AVCodecContext *data_enc;
  
 -    ost = new_output_stream(oc, file_idx, NULL);
 -    st  = ost->st;
 -    data_enc = st->codec;
 -    if (!data_stream_copy) {
 -        fprintf(stderr, "Data stream encoding not supported yet (only streamcopy)\n");
 +    ost = new_output_stream(o, oc, AVMEDIA_TYPE_DATA);
 +    if (!ost->stream_copy) {
 +        av_log(NULL, AV_LOG_FATAL, "Data stream encoding not supported yet (only streamcopy)\n");
          exit_program(1);
      }
  
 -    data_enc->codec_type = AVMEDIA_TYPE_DATA;
 -
 -    if (data_codec_tag)
 -        data_enc->codec_tag= data_codec_tag;
 -
 -    if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
 -        data_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
 -    }
 -    if (data_stream_copy) {
 -        st->stream_copy = 1;
 -    }
 +    return ost;
 +}
  
 -    data_disable = 0;
 -    av_freep(&data_codec_name);
 -    data_stream_copy = 0;
 +static OutputStream *new_attachment_stream(OptionsContext *o, AVFormatContext *oc)
 +{
 +    OutputStream *ost = new_output_stream(o, oc, AVMEDIA_TYPE_ATTACHMENT);
 +    ost->stream_copy = 1;
 +    return ost;
  }
  
 -static void new_subtitle_stream(AVFormatContext *oc, int file_idx)
 +static OutputStream *new_subtitle_stream(OptionsContext *o, AVFormatContext *oc)
  {
      AVStream *st;
      OutputStream *ost;
 -    AVCodec *codec=NULL;
      AVCodecContext *subtitle_enc;
 -    enum CodecID codec_id = CODEC_ID_NONE;
  
 -    if(!subtitle_stream_copy){
 -        if (subtitle_codec_name) {
 -            codec_id = find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 1);
 -            codec = avcodec_find_encoder_by_name(subtitle_codec_name);
 -        } else {
 -            codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_SUBTITLE);
 -            codec = avcodec_find_encoder(codec_id);
 -        }
 -    }
 -    ost = new_output_stream(oc, file_idx, codec);
 +    ost = new_output_stream(o, oc, AVMEDIA_TYPE_SUBTITLE);
      st  = ost->st;
      subtitle_enc = st->codec;
  
 -    ost->bitstream_filters = subtitle_bitstream_filters;
 -    subtitle_bitstream_filters= NULL;
 -
      subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
  
 -    if(subtitle_codec_tag)
 -        subtitle_enc->codec_tag= subtitle_codec_tag;
 +    return ost;
 +}
 +
 +/* arg format is "output-stream-index:streamid-value". */
 +static int opt_streamid(OptionsContext *o, const char *opt, const char *arg)
 +{
 +    int idx;
 +    char *p;
 +    char idx_str[16];
 +
 +    av_strlcpy(idx_str, arg, sizeof(idx_str));
 +    p = strchr(idx_str, ':');
 +    if (!p) {
 +        av_log(NULL, AV_LOG_FATAL,
 +               "Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
 +               arg, opt);
 +        exit_program(1);
 +    }
 +    *p++ = '\0';
 +    idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, MAX_STREAMS-1);
 +    o->streamid_map = grow_array(o->streamid_map, sizeof(*o->streamid_map), &o->nb_streamid_map, idx+1);
 +    o->streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);
 +    return 0;
 +}
 +
 +static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata)
 +{
 +    AVFormatContext *is = ifile->ctx;
 +    AVFormatContext *os = ofile->ctx;
 +    int i;
 +
 +    for (i = 0; i < is->nb_chapters; i++) {
 +        AVChapter *in_ch = is->chapters[i], *out_ch;
 +        int64_t ts_off   = av_rescale_q(ofile->start_time - ifile->ts_offset,
 +                                      AV_TIME_BASE_Q, in_ch->time_base);
 +        int64_t rt       = (ofile->recording_time == INT64_MAX) ? INT64_MAX :
 +                           av_rescale_q(ofile->recording_time, AV_TIME_BASE_Q, in_ch->time_base);
 +
 +
 +        if (in_ch->end < ts_off)
 +            continue;
 +        if (rt != INT64_MAX && in_ch->start > rt + ts_off)
 +            break;
 +
 +        out_ch = av_mallocz(sizeof(AVChapter));
 +        if (!out_ch)
 +            return AVERROR(ENOMEM);
 +
 +        out_ch->id        = in_ch->id;
 +        out_ch->time_base = in_ch->time_base;
 +        out_ch->start     = FFMAX(0,  in_ch->start - ts_off);
 +        out_ch->end       = FFMIN(rt, in_ch->end   - ts_off);
  
 -    if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
 -        subtitle_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
 -    }
 -    if (subtitle_stream_copy) {
 -        st->stream_copy = 1;
 -    } else {
 -        subtitle_enc->codec_id = codec_id;
 -    }
 +        if (copy_metadata)
 +            av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
  
 -    if (subtitle_language) {
 -        av_dict_set(&st->metadata, "language", subtitle_language, 0);
 -        av_freep(&subtitle_language);
 +        os->nb_chapters++;
 +        os->chapters = av_realloc_f(os->chapters, os->nb_chapters, sizeof(AVChapter));
 +        if (!os->chapters)
 +            return AVERROR(ENOMEM);
 +        os->chapters[os->nb_chapters - 1] = out_ch;
      }
 -
 -    subtitle_disable = 0;
 -    av_freep(&subtitle_codec_name);
 -    subtitle_stream_copy = 0;
 +    return 0;
  }
  
 -static int opt_new_stream(const char *opt, const char *arg)
 +static int read_ffserver_streams(OptionsContext *o, AVFormatContext *s, const char *filename)
  {
 -    AVFormatContext *oc;
 -    int file_idx = nb_output_files - 1;
 -    if (nb_output_files <= 0) {
 -        fprintf(stderr, "At least one output file must be specified\n");
 -        exit_program(1);
 -    }
 -    oc = output_files[file_idx];
 +    int i, err;
-     AVFormatContext *ic = NULL;
++    AVFormatContext *ic = avformat_alloc_context();
  
 -    if      (!strcmp(opt, "newvideo"   )) new_video_stream   (oc, file_idx);
 -    else if (!strcmp(opt, "newaudio"   )) new_audio_stream   (oc, file_idx);
 -    else if (!strcmp(opt, "newsubtitle")) new_subtitle_stream(oc, file_idx);
 -    else if (!strcmp(opt, "newdata"    )) new_data_stream    (oc, file_idx);
 -    else av_assert0(0);
 -    return 0;
 -}
++    ic->interrupt_callback = int_cb;
 +    err = avformat_open_input(&ic, filename, NULL, NULL);
 +    if (err < 0)
 +        return err;
 +    /* copy stream format */
 +    for(i=0;i<ic->nb_streams;i++) {
 +        AVStream *st;
 +        OutputStream *ost;
 +        AVCodec *codec;
 +        AVCodecContext *avctx;
  
 -/* arg format is "output-stream-index:streamid-value". */
 -static int opt_streamid(const char *opt, const char *arg)
 -{
 -    int idx;
 -    char *p;
 -    char idx_str[16];
 +        codec = avcodec_find_encoder(ic->streams[i]->codec->codec_id);
 +        ost   = new_output_stream(o, s, codec->type);
 +        st    = ost->st;
 +        avctx = st->codec;
  
 -    av_strlcpy(idx_str, arg, sizeof(idx_str));
 -    p = strchr(idx_str, ':');
 -    if (!p) {
 -        fprintf(stderr,
 -                "Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
 -                arg, opt);
 -        exit_program(1);
 +        // FIXME: a more elegant solution is needed
 +        memcpy(st, ic->streams[i], sizeof(AVStream));
 +        st->info = av_malloc(sizeof(*st->info));
 +        memcpy(st->info, ic->streams[i]->info, sizeof(*st->info));
 +        st->codec= avctx;
 +        avcodec_copy_context(st->codec, ic->streams[i]->codec);
 +
 +        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && !ost->stream_copy)
 +            choose_sample_fmt(st, codec);
 +        else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && !ost->stream_copy)
 +            choose_pixel_fmt(st, codec);
      }
 -    *p++ = '\0';
 -    idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, INT_MAX);
 -    streamid_map = grow_array(streamid_map, sizeof(*streamid_map), &nb_streamid_map, idx+1);
 -    streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);
 +
 +    av_close_input_file(ic);
      return 0;
  }
  
  static void opt_output_file(void *optctx, const char *filename)
  {
 +    OptionsContext *o = optctx;
      AVFormatContext *oc;
 -    int err, use_video, use_audio, use_subtitle, use_data;
 -    int input_has_video, input_has_audio, input_has_subtitle, input_has_data;
 +    int i, err;
      AVOutputFormat *file_oformat;
 +    OutputStream *ost;
 +    InputStream  *ist;
  
      if (!strcmp(filename, "-"))
          filename = "pipe:";
  
 -    oc = avformat_alloc_context();
 +    err = avformat_alloc_output_context2(&oc, NULL, o->format, filename);
      if (!oc) {
 -        print_error(filename, AVERROR(ENOMEM));
 +        print_error(filename, err);
          exit_program(1);
      }
 +    file_oformat= oc->oformat;
++    oc->interrupt_callback = int_cb;
  
 -    if (last_asked_format) {
 -        file_oformat = av_guess_format(last_asked_format, NULL, NULL);
 -        if (!file_oformat) {
 -            fprintf(stderr, "Requested output format '%s' is not a suitable output format\n", last_asked_format);
 +    if (!strcmp(file_oformat->name, "ffm") &&
 +        av_strstart(filename, "http:", NULL)) {
 +        int j;
 +        /* special case for files sent to ffserver: we get the stream
 +           parameters from ffserver */
 +        int err = read_ffserver_streams(o, oc, filename);
 +        if (err < 0) {
 +            print_error(filename, err);
              exit_program(1);
          }
 -        last_asked_format = NULL;
 +        for(j = nb_output_streams - oc->nb_streams; j < nb_output_streams; j++) {
 +            ost = &output_streams[j];
 +            for (i = 0; i < nb_input_streams; i++) {
 +                ist = &input_streams[i];
 +                if(ist->st->codec->codec_type == ost->st->codec->codec_type){
 +                    ost->sync_ist= ist;
 +                    ost->source_index= i;
 +                    ist->discard = 0;
 +                    break;
 +                }
 +            }
 +        }
 +    } else if (!o->nb_stream_maps) {
 +        /* pick the "best" stream of each type */
 +#define NEW_STREAM(type, index)\
 +        if (index >= 0) {\
 +            ost = new_ ## type ## _stream(o, oc);\
 +            ost->source_index = index;\
 +            ost->sync_ist     = &input_streams[index];\
 +            input_streams[index].discard = 0;\
 +        }
 +
 +        /* video: highest resolution */
 +        if (!o->video_disable && oc->oformat->video_codec != CODEC_ID_NONE) {
 +            int area = 0, idx = -1;
 +            for (i = 0; i < nb_input_streams; i++) {
 +                ist = &input_streams[i];
 +                if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
 +                    ist->st->codec->width * ist->st->codec->height > area) {
 +                    area = ist->st->codec->width * ist->st->codec->height;
 +                    idx = i;
 +                }
 +            }
 +            NEW_STREAM(video, idx);
 +        }
 +
 +        /* audio: most channels */
 +        if (!o->audio_disable && oc->oformat->audio_codec != CODEC_ID_NONE) {
 +            int channels = 0, idx = -1;
 +            for (i = 0; i < nb_input_streams; i++) {
 +                ist = &input_streams[i];
 +                if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
 +                    ist->st->codec->channels > channels) {
 +                    channels = ist->st->codec->channels;
 +                    idx = i;
 +                }
 +            }
 +            NEW_STREAM(audio, idx);
 +        }
 +
 +        /* subtitles: pick first */
 +        if (!o->subtitle_disable && (oc->oformat->subtitle_codec != CODEC_ID_NONE || subtitle_codec_name)) {
 +            for (i = 0; i < nb_input_streams; i++)
 +                if (input_streams[i].st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
 +                    NEW_STREAM(subtitle, i);
 +                    break;
 +                }
 +        }
 +        /* do something with data? */
      } else {
 -        file_oformat = av_guess_format(NULL, filename, NULL);
 -        if (!file_oformat) {
 -            fprintf(stderr, "Unable to find a suitable output format for '%s'\n",
 -                    filename);
 -            exit_program(1);
 +        for (i = 0; i < o->nb_stream_maps; i++) {
 +            StreamMap *map = &o->stream_maps[i];
 +
 +            if (map->disabled)
 +                continue;
 +
 +            ist = &input_streams[input_files[map->file_index].ist_index + map->stream_index];
 +            if(o->subtitle_disable && ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
 +                continue;
 +            if(o->   audio_disable && ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
 +                continue;
 +            if(o->   video_disable && ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
 +                continue;
 +
 +            switch (ist->st->codec->codec_type) {
 +            case AVMEDIA_TYPE_VIDEO:    ost = new_video_stream(o, oc);    break;
 +            case AVMEDIA_TYPE_AUDIO:    ost = new_audio_stream(o, oc);    break;
 +            case AVMEDIA_TYPE_SUBTITLE: ost = new_subtitle_stream(o, oc); break;
 +            case AVMEDIA_TYPE_DATA:     ost = new_data_stream(o, oc);     break;
 +            case AVMEDIA_TYPE_ATTACHMENT: ost = new_attachment_stream(o, oc); break;
 +            default:
 +                av_log(NULL, AV_LOG_FATAL, "Cannot map stream #%d:%d - unsupported type.\n",
 +                       map->file_index, map->stream_index);
 +                exit_program(1);
 +            }
 +
 +            ost->source_index = input_files[map->file_index].ist_index + map->stream_index;
 +            ost->sync_ist = &input_streams[input_files[map->sync_file_index].ist_index +
 +                                           map->sync_stream_index];
 +            ist->discard = 0;
          }
      }
  
 -    oc->oformat = file_oformat;
 -    av_strlcpy(oc->filename, filename, sizeof(oc->filename));
 +    /* handle attached files */
 +    for (i = 0; i < o->nb_attachments; i++) {
 +        AVIOContext *pb;
 +        uint8_t *attachment;
 +        const char *p;
 +        int64_t len;
  
-         if ((err = avio_open(&pb, o->attachments[i], AVIO_FLAG_READ)) < 0) {
 -    if (!strcmp(file_oformat->name, "ffm") &&
 -        av_strstart(filename, "http:", NULL)) {
 -        /* special case for files sent to avserver: we get the stream
 -           parameters from avserver */
 -        int err = read_avserver_streams(oc, filename);
 -        if (err < 0) {
 -            print_error(filename, err);
++        if ((err = avio_open2(&pb, o->attachments[i], AVIO_FLAG_READ, &int_cb, NULL)) < 0) {
 +            av_log(NULL, AV_LOG_FATAL, "Could not open attachment file %s.\n",
 +                   o->attachments[i]);
              exit_program(1);
          }
 -    } else {
 -        use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;
 -        use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;
 -        use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;
 -        use_data = data_stream_copy ||  data_codec_name; /* XXX once generic data codec will be available add a ->data_codec reference and use it here */
 -
 -        /* disable if no corresponding type found */
 -        check_inputs(&input_has_video,
 -                     &input_has_audio,
 -                     &input_has_subtitle,
 -                     &input_has_data);
 -
 -        if (!input_has_video)
 -            use_video = 0;
 -        if (!input_has_audio)
 -            use_audio = 0;
 -        if (!input_has_subtitle)
 -            use_subtitle = 0;
 -        if (!input_has_data)
 -            use_data = 0;
 -
 -        /* manual disable */
 -        if (audio_disable)    use_audio    = 0;
 -        if (video_disable)    use_video    = 0;
 -        if (subtitle_disable) use_subtitle = 0;
 -        if (data_disable)     use_data     = 0;
 -
 -        if (use_video)    new_video_stream(oc, nb_output_files);
 -        if (use_audio)    new_audio_stream(oc, nb_output_files);
 -        if (use_subtitle) new_subtitle_stream(oc, nb_output_files);
 -        if (use_data)     new_data_stream(oc, nb_output_files);
 -
 -        av_dict_copy(&oc->metadata, metadata, 0);
 -        av_dict_free(&metadata);
 -    }
 -
 -    av_dict_copy(&output_opts[nb_output_files], format_opts, 0);
 -    output_files[nb_output_files++] = oc;
 +        if ((len = avio_size(pb)) <= 0) {
 +            av_log(NULL, AV_LOG_FATAL, "Could not get size of the attachment %s.\n",
 +                   o->attachments[i]);
 +            exit_program(1);
 +        }
 +        if (!(attachment = av_malloc(len))) {
 +            av_log(NULL, AV_LOG_FATAL, "Attachment %s too large to fit into memory.\n",
 +                   o->attachments[i]);
 +            exit_program(1);
 +        }
 +        avio_read(pb, attachment, len);
 +
 +        ost = new_attachment_stream(o, oc);
 +        ost->stream_copy               = 0;
 +        ost->source_index              = -1;
 +        ost->attachment_filename       = o->attachments[i];
 +        ost->st->codec->extradata      = attachment;
 +        ost->st->codec->extradata_size = len;
 +
 +        p = strrchr(o->attachments[i], '/');
 +        av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE);
 +        avio_close(pb);
 +    }
 +
 +    output_files = grow_array(output_files, sizeof(*output_files), &nb_output_files, nb_output_files + 1);
 +    output_files[nb_output_files - 1].ctx       = oc;
 +    output_files[nb_output_files - 1].ost_index = nb_output_streams - oc->nb_streams;
 +    output_files[nb_output_files - 1].recording_time = o->recording_time;
 +    output_files[nb_output_files - 1].start_time     = o->start_time;
 +    output_files[nb_output_files - 1].limit_filesize = o->limit_filesize;
 +    av_dict_copy(&output_files[nb_output_files - 1].opts, format_opts, 0);
  
      /* check filename in case of an image number is expected */
      if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
  
      if (!(oc->oformat->flags & AVFMT_NOFILE)) {
          /* test if it already exists to avoid loosing precious files */
 -        if (!file_overwrite &&
 -            (strchr(filename, ':') == NULL ||
 -             filename[1] == ':' ||
 -             av_strstart(filename, "file:", NULL))) {
 -            if (avio_check(filename, 0) == 0) {
 -                if (!using_stdin) {
 -                    fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
 -                    fflush(stderr);
 -                    if (!read_yesno()) {
 -                        fprintf(stderr, "Not overwriting - exiting\n");
 -                        exit_program(1);
 -                    }
 -                }
 -                else {
 -                    fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
 -                    exit_program(1);
 -                }
 -            }
 -        }
 +        assert_file_overwrite(filename);
  
          /* open the file */
--        if ((err = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE)) < 0) {
++        if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
++                              &oc->interrupt_callback,
++                              &output_files[nb_output_files - 1].opts)) < 0) {
              print_error(filename, err);
              exit_program(1);
          }
      }
  
 -    oc->preload= (int)(mux_preload*AV_TIME_BASE);
 -    oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);
 +    if (o->mux_preload) {
 +        uint8_t buf[64];
 +        snprintf(buf, sizeof(buf), "%d", (int)(o->mux_preload*AV_TIME_BASE));
 +        av_dict_set(&output_files[nb_output_files - 1].opts, "preload", buf, 0);
 +    }
 +    oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
 +
      if (loop_output >= 0) {
          av_log(NULL, AV_LOG_WARNING, "-loop_output is deprecated, use -loop\n");
          oc->loop_output = loop_output;
      }
 -    oc->flags |= AVFMT_FLAG_NONBLOCK;
  
 -    frame_rate    = (AVRational){0, 0};
 -    frame_width   = 0;
 -    frame_height  = 0;
 -    audio_sample_rate = 0;
 -    audio_channels    = 0;
 -    audio_sample_fmt  = AV_SAMPLE_FMT_NONE;
 +    /* copy chapters */
 +    if (o->chapters_input_file >= nb_input_files) {
 +        if (o->chapters_input_file == INT_MAX) {
 +            /* copy chapters from the first input file that has them*/
 +            o->chapters_input_file = -1;
 +            for (i = 0; i < nb_input_files; i++)
 +                if (input_files[i].ctx->nb_chapters) {
 +                    o->chapters_input_file = i;
 +                    break;
 +                }
 +        } else {
 +            av_log(NULL, AV_LOG_FATAL, "Invalid input file index %d in chapter mapping.\n",
 +                   o->chapters_input_file);
 +            exit_program(1);
 +        }
 +    }
 +    if (o->chapters_input_file >= 0)
 +        copy_chapters(&input_files[o->chapters_input_file], &output_files[nb_output_files - 1],
 +                      !o->metadata_chapters_manual);
 +
 +    /* copy metadata */
 +    for (i = 0; i < o->nb_meta_data_maps; i++) {
 +        AVFormatContext *files[2];
 +        AVDictionary    **meta[2];
 +        int j;
  
 -    av_freep(&forced_key_frames);
 -    uninit_opts();
 -    init_opts();
 +#define METADATA_CHECK_INDEX(index, nb_elems, desc)\
 +        if ((index) < 0 || (index) >= (nb_elems)) {\
 +            av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps\n",\
 +                     (desc), (index));\
 +            exit_program(1);\
 +        }
 +
 +        int in_file_index = o->meta_data_maps[i][1].file;
 +        if (in_file_index < 0)
 +            continue;
 +        METADATA_CHECK_INDEX(in_file_index, nb_input_files, "input file")
 +
 +        files[0] = oc;
 +        files[1] = input_files[in_file_index].ctx;
 +
 +        for (j = 0; j < 2; j++) {
 +            MetadataMap *map = &o->meta_data_maps[i][j];
 +
 +            switch (map->type) {
 +            case 'g':
 +                meta[j] = &files[j]->metadata;
 +                break;
 +            case 's':
 +                METADATA_CHECK_INDEX(map->index, files[j]->nb_streams, "stream")
 +                meta[j] = &files[j]->streams[map->index]->metadata;
 +                break;
 +            case 'c':
 +                METADATA_CHECK_INDEX(map->index, files[j]->nb_chapters, "chapter")
 +                meta[j] = &files[j]->chapters[map->index]->metadata;
 +                break;
 +            case 'p':
 +                METADATA_CHECK_INDEX(map->index, files[j]->nb_programs, "program")
 +                meta[j] = &files[j]->programs[map->index]->metadata;
 +                break;
 +            default:
 +                abort();
 +            }
 +        }
 +
 +        av_dict_copy(meta[0], *meta[1], AV_DICT_DONT_OVERWRITE);
 +    }
 +
 +    /* copy global metadata by default */
 +    if (!o->metadata_global_manual && nb_input_files){
 +        av_dict_copy(&oc->metadata, input_files[0].ctx->metadata,
 +                     AV_DICT_DONT_OVERWRITE);
 +        if(o->recording_time != INT64_MAX)
 +            av_dict_set(&oc->metadata, "duration", NULL, 0);
 +    }
 +    if (!o->metadata_streams_manual)
 +        for (i = output_files[nb_output_files - 1].ost_index; i < nb_output_streams; i++) {
 +            InputStream *ist;
 +            if (output_streams[i].source_index < 0)         /* this is true e.g. for attached files */
 +                continue;
 +            ist = &input_streams[output_streams[i].source_index];
 +            av_dict_copy(&output_streams[i].st->metadata, ist->st->metadata, AV_DICT_DONT_OVERWRITE);
 +        }
 +
 +    /* process manually set metadata */
 +    for (i = 0; i < o->nb_metadata; i++) {
 +        AVDictionary **m;
 +        char type, *val;
 +        int index = 0;
 +
 +        val = strchr(o->metadata[i].u.str, '=');
 +        if (!val) {
 +            av_log(NULL, AV_LOG_FATAL, "No '=' character in metadata string %s.\n",
 +                   o->metadata[i].u.str);
 +            exit_program(1);
 +        }
 +        *val++ = 0;
 +
 +        parse_meta_type(o->metadata[i].specifier, &type, &index);
 +        switch (type) {
 +        case 'g':
 +            m = &oc->metadata;
 +            break;
 +        case 's':
 +            if (index < 0 || index >= oc->nb_streams) {
 +                av_log(NULL, AV_LOG_FATAL, "Invalid stream index %d in metadata specifier.\n", index);
 +                exit_program(1);
 +            }
 +            m = &oc->streams[index]->metadata;
 +            break;
 +        case 'c':
 +            if (index < 0 || index >= oc->nb_chapters) {
 +                av_log(NULL, AV_LOG_FATAL, "Invalid chapter index %d in metadata specifier.\n", index);
 +                exit_program(1);
 +            }
 +            m = &oc->chapters[index]->metadata;
 +            break;
 +        default:
 +            av_log(NULL, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", o->metadata[i].specifier);
 +            exit_program(1);
 +        }
 +
 +        av_dict_set(m, o->metadata[i].u.str, *val ? val : NULL, 0);
 +    }
 +
 +    reset_options(o, 0);
  }
  
  /* same option as mencoder */
  static int opt_pass(const char *opt, const char *arg)
  {
 -    do_pass = parse_number_or_die(opt, arg, OPT_INT, 1, 2);
 +    do_pass = parse_number_or_die(opt, arg, OPT_INT, 1, 3);
      return 0;
  }
  
@@@ -4258,21 -3888,49 +4265,21 @@@ static int64_t getmaxrss(void
  #endif
  }
  
 -static void parse_matrix_coeffs(uint16_t *dest, const char *str)
 -{
 -    int i;
 -    const char *p = str;
 -    for(i = 0;; i++) {
 -        dest[i] = atoi(p);
 -        if(i == 63)
 -            break;
 -        p = strchr(p, ',');
 -        if(!p) {
 -            fprintf(stderr, "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
 -            exit_program(1);
 -        }
 -        p++;
 -    }
 -}
 -
 -static void opt_inter_matrix(const char *arg)
 +static int opt_audio_qscale(OptionsContext *o, const char *opt, const char *arg)
  {
 -    inter_matrix = av_mallocz(sizeof(uint16_t) * 64);
 -    parse_matrix_coeffs(inter_matrix, arg);
 -}
 -
 -static void opt_intra_matrix(const char *arg)
 -{
 -    intra_matrix = av_mallocz(sizeof(uint16_t) * 64);
 -    parse_matrix_coeffs(intra_matrix, arg);
 +    return parse_option(o, "q:a", arg, options);
  }
  
  static void show_usage(void)
  {
      printf("Hyper fast Audio and Video encoder\n");
 -    printf("usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...\n");
 +    printf("usage: %s [options] [[infile options] -i infile]... {[outfile options] outfile}...\n", program_name);
      printf("\n");
  }
  
 -static void show_help(void)
 +static int opt_help(const char *opt, const char *arg)
  {
 -    AVCodec *c;
 -    AVOutputFormat *oformat = NULL;
 -    AVInputFormat  *iformat = NULL;
 -    const AVClass *class;
 -
 +    int flags = AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM;
      av_log_set_callback(log_callback_help);
      show_usage();
      show_help_options(options, "Main options:\n",
                        OPT_GRAB,
                        OPT_GRAB);
      printf("\n");
 -    class = avcodec_get_class();
 -    av_opt_show2(&class, NULL, AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM, 0);
 -    printf("\n");
 -
 -    /* individual codec options */
 -    c = NULL;
 -    while ((c = av_codec_next(c))) {
 -        if (c->priv_class) {
 -            av_opt_show2(&c->priv_class, NULL, AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM, 0);
 -            printf("\n");
 -        }
 -    }
 -
 -    class = avformat_get_class();
 -    av_opt_show2(&class, NULL, AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM, 0);
 -    printf("\n");
 -
 -    /* individual muxer options */
 -    while ((oformat = av_oformat_next(oformat))) {
 -        if (oformat->priv_class) {
 -            av_opt_show2(&oformat->priv_class, NULL, AV_OPT_FLAG_ENCODING_PARAM, 0);
 -            printf("\n");
 -        }
 -    }
 -
 -    /* individual demuxer options */
 -    while ((iformat = av_iformat_next(iformat))) {
 -        if (iformat->priv_class) {
 -            av_opt_show2(&iformat->priv_class, NULL, AV_OPT_FLAG_DECODING_PARAM, 0);
 -            printf("\n");
 -        }
 -    }
 +    show_help_children(avcodec_get_class(), flags);
 +    show_help_children(avformat_get_class(), flags);
 +    show_help_children(sws_get_class(), flags);
  
 -    class = sws_get_class();
 -    av_opt_show2(&class, NULL, AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM, 0);
 +    return 0;
  }
  
 -static int opt_target(const char *opt, const char *arg)
 +static int opt_target(OptionsContext *o, const char *opt, const char *arg)
  {
      enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
      static const char *const frame_rates[] = {"25", "30000/1001", "24000/1001"};
          norm = FILM;
          arg += 5;
      } else {
 -        int fr;
 -        /* Calculate FR via float to avoid int overflow */
 -        fr = (int)(frame_rate.num * 1000.0 / frame_rate.den);
 -        if(fr == 25000) {
 -            norm = PAL;
 -        } else if((fr == 29970) || (fr == 23976)) {
 -            norm = NTSC;
 -        } else {
 -            /* Try to determine PAL/NTSC by peeking in the input files */
 -            if(nb_input_files) {
 -                int i, j;
 -                for (j = 0; j < nb_input_files; j++) {
 -                    for (i = 0; i < input_files[j].ctx->nb_streams; i++) {
 -                        AVCodecContext *c = input_files[j].ctx->streams[i]->codec;
 -                        if(c->codec_type != AVMEDIA_TYPE_VIDEO)
 -                            continue;
 -                        fr = c->time_base.den * 1000 / c->time_base.num;
 -                        if(fr == 25000) {
 -                            norm = PAL;
 -                            break;
 -                        } else if((fr == 29970) || (fr == 23976)) {
 -                            norm = NTSC;
 -                            break;
 -                        }
 -                    }
 -                    if(norm != UNKNOWN)
 +        /* Try to determine PAL/NTSC by peeking in the input files */
 +        if(nb_input_files) {
 +            int i, j, fr;
 +            for (j = 0; j < nb_input_files; j++) {
 +                for (i = 0; i < input_files[j].nb_streams; i++) {
 +                    AVCodecContext *c = input_files[j].ctx->streams[i]->codec;
 +                    if(c->codec_type != AVMEDIA_TYPE_VIDEO)
 +                        continue;
 +                    fr = c->time_base.den * 1000 / c->time_base.num;
 +                    if(fr == 25000) {
 +                        norm = PAL;
                          break;
 +                    } else if((fr == 29970) || (fr == 23976)) {
 +                        norm = NTSC;
 +                        break;
 +                    }
                  }
 +                if(norm != UNKNOWN)
 +                    break;
              }
          }
 -        if(verbose > 0 && norm != UNKNOWN)
 -            fprintf(stderr, "Assuming %s for target.\n", norm == PAL ? "PAL" : "NTSC");
 +        if (norm != UNKNOWN)
 +            av_log(NULL, AV_LOG_INFO, "Assuming %s for target.\n", norm == PAL ? "PAL" : "NTSC");
      }
  
      if(norm == UNKNOWN) {
 -        fprintf(stderr, "Could not determine norm (PAL/NTSC/NTSC-Film) for target.\n");
 -        fprintf(stderr, "Please prefix target with \"pal-\", \"ntsc-\" or \"film-\",\n");
 -        fprintf(stderr, "or set a framerate with \"-r xxx\".\n");
 +        av_log(NULL, AV_LOG_FATAL, "Could not determine norm (PAL/NTSC/NTSC-Film) for target.\n");
 +        av_log(NULL, AV_LOG_FATAL, "Please prefix target with \"pal-\", \"ntsc-\" or \"film-\",\n");
 +        av_log(NULL, AV_LOG_FATAL, "or set a framerate with \"-r xxx\".\n");
          exit_program(1);
      }
  
      if(!strcmp(arg, "vcd")) {
 -        opt_video_codec("vcodec", "mpeg1video");
 -        opt_audio_codec("vcodec", "mp2");
 -        opt_format("f", "vcd");
 +        opt_video_codec(o, "c:v", "mpeg1video");
 +        opt_audio_codec(o, "c:a", "mp2");
 +        parse_option(o, "f", "vcd", options);
  
 -        opt_frame_size("s", norm == PAL ? "352x288" : "352x240");
 -        opt_frame_rate("r", frame_rates[norm]);
 +        parse_option(o, "s", norm == PAL ? "352x288" : "352x240", options);
 +        parse_option(o, "r", frame_rates[norm], options);
          opt_default("g", norm == PAL ? "15" : "18");
  
          opt_default("b", "1150000");
          opt_default("minrate", "1150000");
          opt_default("bufsize", "327680"); // 40*1024*8;
  
 -        opt_default("ab", "224000");
 -        audio_sample_rate = 44100;
 -        audio_channels = 2;
 +        opt_default("b:a", "224000");
 +        parse_option(o, "ar", "44100", options);
 +        parse_option(o, "ac", "2", options);
  
          opt_default("packetsize", "2324");
          opt_default("muxrate", "1411200"); // 2352 * 75 * 8;
             and the first pack from the other stream, respectively, may also have
             been written before.
             So the real data starts at SCR 36000+3*1200. */
 -        mux_preload= (36000+3*1200) / 90000.0; //0.44
 +        o->mux_preload = (36000+3*1200) / 90000.0; //0.44
      } else if(!strcmp(arg, "svcd")) {
  
 -        opt_video_codec("vcodec", "mpeg2video");
 -        opt_audio_codec("acodec", "mp2");
 -        opt_format("f", "svcd");
 +        opt_video_codec(o, "c:v", "mpeg2video");
 +        opt_audio_codec(o, "c:a", "mp2");
 +        parse_option(o, "f", "svcd", options);
  
 -        opt_frame_size("s", norm == PAL ? "480x576" : "480x480");
 -        opt_frame_rate("r", frame_rates[norm]);
 +        parse_option(o, "s", norm == PAL ? "480x576" : "480x480", options);
 +        parse_option(o, "r", frame_rates[norm], options);
 +        parse_option(o, "pix_fmt", "yuv420p", options);
          opt_default("g", norm == PAL ? "15" : "18");
  
          opt_default("b", "2040000");
          opt_default("flags", "+scan_offset");
  
  
 -        opt_default("ab", "224000");
 -        audio_sample_rate = 44100;
 +        opt_default("b:a", "224000");
 +        parse_option(o, "ar", "44100", options);
  
          opt_default("packetsize", "2324");
  
      } else if(!strcmp(arg, "dvd")) {
  
 -        opt_video_codec("vcodec", "mpeg2video");
 -        opt_audio_codec("vcodec", "ac3");
 -        opt_format("f", "dvd");
 +        opt_video_codec(o, "c:v", "mpeg2video");
 +        opt_audio_codec(o, "c:a", "ac3");
 +        parse_option(o, "f", "dvd", options);
  
 -        opt_frame_size("vcodec", norm == PAL ? "720x576" : "720x480");
 -        opt_frame_rate("r", frame_rates[norm]);
 +        parse_option(o, "s", norm == PAL ? "720x576" : "720x480", options);
 +        parse_option(o, "r", frame_rates[norm], options);
 +        parse_option(o, "pix_fmt", "yuv420p", options);
          opt_default("g", norm == PAL ? "15" : "18");
  
          opt_default("b", "6000000");
          opt_default("packetsize", "2048");  // from www.mpucoder.com: DVD sectors contain 2048 bytes of data, this is also the size of one pack.
          opt_default("muxrate", "10080000"); // from mplex project: data_rate = 1260000. mux_rate = data_rate * 8
  
 -        opt_default("ab", "448000");
 -        audio_sample_rate = 48000;
 +        opt_default("b:a", "448000");
 +        parse_option(o, "ar", "48000", options);
  
      } else if(!strncmp(arg, "dv", 2)) {
  
 -        opt_format("f", "dv");
 +        parse_option(o, "f", "dv", options);
  
 -        opt_frame_size("s", norm == PAL ? "720x576" : "720x480");
 -        opt_frame_pix_fmt("pix_fmt", !strncmp(arg, "dv50", 4) ? "yuv422p" :
 -                          norm == PAL ? "yuv420p" : "yuv411p");
 -        opt_frame_rate("r", frame_rates[norm]);
 +        parse_option(o, "s", norm == PAL ? "720x576" : "720x480", options);
 +        parse_option(o, "pix_fmt", !strncmp(arg, "dv50", 4) ? "yuv422p" :
 +                          norm == PAL ? "yuv420p" : "yuv411p", options);
 +        parse_option(o, "r", frame_rates[norm], options);
  
 -        audio_sample_rate = 48000;
 -        audio_channels = 2;
 +        parse_option(o, "ar", "48000", options);
 +        parse_option(o, "ac", "2", options);
  
      } else {
 -        fprintf(stderr, "Unknown target: %s\n", arg);
 +        av_log(NULL, AV_LOG_ERROR, "Unknown target: %s\n", arg);
          return AVERROR(EINVAL);
      }
      return 0;
@@@ -4462,34 -4157,37 +4469,34 @@@ static int opt_vstats(const char *opt, 
      return opt_vstats_file(opt, filename);
  }
  
 -static int opt_bsf(const char *opt, const char *arg)
 +static int opt_video_frames(OptionsContext *o, const char *opt, const char *arg)
  {
 -    AVBitStreamFilterContext *bsfc= av_bitstream_filter_init(arg); //FIXME split name and args for filter at '='
 -    AVBitStreamFilterContext **bsfp;
 -
 -    if(!bsfc){
 -        fprintf(stderr, "Unknown bitstream filter %s\n", arg);
 -        exit_program(1);
 -    }
 -
 -    bsfp= *opt == 'v' ? &video_bitstream_filters :
 -          *opt == 'a' ? &audio_bitstream_filters :
 -                        &subtitle_bitstream_filters;
 -    while(*bsfp)
 -        bsfp= &(*bsfp)->next;
 +    return parse_option(o, "frames:v", arg, options);
 +}
  
 -    *bsfp= bsfc;
 +static int opt_audio_frames(OptionsContext *o, const char *opt, const char *arg)
 +{
 +    return parse_option(o, "frames:a", arg, options);
 +}
  
 -    return 0;
 +static int opt_data_frames(OptionsContext *o, const char *opt, const char *arg)
 +{
 +    return parse_option(o, "frames:d", arg, options);
  }
  
 -static int opt_preset(const char *opt, const char *arg)
 +static int opt_preset(OptionsContext *o, const char *opt, const char *arg)
  {
      FILE *f=NULL;
      char filename[1000], tmp[1000], tmp2[1000], line[1000];
 -    char *codec_name = *opt == 'v' ? video_codec_name :
 -                       *opt == 'a' ? audio_codec_name :
 -                                     subtitle_codec_name;
 +    const char *codec_name = *opt == 'v' ? video_codec_name :
 +                             *opt == 'a' ? audio_codec_name :
 +                                           subtitle_codec_name;
  
      if (!(f = get_preset_file(filename, sizeof(filename), arg, *opt == 'f', codec_name))) {
 -        fprintf(stderr, "File for preset '%s' not found\n", arg);
 +        if(!strncmp(arg, "libx264-lossless", strlen("libx264-lossless"))){
 +            av_log(0, AV_LOG_FATAL, "Please use -preset <speed> -qp 0\n");
 +        }else
 +            av_log(0, AV_LOG_FATAL, "File for preset '%s' not found\n", arg);
          exit_program(1);
      }
  
              continue;
          e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
          if(e){
 -            fprintf(stderr, "%s: Invalid syntax: '%s'\n", filename, line);
 +            av_log(0, AV_LOG_FATAL, "%s: Invalid syntax: '%s'\n", filename, line);
              exit_program(1);
          }
          if(!strcmp(tmp, "acodec")){
 -            opt_audio_codec(tmp, tmp2);
 +            opt_audio_codec(o, tmp, tmp2);
          }else if(!strcmp(tmp, "vcodec")){
 -            opt_video_codec(tmp, tmp2);
 +            opt_video_codec(o, tmp, tmp2);
          }else if(!strcmp(tmp, "scodec")){
 -            opt_subtitle_codec(tmp, tmp2);
 +            opt_subtitle_codec(o, tmp, tmp2);
          }else if(!strcmp(tmp, "dcodec")){
 -            opt_data_codec(tmp, tmp2);
 +            opt_data_codec(o, tmp, tmp2);
          }else if(opt_default(tmp, tmp2) < 0){
 -            fprintf(stderr, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n", filename, line, tmp, tmp2);
 +            av_log(0, AV_LOG_FATAL, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n", filename, line, tmp, tmp2);
              exit_program(1);
          }
      }
      return 0;
  }
  
 +static void log_callback_null(void *ptr, int level, const char *fmt, va_list vl)
 +{
 +}
 +
 +static int opt_passlogfile(const char *opt, const char *arg)
 +{
 +    pass_logfilename_prefix = arg;
 +#if CONFIG_LIBX264_ENCODER
 +    return opt_default("passlogfile", arg);
 +#else
 +    return 0;
 +#endif
 +}
 +
 +static int opt_old2new(OptionsContext *o, const char *opt, const char *arg)
 +{
 +    char *s= av_malloc(strlen(opt)+2);
 +    snprintf(s, strlen(opt)+2, "%s:%c", opt+1, *opt);
 +    return parse_option(o, s, arg, options);
 +}
 +
 +static int opt_bitrate(OptionsContext *o, const char *opt, const char *arg)
 +{
 +    if(!strcmp(opt, "b")){
 +        av_log(0,AV_LOG_WARNING, "Please use -b:a or -b:v, -b is ambiguous\n");
 +        return parse_option(o, av_strdup("b:v"), arg, options);
 +    }
 +    return opt_default(opt, arg);
 +}
 +
 +static int opt_video_filters(OptionsContext *o, const char *opt, const char *arg)
 +{
 +    return parse_option(o, "filter:v", arg, options);
 +}
 +
 +#define OFFSET(x) offsetof(OptionsContext, x)
  static const OptionDef options[] = {
      /* main options */
  #include "cmdutils_common_opts.h"
 -    { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
 -    { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
 +    { "f", HAS_ARG | OPT_STRING | OPT_OFFSET, {.off = OFFSET(format)}, "force format", "fmt" },
 +    { "i", HAS_ARG | OPT_FUNC2, {(void*)opt_input_file}, "input file name", "filename" },
      { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
 -    { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file.stream[:syncfile.syncstream]" },
 -    { "map_meta_data", HAS_ARG | OPT_EXPERT, {(void*)opt_map_meta_data}, "DEPRECATED set meta data information of outfile from infile",
 +    { "c", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(codec_names)}, "codec name", "codec" },
 +    { "codec", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(codec_names)}, "codec name", "codec" },
 +    { "pre", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(presets)}, "preset name", "preset" },
 +    { "map", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map}, "set input stream mapping", "file.stream[:syncfile.syncstream]" },
 +    { "map_channel", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map_channel}, "map an audio channel from one stream to another", "file.stream.channel[:syncfile.syncstream]" },
 +    { "map_meta_data", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map_meta_data}, "DEPRECATED set meta data information of outfile from infile",
        "outfile[,metadata]:infile[,metadata]" },
 -    { "map_metadata", HAS_ARG | OPT_EXPERT, {(void*)opt_map_metadata}, "set metadata information of outfile from infile",
 +    { "map_metadata", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map_metadata}, "set metadata information of outfile from infile",
        "outfile[,metadata]:infile[,metadata]" },
 -    { "map_chapters",  HAS_ARG | OPT_EXPERT, {(void*)opt_map_chapters},  "set chapters mapping", "outfile:infile" },
 -    { "t", HAS_ARG, {(void*)opt_recording_time}, "record or transcode \"duration\" seconds of audio/video", "duration" },
 -    { "fs", HAS_ARG | OPT_INT64, {(void*)&limit_filesize}, "set the limit file size in bytes", "limit_size" }, //
 -    { "ss", HAS_ARG, {(void*)opt_start_time}, "set the start time offset", "time_off" },
 -    { "itsoffset", HAS_ARG, {(void*)opt_input_ts_offset}, "set the input ts offset", "time_off" },
 -    { "itsscale", HAS_ARG, {(void*)opt_input_ts_scale}, "set the input ts scale", "stream:scale" },
 -    { "timestamp", HAS_ARG, {(void*)opt_recording_timestamp}, "set the recording timestamp ('now' to set the current time)", "time" },
 -    { "metadata", HAS_ARG, {(void*)opt_metadata}, "add metadata", "string=string" },
 -    { "dframes", OPT_INT | HAS_ARG, {(void*)&max_frames[AVMEDIA_TYPE_DATA]}, "set the number of data frames to record", "number" },
 +    { "map_chapters",  OPT_INT | HAS_ARG | OPT_EXPERT | OPT_OFFSET, {.off = OFFSET(chapters_input_file)},  "set chapters mapping", "input_file_index" },
 +    { "t", HAS_ARG | OPT_TIME | OPT_OFFSET, {.off = OFFSET(recording_time)}, "record or transcode \"duration\" seconds of audio/video", "duration" },
 +    { "fs", HAS_ARG | OPT_INT64 | OPT_OFFSET, {.off = OFFSET(limit_filesize)}, "set the limit file size in bytes", "limit_size" }, //
 +    { "ss", HAS_ARG | OPT_TIME | OPT_OFFSET, {.off = OFFSET(start_time)}, "set the start time offset", "time_off" },
 +    { "itsoffset", HAS_ARG | OPT_TIME | OPT_OFFSET, {.off = OFFSET(input_ts_offset)}, "set the input ts offset", "time_off" },
 +    { "itsscale", HAS_ARG | OPT_DOUBLE | OPT_SPEC, {.off = OFFSET(ts_scale)}, "set the input ts scale", "scale" },
 +    { "timestamp", HAS_ARG | OPT_FUNC2, {(void*)opt_recording_timestamp}, "set the recording timestamp ('now' to set the current time)", "time" },
 +    { "metadata", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(metadata)}, "add metadata", "string=string" },
 +    { "dframes", HAS_ARG | OPT_FUNC2, {(void*)opt_data_frames}, "set the number of data frames to record", "number" },
      { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
        "add timings for benchmarking" },
      { "timelimit", HAS_ARG, {(void*)opt_timelimit}, "set max runtime in seconds", "limit" },
        "dump each input packet" },
      { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
        "when dumping packets, also dump the payload" },
 -    { "re", OPT_BOOL | OPT_EXPERT, {(void*)&rate_emu}, "read input at native frame rate", "" },
 +    { "re", OPT_BOOL | OPT_EXPERT | OPT_OFFSET, {.off = OFFSET(rate_emu)}, "read input at native frame rate", "" },
      { "loop_input", OPT_BOOL | OPT_EXPERT, {(void*)&loop_input}, "deprecated, use -loop" },
      { "loop_output", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&loop_output}, "deprecated, use -loop", "" },
 -    { "v", HAS_ARG, {(void*)opt_verbose}, "set ffmpeg verbosity level", "number" },
 -    { "target", HAS_ARG, {(void*)opt_target}, "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\", \"dv50\", \"pal-vcd\", \"ntsc-svcd\", ...)", "type" },
 -    { "threads",  HAS_ARG | OPT_EXPERT, {(void*)opt_thread_count}, "thread count", "count" },
 +    { "target", HAS_ARG | OPT_FUNC2, {(void*)opt_target}, "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\", \"dv50\", \"pal-vcd\", \"ntsc-svcd\", ...)", "type" },
      { "vsync", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&video_sync_method}, "video sync method", "" },
      { "async", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&audio_sync_method}, "audio sync method", "" },
      { "adrift_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, {(void*)&audio_drift_threshold}, "audio drift threshold", "threshold" },
      { "copytb", OPT_BOOL | OPT_EXPERT, {(void*)&copy_tb}, "copy input stream time base when stream copying" },
      { "shortest", OPT_BOOL | OPT_EXPERT, {(void*)&opt_shortest}, "finish encoding within shortest input" }, //
      { "dts_delta_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, {(void*)&dts_delta_threshold}, "timestamp discontinuity delta threshold", "threshold" },
 -    { "programid", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&opt_programid}, "desired program number", "" },
      { "xerror", OPT_BOOL, {(void*)&exit_on_error}, "exit on error", "error" },
 -    { "copyinkf", OPT_BOOL | OPT_EXPERT, {(void*)&copy_initial_nonkeyframes}, "copy initial non-keyframes" },
 +    { "copyinkf", OPT_BOOL | OPT_EXPERT | OPT_SPEC, {.off = OFFSET(copy_initial_nonkeyframes)}, "copy initial non-keyframes" },
 +    { "frames", OPT_INT64 | HAS_ARG | OPT_SPEC, {.off = OFFSET(max_frames)}, "set the number of frames to record", "number" },
 +    { "tag",   OPT_STRING | HAS_ARG | OPT_SPEC, {.off = OFFSET(codec_tags)}, "force codec tag/fourcc", "fourcc/tag" },
 +    { "q", HAS_ARG | OPT_EXPERT | OPT_DOUBLE | OPT_SPEC, {.off = OFFSET(qscale)}, "use fixed quality scale (VBR)", "q" },
 +    { "qscale", HAS_ARG | OPT_EXPERT | OPT_DOUBLE | OPT_SPEC, {.off = OFFSET(qscale)}, "use fixed quality scale (VBR)", "q" },
 +#if CONFIG_AVFILTER
 +    { "filter", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(filters)}, "set stream filterchain", "filter_list" },
 +#endif
 +    { "stats", OPT_BOOL, {&print_stats}, "print progress report during encoding", },
 +    { "attach", HAS_ARG | OPT_FUNC2, {(void*)opt_attach}, "add an attachment to the output file", "filename" },
 +    { "dump_attachment", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(dump_attachment)}, "extract an attachment into a file", "filename" },
  
      /* video options */
 -    { "vframes", OPT_INT | HAS_ARG | OPT_VIDEO, {(void*)&max_frames[AVMEDIA_TYPE_VIDEO]}, "set the number of video frames to record", "number" },
 -    { "r", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_rate}, "set frame rate (Hz value, fraction or abbreviation)", "rate" },
 -    { "s", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
 -    { "aspect", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_aspect_ratio}, "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
 -    { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_frame_pix_fmt}, "set pixel format, 'list' as argument shows all the pixel formats supported", "format" },
 -    { "croptop", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Removed, use the crop filter instead", "size" },
 +    { "vframes", HAS_ARG | OPT_VIDEO | OPT_FUNC2, {(void*)opt_video_frames}, "set the number of video frames to record", "number" },
 +    { "r", HAS_ARG | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_rates)}, "set frame rate (Hz value, fraction or abbreviation)", "rate" },
 +    { "s", HAS_ARG | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_sizes)}, "set frame size (WxH or abbreviation)", "size" },
 +    { "aspect", HAS_ARG | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_aspect_ratios)}, "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
 +    { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_pix_fmts)}, "set pixel format", "format" },
 +    { "bits_per_raw_sample", OPT_INT | HAS_ARG | OPT_VIDEO, {(void*)&frame_bits_per_raw_sample}, "set the number of bits per raw sample", "number" },
 +    { "croptop",  HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Removed, use the crop filter instead", "size" },
      { "cropbottom", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Removed, use the crop filter instead", "size" },
      { "cropleft", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Removed, use the crop filter instead", "size" },
      { "cropright", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Removed, use the crop filter instead", "size" },
      { "padright", HAS_ARG | OPT_VIDEO, {(void*)opt_pad}, "Removed, use the pad filter instead", "size" },
      { "padcolor", HAS_ARG | OPT_VIDEO, {(void*)opt_pad}, "Removed, use the pad filter instead", "color" },
      { "intra", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&intra_only}, "use only intra frames"},
 -    { "vn", OPT_BOOL | OPT_VIDEO, {(void*)&video_disable}, "disable video" },
 +    { "vn", OPT_BOOL | OPT_VIDEO | OPT_OFFSET, {.off = OFFSET(video_disable)}, "disable video" },
      { "vdt", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)&video_discard}, "discard threshold", "n" },
 -    { "qscale", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qscale}, "use fixed video quantizer scale (VBR)", "q" },
 -    { "rc_override", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_video_rc_override_string}, "rate control override for specific intervals", "override" },
 -    { "vcodec", HAS_ARG | OPT_VIDEO, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
 -    { "me_threshold", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_me_threshold}, "motion estimaton threshold",  "threshold" },
 -    { "sameq", OPT_BOOL | OPT_VIDEO, {(void*)&same_quality},
 +    { "rc_override", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(rc_overrides)}, "rate control override for specific intervals", "override" },
 +    { "vcodec", HAS_ARG | OPT_VIDEO | OPT_FUNC2, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
 +    { "sameq", OPT_BOOL | OPT_VIDEO, {(void*)&same_quant}, "use same quantizer as source (implies VBR)" },
 +    { "same_quant", OPT_BOOL | OPT_VIDEO, {(void*)&same_quant},
        "use same quantizer as source (implies VBR)" },
      { "pass", HAS_ARG | OPT_VIDEO, {(void*)opt_pass}, "select the pass number (1 or 2)", "n" },
 -    { "passlogfile", HAS_ARG | OPT_STRING | OPT_VIDEO, {(void*)&pass_logfilename_prefix}, "select two pass log file name prefix", "prefix" },
 +    { "passlogfile", HAS_ARG | OPT_VIDEO, {(void*)&opt_passlogfile}, "select two pass log file name prefix", "prefix" },
      { "deinterlace", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_deinterlace},
        "deinterlace pictures" },
      { "psnr", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
      { "vstats", OPT_EXPERT | OPT_VIDEO, {(void*)&opt_vstats}, "dump video coding statistics to file" },
      { "vstats_file", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_vstats_file}, "dump video coding statistics to file", "file" },
  #if CONFIG_AVFILTER
 -    { "vf", OPT_STRING | HAS_ARG, {(void*)&vfilters}, "video filters", "filter list" },
 +    { "vf", HAS_ARG | OPT_VIDEO | OPT_FUNC2, {(void*)opt_video_filters}, "video filters", "filter list" },
  #endif
 -    { "intra_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_intra_matrix}, "specify intra matrix coeffs", "matrix" },
 -    { "inter_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_inter_matrix}, "specify inter matrix coeffs", "matrix" },
 -    { "top", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_top_field_first}, "top=1/bottom=0/auto=-1 field first", "" },
 +    { "intra_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(intra_matrices)}, "specify intra matrix coeffs", "matrix" },
 +    { "inter_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(inter_matrices)}, "specify inter matrix coeffs", "matrix" },
 +    { "top", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_INT| OPT_SPEC, {.off = OFFSET(top_field_first)}, "top=1/bottom=0/auto=-1 field first", "" },
      { "dc", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)&intra_dc_precision}, "intra_dc_precision", "precision" },
 -    { "vtag", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_codec_tag}, "force video tag/fourcc", "fourcc/tag" },
 -    { "newvideo", OPT_VIDEO, {(void*)opt_new_stream}, "add a new video stream to the current output stream" },
 -    { "vlang", HAS_ARG | OPT_STRING | OPT_VIDEO, {(void *)&video_language}, "set the ISO 639 language code (3 letters) of the current video stream" , "code" },
 +    { "vtag", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_FUNC2, {(void*)opt_old2new}, "force video tag/fourcc", "fourcc/tag" },
      { "qphist", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, { (void *)&qp_hist }, "show QP histogram" },
 -    { "force_fps", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&force_fps}, "force the selected framerate, disable the best supported framerate selection" },
 -    { "streamid", HAS_ARG | OPT_EXPERT, {(void*)opt_streamid}, "set the value of an outfile streamid", "streamIndex:value" },
 -    { "force_key_frames", OPT_STRING | HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void *)&forced_key_frames}, "force key frames at specified timestamps", "timestamps" },
 +    { "force_fps", OPT_BOOL | OPT_EXPERT | OPT_VIDEO | OPT_SPEC, {.off = OFFSET(force_fps)}, "force the selected framerate, disable the best supported framerate selection" },
 +    { "streamid", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_streamid}, "set the value of an outfile streamid", "streamIndex:value" },
 +    { "force_key_frames", OPT_STRING | HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_SPEC, {.off = OFFSET(forced_key_frames)}, "force key frames at specified timestamps", "timestamps" },
 +    { "b", HAS_ARG | OPT_VIDEO | OPT_FUNC2, {(void*)opt_bitrate}, "video bitrate (please use -b:v)", "bitrate" },
  
      /* audio options */
 -    { "aframes", OPT_INT | HAS_ARG | OPT_AUDIO, {(void*)&max_frames[AVMEDIA_TYPE_AUDIO]}, "set the number of audio frames to record", "number" },
 -    { "aq", OPT_FLOAT | HAS_ARG | OPT_AUDIO, {(void*)&audio_qscale}, "set audio quality (codec-specific)", "quality", },
 -    { "ar", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
 -    { "ac", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
 -    { "an", OPT_BOOL | OPT_AUDIO, {(void*)&audio_disable}, "disable audio" },
 -    { "acodec", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
 -    { "atag", HAS_ARG | OPT_EXPERT | OPT_AUDIO, {(void*)opt_codec_tag}, "force audio tag/fourcc", "fourcc/tag" },
 +    { "aframes", HAS_ARG | OPT_AUDIO | OPT_FUNC2, {(void*)opt_audio_frames}, "set the number of audio frames to record", "number" },
 +    { "aq", HAS_ARG | OPT_AUDIO | OPT_FUNC2, {(void*)opt_audio_qscale}, "set audio quality (codec-specific)", "quality", },
 +    { "ar", HAS_ARG | OPT_AUDIO | OPT_INT | OPT_SPEC, {.off = OFFSET(audio_sample_rate)}, "set audio sampling rate (in Hz)", "rate" },
 +    { "ac", HAS_ARG | OPT_AUDIO | OPT_INT | OPT_SPEC, {.off = OFFSET(audio_channels)}, "set number of audio channels", "channels" },
 +    { "an", OPT_BOOL | OPT_AUDIO | OPT_OFFSET, {.off = OFFSET(audio_disable)}, "disable audio" },
 +    { "acodec", HAS_ARG | OPT_AUDIO | OPT_FUNC2, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
 +    { "atag", HAS_ARG | OPT_EXPERT | OPT_AUDIO | OPT_FUNC2, {(void*)opt_old2new}, "force audio tag/fourcc", "fourcc/tag" },
      { "vol", OPT_INT | HAS_ARG | OPT_AUDIO, {(void*)&audio_volume}, "change audio volume (256=normal)" , "volume" }, //
 -    { "newaudio", OPT_AUDIO, {(void*)opt_new_stream}, "add a new audio stream to the current output stream" },
 -    { "alang", HAS_ARG | OPT_STRING | OPT_AUDIO, {(void *)&audio_language}, "set the ISO 639 language code (3 letters) of the current audio stream" , "code" },
 -    { "sample_fmt", HAS_ARG | OPT_EXPERT | OPT_AUDIO, {(void*)opt_audio_sample_fmt}, "set sample format, 'list' as argument shows all the sample formats supported", "format" },
 +    { "sample_fmt", HAS_ARG | OPT_EXPERT | OPT_AUDIO | OPT_SPEC | OPT_STRING, {.off = OFFSET(sample_fmts)}, "set sample format", "format" },
 +    { "rmvol", HAS_ARG | OPT_AUDIO | OPT_FLOAT | OPT_SPEC, {.off = OFFSET(rematrix_volume)}, "rematrix volume (as factor)", "volume" },
  
      /* subtitle options */
 -    { "sn", OPT_BOOL | OPT_SUBTITLE, {(void*)&subtitle_disable}, "disable subtitle" },
 -    { "scodec", HAS_ARG | OPT_SUBTITLE, {(void*)opt_subtitle_codec}, "force subtitle codec ('copy' to copy stream)", "codec" },
 -    { "newsubtitle", OPT_SUBTITLE, {(void*)opt_new_stream}, "add a new subtitle stream to the current output stream" },
 -    { "slang", HAS_ARG | OPT_STRING | OPT_SUBTITLE, {(void *)&subtitle_language}, "set the ISO 639 language code (3 letters) of the current subtitle stream" , "code" },
 -    { "stag", HAS_ARG | OPT_EXPERT | OPT_SUBTITLE, {(void*)opt_codec_tag}, "force subtitle tag/fourcc", "fourcc/tag" },
 +    { "sn", OPT_BOOL | OPT_SUBTITLE | OPT_OFFSET, {.off = OFFSET(subtitle_disable)}, "disable subtitle" },
 +    { "scodec", HAS_ARG | OPT_SUBTITLE | OPT_FUNC2, {(void*)opt_subtitle_codec}, "force subtitle codec ('copy' to copy stream)", "codec" },
 +    { "stag", HAS_ARG | OPT_EXPERT | OPT_SUBTITLE | OPT_FUNC2, {(void*)opt_old2new}, "force subtitle tag/fourcc", "fourcc/tag" },
  
      /* grab options */
      { "vc", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_channel}, "deprecated, use -channel", "channel" },
      { "isync", OPT_BOOL | OPT_EXPERT | OPT_GRAB, {(void*)&input_sync}, "sync read on input", "" },
  
      /* muxer options */
 -    { "muxdelay", OPT_FLOAT | HAS_ARG | OPT_EXPERT, {(void*)&mux_max_delay}, "set the maximum demux-decode delay", "seconds" },
 -    { "muxpreload", OPT_FLOAT | HAS_ARG | OPT_EXPERT, {(void*)&mux_preload}, "set the initial demux-decode delay", "seconds" },
 +    { "muxdelay", OPT_FLOAT | HAS_ARG | OPT_EXPERT   | OPT_OFFSET, {.off = OFFSET(mux_max_delay)}, "set the maximum demux-decode delay", "seconds" },
 +    { "muxpreload", OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET, {.off = OFFSET(mux_preload)},   "set the initial demux-decode delay", "seconds" },
  
 -    { "absf", HAS_ARG | OPT_AUDIO | OPT_EXPERT, {(void*)opt_bsf}, "", "bitstream_filter" },
 -    { "vbsf", HAS_ARG | OPT_VIDEO | OPT_EXPERT, {(void*)opt_bsf}, "", "bitstream_filter" },
 -    { "sbsf", HAS_ARG | OPT_SUBTITLE | OPT_EXPERT, {(void*)opt_bsf}, "", "bitstream_filter" },
 +    { "bsf", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(bitstream_filters)}, "A comma-separated list of bitstream filters", "bitstream_filters" },
 +    { "absf", HAS_ARG | OPT_AUDIO | OPT_EXPERT| OPT_FUNC2, {(void*)opt_old2new}, "deprecated", "audio bitstream_filters" },
 +    { "vbsf", HAS_ARG | OPT_VIDEO | OPT_EXPERT| OPT_FUNC2, {(void*)opt_old2new}, "deprecated", "video bitstream_filters" },
  
 -    { "apre", HAS_ARG | OPT_AUDIO | OPT_EXPERT, {(void*)opt_preset}, "set the audio options to the indicated preset", "preset" },
 -    { "vpre", HAS_ARG | OPT_VIDEO | OPT_EXPERT, {(void*)opt_preset}, "set the video options to the indicated preset", "preset" },
 -    { "spre", HAS_ARG | OPT_SUBTITLE | OPT_EXPERT, {(void*)opt_preset}, "set the subtitle options to the indicated preset", "preset" },
 -    { "fpre", HAS_ARG | OPT_EXPERT, {(void*)opt_preset}, "set options from indicated preset file", "filename" },
 +    { "apre", HAS_ARG | OPT_AUDIO | OPT_EXPERT| OPT_FUNC2, {(void*)opt_preset}, "set the audio options to the indicated preset", "preset" },
 +    { "vpre", HAS_ARG | OPT_VIDEO | OPT_EXPERT| OPT_FUNC2, {(void*)opt_preset}, "set the video options to the indicated preset", "preset" },
 +    { "spre", HAS_ARG | OPT_SUBTITLE | OPT_EXPERT| OPT_FUNC2, {(void*)opt_preset}, "set the subtitle options to the indicated preset", "preset" },
 +    { "fpre", HAS_ARG | OPT_EXPERT| OPT_FUNC2, {(void*)opt_preset}, "set options from indicated preset file", "filename" },
      /* data codec support */
 -    { "dcodec", HAS_ARG | OPT_DATA, {(void*)opt_data_codec}, "force data codec ('copy' to copy stream)", "codec" },
 +    { "dcodec", HAS_ARG | OPT_DATA | OPT_FUNC2, {(void*)opt_data_codec}, "force data codec ('copy' to copy stream)", "codec" },
  
      { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
      { NULL, },
  
  int main(int argc, char **argv)
  {
 +    OptionsContext o = { 0 };
      int64_t ti;
  
 +    reset_options(&o, 0);
 +
      av_log_set_flags(AV_LOG_SKIP_REPEATED);
      parse_loglevel(argc, argv, options);
  
 +    if(argc>1 && !strcmp(argv[1], "-d")){
 +        run_as_daemon=1;
 +        av_log_set_callback(log_callback_null);
 +        argc--;
 +        argv++;
 +    }
 +
      avcodec_register_all();
  #if CONFIG_AVDEVICE
      avdevice_register_all();
      av_register_all();
      avformat_network_init();
  
- #if HAVE_ISATTY
-     if(isatty(STDIN_FILENO))
-         avio_set_interrupt_cb(decode_interrupt_cb);
- #endif
-     show_banner();
 -    avio_set_interrupt_cb(decode_interrupt_cb);
  
 -    init_opts();
 -
 -    show_banner();
 -
 -    av_log(NULL, AV_LOG_WARNING, "This program is not developed anymore and is only "
 -                                 "provided for compatibility. Use avconv instead "
 -                                 "(see Changelog for the list of incompatible changes).\n");
 +    term_init();
  
      /* parse options */
 -    parse_options(NULL, argc, argv, options, opt_output_file);
 +    parse_options(&o, argc, argv, options, opt_output_file);
  
      if(nb_output_files <= 0 && nb_input_files == 0) {
          show_usage();
 -        fprintf(stderr, "Use -h to get full help or, even better, run 'man ffmpeg'\n");
 +        av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
          exit_program(1);
      }
  
      /* file converter / grab */
      if (nb_output_files <= 0) {
 -        fprintf(stderr, "At least one output file must be specified\n");
 +        av_log(NULL, AV_LOG_FATAL, "At least one output file must be specified\n");
          exit_program(1);
      }
  
      if (nb_input_files == 0) {
 -        fprintf(stderr, "At least one input file must be specified\n");
 +        av_log(NULL, AV_LOG_FATAL, "At least one input file must be specified\n");
          exit_program(1);
      }
  
      ti = getutime();
 -    if (transcode(output_files, nb_output_files, input_files, nb_input_files,
 -                  stream_maps, nb_stream_maps) < 0)
 +    if (transcode(output_files, nb_output_files, input_files, nb_input_files) < 0)
          exit_program(1);
      ti = getutime() - ti;
      if (do_benchmark) {
diff --combined ffplay.c
index 2ce7ea2047c9a55417a06b052f686848711c1c67,556c463aecd65367230821f57133bd2f9957a0aa..46cf6a12d802229d41e91a501cde9efc4a55da64
+++ b/ffplay.c
@@@ -1,21 -1,21 +1,21 @@@
  /*
 - * avplay : Simple Media Player based on the Libav libraries
 + * ffplay : Simple Media Player based on the FFmpeg libraries
   * Copyright (c) 2003 Fabrice Bellard
   *
 - * 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 "libavutil/dict.h"
  #include "libavutil/parseutils.h"
  #include "libavutil/samplefmt.h"
 +#include "libavutil/avassert.h"
  #include "libavformat/avformat.h"
  #include "libavdevice/avdevice.h"
  #include "libswscale/swscale.h"
  #include "libavcodec/audioconvert.h"
  #include "libavutil/opt.h"
  #include "libavcodec/avfft.h"
 +#include "libswresample/swresample.h"
  
  #if CONFIG_AVFILTER
 +# include "libavfilter/avcodec.h"
  # include "libavfilter/avfilter.h"
  # include "libavfilter/avfiltergraph.h"
 +# include "libavfilter/buffersink.h"
  #endif
  
 -#include "cmdutils.h"
 -
  #include <SDL.h>
  #include <SDL_thread.h>
  
 -#ifdef __MINGW32__
 -#undef main /* We don't want SDL to override our main() */
 -#endif
 +#include "cmdutils.h"
  
  #include <unistd.h>
  #include <assert.h>
  
 -const char program_name[] = "avplay";
 +const char program_name[] = "ffplay";
  const int program_birth_year = 2003;
  
  #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
@@@ -71,6 -71,8 +71,6 @@@
  /* no AV correction is done if too big error */
  #define AV_NOSYNC_THRESHOLD 10.0
  
 -#define FRAME_SKIP_FACTOR 0.05
 -
  /* maximum audio speed change to get correct sync */
  #define SAMPLE_CORRECTION_PERCENT_MAX 10
  
@@@ -96,9 -98,8 +96,9 @@@ typedef struct PacketQueue 
  
  typedef struct VideoPicture {
      double pts;                                  ///<presentation time stamp for this picture
 -    double target_clock;                         ///<av_gettime() time at which this should be displayed ideally
 +    double duration;                             ///<expected duration of the frame
      int64_t pos;                                 ///<byte position in file
 +    int skip;
      SDL_Overlay *bmp;
      int width, height; /* source height & width */
      int allocated;
@@@ -122,7 -123,7 +122,7 @@@ enum 
  };
  
  typedef struct VideoState {
 -    SDL_Thread *parse_tid;
 +    SDL_Thread *read_tid;
      SDL_Thread *video_tid;
      SDL_Thread *refresh_tid;
      AVInputFormat *iformat;
      int64_t seek_rel;
      int read_pause_return;
      AVFormatContext *ic;
 -    int dtg_active_format;
  
      int audio_stream;
  
      PacketQueue audioq;
      int audio_hw_buf_size;
      /* samples output by the codec. we reserve more space for avsync
 -       compensation */
 -    DECLARE_ALIGNED(16,uint8_t,audio_buf1)[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
 -    DECLARE_ALIGNED(16,uint8_t,audio_buf2)[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
 +       compensation, resampling and format conversion */
 +    DECLARE_ALIGNED(16,uint8_t,audio_buf1)[AVCODEC_MAX_AUDIO_FRAME_SIZE * 4];
 +    DECLARE_ALIGNED(16,uint8_t,audio_buf2)[AVCODEC_MAX_AUDIO_FRAME_SIZE * 4];
      uint8_t *audio_buf;
      unsigned int audio_buf_size; /* in bytes */
      int audio_buf_index; /* in bytes */
 +    int audio_write_buf_size;
      AVPacket audio_pkt_temp;
      AVPacket audio_pkt;
      enum AVSampleFormat audio_src_fmt;
 -    AVAudioConvert *reformat_ctx;
 -
 -    int show_audio; /* if true, display audio samples */
 +    enum AVSampleFormat audio_tgt_fmt;
 +    int audio_src_channels;
 +    int audio_tgt_channels;
 +    int64_t audio_src_channel_layout;
 +    int64_t audio_tgt_channel_layout;
 +    int audio_src_freq;
 +    int audio_tgt_freq;
 +    struct SwrContext *swr_ctx;
 +    double audio_current_pts;
 +    double audio_current_pts_drift;
 +    int frame_drops_early;
 +    int frame_drops_late;
 +
 +    enum ShowMode {
 +        SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
 +    } show_mode;
      int16_t sample_array[SAMPLE_ARRAY_SIZE];
      int sample_array_index;
      int last_i_start;
  
      double frame_timer;
      double frame_last_pts;
 -    double frame_last_delay;
 +    double frame_last_duration;
 +    double frame_last_dropped_pts;
 +    double frame_last_returned_time;
 +    double frame_last_filter_delay;
 +    int64_t frame_last_dropped_pos;
      double video_clock;                          ///<pts of last decoded frame / predicted pts of next decoded frame
      int video_stream;
      AVStream *video_st;
      struct SwsContext *img_convert_ctx;
  #endif
  
 -    //    QETimer *video_timer;
      char filename[1024];
      int width, height, xleft, ytop;
 -
 -    PtsCorrectionContext pts_ctx;
 +    int step;
  
  #if CONFIG_AVFILTER
      AVFilterContext *out_video_filter;          ///<the last filter in the video chain
  #endif
  
 -    float skip_frames;
 -    float skip_frames_index;
      int refresh;
  } VideoState;
  
 -static void show_help(void);
 +static int opt_help(const char *opt, const char *arg);
  
  /* options specified by the user */
  static AVInputFormat *file_iformat;
@@@ -252,6 -240,10 +252,6 @@@ static int show_status = 1
  static int av_sync_type = AV_SYNC_AUDIO_MASTER;
  static int64_t start_time = AV_NOPTS_VALUE;
  static int64_t duration = AV_NOPTS_VALUE;
 -static int debug = 0;
 -static int debug_mv = 0;
 -static int step = 0;
 -static int thread_count = 1;
  static int workaround_bugs = 1;
  static int fast = 0;
  static int genpts = 0;
@@@ -267,11 -259,7 +267,11 @@@ static int autoexit
  static int exit_on_keydown;
  static int exit_on_mousedown;
  static int loop=1;
 -static int framedrop=1;
 +static int framedrop=-1;
 +static enum ShowMode show_mode = SHOW_MODE_NONE;
 +static const char *audio_codec_name;
 +static const char *subtitle_codec_name;
 +static const char *video_codec_name;
  
  static int rdftspeed=20;
  #if CONFIG_AVFILTER
@@@ -280,6 -268,7 +280,6 @@@ static char *vfilters = NULL
  
  /* current context */
  static int is_full_screen;
 -static VideoState *cur_stream;
  static int64_t audio_callback_time;
  
  static AVPacket flush_pkt;
  
  static SDL_Surface *screen;
  
 -void exit_program(int ret)
 +void av_noreturn exit_program(int ret)
  {
      exit(ret);
  }
  
 -static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
 +static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
 +{
 +    AVPacketList *pkt1;
 +
 +    /* duplicate the packet */
 +    if (pkt!=&flush_pkt && av_dup_packet(pkt) < 0)
 +        return -1;
 +
 +    pkt1 = av_malloc(sizeof(AVPacketList));
 +    if (!pkt1)
 +        return -1;
 +    pkt1->pkt = *pkt;
 +    pkt1->next = NULL;
 +
 +
 +    SDL_LockMutex(q->mutex);
 +
 +    if (!q->last_pkt)
 +
 +        q->first_pkt = pkt1;
 +    else
 +        q->last_pkt->next = pkt1;
 +    q->last_pkt = pkt1;
 +    q->nb_packets++;
 +    q->size += pkt1->pkt.size + sizeof(*pkt1);
 +    /* XXX: should duplicate packet data in DV case */
 +    SDL_CondSignal(q->cond);
 +
 +    SDL_UnlockMutex(q->mutex);
 +    return 0;
 +}
  
  /* packet queue handling */
  static void packet_queue_init(PacketQueue *q)
@@@ -360,6 -319,38 +360,6 @@@ static void packet_queue_end(PacketQueu
      SDL_DestroyCond(q->cond);
  }
  
 -static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
 -{
 -    AVPacketList *pkt1;
 -
 -    /* duplicate the packet */
 -    if (pkt!=&flush_pkt && av_dup_packet(pkt) < 0)
 -        return -1;
 -
 -    pkt1 = av_malloc(sizeof(AVPacketList));
 -    if (!pkt1)
 -        return -1;
 -    pkt1->pkt = *pkt;
 -    pkt1->next = NULL;
 -
 -
 -    SDL_LockMutex(q->mutex);
 -
 -    if (!q->last_pkt)
 -
 -        q->first_pkt = pkt1;
 -    else
 -        q->last_pkt->next = pkt1;
 -    q->last_pkt = pkt1;
 -    q->nb_packets++;
 -    q->size += pkt1->pkt.size + sizeof(*pkt1);
 -    /* XXX: should duplicate packet data in DV case */
 -    SDL_CondSignal(q->cond);
 -
 -    SDL_UnlockMutex(q->mutex);
 -    return 0;
 -}
 -
  static void packet_queue_abort(PacketQueue *q)
  {
      SDL_LockMutex(q->mutex);
@@@ -665,10 -656,10 +665,10 @@@ static void video_image_display(VideoSt
      vp = &is->pictq[is->pictq_rindex];
      if (vp->bmp) {
  #if CONFIG_AVFILTER
 -         if (vp->picref->video->pixel_aspect.num == 0)
 +         if (vp->picref->video->sample_aspect_ratio.num == 0)
               aspect_ratio = 0;
           else
 -             aspect_ratio = av_q2d(vp->picref->video->pixel_aspect);
 +             aspect_ratio = av_q2d(vp->picref->video->sample_aspect_ratio);
  #else
  
          /* XXX: use variable in the frame */
              aspect_ratio = 1.0;
          aspect_ratio *= (float)vp->width / (float)vp->height;
  
 -        if (is->subtitle_st)
 -        {
 -            if (is->subpq_size > 0)
 -            {
 +        if (is->subtitle_st) {
 +            if (is->subpq_size > 0) {
                  sp = &is->subpq[is->subpq_rindex];
  
 -                if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000))
 -                {
 +                if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
                      SDL_LockYUVOverlay (vp->bmp);
  
                      pict.data[0] = vp->bmp->pixels[0];
          is->no_background = 0;
          rect.x = is->xleft + x;
          rect.y = is->ytop  + y;
 -        rect.w = width;
 -        rect.h = height;
 +        rect.w = FFMAX(width,  1);
 +        rect.h = FFMAX(height, 1);
          SDL_DisplayYUVOverlay(vp->bmp, &rect);
      }
  }
  
 -/* get the current audio output buffer size, in samples. With SDL, we
 -   cannot have a precise information */
 -static int audio_write_get_buf_size(VideoState *is)
 -{
 -    return is->audio_buf_size - is->audio_buf_index;
 -}
 -
  static inline int compute_mod(int a, int b)
  {
 -    a = a % b;
 -    if (a >= 0)
 -        return a;
 -    else
 -        return a + b;
 +    return a < 0 ? a%b + b : a%b;
  }
  
  static void video_audio_display(VideoState *s)
      nb_freq= 1<<(rdft_bits-1);
  
      /* compute display index : center on currently output samples */
 -    channels = s->audio_st->codec->channels;
 +    channels = s->audio_tgt_channels;
      nb_display_channels = channels;
      if (!s->paused) {
 -        int data_used= s->show_audio==1 ? s->width : (2*nb_freq);
 +        int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
          n = 2 * channels;
 -        delay = audio_write_get_buf_size(s);
 +        delay = s->audio_write_buf_size;
          delay /= n;
  
          /* to be more precise, we take into account the time spent since
             the last buffer computation */
          if (audio_callback_time) {
              time_diff = av_gettime() - audio_callback_time;
 -            delay -= (time_diff * s->audio_st->codec->sample_rate) / 1000000;
 +            delay -= (time_diff * s->audio_tgt_freq) / 1000000;
          }
  
          delay += 2*data_used;
              delay = data_used;
  
          i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
 -        if(s->show_audio==1){
 +        if (s->show_mode == SHOW_MODE_WAVES) {
              h= INT_MIN;
              for(i=0; i<1000; i+=channels){
                  int idx= (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
      }
  
      bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
 -    if(s->show_audio==1){
 +    if (s->show_mode == SHOW_MODE_WAVES) {
          fill_rectangle(screen,
                         s->xleft, s->ytop, s->width, s->height,
                         bgcolor);
      }
  }
  
 +static void stream_close(VideoState *is)
 +{
 +    VideoPicture *vp;
 +    int i;
 +    /* XXX: use a special url_shutdown call to abort parse cleanly */
 +    is->abort_request = 1;
 +    SDL_WaitThread(is->read_tid, NULL);
 +    SDL_WaitThread(is->refresh_tid, NULL);
 +
 +    /* free all pictures */
 +    for(i=0;i<VIDEO_PICTURE_QUEUE_SIZE; i++) {
 +        vp = &is->pictq[i];
 +#if CONFIG_AVFILTER
 +        if (vp->picref) {
 +            avfilter_unref_buffer(vp->picref);
 +            vp->picref = NULL;
 +        }
 +#endif
 +        if (vp->bmp) {
 +            SDL_FreeYUVOverlay(vp->bmp);
 +            vp->bmp = NULL;
 +        }
 +    }
 +    SDL_DestroyMutex(is->pictq_mutex);
 +    SDL_DestroyCond(is->pictq_cond);
 +    SDL_DestroyMutex(is->subpq_mutex);
 +    SDL_DestroyCond(is->subpq_cond);
 +#if !CONFIG_AVFILTER
 +    if (is->img_convert_ctx)
 +        sws_freeContext(is->img_convert_ctx);
 +#endif
 +    av_free(is);
 +}
 +
 +static void do_exit(VideoState *is)
 +{
 +    if (is) {
 +        stream_close(is);
 +    }
 +    av_lockmgr_register(NULL);
 +    uninit_opts();
 +#if CONFIG_AVFILTER
 +    avfilter_uninit();
 +#endif
 +    avformat_network_deinit();
 +    if (show_status)
 +        printf("\n");
 +    SDL_Quit();
 +    av_log(NULL, AV_LOG_QUIET, "%s", "");
 +    exit(0);
 +}
 +
  static int video_open(VideoState *is){
      int flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
      int w,h;
      if(screen && is->width == screen->w && screen->w == w
         && is->height== screen->h && screen->h == h)
          return 0;
 -
 -#if defined(__APPLE__) && !SDL_VERSION_ATLEAST(1, 2, 14)
 -    /* setting bits_per_pixel = 0 or 32 causes blank video on OS X and older SDL */
 -    screen = SDL_SetVideoMode(w, h, 24, flags);
 -#else
      screen = SDL_SetVideoMode(w, h, 0, flags);
 -#endif
      if (!screen) {
          fprintf(stderr, "SDL: could not set video mode - exiting\n");
 -        return -1;
 +        do_exit(is);
      }
      if (!window_title)
          window_title = input_filename;
  static void video_display(VideoState *is)
  {
      if(!screen)
 -        video_open(cur_stream);
 -    if (is->audio_st && is->show_audio)
 +        video_open(is);
 +    if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
          video_audio_display(is);
      else if (is->video_st)
          video_image_display(is);
@@@ -988,8 -947,7 +988,8 @@@ static int refresh_thread(void *opaque
              is->refresh=1;
              SDL_PushEvent(&event);
          }
 -        usleep(is->audio_st && is->show_audio ? rdftspeed*1000 : 5000); //FIXME ideally we should wait the correct time but SDLs event passing is so slow it would be silly
 +        //FIXME ideally we should wait the correct time but SDLs event passing is so slow it would be silly
 +        usleep(is->audio_st && is->show_mode != SHOW_MODE_VIDEO ? rdftspeed*1000 : 5000);
      }
      return 0;
  }
  /* get the current audio clock value */
  static double get_audio_clock(VideoState *is)
  {
 -    double pts;
 -    int hw_buf_size, bytes_per_sec;
 -    pts = is->audio_clock;
 -    hw_buf_size = audio_write_get_buf_size(is);
 -    bytes_per_sec = 0;
 -    if (is->audio_st) {
 -        bytes_per_sec = is->audio_st->codec->sample_rate *
 -            2 * is->audio_st->codec->channels;
 +    if (is->paused) {
 +        return is->audio_current_pts;
 +    } else {
 +        return is->audio_current_pts_drift + av_gettime() / 1000000.0;
      }
 -    if (bytes_per_sec)
 -        pts -= (double)hw_buf_size / bytes_per_sec;
 -    return pts;
  }
  
  /* get the current video clock value */
@@@ -1057,7 -1022,7 +1057,7 @@@ static void stream_seek(VideoState *is
  }
  
  /* pause or resume the video */
 -static void stream_pause(VideoState *is)
 +static void stream_toggle_pause(VideoState *is)
  {
      if (is->paused) {
          is->frame_timer += av_gettime() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
      is->paused = !is->paused;
  }
  
 -static double compute_target_time(double frame_current_pts, VideoState *is)
 +static double compute_target_delay(double delay, VideoState *is)
  {
 -    double delay, sync_threshold, diff;
 -
 -    /* compute nominal delay */
 -    delay = frame_current_pts - is->frame_last_pts;
 -    if (delay <= 0 || delay >= 10.0) {
 -        /* if incorrect delay, use previous one */
 -        delay = is->frame_last_delay;
 -    } else {
 -        is->frame_last_delay = delay;
 -    }
 -    is->frame_last_pts = frame_current_pts;
 +    double sync_threshold, diff;
  
      /* update delay to follow master synchronisation source */
      if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) ||
                  delay = 2 * delay;
          }
      }
 -    is->frame_timer += delay;
  
 -    av_dlog(NULL, "video: delay=%0.3f pts=%0.3f A-V=%f\n",
 -            delay, frame_current_pts, -diff);
 +    av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
 +            delay, -diff);
  
 -    return is->frame_timer;
 +    return delay;
 +}
 +
 +static void pictq_next_picture(VideoState *is) {
 +    /* update queue size and signal for next picture */
 +    if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
 +        is->pictq_rindex = 0;
 +
 +    SDL_LockMutex(is->pictq_mutex);
 +    is->pictq_size--;
 +    SDL_CondSignal(is->pictq_cond);
 +    SDL_UnlockMutex(is->pictq_mutex);
 +}
 +
 +static void update_video_pts(VideoState *is, double pts, int64_t pos) {
 +    double time = av_gettime() / 1000000.0;
 +    /* update current video pts */
 +    is->video_current_pts = pts;
 +    is->video_current_pts_drift = is->video_current_pts - time;
 +    is->video_current_pos = pos;
 +    is->frame_last_pts = pts;
  }
  
  /* called to display each frame */
 -static void video_refresh_timer(void *opaque)
 +static void video_refresh(void *opaque)
  {
      VideoState *is = opaque;
      VideoPicture *vp;
 +    double time;
  
      SubPicture *sp, *sp2;
  
      if (is->video_st) {
  retry:
          if (is->pictq_size == 0) {
 +            SDL_LockMutex(is->pictq_mutex);
 +            if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
 +                update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos);
 +                is->frame_last_dropped_pts = AV_NOPTS_VALUE;
 +            }
 +            SDL_UnlockMutex(is->pictq_mutex);
              //nothing to do, no picture to display in the que
          } else {
 -            double time= av_gettime()/1000000.0;
 -            double next_target;
 +            double last_duration, duration, delay;
              /* dequeue the picture */
              vp = &is->pictq[is->pictq_rindex];
  
 -            if(time < vp->target_clock)
 +            if (vp->skip) {
 +                pictq_next_picture(is);
 +                goto retry;
 +            }
 +
 +            /* compute nominal last_duration */
 +            last_duration = vp->pts - is->frame_last_pts;
 +            if (last_duration > 0 && last_duration < 10.0) {
 +                /* if duration of the last frame was sane, update last_duration in video state */
 +                is->frame_last_duration = last_duration;
 +            }
 +            delay = compute_target_delay(is->frame_last_duration, is);
 +
 +            time= av_gettime()/1000000.0;
 +            if(time < is->frame_timer + delay)
                  return;
 -            /* update current video pts */
 -            is->video_current_pts = vp->pts;
 -            is->video_current_pts_drift = is->video_current_pts - time;
 -            is->video_current_pos = vp->pos;
 -            if(is->pictq_size > 1){
 -                VideoPicture *nextvp= &is->pictq[(is->pictq_rindex+1)%VIDEO_PICTURE_QUEUE_SIZE];
 -                assert(nextvp->target_clock >= vp->target_clock);
 -                next_target= nextvp->target_clock;
 -            }else{
 -                next_target= vp->target_clock + is->video_clock - vp->pts; //FIXME pass durations cleanly
 +
 +            if (delay > 0)
 +                is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
 +
 +            SDL_LockMutex(is->pictq_mutex);
 +            update_video_pts(is, vp->pts, vp->pos);
 +            SDL_UnlockMutex(is->pictq_mutex);
 +
 +            if(is->pictq_size > 1) {
 +                 VideoPicture *nextvp= &is->pictq[(is->pictq_rindex+1)%VIDEO_PICTURE_QUEUE_SIZE];
 +                 duration = nextvp->pts - vp->pts; // More accurate this way, 1/time_base is often not reflecting FPS
 +            } else {
 +                 duration = vp->duration;
              }
 -            if(framedrop && time > next_target){
 -                is->skip_frames *= 1.0 + FRAME_SKIP_FACTOR;
 -                if(is->pictq_size > 1 || time > next_target + 0.5){
 -                    /* update queue size and signal for next picture */
 -                    if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
 -                        is->pictq_rindex = 0;
 -
 -                    SDL_LockMutex(is->pictq_mutex);
 -                    is->pictq_size--;
 -                    SDL_CondSignal(is->pictq_cond);
 -                    SDL_UnlockMutex(is->pictq_mutex);
 +
 +            if((framedrop>0 || (framedrop && is->audio_st)) && time > is->frame_timer + duration){
 +                if(is->pictq_size > 1){
 +                    is->frame_drops_late++;
 +                    pictq_next_picture(is);
                      goto retry;
                  }
              }
              if (!display_disable)
                  video_display(is);
  
 -            /* update queue size and signal for next picture */
 -            if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
 -                is->pictq_rindex = 0;
 -
 -            SDL_LockMutex(is->pictq_mutex);
 -            is->pictq_size--;
 -            SDL_CondSignal(is->pictq_cond);
 -            SDL_UnlockMutex(is->pictq_mutex);
 +            pictq_next_picture(is);
          }
      } else if (is->audio_st) {
          /* draw the next audio frame */
              av_diff = 0;
              if (is->audio_st && is->video_st)
                  av_diff = get_audio_clock(is) - get_video_clock(is);
 -            printf("%7.2f A-V:%7.3f s:%3.1f aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64"   \r",
 -                   get_master_clock(is), av_diff, FFMAX(is->skip_frames-1, 0), aqsize / 1024, vqsize / 1024, sqsize, is->pts_ctx.num_faulty_dts, is->pts_ctx.num_faulty_pts);
 +            printf("%7.2f A-V:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64"   \r",
 +                   get_master_clock(is),
 +                   av_diff,
 +                   is->frame_drops_early + is->frame_drops_late,
 +                   aqsize / 1024,
 +                   vqsize / 1024,
 +                   sqsize,
 +                   is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
 +                   is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
              fflush(stdout);
              last_time = cur_time;
          }
      }
  }
  
 -static void stream_close(VideoState *is)
 -{
 -    VideoPicture *vp;
 -    int i;
 -    /* XXX: use a special url_shutdown call to abort parse cleanly */
 -    is->abort_request = 1;
 -    SDL_WaitThread(is->parse_tid, NULL);
 -    SDL_WaitThread(is->refresh_tid, NULL);
 -
 -    /* free all pictures */
 -    for(i=0;i<VIDEO_PICTURE_QUEUE_SIZE; i++) {
 -        vp = &is->pictq[i];
 -#if CONFIG_AVFILTER
 -        if (vp->picref) {
 -            avfilter_unref_buffer(vp->picref);
 -            vp->picref = NULL;
 -        }
 -#endif
 -        if (vp->bmp) {
 -            SDL_FreeYUVOverlay(vp->bmp);
 -            vp->bmp = NULL;
 -        }
 -    }
 -    SDL_DestroyMutex(is->pictq_mutex);
 -    SDL_DestroyCond(is->pictq_cond);
 -    SDL_DestroyMutex(is->subpq_mutex);
 -    SDL_DestroyCond(is->subpq_cond);
 -#if !CONFIG_AVFILTER
 -    if (is->img_convert_ctx)
 -        sws_freeContext(is->img_convert_ctx);
 -#endif
 -    av_free(is);
 -}
 -
 -static void do_exit(void)
 -{
 -    if (cur_stream) {
 -        stream_close(cur_stream);
 -        cur_stream = NULL;
 -    }
 -    uninit_opts();
 -#if CONFIG_AVFILTER
 -    avfilter_uninit();
 -#endif
 -    avformat_network_deinit();
 -    if (show_status)
 -        printf("\n");
 -    SDL_Quit();
 -    av_log(NULL, AV_LOG_QUIET, "");
 -    exit(0);
 -}
 -
  /* allocate a picture (needs to do that in main thread to avoid
     potential locking problems */
  static void alloc_picture(void *opaque)
          fprintf(stderr, "Error: the video system does not support an image\n"
                          "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
                          "to reduce the image size.\n", vp->width, vp->height );
 -        do_exit();
 +        do_exit(is);
      }
  
      SDL_LockMutex(is->pictq_mutex);
      SDL_UnlockMutex(is->pictq_mutex);
  }
  
 -/**
 - *
 - * @param pts the dts of the pkt / pts of the frame and guessed if not known
 - */
 -static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t pos)
 +static int queue_picture(VideoState *is, AVFrame *src_frame, double pts1, int64_t pos)
  {
      VideoPicture *vp;
 -#if CONFIG_AVFILTER
 -    AVPicture pict_src;
 -#else
 -    int dst_pix_fmt = PIX_FMT_YUV420P;
 +    double frame_delay, pts = pts1;
 +
 +    /* compute the exact PTS for the picture if it is omitted in the stream
 +     * pts1 is the dts of the pkt / pts of the frame */
 +    if (pts != 0) {
 +        /* update video clock with pts, if present */
 +        is->video_clock = pts;
 +    } else {
 +        pts = is->video_clock;
 +    }
 +    /* update video clock for next frame */
 +    frame_delay = av_q2d(is->video_st->codec->time_base);
 +    /* for MPEG2, the frame can be repeated, so we update the
 +       clock accordingly */
 +    frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
 +    is->video_clock += frame_delay;
 +
 +#if defined(DEBUG_SYNC) && 0
 +    printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
 +           av_get_picture_type_char(src_frame->pict_type), pts, pts1);
  #endif
 +
      /* wait until we have space to put a new picture */
      SDL_LockMutex(is->pictq_mutex);
  
 -    if(is->pictq_size>=VIDEO_PICTURE_QUEUE_SIZE && !is->refresh)
 -        is->skip_frames= FFMAX(1.0 - FRAME_SKIP_FACTOR, is->skip_frames * (1.0-FRAME_SKIP_FACTOR));
 -
      while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
             !is->videoq.abort_request) {
          SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  
      vp = &is->pictq[is->pictq_windex];
  
 +    vp->duration = frame_delay;
 +
      /* alloc or resize hardware picture buffer */
      if (!vp->bmp || vp->reallocate ||
  #if CONFIG_AVFILTER
          while (!vp->allocated && !is->videoq.abort_request) {
              SDL_CondWait(is->pictq_cond, is->pictq_mutex);
          }
 +        /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
 +        if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
 +            while (!vp->allocated) {
 +                SDL_CondWait(is->pictq_cond, is->pictq_mutex);
 +            }
 +        }
          SDL_UnlockMutex(is->pictq_mutex);
  
          if (is->videoq.abort_request)
          pict.linesize[2] = vp->bmp->pitches[1];
  
  #if CONFIG_AVFILTER
 -        pict_src.data[0] = src_frame->data[0];
 -        pict_src.data[1] = src_frame->data[1];
 -        pict_src.data[2] = src_frame->data[2];
 -
 -        pict_src.linesize[0] = src_frame->linesize[0];
 -        pict_src.linesize[1] = src_frame->linesize[1];
 -        pict_src.linesize[2] = src_frame->linesize[2];
 -
          //FIXME use direct rendering
 -        av_picture_copy(&pict, &pict_src,
 +        av_picture_copy(&pict, (AVPicture *)src_frame,
                          vp->pix_fmt, vp->width, vp->height);
  #else
          sws_flags = av_get_int(sws_opts, "sws_flags", NULL);
          is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
              vp->width, vp->height, vp->pix_fmt, vp->width, vp->height,
 -            dst_pix_fmt, sws_flags, NULL, NULL, NULL);
 +            PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
          if (is->img_convert_ctx == NULL) {
              fprintf(stderr, "Cannot initialize the conversion context\n");
              exit(1);
  
          vp->pts = pts;
          vp->pos = pos;
 +        vp->skip = 0;
  
          /* now we can update the picture count */
          if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
              is->pictq_windex = 0;
          SDL_LockMutex(is->pictq_mutex);
 -        vp->target_clock= compute_target_time(vp->pts, is);
 -
          is->pictq_size++;
          SDL_UnlockMutex(is->pictq_mutex);
      }
      return 0;
  }
  
 -/**
 - * compute the exact PTS for the picture if it is omitted in the stream
 - * @param pts1 the dts of the pkt / pts of the frame
 - */
 -static int output_picture2(VideoState *is, AVFrame *src_frame, double pts1, int64_t pos)
 -{
 -    double frame_delay, pts;
 -
 -    pts = pts1;
 -
 -    if (pts != 0) {
 -        /* update video clock with pts, if present */
 -        is->video_clock = pts;
 -    } else {
 -        pts = is->video_clock;
 -    }
 -    /* update video clock for next frame */
 -    frame_delay = av_q2d(is->video_st->codec->time_base);
 -    /* for MPEG2, the frame can be repeated, so we update the
 -       clock accordingly */
 -    frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
 -    is->video_clock += frame_delay;
 -
 -    return queue_picture(is, src_frame, pts, pos);
 -}
 -
  static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt)
  {
      int got_picture, i;
          SDL_LockMutex(is->pictq_mutex);
          //Make sure there are no long delay timers (ideally we should just flush the que but thats harder)
          for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
 -            is->pictq[i].target_clock= 0;
 +            is->pictq[i].skip = 1;
          }
          while (is->pictq_size && !is->videoq.abort_request) {
              SDL_CondWait(is->pictq_cond, is->pictq_mutex);
          }
          is->video_current_pos = -1;
 -        SDL_UnlockMutex(is->pictq_mutex);
 -
 -        init_pts_correction(&is->pts_ctx);
          is->frame_last_pts = AV_NOPTS_VALUE;
 -        is->frame_last_delay = 0;
 +        is->frame_last_duration = 0;
          is->frame_timer = (double)av_gettime() / 1000000.0;
 -        is->skip_frames = 1;
 -        is->skip_frames_index = 0;
 +        is->frame_last_dropped_pts = AV_NOPTS_VALUE;
 +        SDL_UnlockMutex(is->pictq_mutex);
 +
          return 0;
      }
  
      avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt);
  
      if (got_picture) {
 +        int ret = 1;
 +
          if (decoder_reorder_pts == -1) {
 -            *pts = guess_correct_pts(&is->pts_ctx, frame->pkt_pts, frame->pkt_dts);
 +            *pts = frame->best_effort_timestamp;
          } else if (decoder_reorder_pts) {
              *pts = frame->pkt_pts;
          } else {
              *pts = 0;
          }
  
 -        is->skip_frames_index += 1;
 -        if(is->skip_frames_index >= is->skip_frames){
 -            is->skip_frames_index -= FFMAX(is->skip_frames, 1.0);
 -            return 1;
 +        if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) || is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK) &&
 +             (framedrop>0 || (framedrop && is->audio_st))) {
 +            SDL_LockMutex(is->pictq_mutex);
 +            if (is->frame_last_pts != AV_NOPTS_VALUE && *pts) {
 +                double clockdiff = get_video_clock(is) - get_master_clock(is);
 +                double dpts = av_q2d(is->video_st->time_base) * *pts;
 +                double ptsdiff = dpts - is->frame_last_pts;
 +                if (fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
 +                     ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
 +                     clockdiff + ptsdiff - is->frame_last_filter_delay < 0) {
 +                    is->frame_last_dropped_pos = pkt->pos;
 +                    is->frame_last_dropped_pts = dpts;
 +                    is->frame_drops_early++;
 +                    ret = 0;
 +                }
 +            }
 +            SDL_UnlockMutex(is->pictq_mutex);
          }
  
 +        if (ret)
 +            is->frame_last_returned_time = av_gettime() / 1000000.0;
 +
 +        return ret;
      }
      return 0;
  }
@@@ -1540,8 -1531,6 +1540,8 @@@ static int input_get_buffer(AVCodecCont
      unsigned edge;
      int pixel_size;
  
 +    av_assert0(codec->flags & CODEC_FLAG_EMU_EDGE);
 +
      if (codec->codec->capabilities & CODEC_CAP_NEG_LINESIZES)
          perms |= AV_PERM_NEG_LINESIZES;
  
  
      w = codec->width;
      h = codec->height;
 +
 +    if(av_image_check_size(w, h, 0, codec))
 +        return -1;
 +
      avcodec_align_dimensions2(codec, &w, &h, stride);
      edge = codec->flags & CODEC_FLAG_EMU_EDGE ? 0 : avcodec_get_edge_width();
      w += edge << 1;
@@@ -1624,9 -1609,7 +1624,9 @@@ static int input_init(AVFilterContext *
      priv->is = opaque;
      codec    = priv->is->video_st->codec;
      codec->opaque = ctx;
 -    if(codec->codec->capabilities & CODEC_CAP_DR1) {
 +    if((codec->codec->capabilities & CODEC_CAP_DR1)
 +    ) {
 +        av_assert0(codec->flags & CODEC_FLAG_EMU_EDGE);
          priv->use_dr1 = 1;
          codec->get_buffer     = input_get_buffer;
          codec->release_buffer = input_release_buffer;
@@@ -1658,7 -1641,7 +1658,7 @@@ static int input_request_frame(AVFilter
      if (ret < 0)
          return -1;
  
 -    if(priv->use_dr1) {
 +    if(priv->use_dr1 && priv->frame->opaque) {
          picref = avfilter_ref_buffer(priv->frame->opaque, ~0);
      } else {
          picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, link->w, link->h);
      }
      av_free_packet(&pkt);
  
 +    avfilter_copy_frame_props(picref, priv->frame);
      picref->pts = pts;
 -    picref->pos = pkt.pos;
 -    picref->video->pixel_aspect = priv->is->video_st->codec->sample_aspect_ratio;
 +
      avfilter_start_frame(link, picref);
      avfilter_draw_slice(link, 0, link->h, 1);
      avfilter_end_frame(link);
@@@ -1685,27 -1668,25 +1685,27 @@@ static int input_query_formats(AVFilter
          priv->is->video_st->codec->pix_fmt, PIX_FMT_NONE
      };
  
 -    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
 +    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
      return 0;
  }
  
  static int input_config_props(AVFilterLink *link)
  {
      FilterPriv *priv  = link->src->priv;
 -    AVCodecContext *c = priv->is->video_st->codec;
 +    AVStream *s = priv->is->video_st;
  
 -    link->w = c->width;
 -    link->h = c->height;
 -    link->time_base = priv->is->video_st->time_base;
 +    link->w = s->codec->width;
 +    link->h = s->codec->height;
 +    link->sample_aspect_ratio = s->sample_aspect_ratio.num ?
 +        s->sample_aspect_ratio : s->codec->sample_aspect_ratio;
 +    link->time_base = s->time_base;
  
      return 0;
  }
  
  static AVFilter input_filter =
  {
 -    .name      = "avplay_input",
 +    .name      = "ffplay_input",
  
      .priv_size = sizeof(FilterPriv),
  
@@@ -1726,8 -1707,7 +1726,8 @@@ static int configure_video_filters(AVFi
  {
      char sws_flags_str[128];
      int ret;
 -    FFSinkContext ffsink_ctx = { .pix_fmt = PIX_FMT_YUV420P };
 +    enum PixelFormat pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
 +    AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
      AVFilterContext *filt_src = NULL, *filt_out = NULL;
      snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%d", sws_flags);
      graph->scale_sws_opts = av_strdup(sws_flags_str);
      if ((ret = avfilter_graph_create_filter(&filt_src, &input_filter, "src",
                                              NULL, is, graph)) < 0)
          return ret;
 -    if ((ret = avfilter_graph_create_filter(&filt_out, &ffsink, "out",
 -                                            NULL, &ffsink_ctx, graph)) < 0)
 +#if FF_API_OLD_VSINK_API
 +    ret = avfilter_graph_create_filter(&filt_out, avfilter_get_by_name("buffersink"), "out",
 +                                       NULL, pix_fmts, graph);
 +#else
 +    buffersink_params->pixel_fmts = pix_fmts;
 +    ret = avfilter_graph_create_filter(&filt_out, avfilter_get_by_name("buffersink"), "out",
 +                                       NULL, buffersink_params, graph);
 +#endif
 +    av_freep(&buffersink_params);
 +    if (ret < 0)
          return ret;
  
      if(vfilters) {
 -        AVFilterInOut *outputs = av_malloc(sizeof(AVFilterInOut));
 -        AVFilterInOut *inputs  = av_malloc(sizeof(AVFilterInOut));
 +        AVFilterInOut *outputs = avfilter_inout_alloc();
 +        AVFilterInOut *inputs  = avfilter_inout_alloc();
  
          outputs->name    = av_strdup("in");
          outputs->filter_ctx = filt_src;
          inputs->pad_idx = 0;
          inputs->next    = NULL;
  
 -        if ((ret = avfilter_graph_parse(graph, vfilters, inputs, outputs, NULL)) < 0)
 +        if ((ret = avfilter_graph_parse(graph, vfilters, &inputs, &outputs, NULL)) < 0)
              return ret;
 -        av_freep(&vfilters);
      } else {
          if ((ret = avfilter_link(filt_src, 0, filt_out, 0)) < 0)
              return ret;
@@@ -1782,13 -1755,14 +1782,13 @@@ static int video_thread(void *arg
  {
      VideoState *is = arg;
      AVFrame *frame= avcodec_alloc_frame();
 -    int64_t pts_int;
 +    int64_t pts_int = AV_NOPTS_VALUE, pos = -1;
      double pts;
      int ret;
  
  #if CONFIG_AVFILTER
      AVFilterGraph *graph = avfilter_graph_alloc();
      AVFilterContext *filt_out = NULL;
 -    int64_t pos;
      int last_w = is->video_st->codec->width;
      int last_h = is->video_st->codec->height;
  
          AVPacket pkt;
  #else
          AVFilterBufferRef *picref;
 -        AVRational tb;
 +        AVRational tb = filt_out->inputs[0]->time_base;
  #endif
          while (is->paused && !is->videoq.abort_request)
              SDL_Delay(10);
  #if CONFIG_AVFILTER
          if (   last_w != is->video_st->codec->width
              || last_h != is->video_st->codec->height) {
 -            av_dlog(NULL, "Changing size %dx%d -> %dx%d\n", last_w, last_h,
 -                    is->video_st->codec->width, is->video_st->codec->height);
 +            av_log(NULL, AV_LOG_INFO, "Frame changed from size:%dx%d to size:%dx%d\n",
 +                   last_w, last_h, is->video_st->codec->width, is->video_st->codec->height);
              avfilter_graph_free(&graph);
              graph = avfilter_graph_alloc();
              if ((ret = configure_video_filters(graph, is, vfilters)) < 0)
              last_w = is->video_st->codec->width;
              last_h = is->video_st->codec->height;
          }
 -        ret = get_filtered_video_frame(filt_out, frame, &picref, &tb);
 +        ret = av_buffersink_get_buffer_ref(filt_out, &picref, 0);
          if (picref) {
 +            avfilter_fill_frame_from_video_buffer_ref(frame, picref);
              pts_int = picref->pts;
              pos     = picref->pos;
              frame->opaque = picref;
          }
  #else
          ret = get_video_frame(is, frame, &pts_int, &pkt);
 +        pos = pkt.pos;
 +        av_free_packet(&pkt);
  #endif
  
          if (ret < 0) goto the_end;
  
 -        if (!ret)
 +        is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
 +        if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
 +            is->frame_last_filter_delay = 0;
 +
 +#if CONFIG_AVFILTER
 +        if (!picref)
              continue;
 +#endif
  
          pts = pts_int*av_q2d(is->video_st->time_base);
  
 -#if CONFIG_AVFILTER
 -        ret = output_picture2(is, frame, pts, pos);
 -#else
 -        ret = output_picture2(is, frame, pts,  pkt.pos);
 -        av_free_packet(&pkt);
 -#endif
 +        ret = queue_picture(is, frame, pts, pos);
 +
          if (ret < 0)
              goto the_end;
  
 -        if (step)
 -            if (cur_stream)
 -                stream_pause(cur_stream);
 +        if (is->step)
 +            stream_toggle_pause(is);
      }
   the_end:
  #if CONFIG_AVFILTER
@@@ -1966,7 -1936,7 +1966,7 @@@ static int synchronize_audio(VideoStat
      int n, samples_size;
      double ref_clock;
  
 -    n = 2 * is->audio_st->codec->channels;
 +    n = av_get_bytes_per_sample(is->audio_tgt_fmt) * is->audio_tgt_channels;
      samples_size = samples_size1;
  
      /* if not master, then we try to remove or add samples to correct the clock */
                  avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
  
                  if (fabs(avg_diff) >= is->audio_diff_threshold) {
 -                    wanted_size = samples_size + ((int)(diff * is->audio_st->codec->sample_rate) * n);
 +                    wanted_size = samples_size + ((int)(diff * is->audio_tgt_freq) * n);
                      nb_samples = samples_size / n;
  
                      min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
                      max_size = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
                      if (wanted_size < min_size)
                          wanted_size = min_size;
 -                    else if (wanted_size > max_size)
 -                        wanted_size = max_size;
 +                    else if (wanted_size > FFMIN3(max_size, sizeof(is->audio_buf1), sizeof(is->audio_buf2)))
 +                        wanted_size = FFMIN3(max_size, sizeof(is->audio_buf1), sizeof(is->audio_buf2));
  
                      /* add or remove samples to correction the synchro */
                      if (wanted_size < samples_size) {
@@@ -2039,8 -2009,7 +2039,8 @@@ static int audio_decode_frame(VideoStat
      AVPacket *pkt_temp = &is->audio_pkt_temp;
      AVPacket *pkt = &is->audio_pkt;
      AVCodecContext *dec= is->audio_st->codec;
 -    int n, len1, data_size;
 +    int len1, len2, data_size, resampled_data_size;
 +    int64_t dec_channel_layout;
      double pts;
      int new_packet = 0;
      int flush_complete = 0;
                  continue;
              }
  
 -            if (dec->sample_fmt != is->audio_src_fmt) {
 -                if (is->reformat_ctx)
 -                    av_audio_convert_free(is->reformat_ctx);
 -                is->reformat_ctx= av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1,
 -                                                         dec->sample_fmt, 1, NULL, 0);
 -                if (!is->reformat_ctx) {
 -                    fprintf(stderr, "Cannot convert %s sample format to %s sample format\n",
 +            dec_channel_layout = (dec->channel_layout && dec->channels == av_get_channel_layout_nb_channels(dec->channel_layout)) ? dec->channel_layout : av_get_default_channel_layout(dec->channels);
 +
 +            if (dec->sample_fmt != is->audio_src_fmt || dec_channel_layout != is->audio_src_channel_layout || dec->sample_rate != is->audio_src_freq) {
 +                if (is->swr_ctx)
 +                    swr_free(&is->swr_ctx);
 +                is->swr_ctx = swr_alloc_set_opts(NULL,
 +                                                 is->audio_tgt_channel_layout, is->audio_tgt_fmt, is->audio_tgt_freq,
 +                                                 dec_channel_layout,           dec->sample_fmt,   dec->sample_rate,
 +                                                 0, NULL);
 +                if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
 +                    fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
 +                        dec->sample_rate,
                          av_get_sample_fmt_name(dec->sample_fmt),
 -                        av_get_sample_fmt_name(AV_SAMPLE_FMT_S16));
 -                        break;
 +                        dec->channels,
 +                        is->audio_tgt_freq,
 +                        av_get_sample_fmt_name(is->audio_tgt_fmt),
 +                        is->audio_tgt_channels);
 +                    break;
                  }
 -                is->audio_src_fmt= dec->sample_fmt;
 +                is->audio_src_channel_layout = dec_channel_layout;
 +                is->audio_src_channels = dec->channels;
 +                is->audio_src_freq = dec->sample_rate;
 +                is->audio_src_fmt = dec->sample_fmt;
              }
  
 -            if (is->reformat_ctx) {
 -                const void *ibuf[6]= {is->audio_buf1};
 -                void *obuf[6]= {is->audio_buf2};
 -                int istride[6]= {av_get_bytes_per_sample(dec->sample_fmt)};
 -                int ostride[6]= {2};
 -                int len= data_size/istride[0];
 -                if (av_audio_convert(is->reformat_ctx, obuf, ostride, ibuf, istride, len)<0) {
 -                    printf("av_audio_convert() failed\n");
 +            resampled_data_size = data_size;
 +            if (is->swr_ctx) {
 +                const uint8_t *in[] = {is->audio_buf1};
 +                uint8_t *out[] = {is->audio_buf2};
 +                len2 = swr_convert(is->swr_ctx, out, sizeof(is->audio_buf2) / is->audio_tgt_channels / av_get_bytes_per_sample(is->audio_tgt_fmt),
 +                                                in, data_size / dec->channels / av_get_bytes_per_sample(dec->sample_fmt));
 +                if (len2 < 0) {
 +                    fprintf(stderr, "audio_resample() failed\n");
                      break;
                  }
 -                is->audio_buf= is->audio_buf2;
 -                /* FIXME: existing code assume that data_size equals framesize*channels*2
 -                          remove this legacy cruft */
 -                data_size= len*2;
 -            }else{
 +                if (len2 == sizeof(is->audio_buf2) / is->audio_tgt_channels / av_get_bytes_per_sample(is->audio_tgt_fmt)) {
 +                    fprintf(stderr, "warning: audio buffer is probably too small\n");
 +                    swr_init(is->swr_ctx);
 +                }
 +                is->audio_buf = is->audio_buf2;
 +                resampled_data_size = len2 * is->audio_tgt_channels * av_get_bytes_per_sample(is->audio_tgt_fmt);
 +            } else {
                  is->audio_buf= is->audio_buf1;
              }
  
              /* if no pts, then compute it */
              pts = is->audio_clock;
              *pts_ptr = pts;
 -            n = 2 * dec->channels;
 -            is->audio_clock += (double)data_size /
 -                (double)(n * dec->sample_rate);
 +            is->audio_clock += (double)data_size / (dec->channels * dec->sample_rate * av_get_bytes_per_sample(dec->sample_fmt));
  #ifdef DEBUG
              {
                  static double last_clock;
                  last_clock = is->audio_clock;
              }
  #endif
 -            return data_size;
 +            return resampled_data_size;
          }
  
          /* free the current packet */
@@@ -2162,7 -2120,6 +2162,7 @@@ static void sdl_audio_callback(void *op
  {
      VideoState *is = opaque;
      int audio_size, len1;
 +    int bytes_per_sec;
      double pts;
  
      audio_callback_time = av_gettime();
             if (audio_size < 0) {
                  /* if error, just output silence */
                 is->audio_buf = is->audio_buf1;
 -               is->audio_buf_size = 1024;
 +               is->audio_buf_size = 256 * is->audio_tgt_channels * av_get_bytes_per_sample(is->audio_tgt_fmt);
                 memset(is->audio_buf, 0, is->audio_buf_size);
             } else {
 -               if (is->show_audio)
 +               if (is->show_mode != SHOW_MODE_VIDEO)
                     update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
                 audio_size = synchronize_audio(is, (int16_t *)is->audio_buf, audio_size,
                                                pts);
          stream += len1;
          is->audio_buf_index += len1;
      }
 +    bytes_per_sec = is->audio_tgt_freq * is->audio_tgt_channels * av_get_bytes_per_sample(is->audio_tgt_fmt);
 +    is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
 +    /* Let's assume the audio driver that is used by SDL has two periods. */
 +    is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec;
 +    is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
  }
  
  /* open a given stream. Return 0 if OK */
@@@ -2208,7 -2160,6 +2208,7 @@@ static int stream_component_open(VideoS
      SDL_AudioSpec wanted_spec, spec;
      AVDictionary *opts;
      AVDictionaryEntry *t = NULL;
 +    int64_t wanted_channel_layout = 0;
  
      if (stream_index < 0 || stream_index >= ic->nb_streams)
          return -1;
  
      opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index]);
  
 -    /* prepare audio output */
 -    if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
 -        if (avctx->channels > 0) {
 -            avctx->request_channels = FFMIN(2, avctx->channels);
 -        } else {
 -            avctx->request_channels = 2;
 -        }
 +    codec = avcodec_find_decoder(avctx->codec_id);
 +    switch(avctx->codec_type){
 +        case AVMEDIA_TYPE_AUDIO   : if(audio_codec_name   ) codec= avcodec_find_decoder_by_name(   audio_codec_name); break;
 +        case AVMEDIA_TYPE_SUBTITLE: if(subtitle_codec_name) codec= avcodec_find_decoder_by_name(subtitle_codec_name); break;
 +        case AVMEDIA_TYPE_VIDEO   : if(video_codec_name   ) codec= avcodec_find_decoder_by_name(   video_codec_name); break;
      }
 +    if (!codec)
 +        return -1;
  
 -    codec = avcodec_find_decoder(avctx->codec_id);
 -    avctx->debug_mv = debug_mv;
 -    avctx->debug = debug;
      avctx->workaround_bugs = workaround_bugs;
      avctx->lowres = lowres;
 -    if(lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
 +    if(avctx->lowres > codec->max_lowres){
 +        av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
 +                codec->max_lowres);
 +        avctx->lowres= codec->max_lowres;
 +    }
 +    if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
      avctx->idct_algo= idct;
      if(fast) avctx->flags2 |= CODEC_FLAG2_FAST;
      avctx->skip_frame= skip_frame;
      avctx->skip_loop_filter= skip_loop_filter;
      avctx->error_recognition= error_recognition;
      avctx->error_concealment= error_concealment;
 -    avctx->thread_count= thread_count;
 +
 +    if(codec->capabilities & CODEC_CAP_DR1)
 +        avctx->flags |= CODEC_FLAG_EMU_EDGE;
 +
 +    if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
 +        wanted_channel_layout = (avctx->channel_layout && avctx->channels == av_get_channel_layout_nb_channels(avctx->channels)) ? avctx->channel_layout : av_get_default_channel_layout(avctx->channels);
 +        wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
 +        wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
 +        wanted_spec.freq = avctx->sample_rate;
 +        if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
 +            fprintf(stderr, "Invalid sample rate or channel count!\n");
 +            return -1;
 +        }
 +    }
  
      if (!codec ||
          avcodec_open2(avctx, codec, &opts) < 0)
  
      /* prepare audio output */
      if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
 -        wanted_spec.freq = avctx->sample_rate;
          wanted_spec.format = AUDIO_S16SYS;
 -        wanted_spec.channels = avctx->channels;
          wanted_spec.silence = 0;
          wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
          wanted_spec.callback = sdl_audio_callback;
              return -1;
          }
          is->audio_hw_buf_size = spec.size;
 -        is->audio_src_fmt= AV_SAMPLE_FMT_S16;
 +        if (spec.format != AUDIO_S16SYS) {
 +            fprintf(stderr, "SDL advised audio format %d is not supported!\n", spec.format);
 +            return -1;
 +        }
 +        if (spec.channels != wanted_spec.channels) {
 +            wanted_channel_layout = av_get_default_channel_layout(spec.channels);
 +            if (!wanted_channel_layout) {
 +                fprintf(stderr, "SDL advised channel count %d is not supported!\n", spec.channels);
 +                return -1;
 +            }
 +        }
 +        is->audio_src_fmt = is->audio_tgt_fmt = AV_SAMPLE_FMT_S16;
 +        is->audio_src_freq = is->audio_tgt_freq = spec.freq;
 +        is->audio_src_channel_layout = is->audio_tgt_channel_layout = wanted_channel_layout;
 +        is->audio_src_channels = is->audio_tgt_channels = spec.channels;
      }
  
      ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
          is->audio_diff_avg_count = 0;
          /* since we do not have a precise anough audio fifo fullness,
             we correct audio sync only if larger than this threshold */
 -        is->audio_diff_threshold = 2.0 * SDL_AUDIO_BUFFER_SIZE / avctx->sample_rate;
 +        is->audio_diff_threshold = 2.0 * SDL_AUDIO_BUFFER_SIZE / wanted_spec.freq;
  
          memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
          packet_queue_init(&is->audioq);
@@@ -2347,9 -2271,10 +2347,9 @@@ static void stream_component_close(Vide
          SDL_CloseAudio();
  
          packet_queue_end(&is->audioq);
 +        if (is->swr_ctx)
 +            swr_free(&is->swr_ctx);
          av_free_packet(&is->audio_pkt);
 -        if (is->reformat_ctx)
 -            av_audio_convert_free(is->reformat_ctx);
 -        is->reformat_ctx = NULL;
  
          if (is->rdft) {
              av_rdft_end(is->rdft);
     variable instead of a thread local variable */
  static VideoState *global_video_state;
  
- static int decode_interrupt_cb(void)
+ static int decode_interrupt_cb(void *ctx)
  {
      return (global_video_state && global_video_state->abort_request);
  }
  
  /* this thread gets the stream from the disk or the network */
 -static int decode_thread(void *arg)
 +static int read_thread(void *arg)
  {
      VideoState *is = arg;
      AVFormatContext *ic = NULL;
      is->subtitle_stream = -1;
  
      global_video_state = is;
-     avio_set_interrupt_cb(decode_interrupt_cb);
  
+     ic = avformat_alloc_context();
+     ic->interrupt_callback.callback = decode_interrupt_cb;
      err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
      if (err < 0) {
          print_error(is->filename, err);
      if(genpts)
          ic->flags |= AVFMT_FLAG_GENPTS;
  
 +    av_dict_set(&codec_opts, "request_channels", "2", 0);
 +
      opts = setup_find_stream_info_opts(ic, codec_opts);
      orig_nb_streams = ic->nb_streams;
  
      av_freep(&opts);
  
      if(ic->pb)
 -        ic->pb->eof_reached= 0; //FIXME hack, avplay maybe should not use url_feof() to test for the end
 +        ic->pb->eof_reached= 0; //FIXME hack, ffplay maybe should not use url_feof() to test for the end
  
      if(seek_by_bytes<0)
          seek_by_bytes= !!(ic->iformat->flags & AVFMT_TS_DISCONT);
          av_dump_format(ic, 0, is->filename, 0);
      }
  
 +    is->show_mode = show_mode;
 +
      /* open the streams */
      if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
          stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
          ret= stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
      }
      is->refresh_tid = SDL_CreateThread(refresh_thread, is);
 -    if(ret<0) {
 -        if (!display_disable)
 -            is->show_audio = 2;
 -    }
 +    if (is->show_mode == SHOW_MODE_NONE)
 +        is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
  
      if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
          stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
              else
                  av_read_play(ic);
          }
 -#if CONFIG_RTSP_DEMUXER
 -        if (is->paused && !strcmp(ic->iformat->name, "rtsp")) {
 +#if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
 +        if (is->paused &&
 +                (!strcmp(ic->iformat->name, "rtsp") ||
 +                 (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
              /* wait 10 ms to avoid trying to get another packet */
              /* XXX: horrible */
              SDL_Delay(10);
              SDL_Delay(10);
              if(is->audioq.size + is->videoq.size + is->subtitleq.size ==0){
                  if(loop!=1 && (!loop || --loop)){
 -                    stream_seek(cur_stream, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
 +                    stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
                  }else if(autoexit){
                      ret=AVERROR_EOF;
                      goto fail;
                  }
              }
 +            eof=0;
              continue;
          }
          ret = av_read_frame(ic, pkt);
          if (ret < 0) {
 -            if (ret == AVERROR_EOF || (ic->pb && ic->pb->eof_reached))
 +            if (ret == AVERROR_EOF || url_feof(ic->pb))
                  eof=1;
              if (ic->pb && ic->pb->error)
                  break;
@@@ -2705,8 -2626,8 +2706,8 @@@ static VideoState *stream_open(const ch
      is->subpq_cond = SDL_CreateCond();
  
      is->av_sync_type = av_sync_type;
 -    is->parse_tid = SDL_CreateThread(decode_thread, is);
 -    if (!is->parse_tid) {
 +    is->read_tid = SDL_CreateThread(read_thread, is);
 +    if (!is->read_tid) {
          av_free(is);
          return NULL;
      }
@@@ -2763,44 -2684,49 +2764,44 @@@ static void stream_cycle_channel(VideoS
  }
  
  
 -static void toggle_full_screen(void)
 +static void toggle_full_screen(VideoState *is)
  {
      is_full_screen = !is_full_screen;
  #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
 -    /* OSX needs to empty the picture_queue */
 +    /* OSX needs to reallocate the SDL overlays */
      for (int i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
 -        cur_stream->pictq[i].reallocate = 1;
 +        is->pictq[i].reallocate = 1;
      }
  #endif
 -    video_open(cur_stream);
 +    video_open(is);
  }
  
 -static void toggle_pause(void)
 +static void toggle_pause(VideoState *is)
  {
 -    if (cur_stream)
 -        stream_pause(cur_stream);
 -    step = 0;
 +    stream_toggle_pause(is);
 +    is->step = 0;
  }
  
 -static void step_to_next_frame(void)
 +static void step_to_next_frame(VideoState *is)
  {
 -    if (cur_stream) {
 -        /* if the stream is paused unpause it, then step */
 -        if (cur_stream->paused)
 -            stream_pause(cur_stream);
 -    }
 -    step = 1;
 +    /* if the stream is paused unpause it, then step */
 +    if (is->paused)
 +        stream_toggle_pause(is);
 +    is->step = 1;
  }
  
 -static void toggle_audio_display(void)
 +static void toggle_audio_display(VideoState *is)
  {
 -    if (cur_stream) {
 -        int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
 -        cur_stream->show_audio = (cur_stream->show_audio + 1) % 3;
 -        fill_rectangle(screen,
 -                    cur_stream->xleft, cur_stream->ytop, cur_stream->width, cur_stream->height,
 -                    bgcolor);
 -        SDL_UpdateRect(screen, cur_stream->xleft, cur_stream->ytop, cur_stream->width, cur_stream->height);
 -    }
 +    int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
 +    is->show_mode = (is->show_mode + 1) % SHOW_MODE_NB;
 +    fill_rectangle(screen,
 +                is->xleft, is->ytop, is->width, is->height,
 +                bgcolor);
 +    SDL_UpdateRect(screen, is->xleft, is->ytop, is->width, is->height);
  }
  
  /* handle an event sent by the GUI */
 -static void event_loop(void)
 +static void event_loop(VideoState *cur_stream)
  {
      SDL_Event event;
      double incr, pos, frac;
          switch(event.type) {
          case SDL_KEYDOWN:
              if (exit_on_keydown) {
 -                do_exit();
 +                do_exit(cur_stream);
                  break;
              }
              switch(event.key.keysym.sym) {
              case SDLK_ESCAPE:
              case SDLK_q:
 -                do_exit();
 +                do_exit(cur_stream);
                  break;
              case SDLK_f:
 -                toggle_full_screen();
 +                toggle_full_screen(cur_stream);
                  break;
              case SDLK_p:
              case SDLK_SPACE:
 -                toggle_pause();
 +                toggle_pause(cur_stream);
                  break;
              case SDLK_s: //S: Step to next frame
 -                step_to_next_frame();
 +                step_to_next_frame(cur_stream);
                  break;
              case SDLK_a:
 -                if (cur_stream)
 -                    stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
 +                stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
                  break;
              case SDLK_v:
 -                if (cur_stream)
 -                    stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
 +                stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
                  break;
              case SDLK_t:
 -                if (cur_stream)
 -                    stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
 +                stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
                  break;
              case SDLK_w:
 -                toggle_audio_display();
 +                toggle_audio_display(cur_stream);
                  break;
              case SDLK_LEFT:
                  incr = -10.0;
              case SDLK_DOWN:
                  incr = -60.0;
              do_seek:
 -                if (cur_stream) {
 -                    if (seek_by_bytes) {
 -                        if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos>=0){
 -                            pos= cur_stream->video_current_pos;
 -                        }else if(cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos>=0){
 -                            pos= cur_stream->audio_pkt.pos;
 -                        }else
 -                            pos = avio_tell(cur_stream->ic->pb);
 -                        if (cur_stream->ic->bit_rate)
 -                            incr *= cur_stream->ic->bit_rate / 8.0;
 -                        else
 -                            incr *= 180000.0;
 -                        pos += incr;
 -                        stream_seek(cur_stream, pos, incr, 1);
 -                    } else {
 -                        pos = get_master_clock(cur_stream);
 -                        pos += incr;
 -                        stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
 -                    }
 +                if (seek_by_bytes) {
 +                    if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos>=0){
 +                        pos= cur_stream->video_current_pos;
 +                    }else if(cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos>=0){
 +                        pos= cur_stream->audio_pkt.pos;
 +                    }else
 +                        pos = avio_tell(cur_stream->ic->pb);
 +                    if (cur_stream->ic->bit_rate)
 +                        incr *= cur_stream->ic->bit_rate / 8.0;
 +                    else
 +                        incr *= 180000.0;
 +                    pos += incr;
 +                    stream_seek(cur_stream, pos, incr, 1);
 +                } else {
 +                    pos = get_master_clock(cur_stream);
 +                    pos += incr;
 +                    stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
                  }
                  break;
              default:
              break;
          case SDL_MOUSEBUTTONDOWN:
              if (exit_on_mousedown) {
 -                do_exit();
 +                do_exit(cur_stream);
                  break;
              }
          case SDL_MOUSEMOTION:
                      break;
                  x= event.motion.x;
              }
 -            if (cur_stream) {
 -                if(seek_by_bytes || cur_stream->ic->duration<=0){
 -                    uint64_t size=  avio_size(cur_stream->ic->pb);
 -                    stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
 -                }else{
 -                    int64_t ts;
 -                    int ns, hh, mm, ss;
 -                    int tns, thh, tmm, tss;
 -                    tns = cur_stream->ic->duration/1000000LL;
 -                    thh = tns/3600;
 -                    tmm = (tns%3600)/60;
 -                    tss = (tns%60);
 -                    frac = x/cur_stream->width;
 -                    ns = frac*tns;
 -                    hh = ns/3600;
 -                    mm = (ns%3600)/60;
 -                    ss = (ns%60);
 -                    fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d)       \n", frac*100,
 -                            hh, mm, ss, thh, tmm, tss);
 -                    ts = frac*cur_stream->ic->duration;
 -                    if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
 -                        ts += cur_stream->ic->start_time;
 -                    stream_seek(cur_stream, ts, 0, 0);
 -                }
 +            if(seek_by_bytes || cur_stream->ic->duration<=0){
 +                uint64_t size=  avio_size(cur_stream->ic->pb);
 +                stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
 +            }else{
 +                int64_t ts;
 +                int ns, hh, mm, ss;
 +                int tns, thh, tmm, tss;
 +                tns = cur_stream->ic->duration/1000000LL;
 +                thh = tns/3600;
 +                tmm = (tns%3600)/60;
 +                tss = (tns%60);
 +                frac = x/cur_stream->width;
 +                ns = frac*tns;
 +                hh = ns/3600;
 +                mm = (ns%3600)/60;
 +                ss = (ns%60);
 +                fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d)       \n", frac*100,
 +                        hh, mm, ss, thh, tmm, tss);
 +                ts = frac*cur_stream->ic->duration;
 +                if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
 +                    ts += cur_stream->ic->start_time;
 +                stream_seek(cur_stream, ts, 0, 0);
              }
              break;
          case SDL_VIDEORESIZE:
 -            if (cur_stream) {
 -                screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
 -                                          SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
 -                screen_width = cur_stream->width = event.resize.w;
 -                screen_height= cur_stream->height= event.resize.h;
 -            }
 +            screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
 +                                      SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
 +            screen_width = cur_stream->width = event.resize.w;
 +            screen_height= cur_stream->height= event.resize.h;
              break;
          case SDL_QUIT:
          case FF_QUIT_EVENT:
 -            do_exit();
 +            do_exit(cur_stream);
              break;
          case FF_ALLOC_EVENT:
              video_open(event.user.data1);
              alloc_picture(event.user.data1);
              break;
          case FF_REFRESH_EVENT:
 -            video_refresh_timer(event.user.data1);
 +            video_refresh(event.user.data1);
              cur_stream->refresh=0;
              break;
          default:
  
  static int opt_frame_size(const char *opt, const char *arg)
  {
 -    av_log(NULL, AV_LOG_ERROR,
 -           "Option '%s' has been removed, use private format options instead\n", opt);
 -    return AVERROR(EINVAL);
 +    av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
 +    return opt_default("video_size", arg);
  }
  
  static int opt_width(const char *opt, const char *arg)
@@@ -2967,8 -2903,9 +2968,8 @@@ static int opt_format(const char *opt, 
  
  static int opt_frame_pix_fmt(const char *opt, const char *arg)
  {
 -    av_log(NULL, AV_LOG_ERROR,
 -           "Option '%s' has been removed, use private format options instead\n", opt);
 -    return AVERROR(EINVAL);
 +    av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
 +    return opt_default("pixel_format", arg);
  }
  
  static int opt_sync(const char *opt, const char *arg)
@@@ -2998,39 -2935,28 +2999,39 @@@ static int opt_duration(const char *opt
      return 0;
  }
  
 -static int opt_debug(const char *opt, const char *arg)
 +static int opt_show_mode(const char *opt, const char *arg)
  {
 -    av_log_set_level(99);
 -    debug = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
 +    show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
 +                !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
 +                !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT  :
 +                parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
      return 0;
  }
  
 -static int opt_vismv(const char *opt, const char *arg)
 +static void opt_input_file(void *optctx, const char *filename)
  {
 -    debug_mv = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
 -    return 0;
 +    if (input_filename) {
 +        fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
 +                filename, input_filename);
 +        exit_program(1);
 +    }
 +    if (!strcmp(filename, "-"))
 +        filename = "pipe:";
 +    input_filename = filename;
  }
  
 -static int opt_thread_count(const char *opt, const char *arg)
 +static int opt_codec(void *o, const char *opt, const char *arg)
  {
 -    thread_count= parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
 -#if !HAVE_THREADS
 -    fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
 -#endif
 +    switch(opt[strlen(opt)-1]){
 +    case 'a' :    audio_codec_name = arg; break;
 +    case 's' : subtitle_codec_name = arg; break;
 +    case 'v' :    video_codec_name = arg; break;
 +    }
      return 0;
  }
  
 +static int dummy;
 +
  static const OptionDef options[] = {
  #include "cmdutils_common_opts.h"
      { "x", HAS_ARG, {(void*)opt_width}, "force displayed width", "width" },
      { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
      { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_frame_pix_fmt}, "set pixel format", "format" },
      { "stats", OPT_BOOL | OPT_EXPERT, {(void*)&show_status}, "show status", "" },
 -    { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info", "" },
      { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&workaround_bugs}, "workaround bugs", "" },
 -    { "vismv", HAS_ARG | OPT_EXPERT, {(void*)opt_vismv}, "visualize motion vectors", "" },
      { "fast", OPT_BOOL | OPT_EXPERT, {(void*)&fast}, "non spec compliant optimizations", "" },
      { "genpts", OPT_BOOL | OPT_EXPERT, {(void*)&genpts}, "generate pts", "" },
      { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&decoder_reorder_pts}, "let decoder reorder pts 0=off 1=on -1=auto", ""},
      { "er", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&error_recognition}, "set error detection threshold (0-4)",  "threshold" },
      { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&error_concealment}, "set error concealment options",  "bit_mask" },
      { "sync", HAS_ARG | OPT_EXPERT, {(void*)opt_sync}, "set audio-video sync. type (type=audio/video/ext)", "type" },
 -    { "threads", HAS_ARG | OPT_EXPERT, {(void*)opt_thread_count}, "thread count", "count" },
      { "autoexit", OPT_BOOL | OPT_EXPERT, {(void*)&autoexit}, "exit at the end", "" },
      { "exitonkeydown", OPT_BOOL | OPT_EXPERT, {(void*)&exit_on_keydown}, "exit on key down", "" },
      { "exitonmousedown", OPT_BOOL | OPT_EXPERT, {(void*)&exit_on_mousedown}, "exit on mouse down", "" },
      { "vf", OPT_STRING | HAS_ARG, {(void*)&vfilters}, "video filters", "filter list" },
  #endif
      { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, {(void*)&rdftspeed}, "rdft speed", "msecs" },
 +    { "showmode", HAS_ARG, {(void*)opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
      { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
 -    { "i", 0, {NULL}, "avconv compatibility dummy option", ""},
 +    { "i", OPT_BOOL, {(void *)&dummy}, "read specified file", "input_file"},
 +    { "codec", HAS_ARG | OPT_FUNC2, {(void*)opt_codec}, "force decoder", "decoder" },
      { NULL, },
  };
  
@@@ -3085,7 -3012,7 +3086,7 @@@ static void show_usage(void
      printf("\n");
  }
  
 -static void show_help(void)
 +static int opt_help(const char *opt, const char *arg)
  {
      av_log_set_callback(log_callback_help);
      show_usage();
             "down/up             seek backward/forward 1 minute\n"
             "mouse click         seek to percentage in file corresponding to fraction of width\n"
             );
 +    return 0;
  }
  
 -static void opt_input_file(void *optctx, const char *filename)
 +static int lockmgr(void **mtx, enum AVLockOp op)
  {
 -    if (input_filename) {
 -        fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
 -                filename, input_filename);
 -        exit(1);
 -    }
 -    if (!strcmp(filename, "-"))
 -        filename = "pipe:";
 -    input_filename = filename;
 +   switch(op) {
 +      case AV_LOCK_CREATE:
 +          *mtx = SDL_CreateMutex();
 +          if(!*mtx)
 +              return 1;
 +          return 0;
 +      case AV_LOCK_OBTAIN:
 +          return !!SDL_LockMutex(*mtx);
 +      case AV_LOCK_RELEASE:
 +          return !!SDL_UnlockMutex(*mtx);
 +      case AV_LOCK_DESTROY:
 +          SDL_DestroyMutex(*mtx);
 +          return 0;
 +   }
 +   return 1;
  }
  
  /* Called from the main */
  int main(int argc, char **argv)
  {
      int flags;
 +    VideoState *is;
  
      av_log_set_flags(AV_LOG_SKIP_REPEATED);
      parse_loglevel(argc, argv, options);
          video_disable = 1;
      }
      flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
 +    if (audio_disable)
 +        flags &= ~SDL_INIT_AUDIO;
  #if !defined(__MINGW32__) && !defined(__APPLE__)
      flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
  #endif
      if (SDL_Init (flags)) {
          fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
 +        fprintf(stderr, "(Did you set the DISPLAY variable?)\n");
          exit(1);
      }
  
      SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
      SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
  
 +    if (av_lockmgr_register(lockmgr)) {
 +        fprintf(stderr, "Could not initialize lock manager!\n");
 +        do_exit(NULL);
 +    }
 +
      av_init_packet(&flush_pkt);
      flush_pkt.data= "FLUSH";
  
 -    cur_stream = stream_open(input_filename, file_iformat);
 +    is = stream_open(input_filename, file_iformat);
 +    if (!is) {
 +        fprintf(stderr, "Failed to initialize VideoState!\n");
 +        do_exit(NULL);
 +    }
  
 -    event_loop();
 +    event_loop(is);
  
      /* never returns */
  
diff --combined libavcodec/cinepak.c
index fe6552487070c83e0dac588ee6ebf0ee6c3caa64,9cf38edb8e56bdb7386bea3af4103dbc0bf6ae61..ebdbb5eaecec21fe9d1b4ea925f132248ba9de46
@@@ -2,20 -2,20 +2,20 @@@
   * Cinepak Video Decoder
   * Copyright (C) 2003 the ffmpeg project
   *
 - * 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
   */
  
@@@ -147,7 -147,7 +147,7 @@@ static int cinepak_decode_vectors (Cine
          for (x=strip->x1; x < strip->x2; x+=4) {
              if ((chunk_id & 0x01) && !(mask >>= 1)) {
                  if ((data + 4) > eod)
-                     return -1;
+                     return AVERROR_INVALIDDATA;
  
                  flag  = AV_RB32 (data);
                  data += 4;
              if (!(chunk_id & 0x01) || (flag & mask)) {
                  if (!(chunk_id & 0x02) && !(mask >>= 1)) {
                      if ((data + 4) > eod)
-                         return -1;
+                         return AVERROR_INVALIDDATA;
  
                      flag  = AV_RB32 (data);
                      data += 4;
  
                  if ((chunk_id & 0x02) || (~flag & mask)) {
                      if (data >= eod)
-                         return -1;
+                         return AVERROR_INVALIDDATA;
  
                      codebook = &strip->v1_codebook[*data++];
                      s->frame.data[0][iy[0] + 0] = codebook->y0;
  
                  } else if (flag & mask) {
                      if ((data + 4) > eod)
-                         return -1;
+                         return AVERROR_INVALIDDATA;
  
                      codebook = &strip->v4_codebook[*data++];
                      s->frame.data[0][iy[0] + 0] = codebook->y0;
@@@ -269,16 -269,16 +269,16 @@@ static int cinepak_decode_strip (Cinepa
      int      chunk_id, chunk_size;
  
      /* coordinate sanity checks */
-     if (strip->x2 > s->width  ||
-         strip->y2 > s->height ||
+     if (strip->x2 > s->width   ||
+         strip->y2 > s->height  ||
          strip->x1 >= strip->x2 || strip->y1 >= strip->y2)
-         return -1;
+         return AVERROR_INVALIDDATA;
  
      while ((data + 4) <= eod) {
          chunk_id   = data[0];
          chunk_size = AV_RB24 (&data[1]) - 4;
          if(chunk_size < 0)
-             return -1;
+             return AVERROR_INVALIDDATA;
  
          data      += 4;
          chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
          data += chunk_size;
      }
  
-     return -1;
+     return AVERROR_INVALIDDATA;
  }
  
  static int cinepak_decode (CinepakContext *s)
      int           encoded_buf_size;
  
      if (s->size < 10)
-         return -1;
+         return AVERROR_INVALIDDATA;
  
      frame_flags = s->data[0];
      num_strips  = AV_RB16 (&s->data[8]);
  
      /* if this is the first frame, check for deviant Sega FILM data */
      if (s->sega_film_skip_bytes == -1) {
-         if (!encoded_buf_size){
+         if (!encoded_buf_size) {
              av_log_ask_for_sample(s->avctx, "encoded_buf_size is 0");
-             return -1;
+             return AVERROR_INVALIDDATA;
          }
          if (encoded_buf_size != s->size && (s->size % encoded_buf_size) != 0) {
              /* If the encoded frame size differs from the frame size as indicated
  
      num_strips = FFMIN(num_strips, MAX_STRIPS);
  
 +    s->frame.key_frame = 0;
 +
      for (i=0; i < num_strips; i++) {
          if ((s->data + 12) > eod)
-             return -1;
+             return AVERROR_INVALIDDATA;
  
          s->strips[i].id = s->data[0];
          s->strips[i].y1 = y0;
          s->strips[i].y2 = y0 + AV_RB16 (&s->data[8]);
          s->strips[i].x2 = s->avctx->width;
  
 +        if (s->strips[i].id == 0x10)
 +            s->frame.key_frame = 1;
 +
          strip_size = AV_RB24 (&s->data[1]) - 12;
-         if(strip_size < 0)
-             return -1;
+         if (strip_size < 0)
+             return AVERROR_INVALIDDATA;
          s->data   += 12;
          strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
  
@@@ -416,7 -411,6 +416,7 @@@ static av_cold int cinepak_decode_init(
          avctx->pix_fmt = PIX_FMT_PAL8;
      }
  
 +    avcodec_get_frame_defaults(&s->frame);
      s->frame.data[0] = NULL;
  
      return 0;
@@@ -427,18 -421,18 +427,18 @@@ static int cinepak_decode_frame(AVCodec
                                  AVPacket *avpkt)
  {
      const uint8_t *buf = avpkt->data;
-     int buf_size = avpkt->size;
+     int ret = 0, buf_size = avpkt->size;
      CinepakContext *s = avctx->priv_data;
  
      s->data = buf;
      s->size = buf_size;
  
 -    s->frame.reference = 1;
 +    s->frame.reference = 3;
      s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
                              FF_BUFFER_HINTS_REUSABLE;
-     if (avctx->reget_buffer(avctx, &s->frame)) {
+     if ((ret = avctx->reget_buffer(avctx, &s->frame))) {
          av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
-         return -1;
+         return ret;
      }
  
      if (s->palette_video) {
diff --combined libavformat/allformats.c
index c4b27e43cdd1ce39e4f49f46132bd69c91c60de2,bee2f5f11fde741a56dc4296db75200d4d19bdfb..31fa5060f748eb0c1de8708285f86d83d122d025
@@@ -2,20 -2,20 +2,20 @@@
   * Register all the formats and protocols
   * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
   *
 - * 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 "avformat.h"
@@@ -51,8 -51,6 +51,8 @@@ void av_register_all(void
      REGISTER_MUXER    (A64, a64);
      REGISTER_DEMUXER  (AAC, aac);
      REGISTER_MUXDEMUX (AC3, ac3);
 +    REGISTER_DEMUXER  (ACT, act);
 +    REGISTER_DEMUXER  (ADF, adf);
      REGISTER_MUXER    (ADTS, adts);
      REGISTER_DEMUXER  (AEA, aea);
      REGISTER_MUXDEMUX (AIFF, aiff);
      REGISTER_DEMUXER  (AVS, avs);
      REGISTER_DEMUXER  (BETHSOFTVID, bethsoftvid);
      REGISTER_DEMUXER  (BFI, bfi);
 +    REGISTER_DEMUXER  (BINTEXT, bintext);
      REGISTER_DEMUXER  (BINK, bink);
 +    REGISTER_MUXDEMUX (BIT, bit);
      REGISTER_DEMUXER  (BMV, bmv);
      REGISTER_DEMUXER  (C93, c93);
 -    REGISTER_DEMUXER  (CAF, caf);
 +    REGISTER_MUXDEMUX (CAF, caf);
      REGISTER_MUXDEMUX (CAVSVIDEO, cavsvideo);
      REGISTER_DEMUXER  (CDG, cdg);
      REGISTER_MUXER    (CRC, crc);
      REGISTER_MUXER    (FRAMECRC, framecrc);
      REGISTER_MUXER    (FRAMEMD5, framemd5);
      REGISTER_MUXDEMUX (G722, g722);
 +    REGISTER_MUXDEMUX (G723_1, g723_1);
 +    REGISTER_DEMUXER  (G729, g729);
      REGISTER_MUXER    (GIF, gif);
      REGISTER_DEMUXER  (GSM, gsm);
      REGISTER_MUXDEMUX (GXF, gxf);
      REGISTER_MUXDEMUX (H263, h263);
      REGISTER_MUXDEMUX (H264, h264);
      REGISTER_DEMUXER  (IDCIN, idcin);
 +    REGISTER_DEMUXER  (IDF, idf);
      REGISTER_DEMUXER  (IFF, iff);
      REGISTER_MUXDEMUX (IMAGE2, image2);
      REGISTER_MUXDEMUX (IMAGE2PIPE, image2pipe);
      REGISTER_DEMUXER  (JV, jv);
      REGISTER_MUXDEMUX (LATM, latm);
      REGISTER_DEMUXER  (LMLM4, lmlm4);
 +    REGISTER_DEMUXER  (LOAS, loas);
      REGISTER_DEMUXER  (LXF, lxf);
      REGISTER_MUXDEMUX (M4V, m4v);
      REGISTER_MUXER    (MD5, md5);
      REGISTER_MUXDEMUX (MATROSKA, matroska);
      REGISTER_MUXER    (MATROSKA_AUDIO, matroska_audio);
 +    REGISTER_MUXDEMUX (MICRODVD, microdvd);
      REGISTER_MUXDEMUX (MJPEG, mjpeg);
      REGISTER_MUXDEMUX (MLP, mlp);
      REGISTER_DEMUXER  (MM, mm);
      REGISTER_MUXDEMUX (PCM_U16BE, pcm_u16be);
      REGISTER_MUXDEMUX (PCM_U16LE, pcm_u16le);
      REGISTER_MUXDEMUX (PCM_U8,    pcm_u8);
 +    REGISTER_DEMUXER  (PMP, pmp);
      REGISTER_MUXER    (PSP, psp);
      REGISTER_DEMUXER  (PVA, pva);
      REGISTER_DEMUXER  (QCP, qcp);
      av_register_rdt_dynamic_payload_handlers();
  #endif
      REGISTER_DEMUXER  (SEGAFILM, segafilm);
 +    REGISTER_MUXER    (SEGMENT, segment);
      REGISTER_DEMUXER  (SHORTEN, shorten);
      REGISTER_DEMUXER  (SIFF, siff);
      REGISTER_DEMUXER  (SMACKER, smacker);
      REGISTER_MUXER    (TGP, tgp);
      REGISTER_DEMUXER  (THP, thp);
      REGISTER_DEMUXER  (TIERTEXSEQ, tiertexseq);
 +    REGISTER_MUXER    (MKVTIMESTAMP_V2, mkvtimestamp_v2);
      REGISTER_DEMUXER  (TMV, tmv);
      REGISTER_MUXDEMUX (TRUEHD, truehd);
      REGISTER_DEMUXER  (TTA, tta);
      REGISTER_MUXER    (WEBM, webm);
      REGISTER_DEMUXER  (WSAUD, wsaud);
      REGISTER_DEMUXER  (WSVQA, wsvqa);
 -    REGISTER_DEMUXER  (WTV, wtv);
 +    REGISTER_MUXDEMUX (WTV, wtv);
      REGISTER_DEMUXER  (WV, wv);
      REGISTER_DEMUXER  (XA, xa);
 +    REGISTER_DEMUXER  (XBIN, xbin);
      REGISTER_DEMUXER  (XMV, xmv);
      REGISTER_DEMUXER  (XWMA, xwma);
      REGISTER_DEMUXER  (YOP, yop);
      REGISTER_MUXDEMUX (YUV4MPEGPIPE, yuv4mpegpipe);
  
      /* external libraries */
 +#if CONFIG_LIBMODPLUG
 +    REGISTER_DEMUXER  (LIBMODPLUG, libmodplug);
 +#endif
      REGISTER_MUXDEMUX (LIBNUT, libnut);
  
      /* protocols */
      REGISTER_PROTOCOL (APPLEHTTP, applehttp);
 +    REGISTER_PROTOCOL (CACHE, cache);
      REGISTER_PROTOCOL (CONCAT, concat);
      REGISTER_PROTOCOL (CRYPTO, crypto);
      REGISTER_PROTOCOL (FILE, file);
      REGISTER_PROTOCOL (GOPHER, gopher);
      REGISTER_PROTOCOL (HTTP, http);
+     REGISTER_PROTOCOL (HTTPPROXY, httpproxy);
      REGISTER_PROTOCOL (HTTPS, https);
      REGISTER_PROTOCOL (MMSH, mmsh);
      REGISTER_PROTOCOL (MMST, mmst);
diff --combined libavformat/applehttp.c
index 7c41d9893c5433dde9e94a280041dd7304a1f290,366d8327ff5880d61eaf5357a85c1c26d4b47ac1..1694096d9ba11ac5c3c3af24a77b69bc908d3817
@@@ -2,20 -2,20 +2,20 @@@
   * Apple HTTP Live Streaming demuxer
   * Copyright (c) 2010 Martin Storsjo
   *
 - * 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
   */
  
@@@ -226,7 -226,7 +226,7 @@@ static int parse_playlist(AppleHTTPCont
          free_segment_list(var);
          var->finished = 0;
      }
 -    while (!in->eof_reached) {
 +    while (!url_feof(in)) {
          read_chomp_line(in, line, sizeof(line));
          if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
              struct variant_info info = {{0}};
@@@ -543,7 -543,7 +543,7 @@@ static int recheck_discard_flags(AVForm
  
      /* Check if any new streams are needed */
      for (i = 0; i < c->n_variants; i++)
 -        c->variants[i]->cur_needed = 0;;
 +        c->variants[i]->cur_needed = 0;
  
      for (i = 0; i < s->nb_streams; i++) {
          AVStream *st = s->streams[i];
@@@ -590,7 -590,7 +590,7 @@@ start
          if (var->needed && !var->pkt.data) {
              ret = av_read_frame(var->ctx, &var->pkt);
              if (ret < 0) {
 -                if (!var->pb.eof_reached)
 +                if (!url_feof(&var->pb))
                      return ret;
                  reset_packet(&var->pkt);
              } else {
@@@ -644,11 -644,11 +644,12 @@@ static int applehttp_read_seek(AVFormat
      for (i = 0; i < c->n_variants; i++) {
          /* Reset reading */
          struct variant *var = c->variants[i];
-         int64_t pos = av_rescale_rnd(c->first_timestamp, 1, stream_index >= 0 ?
+         int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ? 0 :
 -                      av_rescale_rnd(c->first_timestamp, 1,
 -                          stream_index >= 0 ? s->streams[stream_index]->time_base.den : AV_TIME_BASE,
 -                          flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
 -        if (var->input) {
++                      av_rescale_rnd(c->first_timestamp, 1, stream_index >= 0 ?
 +                               s->streams[stream_index]->time_base.den :
 +                               AV_TIME_BASE, flags & AVSEEK_FLAG_BACKWARD ?
 +                               AV_ROUND_DOWN : AV_ROUND_UP);
 +         if (var->input) {
              ffurl_close(var->input);
              var->input = NULL;
          }
diff --combined libavformat/avio.c
index 879844aefc4ca390ddc0998c017bb45fc7c9b456,4750a9d54441146a406f2545df7b09d083e6c0ef..b2b39b32d76f83f51bb4ce759b1a569bee65951d
@@@ -2,20 -2,20 +2,20 @@@
   * unbuffered I/O
   * Copyright (c) 2001 Fabrice Bellard
   *
 - * 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
   */
  
@@@ -83,14 -83,18 +83,16 @@@ const AVClass ffurl_context_class = 
  };
  /*@}*/
  
- static int default_interrupt_cb(void);
  
+ #if FF_API_OLD_INTERRUPT_CB
+ static int default_interrupt_cb(void);
  int (*url_interrupt_cb)(void) = default_interrupt_cb;
+ #endif
  
 -#if FF_API_OLD_AVIO
  URLProtocol *av_protocol_next(URLProtocol *p)
  {
      return ffurl_protocol_next(p);
  }
 -#endif
  
  const char *avio_enum_protocols(void **opaque, int output)
  {
@@@ -265,11 -269,6 +267,11 @@@ int ffurl_alloc(URLContext **puc, cons
      char proto_str[128], proto_nested[128], *ptr;
      size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  
 +    if (!first_protocol) {
 +        av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
 +                                     "Missing call to av_register_all()?\n");
 +    }
 +
      if (filename[proto_len] != ':' || is_dos_path(filename))
          strcpy(proto_str, "file");
      else
@@@ -332,7 -331,7 +334,7 @@@ static inline int retry_transfer_wrappe
          if (ret)
             fast_retries = FFMAX(fast_retries, 2);
          len += ret;
 -        if (ff_check_interrupt(&h->interrupt_callback))
 +        if (len < size && ff_check_interrupt(&h->interrupt_callback))
              return AVERROR_EXIT;
      }
      return len;
@@@ -360,7 -359,7 +362,7 @@@ int ffurl_write(URLContext *h, const un
      if (h->max_packet_size && size > h->max_packet_size)
          return AVERROR(EIO);
  
 -    return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
 +    return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
  }
  
  int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
@@@ -444,6 -443,7 +446,7 @@@ int ffurl_get_file_handle(URLContext *h
      return h->prot->url_get_file_handle(h);
  }
  
+ #if FF_API_OLD_INTERRUPT_CB
  static int default_interrupt_cb(void)
  {
      return 0;
@@@ -455,13 -455,18 +458,18 @@@ void avio_set_interrupt_cb(int (*interr
          interrupt_cb = default_interrupt_cb;
      url_interrupt_cb = interrupt_cb;
  }
+ #endif
  
  int ff_check_interrupt(AVIOInterruptCB *cb)
  {
      int ret;
      if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
          return ret;
+ #if FF_API_OLD_INTERRUPT_CB
      return url_interrupt_cb();
+ #else
+     return 0;
+ #endif
  }
  
  #if FF_API_OLD_AVIO
diff --combined libavformat/avio.h
index 14f3b7b2924b6d89c27854af7c67af213232725c,d7977022bfb187fab1b8646418267101287b5047..88fe1577ff13a9727a1f7b5a845bd8874210c01e
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * copyright (c) 2001 Fabrice Bellard
   *
 - * 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
   */
  #ifndef AVFORMAT_AVIO_H
@@@ -230,13 -230,12 +230,13 @@@ attribute_deprecated int av_url_read_pa
  attribute_deprecated int64_t av_url_read_seek(URLContext *h, int stream_index,
                                                int64_t timestamp, int flags);
  attribute_deprecated void url_set_interrupt_cb(int (*interrupt_cb)(void));
 +
  /**
 - * If protocol is NULL, returns the first registered protocol,
 - * if protocol is non-NULL, returns the next registered protocol after protocol,
 - * or NULL if protocol is the last one.
 + * returns the next registered protocol after the given protocol (the first if
 + * NULL is given), or NULL if protocol is the last one.
   */
 -attribute_deprecated URLProtocol *av_protocol_next(URLProtocol *p);
 +URLProtocol *av_protocol_next(URLProtocol *p);
 +
  /**
   * Register the URLProtocol protocol.
   *
@@@ -328,6 -327,10 +328,6 @@@ attribute_deprecated int url_fdopen(AVI
   * @}
   */
  
 -/**
 - * @deprecated use AVIOContext.eof_reached
 - */
 -attribute_deprecated int url_feof(AVIOContext *s);
  attribute_deprecated int url_ferror(AVIOContext *s);
  
  attribute_deprecated int udp_set_remote_url(URLContext *h, const char *uri);
@@@ -386,13 -389,17 +386,17 @@@ attribute_deprecated int url_exist(cons
   */
  int avio_check(const char *url, int flags);
  
+ #if FF_API_OLD_INTERRUPT_CB
  /**
   * The callback is called in blocking functions to test regulary if
   * asynchronous interruption is needed. AVERROR_EXIT is returned
   * in this case by the interrupted function. 'NULL' means no interrupt
   * callback is given.
+  * @deprecated Use interrupt_callback in AVFormatContext/avio_open2
+  *             instead.
   */
- void avio_set_interrupt_cb(int (*interrupt_cb)(void));
+ attribute_deprecated void avio_set_interrupt_cb(int (*interrupt_cb)(void));
+ #endif
  
  /**
   * Allocate and initialize an AVIOContext for buffered I/O. It must be later
@@@ -468,7 -475,10 +472,7 @@@ int64_t avio_seek(AVIOContext *s, int64
   * Skip given number of bytes forward
   * @return new position or AVERROR.
   */
 -static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
 -{
 -    return avio_seek(s, offset, SEEK_CUR);
 -}
 +int64_t avio_skip(AVIOContext *s, int64_t offset);
  
  /**
   * ftell() equivalent for AVIOContext.
@@@ -485,12 -495,6 +489,12 @@@ static av_always_inline int64_t avio_te
   */
  int64_t avio_size(AVIOContext *s);
  
 +/**
 + * feof() equivalent for AVIOContext.
 + * @return non zero if and only if end of file
 + */
 +int url_feof(AVIOContext *s);
 +
  /** @warning currently size is limited */
  int avio_printf(AVIOContext *s, const char *fmt, ...) av_printf_format(2, 3);
  
@@@ -638,7 -642,6 +642,7 @@@ int avio_close_dyn_buf(AVIOContext *s, 
  
  /**
   * Iterate through names of available protocols.
 + * @note it is recommanded to use av_protocol_next() instead of this
   *
   * @param opaque A private pointer representing current protocol.
   *        It must be a pointer to NULL on first iteration and will
diff --combined libavformat/http.c
index ad4018085c6f887b1717e4290a34e57f7843537e,63cfecd33260ea2d326cb23973b2bec55bf2753f..d45ef226161f74166c6a28337928e5446e5a5234
@@@ -1,21 -1,21 +1,21 @@@
  /*
 - * HTTP protocol for avconv client
 + * HTTP protocol for ffmpeg client
   * Copyright (c) 2000, 2001 Fabrice Bellard
   *
 - * 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
   */
  
@@@ -30,7 -30,7 +30,7 @@@
  #include "url.h"
  #include "libavutil/opt.h"
  
 -/* XXX: POST protocol is not completely implemented because avconv uses
 +/* XXX: POST protocol is not completely implemented because ffmpeg uses
     only a subset of it. */
  
  /* used for protocol handling */
@@@ -110,6 -110,15 +110,15 @@@ static int http_open_cnx(URLContext *h
                   path1, sizeof(path1), s->location);
      ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
  
+     if (!strcmp(proto, "https")) {
+         lower_proto = "tls";
+         use_proxy = 0;
+         if (port < 0)
+             port = 443;
+     }
+     if (port < 0)
+         port = 80;
      if (path1[0] == '\0')
          path = "/";
      else
          av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
                       hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
      }
-     if (!strcmp(proto, "https")) {
-         lower_proto = "tls";
-         if (port < 0)
-             port = 443;
-     }
-     if (port < 0)
-         port = 80;
  
      ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
      err = ffurl_open(&hd, buf, AVIO_FLAG_READ_WRITE,
@@@ -413,10 -415,33 +415,33 @@@ static int http_connect(URLContext *h, 
  }
  
  
- static int http_read(URLContext *h, uint8_t *buf, int size)
+ static int http_buf_read(URLContext *h, uint8_t *buf, int size)
  {
      HTTPContext *s = h->priv_data;
      int len;
+     /* read bytes from input buffer first */
+     len = s->buf_end - s->buf_ptr;
+     if (len > 0) {
+         if (len > size)
+             len = size;
+         memcpy(buf, s->buf_ptr, len);
+         s->buf_ptr += len;
+     } else {
+         if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
+             return AVERROR_EOF;
+         len = ffurl_read(s->hd, buf, size);
+     }
+     if (len > 0) {
+         s->off += len;
+         if (s->chunksize > 0)
+             s->chunksize -= len;
+     }
+     return len;
+ }
+ static int http_read(URLContext *h, uint8_t *buf, int size)
+ {
+     HTTPContext *s = h->priv_data;
  
      if (s->chunksize >= 0) {
          if (!s->chunksize) {
          }
          size = FFMIN(size, s->chunksize);
      }
-     /* read bytes from input buffer first */
-     len = s->buf_end - s->buf_ptr;
-     if (len > 0) {
-         if (len > size)
-             len = size;
-         memcpy(buf, s->buf_ptr, len);
-         s->buf_ptr += len;
-     } else {
-         if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
-             return AVERROR_EOF;
-         len = ffurl_read(s->hd, buf, size);
-     }
-     if (len > 0) {
-         s->off += len;
-         if (s->chunksize > 0)
-             s->chunksize -= len;
-     }
-     return len;
+     return http_buf_read(h, buf, size);
  }
  
  /* used only when posting data */
@@@ -572,3 -580,118 +580,118 @@@ URLProtocol ff_https_protocol = 
      .priv_data_class     = &https_context_class,
  };
  #endif
+ #if CONFIG_HTTPPROXY_PROTOCOL
+ static int http_proxy_close(URLContext *h)
+ {
+     HTTPContext *s = h->priv_data;
+     if (s->hd)
+         ffurl_close(s->hd);
+     return 0;
+ }
+ static int http_proxy_open(URLContext *h, const char *uri, int flags)
+ {
+     HTTPContext *s = h->priv_data;
+     char hostname[1024], hoststr[1024];
+     char auth[1024], pathbuf[1024], *path;
+     char line[1024], lower_url[100];
+     int port, ret = 0;
+     HTTPAuthType cur_auth_type;
+     char *authstr;
+     h->is_streamed = 1;
+     av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
+                  pathbuf, sizeof(pathbuf), uri);
+     ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
+     path = pathbuf;
+     if (*path == '/')
+         path++;
+     ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
+                 NULL);
+ redo:
+     ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
+                      &h->interrupt_callback, NULL);
+     if (ret < 0)
+         return ret;
+     authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
+                                            path, "CONNECT");
+     snprintf(s->buffer, sizeof(s->buffer),
+              "CONNECT %s HTTP/1.1\r\n"
+              "Host: %s\r\n"
+              "Connection: close\r\n"
+              "%s%s"
+              "\r\n",
+              path,
+              hoststr,
+              authstr ? "Proxy-" : "", authstr ? authstr : "");
+     av_freep(&authstr);
+     if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
+         goto fail;
+     s->buf_ptr = s->buffer;
+     s->buf_end = s->buffer;
+     s->line_count = 0;
+     s->filesize = -1;
+     cur_auth_type = s->proxy_auth_state.auth_type;
+     for (;;) {
+         int new_loc;
+         // Note: This uses buffering, potentially reading more than the
+         // HTTP header. If tunneling a protocol where the server starts
+         // the conversation, we might buffer part of that here, too.
+         // Reading that requires using the proper ffurl_read() function
+         // on this URLContext, not using the fd directly (as the tls
+         // protocol does). This shouldn't be an issue for tls though,
+         // since the client starts the conversation there, so there
+         // is no extra data that we might buffer up here.
+         if (http_get_line(s, line, sizeof(line)) < 0) {
+             ret = AVERROR(EIO);
+             goto fail;
+         }
+         av_dlog(h, "header='%s'\n", line);
+         ret = process_line(h, line, s->line_count, &new_loc);
+         if (ret < 0)
+             goto fail;
+         if (ret == 0)
+             break;
+         s->line_count++;
+     }
+     if (s->http_code == 407 && cur_auth_type == HTTP_AUTH_NONE &&
+         s->proxy_auth_state.auth_type != HTTP_AUTH_NONE) {
+         ffurl_close(s->hd);
+         s->hd = NULL;
+         goto redo;
+     }
+     if (s->http_code < 400)
+         return 0;
+     ret = AVERROR(EIO);
+ fail:
+     http_proxy_close(h);
+     return ret;
+ }
+ static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
+ {
+     HTTPContext *s = h->priv_data;
+     return ffurl_write(s->hd, buf, size);
+ }
+ URLProtocol ff_httpproxy_protocol = {
+     .name                = "httpproxy",
+     .url_open            = http_proxy_open,
+     .url_read            = http_buf_read,
+     .url_write           = http_proxy_write,
+     .url_close           = http_proxy_close,
+     .url_get_file_handle = http_get_file_handle,
+     .priv_data_size      = sizeof(HTTPContext),
+ };
+ #endif
diff --combined libavformat/rtmpproto.c
index e73158f12ce189c9b4bb0653d9761323c0d0cd9b,37c3d95acad862dad937863bea6ce4e5950c4132..8935f56369497e7deb32ada6126d18f1c62287ee
@@@ -2,20 -2,20 +2,20 @@@
   * RTMP network protocol
   * Copyright (c) 2009 Kostya Shishkov
   *
 - * 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
   */
  
@@@ -74,6 -74,7 +74,7 @@@ typedef struct RTMPContext 
      int           skip_bytes;                 ///< number of bytes to skip from the input FLV stream in the next write call
      uint8_t       flv_header[11];             ///< partial incoming flv packet header
      int           flv_header_bytes;           ///< number of initialized bytes in flv_header
+     int           nb_invokes;                 ///< keeps track of invoke messages
  } RTMPContext;
  
  #define PLAYER_KEY_OPEN_PART_LEN 30   ///< length of partial key used for first client digest signing
@@@ -166,7 -167,7 +167,7 @@@ static void gen_release_stream(URLConte
      av_log(s, AV_LOG_DEBUG, "Releasing stream...\n");
      p = pkt.data;
      ff_amf_write_string(&p, "releaseStream");
-     ff_amf_write_number(&p, 2.0);
+     ff_amf_write_number(&p, ++rt->nb_invokes);
      ff_amf_write_null(&p);
      ff_amf_write_string(&p, rt->playpath);
  
@@@ -189,7 -190,7 +190,7 @@@ static void gen_fcpublish_stream(URLCon
      av_log(s, AV_LOG_DEBUG, "FCPublish stream...\n");
      p = pkt.data;
      ff_amf_write_string(&p, "FCPublish");
-     ff_amf_write_number(&p, 3.0);
+     ff_amf_write_number(&p, ++rt->nb_invokes);
      ff_amf_write_null(&p);
      ff_amf_write_string(&p, rt->playpath);
  
@@@ -212,7 -213,7 +213,7 @@@ static void gen_fcunpublish_stream(URLC
      av_log(s, AV_LOG_DEBUG, "UnPublishing stream...\n");
      p = pkt.data;
      ff_amf_write_string(&p, "FCUnpublish");
-     ff_amf_write_number(&p, 5.0);
+     ff_amf_write_number(&p, ++rt->nb_invokes);
      ff_amf_write_null(&p);
      ff_amf_write_string(&p, rt->playpath);
  
@@@ -234,7 -235,7 +235,7 @@@ static void gen_create_stream(URLContex
  
      p = pkt.data;
      ff_amf_write_string(&p, "createStream");
-     ff_amf_write_number(&p, rt->is_input ? 3.0 : 4.0);
+     ff_amf_write_number(&p, ++rt->nb_invokes);
      ff_amf_write_null(&p);
  
      ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
diff --combined libavformat/rtpdec.c
index f264efe92e4627f188d2597db1d5bfe70bf4d0aa,88cf15bdea4efee65cfb3cec098bab65c93f2de8..95a1edaa5555e3107918daffb7c2623ba5f1cd4a
@@@ -2,20 -2,20 +2,20 @@@
   * RTP input format
   * Copyright (c) 2002 Fabrice Bellard
   *
 - * 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
   */
  
@@@ -83,11 -83,6 +83,11 @@@ void av_register_rtp_dynamic_payload_ha
      ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler);
      ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler);
      ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
 +
 +    ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler);
 +    ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler);
 +    ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler);
 +    ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler);
  }
  
  RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
@@@ -429,7 -424,7 +429,7 @@@ static void finalize_packet(RTPDemuxCon
      if (timestamp == RTP_NOTS_VALUE)
          return;
  
-     if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE) {
+     if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
          int64_t addend;
          int delta_timestamp;
  
  
      if (!s->base_timestamp)
          s->base_timestamp = timestamp;
-     pkt->pts = s->range_start_offset + timestamp - s->base_timestamp;
+     /* assume that the difference is INT32_MIN < x < INT32_MAX, but allow the first timestamp to exceed INT32_MAX */
+     if (!s->timestamp)
+         s->unwrapped_timestamp += timestamp;
+     else
+         s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
+     s->timestamp = timestamp;
+     pkt->pts = s->unwrapped_timestamp + s->range_start_offset - s->base_timestamp;
  }
  
  static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
diff --combined libavformat/rtpdec.h
index dbb45f5709b63fac8c7893e5dbe9880ec44c635f,eb1e62daec2fdf69d22e85b96d49e3bbba6a4a47..eb468be9e5ecabf3d8116ceb1b641316fa46de2b
@@@ -3,20 -3,20 +3,20 @@@
   * Copyright (c) 2002 Fabrice Bellard
   * Copyright (c) 2006 Ryan Martell <rdm4@martellventures.com>
   *
 - * 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
   */
  #ifndef AVFORMAT_RTPDEC_H
@@@ -151,6 -151,7 +151,7 @@@ struct RTPDemuxContext 
      uint32_t timestamp;
      uint32_t base_timestamp;
      uint32_t cur_timestamp;
+     int64_t  unwrapped_timestamp;
      int64_t  range_start_offset;
      int max_payload_size;
      struct MpegTSContext *ts;   /* only used for MP2T payloads */
diff --combined libavformat/rtspdec.c
index f55b8cf2a2ff54e3465bf82e2a46fed137f14ea2,1b4982f2122484d3a1d567f74ce7bb9f4bd6c13b..8699f77918eca9fe3d77dc4a97f05bb5f57a3207
@@@ -2,20 -2,20 +2,20 @@@
   * RTSP demuxer
   * Copyright (c) 2002 Fabrice Bellard
   *
 - * 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
   */
  
@@@ -52,6 -52,8 +52,8 @@@ static int rtsp_read_play(AVFormatConte
                  rtpctx->last_rtcp_ntp_time  = AV_NOPTS_VALUE;
                  rtpctx->first_rtcp_ntp_time = AV_NOPTS_VALUE;
                  rtpctx->base_timestamp      = 0;
+                 rtpctx->timestamp           = 0;
+                 rtpctx->unwrapped_timestamp = 0;
                  rtpctx->rtcp_ts_offset      = 0;
              }
          }
@@@ -203,7 -205,7 +205,7 @@@ redo
      id  = buf[0];
      len = AV_RB16(buf + 1);
      av_dlog(s, "id=%d len=%d\n", id, len);
 -    if (len > buf_size || len < 12)
 +    if (len > buf_size || len < 8)
          goto redo;
      /* get the data */
      ret = ffurl_read_complete(rt->rtsp_hd, buf, len);
diff --combined libavformat/segafilm.c
index 1ae105eb77ba0013c482f4405dab611b23111c55,b75c8774ce7ad278bf3d94d057dd1f54f471cf4f..0199ee1f5f55629153a9bbbd7eb05291141cc917
@@@ -2,20 -2,20 +2,20 @@@
   * Sega FILM Format (CPK) Demuxer
   * Copyright (c) 2003 The ffmpeg Project
   *
 - * 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
   */
  
@@@ -34,6 -34,7 +34,7 @@@
  #define FDSC_TAG MKBETAG('F', 'D', 'S', 'C')
  #define STAB_TAG MKBETAG('S', 'T', 'A', 'B')
  #define CVID_TAG MKBETAG('c', 'v', 'i', 'd')
+ #define RAW_TAG  MKBETAG('r', 'a', 'w', ' ')
  
  typedef struct {
    int stream;
@@@ -129,8 -130,11 +130,11 @@@ static int film_read_header(AVFormatCon
  
      if (AV_RB32(&scratch[8]) == CVID_TAG) {
          film->video_type = CODEC_ID_CINEPAK;
-     } else
+     } else if (AV_RB32(&scratch[8]) == RAW_TAG) {
+         film->video_type = CODEC_ID_RAWVIDEO;
+     } else {
          film->video_type = CODEC_ID_NONE;
+     }
  
      /* initialize the decoder streams */
      if (film->video_type) {
          st->codec->codec_tag = 0;  /* no fourcc */
          st->codec->width = AV_RB32(&scratch[16]);
          st->codec->height = AV_RB32(&scratch[12]);
+         if (film->video_type == CODEC_ID_RAWVIDEO) {
+             if (scratch[20] == 24) {
+                 st->codec->pix_fmt = PIX_FMT_RGB24;
+             } else {
+                 av_log(s, AV_LOG_ERROR, "raw video is using unhandled %dbpp\n", scratch[20]);
+                 return -1;
+             }
+         }
      }
  
      if (film->audio_type) {
diff --combined libavformat/version.h
index 954d87a88e3f6e9c1109eedadba4b7118d5afbe9,d56bdb7c87e4a01ec80cef20e822d2ca8e714ba6..5c7ed6863e06039f2f9a8c6476e62281b5690d78
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * Version macros.
   *
 - * 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
   */
  
@@@ -24,7 -24,7 +24,7 @@@
  #include "libavutil/avutil.h"
  
  #define LIBAVFORMAT_VERSION_MAJOR 53
- #define LIBAVFORMAT_VERSION_MINOR 20
 -#define LIBAVFORMAT_VERSION_MINOR 15
++#define LIBAVFORMAT_VERSION_MINOR 21
  #define LIBAVFORMAT_VERSION_MICRO  0
  
  #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
@@@ -68,9 -68,6 +68,9 @@@
  #ifndef FF_API_SDP_CREATE
  #define FF_API_SDP_CREATE              (LIBAVFORMAT_VERSION_MAJOR < 54)
  #endif
 +#ifndef FF_API_ALLOC_OUTPUT_CONTEXT
 +#define FF_API_ALLOC_OUTPUT_CONTEXT    (LIBAVFORMAT_VERSION_MAJOR < 54)
 +#endif
  #ifndef FF_API_FORMAT_PARAMETERS
  #define FF_API_FORMAT_PARAMETERS       (LIBAVFORMAT_VERSION_MAJOR < 54)
  #endif
  #ifndef FF_API_REORDER_PRIVATE
  #define FF_API_REORDER_PRIVATE         (LIBAVFORMAT_VERSION_MAJOR < 54)
  #endif
+ #ifndef FF_API_OLD_INTERRUPT_CB
+ #define FF_API_OLD_INTERRUPT_CB        (LIBAVFORMAT_VERSION_MAJOR < 54)
+ #endif
  
  #endif /* AVFORMAT_VERSION_H */
diff --combined tests/lavf-regression.sh
index 292845d4e459e4840aabd2807d88d592b97f52cd,4a72e0ac1c43dc360d29349d07af6bbbefd9ea5e..1c579060cc4085f1691c51c58d34074b958ee9c1
@@@ -44,26 -44,26 +44,26 @@@ do_audio_only(
  }
  
  if [ -n "$do_avi" ] ; then
 -do_lavf avi "-acodec mp2"
 +do_lavf avi "-acodec mp2 -ab 64k"
  fi
  
  if [ -n "$do_asf" ] ; then
 -do_lavf asf "-acodec mp2" "-r 25"
 +do_lavf asf "-acodec mp2 -ab 64k" "-r 25"
  fi
  
  if [ -n "$do_rm" ] ; then
  file=${outfile}lavf.rm
 -do_avconv $file $DEC_OPTS -f image2 -vcodec pgmyuv -i $raw_src $DEC_OPTS -ar 44100 -f s16le -i $pcm_src $ENC_OPTS -t 1 -qscale 10 -acodec ac3_fixed -b:a 64k
 +do_avconv $file $DEC_OPTS -f image2 -vcodec pgmyuv -i $raw_src $DEC_OPTS -ar 44100 -f s16le -i $pcm_src $ENC_OPTS -t 1 -qscale 10 -acodec ac3_fixed -ab 64k
  # broken
  #do_avconv_crc $file -i $target_path/$file
  fi
  
  if [ -n "$do_mpg" ] ; then
 -do_lavf mpg
 +do_lavf mpg "-ab 64k"
  fi
  
  if [ -n "$do_mxf" ] ; then
 -do_lavf mxf "-ar 48000 -bf 2 -timecode_frame_start 264363"
 +do_lavf mxf "-ar 48000 -bf 2 -timecode 02:56:14:13"
  fi
  
  if [ -n "$do_mxf_d10" ]; then
@@@ -71,7 -71,7 +71,7 @@@ do_lavf mxf_d10 "-ar 48000 -ac 2 -r 25 
  fi
  
  if [ -n "$do_ts" ] ; then
- do_lavf ts "-ab 64k"
 -do_lavf ts "-mpegts_transport_stream_id 42"
++do_lavf ts "-ab 64k -mpegts_transport_stream_id 42"
  fi
  
  if [ -n "$do_swf" ] ; then
@@@ -79,7 -79,7 +79,7 @@@ do_lavf swf -a
  fi
  
  if [ -n "$do_ffm" ] ; then
 -do_lavf ffm
 +do_lavf ffm "-ab 64k"
  fi
  
  if [ -n "$do_flv_fmt" ] ; then
@@@ -87,7 -87,7 +87,7 @@@ do_lavf flv -a
  fi
  
  if [ -n "$do_mov" ] ; then
 -do_lavf mov "-acodec pcm_alaw -c:v mpeg4"
 +do_lavf mov "-acodec pcm_alaw -vcodec mpeg4"
  fi
  
  if [ -n "$do_dv_fmt" ] ; then
@@@ -99,11 -99,11 +99,11 @@@ do_lavf gxf "-ar 48000 -r 25 -s pal -a
  fi
  
  if [ -n "$do_nut" ] ; then
 -do_lavf nut "-acodec mp2"
 +do_lavf nut "-acodec mp2 -ab 64k"
  fi
  
  if [ -n "$do_mkv" ] ; then
 -do_lavf mkv "-c:a mp2 -c:v mpeg4"
 +do_lavf mkv "-acodec mp2 -ab 64k -vcodec mpeg4"
  fi
  
  
diff --combined tests/ref/lavf/ts
index 3b2dad1b5e0a394e7dabad26baf007c77a9e7fe5,ebe6a77566c8c1e657c48e43f478badbd5465e19..018d61813a3337c00faca19cc475c68aa294a821
@@@ -1,3 -1,3 +1,3 @@@
- 151774afed45b19da9b7e83613a1e72b *./tests/data/lavf/lavf.ts
 -293142d7286db15e5f4d7d1ca0d9c97c *./tests/data/lavf/lavf.ts
++024f0cdd4c51a158b2a38b901d2ed2e5 *./tests/data/lavf/lavf.ts
  406644 ./tests/data/lavf/lavf.ts
  ./tests/data/lavf/lavf.ts CRC=0x133216c1