]> git.sesse.net Git - ffmpeg/commitdiff
Merge remote-tracking branch 'qatar/master'
authorMichael Niedermayer <michaelni@gmx.at>
Wed, 23 Nov 2011 03:02:17 +0000 (04:02 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Wed, 23 Nov 2011 03:02:17 +0000 (04:02 +0100)
* qatar/master: (22 commits)
  aacdec: Fix PS in ADTS.
  avconv: Consistently use PIX_FMT_NONE.
  dsputil: use cpuflags in x86 emu_edge_core
  dsputil: use movups instead of movdqu in ff_emu_edge_core_sse()
  wma: initialize prev_block_len_bits, next_block_len_bits, and block_len_bits.
  mov: Remove some redundant and obsolete comments.
  Add libavutil/mathematics.h #includes for INFINITY
  doxy: structure libavformat groups
  doxy: introduce an empty structure in libavcodec
  doxy: provide a start page and document libavutil
  doxy: cleanup pixfmt.h
  regtest: split video encode/decode tests into individual targets
  ARM: add explicit .arch and .fpu directives to asm.S
  pthread: do not touch has_b_frames
  avconv: cleanup the transcoding loop in output_packet().
  avconv: split subtitle transcoding out of output_packet().
  avconv: split video transcoding out of output_packet().
  avconv: split audio transcoding out of output_packet().
  avconv: reindent.
  avconv: move streamcopy-only code out of decoding loop.
  ...

Conflicts:
avconv.c
libavcodec/aaccoder.c
libavcodec/pthread.c
libavcodec/version.h
libavutil/audioconvert.h
libavutil/avutil.h
libavutil/mem.h
tests/ref/vsynth1/dv
tests/ref/vsynth1/mpeg2thread
tests/ref/vsynth2/dv
tests/ref/vsynth2/mpeg2thread

Merged-by: Michael Niedermayer <michaelni@gmx.at>
45 files changed:
1  2 
avconv.c
cmdutils.c
configure
ffmpeg.c
libavcodec/aaccoder.c
libavcodec/aacdec.c
libavcodec/arm/asm.S
libavcodec/avcodec.h
libavcodec/nellymoserenc.c
libavcodec/pthread.c
libavcodec/version.h
libavcodec/wma.c
libavcodec/x86/dsputil_mmx.c
libavcodec/x86/dsputil_yasm.asm
libavformat/avformat.h
libavformat/mov.c
libavutil/adler32.h
libavutil/aes.h
libavutil/audioconvert.h
libavutil/avstring.h
libavutil/avutil.h
libavutil/base64.h
libavutil/dict.h
libavutil/error.h
libavutil/imgutils.h
libavutil/intmath.h
libavutil/lzo.h
libavutil/mathematics.h
libavutil/md5.h
libavutil/mem.h
libavutil/opt.h
libavutil/pixfmt.h
libavutil/random_seed.h
libavutil/rational.h
libavutil/sha.h
libavutil/tree.h
tests/codec-regression.sh
tests/ref/vsynth1/dv
tests/ref/vsynth1/dv_411
tests/ref/vsynth1/mpeg2thread
tests/ref/vsynth1/mpeg2thread_ilace
tests/ref/vsynth2/dv
tests/ref/vsynth2/dv_411
tests/ref/vsynth2/mpeg2thread
tests/ref/vsynth2/mpeg2thread_ilace

diff --combined avconv.c
index 81f63eca88c9f42d71e49b9d0e1d4db6b70e07fe,a03c9c1ba9d636175f00f830f3a850618153d0d1..ec2c573d7bec852f742982ab4495f38e40033630
+++ 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
  }
  
 +/* 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 };
@@@ -678,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){
@@@ -722,11 -673,11 +722,11 @@@ static void choose_pixel_fmt(AVStream *
                  p= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_BGRA, PIX_FMT_NONE};
              }
          }
-         for(; *p!=-1; p++){
+         for (; *p != PIX_FMT_NONE; p++) {
              if(*p == st->codec->pix_fmt)
                  break;
          }
-         if (*p == -1) {
+         if (*p == PIX_FMT_NONE) {
              if(st->codec->pix_fmt != PIX_FMT_NONE)
                  av_log(NULL, AV_LOG_WARNING,
                          "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
@@@ -818,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,
@@@ -1132,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);
@@@ -1382,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=%0.2f bitrate=%6.1fkbits/s",
 -            (double)total_size / 1024, ti1, bitrate);
 +             "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 (nb_frames_dup || nb_frames_drop)
          snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
@@@ -1596,26 -1528,327 +1596,338 @@@ static void flush_encoders(OutputStrea
      }
  }
  
 -    ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, decoded_frame->pkt_pts,
 -                                                 decoded_frame->pkt_dts);
+ /*
+  * Check whether a packet from ist should be written into ost at this time
+  */
+ static int check_output_constraints(InputStream *ist, OutputStream *ost)
+ {
+     OutputFile *of = &output_files[ost->file_index];
+     int ist_index  = ist - input_streams;
+     if (ost->source_index != ist_index)
+         return 0;
+     if (of->start_time && ist->pts < of->start_time)
+         return 0;
+     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;
+         return 0;
+     }
+     return 1;
+ }
+ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
+ {
+     OutputFile *of = &output_files[ost->file_index];
+     int64_t ost_tb_start_time = av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base);
++    AVPicture pict;
+     AVPacket opkt;
+     av_init_packet(&opkt);
+     if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
+         !ost->copy_initial_nonkeyframes)
+         return;
+     /* force the input stream PTS */
+     if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
+         audio_size += pkt->size;
+     else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
+         video_size += pkt->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;
+     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, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY))
+             opkt.destruct = av_destruct_packet;
+     } else {
+         opkt.data = pkt->data;
+         opkt.size = pkt->size;
+     }
++    if (of->ctx->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(of->ctx, &opkt, ost->st->codec, ost->bitstream_filters);
+     ost->st->codec->frame_number++;
+     ost->frame_number++;
+     av_free_packet(&opkt);
+ }
+ static void rate_emu_sleep(InputStream *ist)
+ {
+     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)
+             usleep(pts - now);
+     }
+ }
+ static int transcode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
+ {
+     static unsigned int samples_size = 0;
+     int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt);
+     uint8_t *decoded_data_buf  = NULL;
+     int      decoded_data_size = 0;
+     int i, ret;
+     if (pkt && samples_size < FFMAX(pkt->size * bps, AVCODEC_MAX_AUDIO_FRAME_SIZE)) {
+         av_free(samples);
+         samples_size = FFMAX(pkt->size * bps, AVCODEC_MAX_AUDIO_FRAME_SIZE);
+         samples      = av_malloc(samples_size);
+     }
+     decoded_data_size = samples_size;
+     ret = avcodec_decode_audio3(ist->st->codec, samples, &decoded_data_size,
+                                 pkt);
+     if (ret < 0)
+         return ret;
+     pkt->data   += ret;
+     pkt->size   -= ret;
+     *got_output  = decoded_data_size > 0;
+     /* Some bug in mpeg audio decoder gives */
+     /* decoded_data_size < 0, it seems they are overflows */
+     if (!*got_output) {
+         /* no audio frame */
+         return 0;
+     }
+     decoded_data_buf = (uint8_t *)samples;
+     ist->next_pts   += ((int64_t)AV_TIME_BASE/bps * decoded_data_size) /
+                        (ist->st->codec->sample_rate * ist->st->codec->channels);
+     // preprocess audio (volume)
+     if (audio_volume != 256) {
+         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);
+         }
+     }
+     rate_emu_sleep(ist);
+     for (i = 0; i < nb_output_streams; i++) {
+         OutputStream *ost = &output_streams[i];
+         if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
+             continue;
+         do_audio_out(output_files[ost->file_index].ctx, ost, ist,
+                      decoded_data_buf, decoded_data_size);
+     }
+     return 0;
+ }
+ static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int64_t *pkt_pts)
+ {
+     AVFrame *decoded_frame, *filtered_frame = NULL;
+     void *buffer_to_free = NULL;
+     int i, ret = 0;
+     float quality;
+ #if CONFIG_AVFILTER
+     int frame_available = 1;
+ #endif
+     if (!(decoded_frame = avcodec_alloc_frame()))
+         return AVERROR(ENOMEM);
+     pkt->pts  = *pkt_pts;
+     pkt->dts  = ist->pts;
+     *pkt_pts  = AV_NOPTS_VALUE;
+     ret = avcodec_decode_video2(ist->st->codec,
+                                 decoded_frame, got_output, pkt);
+     if (ret < 0)
+         goto fail;
+     quality = same_quant ? decoded_frame->quality : 0;
+     if (!*got_output) {
+         /* no picture yet */
+         av_freep(&decoded_frame);
+         return 0;
+     }
 -            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);
++    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 *
+                           ist->st->codec->time_base.num * ticks) /
+                           ist->st->codec->time_base.den;
+     }
+     pkt->size = 0;
+     pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
+     rate_emu_sleep(ist);
+     for (i = 0; i < nb_output_streams; i++) {
+         OutputStream *ost = &output_streams[i];
+         int frame_size;
+         if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
+             continue;
+ #if CONFIG_AVFILTER
+         if (ost->input_video_filter) {
 -            AVRational ist_pts_tb;
 -            if (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 (!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 = avfilter_poll_frame(ost->output_video_filter->inputs[0]);
+         }
+         while (frame_available) {
 -                ost->st->codec->sample_aspect_ratio = ost->picref->video->pixel_aspect;
++            if (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);
++                }
++            }
+             if (ost->picref->video && !ost->frame_aspect_ratio)
++                ost->st->codec->sample_aspect_ratio = ost->picref->video->sample_aspect_ratio;
+ #else
+             filtered_frame = decoded_frame;
+ #endif
+             do_video_out(output_files[ost->file_index].ctx, ost, ist, filtered_frame, &frame_size,
+                          same_quant ? quality : ost->st->codec->global_quality);
+             if (vstats_filename && frame_size)
+                 do_video_stats(output_files[ost->file_index].ctx, ost, frame_size);
+ #if CONFIG_AVFILTER
++            cont:
+             frame_available = ost->output_video_filter && avfilter_poll_frame(ost->output_video_filter->inputs[0]);
+             if (ost->picref)
+                 avfilter_unref_buffer(ost->picref);
+         }
+         av_freep(&filtered_frame);
+ #endif
+     }
+ fail:
+     av_free(buffer_to_free);
+     av_freep(&decoded_frame);
+     return ret;
+ }
+ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
+ {
+     AVSubtitle subtitle;
+     int i, ret = avcodec_decode_subtitle2(ist->st->codec,
+                                           &subtitle, got_output, pkt);
+     if (ret < 0)
+         return ret;
+     if (!*got_output)
+         return 0;
+     pkt->size = 0;
+     rate_emu_sleep(ist);
+     for (i = 0; i < nb_output_streams; i++) {
+         OutputStream *ost = &output_streams[i];
+         if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
+             continue;
+         do_subtitle_out(output_files[ost->file_index].ctx, ost, ist, &subtitle, pkt->pts);
+     }
+     avsubtitle_free(&subtitle);
+     return 0;
+ }
  /* 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_pts = AV_NOPTS_VALUE;
- #if CONFIG_AVFILTER
-     int frame_available;
- #endif
-     float quality;
  
      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;
          pkt_pts = av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
  
      //while we have more to decode or while the decoder did output something on EOF
-     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;
+     while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {
      handle_eof:
          ist->pts= ist->next_pts;
  
                     "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;
-         data_size = avpkt.size;
-         subtitle_to_free = NULL;
-         if (ist->decoding_needed) {
-             switch(ist->st->codec->codec_type) {
-             case AVMEDIA_TYPE_AUDIO:{
-                 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);
-                 }
-                 decoded_data_size= samples_size;
-                     /* XXX: could avoid copy if PCM 16 bits with same
-                        endianness as CPU */
-                 ret = avcodec_decode_audio3(ist->st->codec, samples, &decoded_data_size,
-                                             &avpkt);
-                 if (ret < 0)
-                     return ret;
-                 avpkt.data += ret;
-                 avpkt.size -= ret;
-                 data_size   = ret;
-                 got_output  = decoded_data_size > 0;
-                 /* Some bug in mpeg audio decoder gives */
-                 /* decoded_data_size < 0, it seems they are overflows */
-                 if (!got_output) {
-                     /* no audio frame */
-                     continue;
-                 }
-                 decoded_data_buf = (uint8_t *)samples;
-                 ist->next_pts += ((int64_t)AV_TIME_BASE/bps * decoded_data_size) /
-                     (ist->st->codec->sample_rate * ist->st->codec->channels);
-                 break;}
-             case AVMEDIA_TYPE_VIDEO:
-                     if (!(decoded_frame = avcodec_alloc_frame()))
-                         return AVERROR(ENOMEM);
-                     avpkt.pts = pkt_pts;
-                     avpkt.dts = ist->pts;
-                     pkt_pts = AV_NOPTS_VALUE;
-                     ret = avcodec_decode_video2(ist->st->codec,
-                                                 decoded_frame, &got_output, &avpkt);
-                     quality = same_quant ? decoded_frame->quality : 0;
-                     if (ret < 0)
-                         goto fail;
-                     if (!got_output) {
-                         /* no picture yet */
-                         av_freep(&decoded_frame);
-                         goto discard_packet;
-                     }
-                     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 *
-                                           ist->st->codec->time_base.num * ticks) /
-                             ist->st->codec->time_base.den;
-                     }
-                     avpkt.size = 0;
-                     buffer_to_free = NULL;
-                     pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
-                     break;
-             case AVMEDIA_TYPE_SUBTITLE:
-                 ret = avcodec_decode_subtitle2(ist->st->codec,
-                                                &subtitle, &got_output, &avpkt);
-                 if (ret < 0)
-                     return ret;
-                 if (!got_output) {
-                     goto discard_packet;
-                 }
-                 subtitle_to_free = &subtitle;
-                 avpkt.size = 0;
-                 break;
-             default:
-                 return -1;
-             }
-         } else {
-             switch(ist->st->codec->codec_type) {
-             case AVMEDIA_TYPE_AUDIO:
-                 ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
-                     ist->st->codec->sample_rate;
-                 break;
-             case AVMEDIA_TYPE_VIDEO:
-                 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 *
-                                       ist->st->codec->time_base.num * ticks) /
-                         ist->st->codec->time_base.den;
-                 }
-                 break;
-             }
-             avpkt.size = 0;
-         }
-         // preprocess audio (volume)
-         if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
-             if (audio_volume != 256) {
-                 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 (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)
-                 usleep(pts - now);
+         switch(ist->st->codec->codec_type) {
+         case AVMEDIA_TYPE_AUDIO:
+             ret = transcode_audio    (ist, &avpkt, &got_output);
+             break;
+         case AVMEDIA_TYPE_VIDEO:
+             ret = transcode_video    (ist, &avpkt, &got_output, &pkt_pts);
+             break;
+         case AVMEDIA_TYPE_SUBTITLE:
+             ret = transcode_subtitles(ist, &avpkt, &got_output);
+             break;
+         default:
+             return -1;
          }
-         /* if output time reached then transcode raw format,
-            encode packets and output them */
-         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;
+         if (ret < 0)
+             return ret;
+         if (!got_output) {
+             if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
                  continue;
-             }
+             goto discard_packet;
+         }
+     }
+  discard_packet:
  
- #if CONFIG_AVFILTER
-             if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
-                 ost->input_video_filter) {
-                 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;
-                 }
+     /* handle stream copy */
+     if (!ist->decoding_needed) {
+         rate_emu_sleep(ist);
+         switch (ist->st->codec->codec_type) {
+         case AVMEDIA_TYPE_AUDIO:
+             ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
+                              ist->st->codec->sample_rate;
+             break;
+         case AVMEDIA_TYPE_VIDEO:
+             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 *
+                                   ist->st->codec->time_base.num * ticks) /
+                                   ist->st->codec->time_base.den;
              }
-             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 (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].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->sample_aspect_ratio;
- #endif
-                         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)) &&
-                         !ost->copy_initial_nonkeyframes)
- #if !CONFIG_AVFILTER
-                         continue;
- #else
-                         goto cont;
- #endif
-                     /* no reencoding needed : output the packet directly */
-                     /* force the input stream PTS */
-                     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;
-                     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;
-                     }
+             break;
+         }
+     }
+     for (i = 0; pkt && i < nb_ostreams; i++) {
+         ost = &ost_table[i];
  
-                     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++;
-                     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]);
-                 if (ost->picref)
-                     avfilter_unref_buffer(ost->picref);
-             }
-             av_freep(&filtered_frame);
- #endif
-             }
+         if (!check_output_constraints(ist, ost) || ost->encoding_needed)
+             continue;
  
- 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;
+         do_streamcopy(ist, ost, pkt);
      }
-  discard_packet:
  
      return 0;
  }
@@@ -2005,6 -1962,16 +2041,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;
@@@ -2094,25 -2062,16 +2130,25 @@@ static int transcode_init(OutputFile *o
                  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->extradata_size= icodec->extradata_size;
 +
 +            codec->time_base = ist->st->time_base;
 +            if(!strcmp(oc->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(!(oc->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:
                      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;
  
                                        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 && !(oc->oformat->flags & AVFMT_VARIABLE_FPS)))){
 +                    av_log(oc, 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);
          }
      }
  
              ret = AVERROR(EINVAL);
              goto dump_format;
          }
 -        assert_avoptions(output_files[i].opts);
 +//        assert_avoptions(output_files[i].opts);
          if (strcmp(oc->oformat->name, "rtp")) {
              want_sdp = 0;
          }
@@@ -2421,7 -2392,6 +2457,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);
@@@ -2935,18 -2851,16 +2971,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:
@@@ -3551,7 -3463,7 +3587,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;
@@@ -3597,7 -3509,7 +3633,7 @@@ 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 = avformat_alloc_context();
  
          // 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)
@@@ -3644,20 -3555,36 +3680,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);
          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) {
@@@ -3986,7 -3914,7 +4022,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)
@@@ -4191,20 -4117,6 +4227,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);
@@@ -4281,7 -4193,6 +4317,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" },
@@@ -4349,13 -4260,6 +4385,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();
diff --combined cmdutils.c
index 76ec5b665a800037143810f585e40be95f53f250,1c2bf4696b1f0633c04055bff2b76ac219ddbb26..ee7d66063ed1cd4bb34440a81dfdf717576504ca
@@@ -2,20 -2,20 +2,20 @@@
   * Various utilities for command line tools
   * 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
   */
  
@@@ -35,6 -35,7 +35,7 @@@
  #include "libswscale/swscale.h"
  #include "libpostproc/postprocess.h"
  #include "libavutil/avstring.h"
+ #include "libavutil/mathematics.h"
  #include "libavutil/parseutils.h"
  #include "libavutil/pixdesc.h"
  #include "libavutil/eval.h"
@@@ -213,7 -214,6 +214,7 @@@ static inline void prepare_app_argument
  }
  #endif /* WIN32 && !__MINGW32CE__ */
  
 +
  int parse_option(void *optctx, const char *opt, const char *arg, const OptionDef *options)
  {
      const OptionDef *po;
@@@ -352,27 -352,25 +353,27 @@@ void parse_loglevel(int argc, char **ar
          opt_loglevel("loglevel", argv[idx + 1]);
  }
  
 -#define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
 +#define FLAGS(o) ((o)->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
  int opt_default(const char *opt, const char *arg)
  {
 -    const AVOption *o;
 +    const AVOption *oc, *of, *os;
      char opt_stripped[128];
      const char *p;
 -    const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc = sws_get_class();
 +    const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc;
  
      if (!(p = strchr(opt, ':')))
          p = opt + strlen(opt);
      av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
  
 -    if ((o = av_opt_find(&cc, opt_stripped, NULL, 0, AV_OPT_SEARCH_CHILDREN|AV_OPT_SEARCH_FAKE_OBJ)) ||
 +    if ((oc = av_opt_find(&cc, opt_stripped, NULL, 0, AV_OPT_SEARCH_CHILDREN|AV_OPT_SEARCH_FAKE_OBJ)) ||
           ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
 -          (o = av_opt_find(&cc, opt+1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
 -        av_dict_set(&codec_opts, opt, arg, FLAGS);
 -    else if ((o = av_opt_find(&fc, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
 -        av_dict_set(&format_opts, opt, arg, FLAGS);
 -    else if ((o = av_opt_find(&sc, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
 +          (oc = av_opt_find(&cc, opt+1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
 +        av_dict_set(&codec_opts, opt, arg, FLAGS(oc));
 +    if ((of = av_opt_find(&fc, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
 +        av_dict_set(&format_opts, opt, arg, FLAGS(of));
 +#if CONFIG_SWSCALE
 +    sc = sws_get_class();
 +    if ((os = av_opt_find(&sc, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
          // XXX we only support sws_flags, not arbitrary sws options
          int ret = av_opt_set(sws_opts, opt, arg, 0);
          if (ret < 0) {
              return ret;
          }
      }
 +#endif
  
 -    if (o)
 +    if (oc || of || os)
          return 0;
      av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
      return AVERROR_OPTION_NOT_FOUND;
@@@ -423,12 -420,6 +424,12 @@@ int opt_loglevel(const char *opt, cons
      return 0;
  }
  
 +int opt_codec_debug(const char *opt, const char *arg)
 +{
 +    av_log_set_level(AV_LOG_DEBUG);
 +    return opt_default(opt, arg);
 +}
 +
  int opt_timelimit(const char *opt, const char *arg)
  {
  #if HAVE_SETRLIMIT
@@@ -472,7 -463,7 +473,7 @@@ static int warned_cfg = 0
          }                                                               \
          if (flags & SHOW_CONFIG) {                                      \
              const char *cfg = libname##_configuration();                \
 -            if (strcmp(LIBAV_CONFIGURATION, cfg)) {                     \
 +            if (strcmp(FFMPEG_CONFIGURATION, cfg)) {                    \
                  if (!warned_cfg) {                                      \
                      av_log(NULL, level,                                 \
                              "%sWARNING: library configuration mismatch\n", \
@@@ -498,23 -489,22 +499,23 @@@ static void print_all_libs_info(int fla
  
  void show_banner(void)
  {
 -    av_log(NULL, AV_LOG_INFO, "%s version " LIBAV_VERSION ", Copyright (c) %d-%d the Libav developers\n",
 +    av_log(NULL, AV_LOG_INFO, "%s version " FFMPEG_VERSION ", Copyright (c) %d-%d the FFmpeg developers\n",
             program_name, program_birth_year, this_year);
      av_log(NULL, AV_LOG_INFO, "  built on %s %s with %s %s\n",
             __DATE__, __TIME__, CC_TYPE, CC_VERSION);
 -    av_log(NULL, AV_LOG_VERBOSE, "  configuration: " LIBAV_CONFIGURATION "\n");
 -    print_all_libs_info(INDENT|SHOW_CONFIG,  AV_LOG_VERBOSE);
 -    print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_VERBOSE);
 +    av_log(NULL, AV_LOG_INFO, "  configuration: " FFMPEG_CONFIGURATION "\n");
 +    print_all_libs_info(INDENT|SHOW_CONFIG,  AV_LOG_INFO);
 +    print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
  }
  
 -void show_version(void) {
 +int opt_version(const char *opt, const char *arg) {
      av_log_set_callback(log_callback_help);
 -    printf("%s " LIBAV_VERSION "\n", program_name);
 +    printf("%s " FFMPEG_VERSION "\n", program_name);
      print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
 +    return 0;
  }
  
 -void show_license(void)
 +int opt_license(const char *opt, const char *arg)
  {
      printf(
  #if CONFIG_NONFREE
      program_name, program_name, program_name
  #endif
      );
 +    return 0;
  }
  
 -void show_formats(void)
 +int opt_formats(const char *opt, const char *arg)
  {
      AVInputFormat *ifmt=NULL;
      AVOutputFormat *ofmt=NULL;
              name,
              long_name ? long_name:" ");
      }
 +    return 0;
  }
  
 -void show_codecs(void)
 +int opt_codecs(const char *opt, const char *arg)
  {
      AVCodec *p=NULL, *p2;
      const char *last_name;
  "even though both encoding and decoding are supported. For example, the h263\n"
  "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
  "worse.\n");
 +    return 0;
  }
  
 -void show_bsfs(void)
 +int opt_bsfs(const char *opt, const char *arg)
  {
      AVBitStreamFilter *bsf=NULL;
  
      while((bsf = av_bitstream_filter_next(bsf)))
          printf("%s\n", bsf->name);
      printf("\n");
 +    return 0;
  }
  
 -void show_protocols(void)
 +int opt_protocols(const char *opt, const char *arg)
  {
 -    void *opaque = NULL;
 -    const char *name;
 +    URLProtocol *up=NULL;
  
      printf("Supported file protocols:\n"
 -           "Input:\n");
 -    while ((name = avio_enum_protocols(&opaque, 0)))
 -        printf("%s\n", name);
 -    printf("Output:\n");
 -    while ((name = avio_enum_protocols(&opaque, 1)))
 -        printf("%s\n", name);
 +           "I.. = Input  supported\n"
 +           ".O. = Output supported\n"
 +           "..S = Seek   supported\n"
 +           "FLAGS NAME\n"
 +           "----- \n");
 +    while((up = av_protocol_next(up)))
 +        printf("%c%c%c   %s\n",
 +               up->url_read  ? 'I' : '.',
 +               up->url_write ? 'O' : '.',
 +               up->url_seek  ? 'S' : '.',
 +               up->name);
 +    return 0;
  }
  
 -void show_filters(void)
 +int opt_filters(const char *opt, const char *arg)
  {
      AVFilter av_unused(**filter) = NULL;
  
      while ((filter = av_filter_next(filter)) && *filter)
          printf("%-16s %s\n", (*filter)->name, (*filter)->description);
  #endif
 +    return 0;
  }
  
 -void show_pix_fmts(void)
 +int opt_pix_fmts(const char *opt, const char *arg)
  {
      enum PixelFormat pix_fmt;
  
                 pix_desc->nb_components,
                 av_get_bits_per_pixel(pix_desc));
      }
 +    return 0;
  }
  
  int show_sample_fmts(const char *opt, const char *arg)
@@@ -843,45 -822,55 +844,45 @@@ int cmdutils_read_file(const char *file
      return ret;
  }
  
 -void init_pts_correction(PtsCorrectionContext *ctx)
 -{
 -    ctx->num_faulty_pts = ctx->num_faulty_dts = 0;
 -    ctx->last_pts = ctx->last_dts = INT64_MIN;
 -}
 -
 -int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts, int64_t dts)
 -{
 -    int64_t pts = AV_NOPTS_VALUE;
 -
 -    if (dts != AV_NOPTS_VALUE) {
 -        ctx->num_faulty_dts += dts <= ctx->last_dts;
 -        ctx->last_dts = dts;
 -    }
 -    if (reordered_pts != AV_NOPTS_VALUE) {
 -        ctx->num_faulty_pts += reordered_pts <= ctx->last_pts;
 -        ctx->last_pts = reordered_pts;
 -    }
 -    if ((ctx->num_faulty_pts<=ctx->num_faulty_dts || dts == AV_NOPTS_VALUE)
 -       && reordered_pts != AV_NOPTS_VALUE)
 -        pts = reordered_pts;
 -    else
 -        pts = dts;
 -
 -    return pts;
 -}
 -
  FILE *get_preset_file(char *filename, size_t filename_size,
                        const char *preset_name, int is_path, const char *codec_name)
  {
      FILE *f = NULL;
      int i;
 -    const char *base[3]= { getenv("AVCONV_DATADIR"),
 +    const char *base[3]= { getenv("FFMPEG_DATADIR"),
                             getenv("HOME"),
 -                           AVCONV_DATADIR,
 +                           FFMPEG_DATADIR,
                           };
  
      if (is_path) {
          av_strlcpy(filename, preset_name, filename_size);
          f = fopen(filename, "r");
      } else {
 +#ifdef _WIN32
 +        char datadir[MAX_PATH], *ls;
 +        base[2] = NULL;
 +
 +        if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
 +        {
 +            for (ls = datadir; ls < datadir + strlen(datadir); ls++)
 +                if (*ls == '\\') *ls = '/';
 +
 +            if (ls = strrchr(datadir, '/'))
 +            {
 +                *ls = 0;
 +                strncat(datadir, "/ffpresets",  sizeof(datadir) - 1 - strlen(datadir));
 +                base[2] = datadir;
 +            }
 +        }
 +#endif
          for (i = 0; i < 3 && !f; i++) {
              if (!base[i])
                  continue;
 -            snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i], i != 1 ? "" : "/.avconv", preset_name);
 +            snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", preset_name);
              f = fopen(filename, "r");
              if (!f && codec_name) {
                  snprintf(filename, filename_size,
 -                         "%s%s/%s-%s.ffpreset", base[i],  i != 1 ? "" : "/.avconv", codec_name, preset_name);
 +                         "%s%s/%s-%s.ffpreset", base[i],  i != 1 ? "" : "/.ffmpeg", codec_name, preset_name);
                  f = fopen(filename, "r");
              }
          }
@@@ -903,7 -892,6 +904,7 @@@ int check_stream_specifier(AVFormatCont
          case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
          case 'd': type = AVMEDIA_TYPE_DATA;     break;
          case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
 +        default: abort(); // never reached, silence warning
          }
          if (type != st->codec->codec_type)
              return 0;
@@@ -1000,6 -988,71 +1001,6 @@@ AVDictionary **setup_find_stream_info_o
      return opts;
  }
  
 -#if CONFIG_AVFILTER
 -
 -static int ffsink_init(AVFilterContext *ctx, const char *args, void *opaque)
 -{
 -    FFSinkContext *priv = ctx->priv;
 -
 -    if (!opaque)
 -        return AVERROR(EINVAL);
 -    *priv = *(FFSinkContext *)opaque;
 -
 -    return 0;
 -}
 -
 -static void null_end_frame(AVFilterLink *inlink) { }
 -
 -static int ffsink_query_formats(AVFilterContext *ctx)
 -{
 -    FFSinkContext *priv = ctx->priv;
 -    enum PixelFormat pix_fmts[] = { priv->pix_fmt, PIX_FMT_NONE };
 -
 -    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
 -    return 0;
 -}
 -
 -AVFilter ffsink = {
 -    .name      = "ffsink",
 -    .priv_size = sizeof(FFSinkContext),
 -    .init      = ffsink_init,
 -
 -    .query_formats = ffsink_query_formats,
 -
 -    .inputs    = (AVFilterPad[]) {{ .name          = "default",
 -                                    .type          = AVMEDIA_TYPE_VIDEO,
 -                                    .end_frame     = null_end_frame,
 -                                    .min_perms     = AV_PERM_READ, },
 -                                  { .name = NULL }},
 -    .outputs   = (AVFilterPad[]) {{ .name = NULL }},
 -};
 -
 -int get_filtered_video_frame(AVFilterContext *ctx, AVFrame *frame,
 -                             AVFilterBufferRef **picref_ptr, AVRational *tb)
 -{
 -    int ret;
 -    AVFilterBufferRef *picref;
 -
 -    if ((ret = avfilter_request_frame(ctx->inputs[0])) < 0)
 -        return ret;
 -    if (!(picref = ctx->inputs[0]->cur_buf))
 -        return AVERROR(ENOENT);
 -    *picref_ptr = picref;
 -    ctx->inputs[0]->cur_buf = NULL;
 -    *tb = ctx->inputs[0]->time_base;
 -
 -    memcpy(frame->data,     picref->data,     sizeof(frame->data));
 -    memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));
 -    frame->interlaced_frame = picref->video->interlaced;
 -    frame->top_field_first  = picref->video->top_field_first;
 -    frame->key_frame        = picref->video->key_frame;
 -    frame->pict_type        = picref->video->pict_type;
 -
 -    return 1;
 -}
 -
 -#endif /* CONFIG_AVFILTER */
 -
  void *grow_array(void *array, int elem_size, int *size, int new_size)
  {
      if (new_size >= INT_MAX / elem_size) {
diff --combined configure
index 511ee7b5d447968743afb4e71ec7335b306c2f8b,57630f2a5350f2c1cc84167d543e345fa885e0f2..9caf9b8e2fabdaeb5c2f242821cedf6a34772c27
+++ 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-libpulse        enable Pulseaudio input via libpulse [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]
@@@ -218,19 -208,16 +218,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.
  
@@@ -300,7 -286,7 +300,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
@@@ -597,10 -583,6 +597,10 @@@ add_cflags()
      append CFLAGS $($filter_cflags "$@")
  }
  
 +add_cxxflags(){
 +    append CXXFLAGS $($filter_cflags "$@")
 +}
 +
  add_asflags(){
      append ASFLAGS $($filter_asflags "$@")
  }
@@@ -625,13 -607,6 +625,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
@@@ -667,14 -642,12 +667,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
  }
  
@@@ -694,17 -667,9 +694,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
  }
@@@ -730,7 -695,7 +730,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
@@@ -741,10 -706,10 +741,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
  }
  
@@@ -761,26 -726,7 +761,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(){
@@@ -812,21 -758,13 +812,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
  }
@@@ -918,14 -853,6 +918,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"
@@@ -986,7 -913,6 +986,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='
@@@ -1137,7 -1054,6 +1137,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
@@@ -1245,7 -1161,6 +1245,7 @@@ CMDLINE_SELECT=
      extra_warnings
      logging
      optimizations
 +    stripping
      symver
      yasm
  "
@@@ -1266,11 -1181,9 +1266,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
@@@ -1373,7 -1283,6 +1373,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"
@@@ -1385,11 -1294,9 +1385,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"
@@@ -1415,18 -1322,13 +1415,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"
@@@ -1450,9 -1352,6 +1450,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"
@@@ -1463,8 -1362,7 +1463,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"
@@@ -1486,7 -1384,6 +1486,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"
@@@ -1496,7 -1393,6 +1496,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"
@@@ -1514,18 -1408,15 +1514,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"
@@@ -1535,7 -1426,6 +1535,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"
@@@ -1572,20 -1462,14 +1572,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"
@@@ -1612,19 -1496,14 +1612,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"
@@@ -1635,15 -1514,15 +1635,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"
  
@@@ -1677,7 -1556,7 +1677,7 @@@ test_deps _encoder _decode
      asv2                                                                \
      bmp                                                                 \
      dnxhd="dnxhd_1080i dnxhd_720p dnxhd_720p_rd"                        \
-     dvvideo="dv dv50"                                                   \
+     dvvideo="dv dv_411 dv50"                                            \
      ffv1                                                                \
      flac                                                                \
      flashsv                                                             \
      mjpeg="jpg mjpeg ljpeg"                                             \
      mp2                                                                 \
      mpeg1video="mpeg mpeg1b"                                            \
-     mpeg2video="mpeg2 mpeg2thread"                                      \
-     mpeg4="mpeg4 mpeg4adv mpeg4nr mpeg4thread error rc"                 \
+     mpeg2video="mpeg2 mpeg2_422 mpeg2_idct_int mpeg2_ilace mpeg2_ivlc_qprd" \
+     mpeg2video="mpeg2thread mpeg2thread_ilace"                          \
+     mpeg4="mpeg4 mpeg4_adap mpeg4_qpel mpeg4_qprd mpeg4adv mpeg4nr"     \
+     mpeg4="mpeg4thread error rc"                                        \
      msmpeg4v3=msmpeg4                                                   \
      msmpeg4v2                                                           \
      pbm=pbmpipe                                                         \
@@@ -1748,7 -1629,7 +1750,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'
@@@ -1757,16 -1638,13 +1759,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=":"
  
@@@ -1792,22 -1670,20 +1794,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)'
@@@ -1823,7 -1699,6 +1825,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'
@@@ -1850,7 -1725,7 +1852,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(){
@@@ -1962,7 -1837,7 +1964,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
@@@ -1976,15 -1851,13 +1978,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
  
@@@ -2026,7 -1899,6 +2028,7 @@@ tmpfile()
  trap 'rm -f -- $TMPFILES' EXIT
  
  tmpfile TMPC  .c
 +tmpfile TMPCPP .cpp
  tmpfile TMPE  $EXESUF
  tmpfile TMPH  .h
  tmpfile TMPO  .o
@@@ -2244,11 -2116,9 +2246,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
@@@ -2303,8 -2173,12 +2305,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
@@@ -2479,7 -2353,6 +2481,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
@@@ -2522,7 -2390,7 +2524,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 ||
          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
  
@@@ -2721,7 -2586,6 +2723,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
  
@@@ -2787,7 -2651,7 +2789,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
@@@ -2968,8 -2832,7 +2970,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
@@@ -2978,36 -2841,28 +2980,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
          :
@@@ -3033,10 -2888,8 +3035,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
@@@ -3052,10 -2905,7 +3054,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
  
@@@ -3133,8 -2970,6 +3135,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
  
@@@ -3172,11 -3000,9 +3174,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
@@@ -3203,13 -3029,12 +3205,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
@@@ -3318,11 -3139,6 +3320,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
  
@@@ -3333,9 -3149,6 +3335,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
@@@ -3375,7 -3188,6 +3377,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}"
@@@ -3390,16 -3202,13 +3392,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}"
@@@ -3410,10 -3219,7 +3412,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}"
@@@ -3421,7 -3227,6 +3423,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}"
@@@ -3451,15 -3256,11 +3453,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
@@@ -3468,13 -3269,9 +3470,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
@@@ -3482,22 -3279,18 +3484,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
@@@ -3508,7 -3301,6 +3510,7 @@@ EXESUF=$EXESU
  EXTRA_VERSION=$extra_version
  DEPFLAGS=$DEPFLAGS
  CCDEP=$CCDEP
 +CXXDEP=$CXXDEP
  ASDEP=$ASDEP
  CC_DEPFLAGS=$CC_DEPFLAGS
  AS_DEPFLAGS=$AS_DEPFLAGS
@@@ -3523,7 -3315,7 +3525,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}
@@@ -3535,7 -3327,6 +3537,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
@@@ -3559,11 -3349,10 +3561,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
@@@ -3614,8 -3403,7 +3616,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
@@@ -3680,11 -3468,10 +3682,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 ffmpeg.c
index 3411561aee2a1a9ceda9842ed9506ef0ce35d514,c731cec53b4b5d0f8ae0de84ba76fa5a64e3b613..84302efa22e683257af16538b6bc356298a57122
+++ 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 *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);
      }
  
@@@ -695,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);
      }
  }
@@@ -706,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);
      }
@@@ -726,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){
@@@ -770,11 -621,11 +770,11 @@@ static void choose_pixel_fmt(AVStream *
                  p= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_BGRA, PIX_FMT_NONE};
              }
          }
--        for(; *p!=-1; p++){
++        for (; *p != PIX_FMT_NONE; p++) {
              if(*p == st->codec->pix_fmt)
                  break;
          }
--        if (*p == -1) {
++        if (*p == PIX_FMT_NONE) {
              if(st->codec->pix_fmt != PIX_FMT_NONE)
                  av_log(NULL, AV_LOG_WARNING,
                          "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
      }
  }
  
 -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,
@@@ -855,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;
@@@ -1076,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);
  }
  
@@@ -1400,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);
  
 -    if (is_last_report && verbose >= 0){
 +    fflush(stderr);
 +
 +    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;
  
 -    if(pkt->dts != AV_NOPTS_VALUE)
 -        ist->next_pts = ist->pts = 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 we have more to decode or while the decoder did output something on EOF
 -    while (avpkt.size > 0 || (!pkt && got_output)) {
 -        uint8_t *data_buf, *decoded_data_buf;
 -        int data_size, decoded_data_size;
 -    handle_eof:
 -        ist->pts= ist->next_pts;
 +        for(;;) {
 +            AVPacket pkt;
 +            int fifo_bytes;
 +            av_init_packet(&pkt);
 +            pkt.stream_index= ost->index;
  
 -        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);
 -            ist->showed_multi_packet_warning=1;
 -        }
 +            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);
 +                    }
  
 -        /* decode the packet if needed */
 -        decoded_data_buf = NULL; /* fail safe */
 -        decoded_data_size= 0;
 -        data_buf  = avpkt.data;
 -        data_size = avpkt.size;
 -        subtitle_to_free = NULL;
 -        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);
 -                    av_free(samples);
 -                    samples= av_malloc(samples_size);
 +                    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;
                  }
 -                decoded_data_size= samples_size;
 -                    /* XXX: could avoid copy if PCM 16 bits with same
 -                       endianness as CPU */
 -                ret = avcodec_decode_audio3(ist->st->codec, samples, &decoded_data_size,
 -                                            &avpkt);
 -                if (ret < 0)
 -                    return ret;
 -                avpkt.data += ret;
 -                avpkt.size -= ret;
 -                data_size   = ret;
 -                got_output  = decoded_data_size > 0;
 -                /* Some bug in mpeg audio decoder gives */
 -                /* decoded_data_size < 0, it seems they are overflows */
 -                if (!got_output) {
 -                    /* no audio frame */
 -                    continue;
 +                if (ret <= 0) {
 +                    ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL);
                  }
 -                decoded_data_buf = (uint8_t *)samples;
 -                ist->next_pts += ((int64_t)AV_TIME_BASE/bps * decoded_data_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);
 -                    avpkt.pts = pkt_pts;
 -                    avpkt.dts = ist->pts;
 -                    pkt_pts = AV_NOPTS_VALUE;
 -
 -                    ret = avcodec_decode_video2(ist->st->codec,
 -                                                &picture, &got_output, &avpkt);
 -                    quality = same_quality ? picture.quality : 0;
 -                    if (ret < 0)
 -                        return ret;
 -                    if (!got_output) {
 -                        /* no picture yet */
 -                        goto discard_packet;
 -                    }
 -                    ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, picture.pkt_pts, picture.pkt_dts);
 -                    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 *
 -                                          ist->st->codec->time_base.num * ticks) /
 -                            ist->st->codec->time_base.den;
 -                    }
 -                    avpkt.size = 0;
 -                    buffer_to_free = NULL;
 -                    pre_process_video_frame(ist, (AVPicture *)&picture, &buffer_to_free);
 -                    break;
 -            case AVMEDIA_TYPE_SUBTITLE:
 -                ret = avcodec_decode_subtitle2(ist->st->codec,
 -                                               &subtitle, &got_output, &avpkt);
 -                if (ret < 0)
 -                    return ret;
 -                if (!got_output) {
 -                    goto discard_packet;
 +                if (ret < 0) {
 +                    av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
 +                    exit_program(1);
                  }
 -                subtitle_to_free = &subtitle;
 -                avpkt.size = 0;
 -                break;
 -            default:
 -                return -1;
 -            }
 -        } else {
 -            switch(ist->st->codec->codec_type) {
 -            case AVMEDIA_TYPE_AUDIO:
 -                ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
 -                    ist->st->codec->sample_rate;
 +                audio_size += ret;
 +                pkt.flags |= AV_PKT_FLAG_KEY;
                  break;
              case AVMEDIA_TYPE_VIDEO:
 -                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 *
 -                                      ist->st->codec->time_base.num * ticks) /
 -                        ist->st->codec->time_base.den;
 +                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;
              }
 -            ret = avpkt.size;
 -            avpkt.size = 0;
 +
 +            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 CONFIG_AVFILTER
 -        if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
 -            for (i = 0; i < nb_ostreams; i++) {
 -                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);
 -                }
++/*
++ * Check whether a packet from ist should be written into ost at this time
++ */
++static int check_output_constraints(InputStream *ist, OutputStream *ost)
++{
++    OutputFile *of = &output_files[ost->file_index];
++    int ist_index  = ist - input_streams;
++
++    if (ost->source_index != ist_index)
++        return 0;
++
++    if (of->start_time && ist->pts < of->start_time)
++        return 0;
++
++    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;
++        return 0;
++    }
++
++    return 1;
++}
++
++static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
++{
++    OutputFile *of = &output_files[ost->file_index];
++    int64_t ost_tb_start_time = av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base);
++    AVPicture pict;
++    AVPacket opkt;
++
++    av_init_packet(&opkt);
++
++    if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
++        !ost->copy_initial_nonkeyframes)
++        return;
++
++    /* force the input stream PTS */
++    if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
++        audio_size += pkt->size;
++    else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
++        video_size += pkt->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;
++
++    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, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY))
++            opkt.destruct = av_destruct_packet;
++    } else {
++        opkt.data = pkt->data;
++        opkt.size = pkt->size;
++    }
++    if (of->ctx->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(of->ctx, &opkt, ost->st->codec, ost->bitstream_filters);
++    ost->st->codec->frame_number++;
++    ost->frame_number++;
++    av_free_packet(&opkt);
++}
++
++static void rate_emu_sleep(InputStream *ist)
++{
++    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)
++            usleep(pts - now);
++    }
++}
++
++static int transcode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
++{
++    static unsigned int samples_size = 0;
++    int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt);
++    uint8_t *decoded_data_buf  = NULL;
++    int      decoded_data_size = 0;
++    int i, ret;
++
++    if (pkt && samples_size < FFMAX(pkt->size * bps, AVCODEC_MAX_AUDIO_FRAME_SIZE)) {
++        av_free(samples);
++        samples_size = FFMAX(pkt->size * bps, AVCODEC_MAX_AUDIO_FRAME_SIZE);
++        samples      = av_malloc(samples_size);
++    }
++    decoded_data_size = samples_size;
++
++    ret = avcodec_decode_audio3(ist->st->codec, samples, &decoded_data_size,
++                                pkt);
++    if (ret < 0)
++        return ret;
++    pkt->data   += ret;
++    pkt->size   -= ret;
++    *got_output  = decoded_data_size > 0;
++
++    /* Some bug in mpeg audio decoder gives */
++    /* decoded_data_size < 0, it seems they are overflows */
++    if (!*got_output) {
++        /* no audio frame */
++        return 0;
++    }
++
++    decoded_data_buf = (uint8_t *)samples;
++    ist->next_pts   += ((int64_t)AV_TIME_BASE/bps * decoded_data_size) /
++                       (ist->st->codec->sample_rate * ist->st->codec->channels);
++
++    // preprocess audio (volume)
++    if (audio_volume != 256) {
++        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;
+         }
 -#endif
 -
 -        // 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;
 -                }
++        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;
+         }
 -
 -        /* frame rate emulation */
 -        if (rate_emu) {
 -            int64_t pts = av_rescale(ist->pts, 1000000, AV_TIME_BASE);
 -            int64_t now = av_gettime() - ist->start;
 -            if (pts > now)
 -                usleep(pts - now);
++        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;
+         }
 -        /* 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;
 -
 -                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)
 -                        ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
 -#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:
++        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);
++        }
++    }
++
++    rate_emu_sleep(ist);
++
++    for (i = 0; i < nb_output_streams; i++) {
++        OutputStream *ost = &output_streams[i];
++
++        if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
++            continue;
++        do_audio_out(output_files[ost->file_index].ctx, ost, ist,
++                     decoded_data_buf, decoded_data_size);
++    }
++    return 0;
++}
++
++static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int64_t *pkt_pts, int64_t *pkt_dts)
++{
++    AVFrame *decoded_frame, *filtered_frame = NULL;
++    void *buffer_to_free = NULL;
++    int i, ret = 0;
++    float quality = 0;
+ #if CONFIG_AVFILTER
 -                            if (ost->picref->video && !ost->frame_aspect_ratio)
 -                                ost->st->codec->sample_aspect_ratio = ost->picref->video->pixel_aspect;
++    int frame_available = 1;
+ #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);
++    if (!(decoded_frame = avcodec_alloc_frame()))
++        return AVERROR(ENOMEM);
++    pkt->pts  = *pkt_pts;
++    pkt->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;
 -                        if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) && !copy_initial_nonkeyframes)
 -#if !CONFIG_AVFILTER
 -                            continue;
 -#else
 -                            goto cont;
 -#endif
++    ret = avcodec_decode_video2(ist->st->codec,
++                                decoded_frame, got_output, pkt);
++    if (ret < 0)
++        goto fail;
 -                        /* no reencoding needed : output the packet directly */
 -                        /* force the input stream PTS */
++    quality = same_quant ? decoded_frame->quality : 0;
++    if (!*got_output) {
++        /* no picture yet */
++        av_freep(&decoded_frame);
++        return 0;
++    }
 -                        avcodec_get_frame_defaults(&avframe);
 -                        ost->st->codec->coded_frame= &avframe;
 -                        avframe.key_frame = pkt->flags & AV_PKT_FLAG_KEY;
++    if(decoded_frame->best_effort_timestamp != AV_NOPTS_VALUE)
++        ist->next_pts = ist->pts = decoded_frame->best_effort_timestamp;
 -                        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 (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 *
++                          ist->st->codec->time_base.num * ticks) /
++                          ist->st->codec->time_base.den;
++    }
++    pkt->size = 0;
 -                        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;
 -                        }
++    pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
 -                        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]);
 -                    if (ost->picref)
 -                        avfilter_unref_buffer(ost->picref);
 -                }
 -#endif
 -                }
++    for(i=0;i<nb_output_streams;i++) {
++        OutputFile *of = &output_files[output_streams[i].file_index];
++            OutputStream *ost = ost = &output_streams[i];
++            if(check_output_constraints(ist, ost)){
++                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);
+             }
 -
 -        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;
 -        }
+     }
 - discard_packet:
 -    if (pkt == NULL) {
 -        /* EOF handling */
++#endif
 -        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;
 -                        }
++    rate_emu_sleep(ist);
 -                        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);
 -                    }
++    for (i = 0; i < nb_output_streams; i++) {
++        OutputStream *ost = &output_streams[i];
++        int frame_size;
++
++        if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
++            continue;
++
++#if CONFIG_AVFILTER
++        if (ost->input_video_filter) {
++            frame_available = avfilter_poll_frame(ost->output_video_filter->inputs[0]);
++        }
++        while (frame_available) {
++            if (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);
+                 }
+             }
++            if (ost->picref->video && !ost->frame_aspect_ratio)
++                ost->st->codec->sample_aspect_ratio = ost->picref->video->sample_aspect_ratio;
++#else
++            filtered_frame = decoded_frame;
++#endif
++
++            do_video_out(output_files[ost->file_index].ctx, ost, ist, filtered_frame, &frame_size,
++                         same_quant ? quality : ost->st->codec->global_quality);
++            if (vstats_filename && frame_size)
++                do_video_stats(output_files[ost->file_index].ctx, ost, frame_size);
++#if CONFIG_AVFILTER
++            cont:
++            frame_available = ost->output_video_filter && avfilter_poll_frame(ost->output_video_filter->inputs[0]);
++            avfilter_unref_buffer(ost->picref);
+         }
++        av_freep(&filtered_frame);
++#endif
+     }
 -    return 0;
++fail:
++    av_free(buffer_to_free);
++    av_freep(&decoded_frame);
++    return ret;
+ }
 -static void print_sdp(AVFormatContext **avc, int n)
++static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
+ {
 -    char sdp[2048];
++    AVSubtitle subtitle;
++    int i, ret = avcodec_decode_subtitle2(ist->st->codec,
++                                          &subtitle, got_output, pkt);
++    if (ret < 0)
++        return ret;
++    if (!*got_output)
++        return 0;
 -    av_sdp_create(avc, n, sdp, sizeof(sdp));
 -    printf("SDP:\n%s\n", sdp);
 -    fflush(stdout);
++    pkt->size = 0;
++
++    rate_emu_sleep(ist);
++
++    for (i = 0; i < nb_output_streams; i++) {
++        OutputStream *ost = &output_streams[i];
++
++        if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
++            continue;
++
++        do_subtitle_out(output_files[ost->file_index].ctx, ost, ist, &subtitle, pkt->pts);
++    }
++
++    avsubtitle_free(&subtitle);
++    return 0;
+ }
 -static int copy_chapters(int infile, int outfile)
 +/* 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;
 -    AVFormatContext *is = input_files[infile].ctx;
 -    AVFormatContext *os = output_files[outfile];
 -    int i;
 +    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;
  
 -    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);
 +    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 (in_ch->end < ts_off)
 -            continue;
 -        if (rt != INT64_MAX && in_ch->start > rt + ts_off)
 +    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 we have more to decode or while the decoder did output something on EOF
-     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;
++    while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {
 +    handle_eof:
 +        ist->pts= ist->next_pts;
 +
 +        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;
-         data_size = avpkt.size;
-         subtitle_to_free = NULL;
-         if (ist->decoding_needed) {
-             switch(ist->st->codec->codec_type) {
-             case AVMEDIA_TYPE_AUDIO:{
-                 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);
-                 }
-                 decoded_data_size= samples_size;
-                     /* XXX: could avoid copy if PCM 16 bits with same
-                        endianness as CPU */
-                 ret = avcodec_decode_audio3(ist->st->codec, samples, &decoded_data_size,
-                                             &avpkt);
-                 if (ret < 0)
-                     return ret;
-                 avpkt.data += ret;
-                 avpkt.size -= ret;
-                 data_size   = ret;
-                 got_output  = decoded_data_size > 0;
-                 /* Some bug in mpeg audio decoder gives */
-                 /* decoded_data_size < 0, it seems they are overflows */
-                 if (!got_output) {
-                     /* no audio frame */
-                     continue;
-                 }
-                 decoded_data_buf = (uint8_t *)samples;
-                 ist->next_pts += ((int64_t)AV_TIME_BASE/bps * decoded_data_size) /
-                     (ist->st->codec->sample_rate * ist->st->codec->channels);
-                 break;}
-             case AVMEDIA_TYPE_VIDEO:
-                     if (!(decoded_frame = avcodec_alloc_frame()))
-                         return AVERROR(ENOMEM);
-                     avpkt.pts = pkt_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,
-                                                 decoded_frame, &got_output, &avpkt);
-                     quality = same_quant ? decoded_frame->quality : 0;
-                     if (ret < 0)
-                         goto fail;
-                     if (!got_output) {
-                         /* no picture yet */
-                         av_freep(&decoded_frame);
-                         goto discard_packet;
-                     }
-                     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 *
-                                           ist->st->codec->time_base.num * ticks) /
-                             ist->st->codec->time_base.den;
-                     }
-                     avpkt.size = 0;
-                     buffer_to_free = NULL;
-                     pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
-                     break;
-             case AVMEDIA_TYPE_SUBTITLE:
-                 ret = avcodec_decode_subtitle2(ist->st->codec,
-                                                &subtitle, &got_output, &avpkt);
-                 if (ret < 0)
-                     return ret;
-                 if (!got_output) {
-                     goto discard_packet;
-                 }
-                 subtitle_to_free = &subtitle;
-                 avpkt.size = 0;
-                 break;
-             default:
-                 return -1;
-             }
-         } else {
-             switch(ist->st->codec->codec_type) {
-             case AVMEDIA_TYPE_AUDIO:
-                 ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
-                     ist->st->codec->sample_rate;
-                 break;
-             case AVMEDIA_TYPE_VIDEO:
-                 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 *
-                                       ist->st->codec->time_base.num * ticks) /
-                         ist->st->codec->time_base.den;
-                 }
-                 break;
-             }
-             avpkt.size = 0;
++        switch(ist->st->codec->codec_type) {
++        case AVMEDIA_TYPE_AUDIO:
++            ret = transcode_audio    (ist, &avpkt, &got_output);
++            break;
++        case AVMEDIA_TYPE_VIDEO:
++            ret = transcode_video    (ist, &avpkt, &got_output, &pkt_pts, &pkt_dts);
++            break;
++        case AVMEDIA_TYPE_SUBTITLE:
++            ret = transcode_subtitles(ist, &avpkt, &got_output);
+             break;
++        default:
++            return -1;
 +        }
  
- #if CONFIG_AVFILTER
-         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) {
-                     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);
-                 }
-             }
 -        out_ch = av_mallocz(sizeof(AVChapter));
 -        if (!out_ch)
 -            return AVERROR(ENOMEM);
++        if (ret < 0)
++            return ret;
++        if (!got_output) {
++            if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
++                continue;
++            goto discard_packet;
 +        }
- #endif
++    }
++ discard_packet:
  
-         // preprocess audio (volume)
-         if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
-             if (audio_volume != 256) {
-                 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);
-                 }
 -        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);
++    /* handle stream copy */
++    if (!ist->decoding_needed) {
++        rate_emu_sleep(ist);
++        switch (ist->st->codec->codec_type) {
++        case AVMEDIA_TYPE_AUDIO:
++            ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
++                             ist->st->codec->sample_rate;
++            break;
++        case AVMEDIA_TYPE_VIDEO:
++            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 *
++                                  ist->st->codec->time_base.num * ticks) /
++                                  ist->st->codec->time_base.den;
 +            }
++            break;
 +        }
++    }
++    for (i = 0; pkt && i < nb_ostreams; i++) {
++        ost = &ost_table[i];
  
-         /* frame rate emulation */
-         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)
-                 usleep(pts - now);
-         }
-         /* if output time reached then transcode raw format,
-            encode packets and output them */
-         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;
-             }
- #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) {
-                 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].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->sample_aspect_ratio;
- #endif
-                         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)) &&
-                         !ost->copy_initial_nonkeyframes)
- #if !CONFIG_AVFILTER
-                         continue;
- #else
-                         goto cont;
- #endif
-                     /* no reencoding needed : output the packet directly */
-                     /* force the input stream PTS */
-                     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;
-                     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 (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++;
-                     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
-         }
 -        if (metadata_chapters_autocopy)
 -            av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
++        if (!check_output_constraints(ist, ost) || ost->encoding_needed)
++            continue;
  
- 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;
 -        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;
++        do_streamcopy(ist, ost, pkt);
      }
-  discard_packet:
 +
      return 0;
  }
  
 -static void parse_forced_key_frames(char *kf, OutputStream *ost,
 -                                    AVCodecContext *avctx)
 +static void print_sdp(OutputFile *output_files, int n)
  {
 -    char *p;
 -    int n = 1, i;
 -    int64_t t;
 +    char sdp[2048];
 +    int i;
 +    AVFormatContext **avc = av_malloc(sizeof(*avc)*n);
  
 -    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");
 +    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 init_input_stream(int ist_index, OutputStream *output_streams, int nb_output_streams,
 +                             char *error, int error_len)
 +{
 +    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);
      }
 -    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);
 -    }
 +
 +    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;
 +
 +    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 *oc;
      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();
  
 -    /* output stream init */
 -    nb_ostreams = 0;
 -    for(i=0;i<nb_output_files;i++) {
 -        os = output_files[i];
 -        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;
 +    /* 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();
      }
 -    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;
 +    /* output stream init */
 +    for (i = 0; i < nb_output_files; i++) {
 +        oc = output_files[i].ctx;
 +        if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
 +            av_dump_format(oc, i, oc->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];
 +        oc  = output_files[ost->file_index].ctx;
          ist = &input_streams[ost->source_index];
  
 -        codec = ost->st->codec;
 -        icodec = ist->st->codec;
 +        if (ost->attachment_filename)
 +            continue;
  
 -        if (metadata_streams_autocopy)
 -            av_dict_copy(&ost->st->metadata, ist->st->metadata,
 -                         AV_DICT_DONT_OVERWRITE);
 +        codec  = ost->st->codec;
 +        icodec = ist->st->codec;
  
 -        ost->st->disposition = ist->st->disposition;
 -        codec->bits_per_raw_sample= icodec->bits_per_raw_sample;
 +        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->codec_id   = icodec->codec_id;
              codec->codec_type = icodec->codec_type;
  
 -            if(!codec->codec_tag){
 -                if(   !os->oformat->codec_tag
 -                   || av_codec_get_id (os->oformat->codec_tag, icodec->codec_tag) == codec->codec_id
 -                   || av_codec_get_tag(os->oformat->codec_tag, icodec->codec_id) <= 0)
 +            if (!codec->codec_tag) {
 +                if (!oc->oformat->codec_tag ||
 +                     av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == codec->codec_id ||
 +                     av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0)
                      codec->codec_tag = icodec->codec_tag;
              }
  
 -            codec->bit_rate = icodec->bit_rate;
 +            codec->bit_rate       = icodec->bit_rate;
              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;
 +            codec->extradata      = av_mallocz(extra_size);
 +            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(oc->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(!(oc->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->sample_rate = icodec->sample_rate;
 -                codec->channels = icodec->channels;
 -                codec->frame_size = icodec->frame_size;
 +                codec->channel_layout     = icodec->channel_layout;
 +                codec->sample_rate        = icodec->sample_rate;
 +                codec->channels           = icodec->channels;
 +                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;
 +                codec->block_align        = icodec->block_align;
                  break;
              case AVMEDIA_TYPE_VIDEO:
 -                codec->pix_fmt = icodec->pix_fmt;
 -                codec->width = icodec->width;
 -                codec->height = icodec->height;
 -                codec->has_b_frames = icodec->has_b_frames;
 +                codec->pix_fmt            = icodec->pix_fmt;
 +                codec->width              = icodec->width;
 +                codec->height             = icodec->height;
 +                codec->has_b_frames       = icodec->has_b_frames;
                  if (!codec->sample_aspect_ratio.num) {
 -                    codec->sample_aspect_ratio =
 +                    codec->sample_aspect_ratio   =
                      ost->st->sample_aspect_ratio =
                          ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio :
                          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->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;
 +                ost->fifo = av_fifo_alloc(1024);
 +                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;
 -                icodec->request_channels = codec->channels;
 -                ist->decoding_needed = 1;
 -                ost->encoding_needed = 1;
 +
 +                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;
                  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;
 +                ost->resample_height  = icodec->height;
 +                ost->resample_width   = icodec->width;
 +                ost->resample_pix_fmt = icodec->pix_fmt;
  
                  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 && !(oc->oformat->flags & AVFMT_VARIABLE_FPS)))){
 +                    av_log(oc, 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){
 -            int size= codec->width * codec->height;
 -            bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 200);
 +            /* 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, 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;
 +            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++) {
 +        oc = output_files[i].ctx;
 +        oc->interrupt_callback = int_cb;
 +        if (avformat_write_header(oc, &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(oc->oformat->name, "rtp")) {
              want_sdp = 0;
          }
      }
   dump_format:
      /* 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);
 +    for (i = 0; i < nb_output_files; i++) {
 +        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");
 +    }
  
      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);
 -    return 0;
 -}
 +    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);
 +        }
 +    }
  
 -static int opt_video_standard(const char *opt, const char *arg)
 -{
 -    av_log(NULL, AV_LOG_WARNING, "This option is deprecated, use -standard.\n");
 -    opt_default("standard", arg);
 -    return 0;
 -}
  
 -static int opt_codec(int *pstream_copy, char **pcodec_name,
 -                      int codec_type, const char *arg)
 -{
 -    av_freep(pcodec_name);
 -    if (!strcmp(arg, "copy")) {
 -        *pstream_copy = 1;
 -    } else {
 -        *pcodec_name = av_strdup(arg);
 +    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);
      }
 -    return 0;
 -}
 +    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];
  
 -static int opt_audio_codec(const char *opt, const char *arg)
 -{
 -    return opt_codec(&audio_stream_copy, &audio_codec_name, AVMEDIA_TYPE_AUDIO, arg);
 -}
 +            m->file_index   = file_idx;
 +            m->stream_index = i;
  
 -static int opt_video_codec(const char *opt, const char *arg)
 -{
 -    return opt_codec(&video_stream_copy, &video_codec_name, AVMEDIA_TYPE_VIDEO, arg);
 -}
 +            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;
 +            }
 +        }
  
 -static int opt_subtitle_codec(const char *opt, const char *arg)
 -{
 -    return opt_codec(&subtitle_stream_copy, &subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, arg);
 -}
 +    if (!m) {
 +        av_log(NULL, AV_LOG_FATAL, "Stream map '%s' matches no streams.\n", arg);
 +        exit_program(1);
 +    }
  
 -static int opt_data_codec(const char *opt, const char *arg)
 -{
 -    return opt_codec(&data_stream_copy, &data_codec_name, AVMEDIA_TYPE_DATA, arg);
 +    av_freep(&map);
 +    return 0;
  }
  
 -static int opt_codec_tag(const char *opt, const char *arg)
 +static int opt_attach(OptionsContext *o, 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;
 -
 -    *codec_tag = strtol(arg, &tail, 0);
 -    if (!tail || *tail)
 -        *codec_tag = AV_RL32(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_map(const char *opt, const char *arg)
 +static int opt_map_channel(OptionsContext *o, const char *opt, const char *arg)
  {
 -    StreamMap *m;
 -    char *p;
 +    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;
 +    }
  
 -    stream_maps = grow_array(stream_maps, sizeof(*stream_maps), &nb_stream_maps, nb_stream_maps + 1);
 -    m = &stream_maps[nb_stream_maps-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);
  
 -    m->file_index = strtol(arg, &p, 0);
 -    if (*p)
 -        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);
 +    }
  
 -    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;
 +    if (n != 5) // only file.stream.channel specified
 +        m->ofile_idx = m->ostream_idx = -1;
 +
 +    /* 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_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 (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;
      }
 -    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);
 -    ic->flags |= AVFMT_FLAG_NONBLOCK;
  
      /* 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_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_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);
 +        }
 +
 +        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->width = frame_width;
 -        video_enc->height = frame_height;
 -        video_enc->pix_fmt = frame_pix_fmt;
 +        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);
  
 -    if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
 -        audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
 +        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);
 +
 +        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];
  
 -    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;
 +    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);
  
 -    if (subtitle_language) {
 -        av_dict_set(&st->metadata, "language", subtitle_language, 0);
 -        av_freep(&subtitle_language);
 -    }
 +        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);
  
 -    subtitle_disable = 0;
 -    av_freep(&subtitle_codec_name);
 -    subtitle_stream_copy = 0;
 -}
 +        if (copy_metadata)
 +            av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
  
 -static int opt_new_stream(const char *opt, const char *arg)
 -{
 -    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);
 +        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;
      }
 -    oc = output_files[file_idx];
 -
 -    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;
  }
  
 -/* arg format is "output-stream-index:streamid-value". */
 -static int opt_streamid(const char *opt, const char *arg)
 +static int read_ffserver_streams(OptionsContext *o, AVFormatContext *s, const char *filename)
  {
 -    int idx;
 -    char *p;
 -    char idx_str[16];
 +    int i, err;
 +    AVFormatContext *ic = avformat_alloc_context();
  
 -    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);
 +    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;
 +
 +        codec = avcodec_find_encoder(ic->streams[i]->codec->codec_id);
 +        ost   = new_output_stream(o, s, codec->type);
 +        st    = ost->st;
 +        avctx = st->codec;
 +
 +        // 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;
 +                }
 +            }
 +            if(!ost->sync_ist){
 +                av_log(NULL, AV_LOG_FATAL, "Missing %s stream which is required by this ffm\n", av_get_media_type_string(ost->st->codec->codec_type));
 +                exit_program(1);
 +            }
 +        }
 +    } 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 (!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;
  }
  
@@@ -4276,21 -3888,49 +4314,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;
@@@ -4480,34 -4157,37 +4518,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();
  
 -    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 libavcodec/aaccoder.c
index b61af1805690281bb522b4f6299730e2adf0c40e,f8a8f3d84487b703a0674b6064dffb84d74c9f8e..31d3b23d2456b60851363441a2e8252a74e83578
@@@ -2,20 -2,20 +2,20 @@@
   * AAC coefficients encoder
   * Copyright (C) 2008-2009 Konstantin 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
   */
  
@@@ -33,7 -33,7 +33,7 @@@
  #include "libavutil/libm.h" // brought forward to work around cygwin header breakage
  
  #include <float.h>
- #include <math.h>
+ #include "libavutil/mathematics.h"
  #include "avcodec.h"
  #include "put_bits.h"
  #include "aac.h"
diff --combined libavcodec/aacdec.c
index 216692829456287763bc4895e877abae3698c28b,1015030b9a89491534d0206b4e7d243e229570ee..b7f21e0c1101bf866fb3defba3662567b5b6a64d
@@@ -7,20 -7,20 +7,20 @@@
   * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
   * Copyright (c) 2010      Janne Grunau <janne-ffmpeg@jannau.net>
   *
 - * 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
   */
  
@@@ -170,7 -170,7 +170,7 @@@ static ChannelElement *get_che(AACConte
  /**
   * Check for the channel element in the current channel position configuration.
   * If it exists, make sure the appropriate element is allocated and map the
 - * channel order to match the internal Libav channel layout.
 + * channel order to match the internal FFmpeg channel layout.
   *
   * @param   che_pos current channel position configuration
   * @param   type channel element type
@@@ -253,6 -253,8 +253,6 @@@ static av_cold int output_configure(AAC
          }
  
          memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
 -
 -        avctx->channel_layout = 0;
      }
  
      avctx->channels = channels;
@@@ -315,10 -317,6 +315,10 @@@ static int decode_pce(AVCodecContext *a
      if (get_bits1(gb))
          skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
  
 +    if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) {
 +        av_log(avctx, AV_LOG_ERROR, overread_err);
 +        return -1;
 +    }
      decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
      decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE,  gb, num_side );
      decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK,  gb, num_back );
@@@ -462,7 -460,7 +462,7 @@@ static int decode_ga_specific_config(AA
  static int decode_audio_specific_config(AACContext *ac,
                                          AVCodecContext *avctx,
                                          MPEG4AudioConfig *m4ac,
 -                                        const uint8_t *data, int data_size)
 +                                        const uint8_t *data, int data_size, int asclen)
  {
      GetBitContext gb;
      int i;
  
      init_get_bits(&gb, data, data_size * 8);
  
 -    if ((i = avpriv_mpeg4audio_get_config(m4ac, data, data_size)) < 0)
 +    if ((i = avpriv_mpeg4audio_get_config(m4ac, data, asclen/8)) < 0)
          return -1;
      if (m4ac->sampling_index > 12) {
          av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
@@@ -574,7 -572,7 +574,7 @@@ static av_cold int aac_decode_init(AVCo
      if (avctx->extradata_size > 0) {
          if (decode_audio_specific_config(ac, ac->avctx, &ac->m4ac,
                                           avctx->extradata,
 -                                         avctx->extradata_size) < 0)
 +                                         avctx->extradata_size, 8*avctx->extradata_size) < 0)
              return -1;
      } else {
          int sr, i;
@@@ -2081,13 -2079,14 +2081,14 @@@ static int parse_adts_frame_header(AACC
  
      size = avpriv_aac_parse_header(gb, &hdr_info);
      if (size > 0) {
 -        if (hdr_info.chan_config) {
 +        if (hdr_info.chan_config && (hdr_info.chan_config!=ac->m4ac.chan_config || ac->m4ac.sample_rate!=hdr_info.sample_rate)) {
              enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
              memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
              ac->m4ac.chan_config = hdr_info.chan_config;
              if (set_default_channel_config(ac->avctx, new_che_pos, hdr_info.chan_config))
                  return -7;
-             if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config, OC_TRIAL_FRAME))
+             if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config,
+                                  FFMAX(ac->output_configured, OC_TRIAL_FRAME)))
                  return -7;
          } else if (ac->output_configured != OC_LOCKED) {
              ac->m4ac.chan_config = 0;
@@@ -2139,15 -2138,6 +2140,15 @@@ static int aac_decode_frame_int(AVCodec
          elem_id = get_bits(gb, 4);
  
          if (elem_type < TYPE_DSE) {
 +            if (!ac->tags_mapped && elem_type == TYPE_CPE && ac->m4ac.chan_config==1) {
 +                enum ChannelPosition new_che_pos[4][MAX_ELEM_ID]= {0};
 +                ac->m4ac.chan_config=2;
 +
 +                if (set_default_channel_config(ac->avctx, new_che_pos, 2)<0)
 +                    return -1;
 +                if (output_configure(ac, ac->che_pos, new_che_pos, 2, OC_TRIAL_FRAME)<0)
 +                    return -1;
 +            }
              if (!(che=get_che(ac, elem_type, elem_id))) {
                  av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
                         elem_type, elem_id);
@@@ -2320,11 -2310,10 +2321,11 @@@ static inline uint32_t latm_get_value(G
  }
  
  static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
 -                                             GetBitContext *gb)
 +                                             GetBitContext *gb, int asclen)
  {
      AVCodecContext *avctx = latmctx->aac_ctx.avctx;
 -    MPEG4AudioConfig m4ac;
 +    AACContext *ac= &latmctx->aac_ctx;
 +    MPEG4AudioConfig m4ac=ac->m4ac;
      int  config_start_bit = get_bits_count(gb);
      int     bits_consumed, esize;
  
          return AVERROR_INVALIDDATA;
      } else {
          bits_consumed =
 -            decode_audio_specific_config(NULL, avctx, &m4ac,
 +            decode_audio_specific_config(ac, avctx, &m4ac,
                                           gb->buffer + (config_start_bit / 8),
 -                                         get_bits_left(gb) / 8);
 +                                         get_bits_left(gb) / 8, asclen);
  
          if (bits_consumed < 0)
              return AVERROR_INVALIDDATA;
 +        if(ac->m4ac.sample_rate != m4ac.sample_rate || m4ac.chan_config != ac->m4ac.chan_config)
 +            ac->m4ac= m4ac;
  
          esize = (bits_consumed+7) / 8;
  
@@@ -2396,11 -2383,11 +2397,11 @@@ static int read_stream_mux_config(struc
  
          // for all but first stream: use_same_config = get_bits(gb, 1);
          if (!audio_mux_version) {
 -            if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
 +            if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
                  return ret;
          } else {
              int ascLen = latm_get_value(gb);
 -            if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
 +            if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
                  return ret;
              ascLen -= ret;
              skip_bits_long(gb, ascLen);
@@@ -2522,7 -2509,7 +2523,7 @@@ static int latm_decode_frame(AVCodecCon
          } else {
              if ((err = decode_audio_specific_config(
                      &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.m4ac,
 -                    avctx->extradata, avctx->extradata_size)) < 0)
 +                    avctx->extradata, avctx->extradata_size, 8*avctx->extradata_size)) < 0)
                  return err;
              latmctx->initialized = 1;
          }
diff --combined libavcodec/arm/asm.S
index 856d2e986fa55d303ccf4626e3eec69439d13fc2,a124918c1c6024ce2a99782a4e96e745a52fd1ee..d711cb8f11662b830140cbc63ce3463697962531
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * Copyright (c) 2008 Mans Rullgard <mans@mansr.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
   */
  
  #   define T @
  #endif
  
+ #if   HAVE_NEON
+         .arch           armv7-a
+ #elif HAVE_ARMV6T2
+         .arch           armv6t2
+ #elif HAVE_ARMV6
+         .arch           armv6
+ #elif HAVE_ARMV5TE
+         .arch           armv5te
+ #endif
+ #if   HAVE_NEON
+         .fpu            neon
+ #elif HAVE_ARMVFP
+         .fpu            vfp
+ #endif
          .syntax unified
  T       .thumb
  
@@@ -97,12 -113,6 +113,12 @@@ T       add             \rn, \rn, \r
  T       ldr             \rt, [\rn]
  .endm
  
 +.macro  ldr_dpren       rt,  rn,  rm:vararg
 +A       ldr             \rt, [\rn, -\rm]
 +T       sub             \rt, \rn, \rm
 +T       ldr             \rt, [\rt]
 +.endm
 +
  .macro  ldr_post        rt,  rn,  rm:vararg
  A       ldr             \rt, [\rn], \rm
  T       ldr             \rt, [\rn]
@@@ -139,12 -149,6 +155,12 @@@ T       ldrh            \rt, [\rn
  T       add             \rn, \rn, \rm
  .endm
  
 +.macro  ldrb_post       rt,  rn,  rm
 +A       ldrb            \rt, [\rn], \rm
 +T       ldrb            \rt, [\rn]
 +T       add             \rn, \rn, \rm
 +.endm
 +
  .macro  str_post       rt,  rn,  rm:vararg
  A       str             \rt, [\rn], \rm
  T       str             \rt, [\rn]
diff --combined libavcodec/avcodec.h
index dfd396c180e6166a612dfc6a786a725cc7846989,dc794b05384225806d94fd8ba604eb577de6e15c..18e16ff5844a7b8d723cfae8334dcbed634fee7c
@@@ -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
   */
  
  #include "libavutil/rational.h"
  
  #include "libavcodec/version.h"
+ /**
+  * @defgroup libavc Encoding/Decoding Library
+  * @{
+  *
+  * @defgroup lavc_decoding Decoding
+  * @{
+  * @}
+  *
+  * @defgroup lavc_encoding Encoding
+  * @{
+  * @}
+  *
+  * @defgroup lavc_codec Codecs
+  * @{
+  * @defgroup lavc_codec_native Native Codecs
+  * @{
+  * @}
+  * @defgroup lavc_codec_wrappers External library wrappers
+  * @{
+  * @}
+  * @defgroup lavc_codec_hwaccel Hardware Accelerators bridge
+  * @{
+  * @}
+  * @}
+  * @defgroup lavc_internal Internal
+  * @{
+  * @}
+  * @}
+  *
+  */
  
  /**
   * Identify the syntax and semantics of the bitstream.
@@@ -47,8 -78,7 +78,8 @@@
   *
   * If you add a codec ID to this list, add it so that
   * 1. no value of a existing codec ID changes (that would break ABI),
 - * 2. it is as close as possible to similar codecs.
 + * 2. Give it a value which when taken as ASCII is recognized uniquely by a human as this specific codec.
 + *    This ensures that 2 forks can independantly add CodecIDs without producing conflicts.
   */
  enum CodecID {
      CODEC_ID_NONE,
      CODEC_ID_WMV3IMAGE,
      CODEC_ID_VC1IMAGE,
  #if LIBAVCODEC_VERSION_MAJOR == 53
 -    CODEC_ID_G723_1,
 -    CODEC_ID_G729,
 +    CODEC_ID_G723_1_DEPRECATED,
 +    CODEC_ID_G729_DEPRECATED,
  #endif
 -    CODEC_ID_UTVIDEO,
 +    CODEC_ID_UTVIDEO_DEPRECATED,
      CODEC_ID_BMV_VIDEO,
      CODEC_ID_VBLE,
 +    CODEC_ID_UTVIDEO = 0x800,
 +
 +    CODEC_ID_G2M        = MKBETAG( 0 ,'G','2','M'),
  
      /* various PCM "codecs" */
      CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
      CODEC_ID_QDMC,
      CODEC_ID_CELT,
  #if LIBAVCODEC_VERSION_MAJOR > 53
 -    CODEC_ID_G723_1,
 -    CODEC_ID_G729,
 +    CODEC_ID_G723_1_DEPRECATED,
 +    CODEC_ID_G729_DEPRECATED,
      CODEC_ID_8SVX_EXP,
      CODEC_ID_8SVX_FIB,
  #endif
      CODEC_ID_BMV_AUDIO,
 +    CODEC_ID_G729 = 0x15800,
 +    CODEC_ID_G723_1= 0x15801,
 +    CODEC_ID_8SVX_RAW   = MKBETAG('8','S','V','X'),
  
      /* subtitle codecs */
      CODEC_ID_FIRST_SUBTITLE = 0x17000,          ///< A dummy ID pointing at the start of subtitle codecs.
      CODEC_ID_HDMV_PGS_SUBTITLE,
      CODEC_ID_DVB_TELETEXT,
      CODEC_ID_SRT,
 +    CODEC_ID_MICRODVD   = MKBETAG('m','D','V','D'),
  
      /* other specific kind of codecs (generally used for attachments) */
      CODEC_ID_FIRST_UNKNOWN = 0x18000,           ///< A dummy ID pointing at the start of various fake codecs.
      CODEC_ID_TTF = 0x18000,
 +    CODEC_ID_BINTEXT    = MKBETAG('B','T','X','T'),
 +    CODEC_ID_XBIN       = MKBETAG('X','B','I','N'),
 +    CODEC_ID_IDF        = MKBETAG( 0 ,'I','D','F'),
  
      CODEC_ID_PROBE = 0x19000, ///< codec_id is not known (like CODEC_ID_NONE) but lavf should attempt to identify it
  
   * Note: If the first 23 bits of the additional bytes are not 0, then damaged
   * MPEG bitstreams could cause overread and segfault.
   */
 -#define FF_INPUT_BUFFER_PADDING_SIZE 8
 +#define FF_INPUT_BUFFER_PADDING_SIZE 16
  
  /**
   * minimum encoding buffer size
@@@ -522,7 -542,6 +553,7 @@@ enum AVColorTransferCharacteristic
      AVCOL_TRC_UNSPECIFIED=2,
      AVCOL_TRC_GAMMA22    =4, ///< also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
      AVCOL_TRC_GAMMA28    =5, ///< also ITU-R BT470BG
 +    AVCOL_TRC_SMPTE240M  =7,
      AVCOL_TRC_NB           , ///< Not part of ABI
  };
  
@@@ -534,7 -553,6 +565,7 @@@ enum AVColorSpace
      AVCOL_SPC_BT470BG    =5, ///< also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
      AVCOL_SPC_SMPTE170M  =6, ///< also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
      AVCOL_SPC_SMPTE240M  =7,
 +    AVCOL_SPC_YCGCO      =8,
      AVCOL_SPC_NB           , ///< Not part of ABI
  };
  
@@@ -638,7 -656,6 +669,7 @@@ typedef struct RcOverride
  #define CODEC_FLAG2_LOCAL_HEADER  0x00000008 ///< Place global headers at every keyframe instead of in extradata.
  #define CODEC_FLAG2_SKIP_RD       0x00004000 ///< RD optimal MB level residual skipping
  #define CODEC_FLAG2_CHUNKS        0x00008000 ///< Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries.
 +#define CODEC_FLAG2_SHOW_ALL      0x00400000 ///< Show all frames before the first keyframe
  /**
   * @defgroup deprecated_flags Deprecated codec flags
   * Use corresponding private codec options instead.
   * Codec should fill in channel configuration and samplerate instead of container
   */
  #define CODEC_CAP_CHANNEL_CONF     0x0400
 +
  /**
   * Codec is able to deal with negative linesizes
   */
  #define CODEC_CAP_NEG_LINESIZES    0x0800
 +
  /**
   * Codec supports frame-level multithreading.
   */
   * Codec supports slice-based (or partition-based) multithreading.
   */
  #define CODEC_CAP_SLICE_THREADS    0x2000
 +/**
 + * Codec is lossless.
 + */
 +#define CODEC_CAP_LOSSLESS         0x80000000
  
  //The following defines may change, don't expect compatibility if you use them.
  #define MB_TYPE_INTRA4x4   0x0001
@@@ -1141,7 -1152,7 +1172,7 @@@ typedef struct AVFrame 
      int64_t reordered_opaque;
  
      /**
 -     * hardware accelerator private data (Libav-allocated)
 +     * hardware accelerator private data (FFmpeg-allocated)
       * - encoding: unused
       * - decoding: Set by libavcodec
       */
       * - decoding: Set by libavcodec.
       */
      void *thread_opaque;
 +
 +    /**
 +     * frame timestamp estimated using various heuristics, in stream time base
 +     * - encoding: unused
 +     * - decoding: set by libavcodec, read by user.
 +     */
 +    int64_t best_effort_timestamp;
 +
 +    /**
 +     * reordered pos from the last AVPacket that has been input into the decoder
 +     * - encoding: unused
 +     * - decoding: Read by user.
 +     */
 +    int64_t pkt_pos;
 +
 +    /**
 +     * reordered sample aspect ratio for the video frame, 0/1 if unknown\unspecified
 +     * - encoding: unused
 +     * - decoding: Read by user.
 +     */
 +    AVRational sample_aspect_ratio;
 +
 +    /**
 +     * width and height of the video frame
 +     * - encoding: unused
 +     * - decoding: Read by user.
 +     */
 +    int width, height;
 +
 +    /**
 +     * format of the frame, -1 if unknown or unset
 +     * It should be cast to the corresponding enum (enum PixelFormat
 +     * for video, enum AVSampleFormat for audio)
 +     * - encoding: unused
 +     * - decoding: Read by user.
 +     */
 +    int format;
 +
  } AVFrame;
  
  struct AVCodecInternal;
   * New fields can be added to the end with minor version bumps.
   * Removal, reordering and changes to existing fields require a major
   * version bump.
 + * Please use AVOptions (av_opt* / av_set/get*()) to access these fields from user
 + * applications.
   * sizeof(AVCodecContext) must not be used outside libav*.
   */
  typedef struct AVCodecContext {
      int frame_number;   ///< audio or video frame number
  
      /**
 -     * Number of frames the decoded output will be delayed relative to
 -     * the encoded input.
 +     * Encoding: Number of frames delay there will be from the encoder input to
 +     *           the decoder output. (we assume the decoder matches the spec)
 +     * Decoding: Number of frames delay in addition to what a standard decoder
 +     *           as specified in the spec would produce.
       * - encoding: Set by libavcodec.
 -     * - decoding: unused
 +     * - decoding: Set by libavcodec.
       */
      int delay;
  
      /**
       * Audio channel layout.
       * - encoding: set by user.
 -     * - decoding: set by libavcodec.
 +     * - decoding: set by user, may be overwritten by libavcodec.
       */
      int64_t channel_layout;
  
       * Hardware accelerator context.
       * For some hardware accelerators, a global context needs to be
       * provided by the user. In that case, this holds display-dependent
 -     * data Libav cannot instantiate itself. Please refer to the
 -     * Libav HW accelerator documentation to know how to fill this
 +     * data FFmpeg cannot instantiate itself. Please refer to the
 +     * FFmpeg HW accelerator documentation to know how to fill this
       * is. e.g. for VA API, this is a struct vaapi_context.
       * - encoding: unused
       * - decoding: Set by user
      enum AVAudioServiceType audio_service_type;
  
      /**
 -     * Used to request a sample format from the decoder.
 -     * - encoding: unused.
 +     * desired sample format
 +     * - encoding: Not used.
       * - decoding: Set by user.
 +     * Decoder will decode to this format if it can.
       */
      enum AVSampleFormat request_sample_fmt;
  
  #define AV_EF_BUFFER    (1<<2)
  #define AV_EF_EXPLODE   (1<<3)
  
 +#define AV_EF_CAREFUL    (1<<16)
 +#define AV_EF_COMPLIANT  (1<<17)
 +#define AV_EF_AGGRESSIVE (1<<18)
 +
      /**
       * Private context used for internal data.
       *
       * libavcodec functions.
       */
      struct AVCodecInternal *internal;
 +
 +    /**
 +     * Current statistics for PTS correction.
 +     * - decoding: maintained and used by libavcodec, not intended to be used by user apps
 +     * - encoding: unused
 +     */
 +    int64_t pts_correction_num_faulty_pts; /// Number of incorrect PTS values so far
 +    int64_t pts_correction_num_faulty_dts; /// Number of incorrect DTS values so far
 +    int64_t pts_correction_last_pts;       /// PTS of the last frame
 +    int64_t pts_correction_last_dts;       /// DTS of the last frame
 +
  } AVCodecContext;
  
  /**
@@@ -3383,11 -3336,6 +3414,11 @@@ uint8_t* av_packet_new_side_data(AVPack
  uint8_t* av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
                                   int *size);
  
 +int av_packet_merge_side_data(AVPacket *pkt);
 +
 +int av_packet_split_side_data(AVPacket *pkt);
 +
 +
  /* resample.c */
  
  struct ReSampleContext;
@@@ -3545,17 -3493,8 +3576,17 @@@ int avpicture_layout(const AVPicture* s
  int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height);
  void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift);
  
 +/**
 + * Get the name of a codec.
 + * @return  a static string identifying the codec; never NULL
 + */
 +const char *avcodec_get_name(enum CodecID id);
 +
  #if FF_API_GET_PIX_FMT_NAME
  /**
 + * Return the short name for a pixel format.
 + *
 + * \see av_get_pix_fmt(), av_get_pix_fmt_string().
   * @deprecated Deprecated in favor of av_get_pix_fmt_name().
   */
  attribute_deprecated
@@@ -3602,8 -3541,7 +3633,8 @@@ size_t av_get_codec_tag_string(char *bu
   * @param[in] dst_pix_fmt destination pixel format
   * @param[in] src_pix_fmt source pixel format
   * @param[in] has_alpha Whether the source pixel format alpha channel is used.
 - * @return Combination of flags informing you what kind of losses will occur.
 + * @return Combination of flags informing you what kind of losses will occur
 + * (maximum loss for an invalid dst_pix_fmt).
   */
  int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
                               int has_alpha);
   * The pixel formats from which it chooses one, are determined by the
   * pix_fmt_mask parameter.
   *
 + * Note, only the first 64 pixel formats will fit in pix_fmt_mask.
 + *
   * @code
   * src_pix_fmt = PIX_FMT_YUV420P;
 - * pix_fmt_mask = (1 << PIX_FMT_YUV422P) || (1 << PIX_FMT_RGB24);
 + * pix_fmt_mask = (1 << PIX_FMT_YUV422P) | (1 << PIX_FMT_RGB24);
   * dst_pix_fmt = avcodec_find_best_pix_fmt(pix_fmt_mask, src_pix_fmt, alpha, &loss);
   * @endcode
   *
  enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
                                int has_alpha, int *loss_ptr);
  
 +/**
 + * Find the best pixel format to convert to given a certain source pixel
 + * format and a selection of two destination pixel formats. When converting from
 + * one pixel format to another, information loss may occur.  For example, when converting
 + * from RGB24 to GRAY, the color information will be lost. Similarly, other losses occur when
 + * converting from some formats to other formats. avcodec_find_best_pix_fmt2() selects which of
 + * the given pixel formats should be used to suffer the least amount of loss.
 + *
 + * If one of the destination formats is PIX_FMT_NONE the other pixel format (if valid) will be
 + * returned.
 + *
 + * @code
 + * src_pix_fmt = PIX_FMT_YUV420P;
 + * dst_pix_fmt1= PIX_FMT_RGB24;
 + * dst_pix_fmt2= PIX_FMT_GRAY8;
 + * dst_pix_fmt3= PIX_FMT_RGB8;
 + * loss= FF_LOSS_CHROMA; // don't care about chroma loss, so chroma loss will be ignored.
 + * dst_pix_fmt = avcodec_find_best_pix_fmt2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, alpha, &loss);
 + * dst_pix_fmt = avcodec_find_best_pix_fmt2(dst_pix_fmt, dst_pix_fmt3, src_pix_fmt, alpha, &loss);
 + * @endcode
 + *
 + * @param[in] dst_pix_fmt1 One of the two destination pixel formats to choose from
 + * @param[in] dst_pix_fmt2 The other of the two destination pixel formats to choose from
 + * @param[in] src_pix_fmt Source pixel format
 + * @param[in] has_alpha Whether the source pixel format alpha channel is used.
 + * @param[in, out] loss_ptr Combination of loss flags. In: selects which of the losses to ignore, i.e.
 + *                               NULL or value of zero means we care about all losses. Out: the loss
 + *                               that occurs when converting from src to selected dst pixel format.
 + * @return The best pixel format to convert to or -1 if none was found.
 + */
 +enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat dst_pix_fmt1, enum PixelFormat dst_pix_fmt2,
 +                                            enum PixelFormat src_pix_fmt, int has_alpha, int *loss_ptr);
 +
  #if FF_API_GET_ALPHA_INFO
  #define FF_ALPHA_TRANSP       0x0001 /* image has some totally transparent pixels */
  #define FF_ALPHA_SEMI_TRANSP  0x0002 /* image has some transparent pixels */
@@@ -4453,7 -4356,7 +4484,7 @@@ unsigned int av_xiphlacing(unsigned cha
  
  /**
   * Logs a generic warning message about a missing feature. This function is
 - * intended to be used internally by Libav (libavcodec, libavformat, etc.)
 + * intended to be used internally by FFmpeg (libavcodec, libavformat, etc.)
   * only, and would normally not be used by applications.
   * @param[in] avc a pointer to an arbitrary struct of which the first field is
   * a pointer to an AVClass struct
@@@ -4467,7 -4370,7 +4498,7 @@@ void av_log_missing_feature(void *avc, 
  
  /**
   * Log a generic warning message asking for a sample. This function is
 - * intended to be used internally by Libav (libavcodec, libavformat, etc.)
 + * intended to be used internally by FFmpeg (libavcodec, libavformat, etc.)
   * only, and would normally not be used by applications.
   * @param[in] avc a pointer to an arbitrary struct of which the first field is
   * a pointer to an AVClass struct
@@@ -4504,7 -4407,7 +4535,7 @@@ enum AVLockOp 
   * lockmgr should store/get a pointer to a user allocated mutex. It's
   * NULL upon AV_LOCK_CREATE and != NULL for all other ops.
   *
 - * @param cb User defined callback. Note: Libav may invoke calls to this
 + * @param cb User defined callback. Note: FFmpeg may invoke calls to this
   *           callback during the call to av_lockmgr_register().
   *           Thus, the application must be prepared to handle that.
   *           If cb is set to NULL the lockmgr will be unregistered.
index 5af1c5c6caed78b367c4217f5543ed7b8bf21864,d3715ac740a98cd589cea1681d27d45de3b4b78c..725270c9fa45580a353b736df8263099e28e7ec3
@@@ -4,20 -4,20 +4,20 @@@
   *
   * Copyright (c) 2008 Bartlomiej Wolowiec
   *
 - * 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
   */
  
   *
   * Generic codec information: libavcodec/nellymoserdec.c
   *
 - * Some information also from: http://samples.libav.org/A-codecs/Nelly_Moser/ASAO/ASAO.zip
 + * Some information also from: http://samples.mplayerhq.hu/A-codecs/Nelly_Moser/ASAO/ASAO.zip
   *                             (Copyright Joseph Artsimovich and UAB "DKD")
   *
   * for more information about nellymoser format, visit:
   * http://wiki.multimedia.cx/index.php?title=Nellymoser
   */
  
+ #include "libavutil/mathematics.h"
  #include "nellymoser.h"
  #include "avcodec.h"
  #include "dsputil.h"
diff --combined libavcodec/pthread.c
index 8a2cdbdcdb2880c3ab16f8ab275dfdfac461a981,9c3453f13d3934d2b7aa913706a711c9b4c45f57..83ecaad069cdd8a9b5953652884329b4d04c6887
@@@ -6,20 -6,20 +6,20 @@@
   * to Michael Niedermayer <michaelni@gmx.at> for writing initial
   * implementation.
   *
 - * 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
   */
  
@@@ -38,8 -38,6 +38,8 @@@
  #include <pthread.h>
  #elif HAVE_W32THREADS
  #include "w32pthreads.h"
 +#elif HAVE_OS2THREADS
 +#include "os2threads.h"
  #endif
  
  typedef int (action_func)(AVCodecContext *c, void *arg);
@@@ -365,8 -363,7 +365,8 @@@ static int update_context_from_thread(A
      }
  
      if (for_user) {
-         dst->delay         = src->thread_count - 1;
-         dst->coded_frame   = src->coded_frame;
++        dst->delay       = src->thread_count - 1;
+         dst->coded_frame = src->coded_frame;
      } else {
          if (dst->codec->update_thread_context)
              err = dst->codec->update_thread_context(dst, src);
@@@ -610,10 -607,6 +610,10 @@@ void ff_thread_finish_setup(AVCodecCont
  
      if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
  
 +    if(p->state == STATE_SETUP_FINISHED){
 +        av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
 +    }
 +
      pthread_mutex_lock(&p->progress_mutex);
      p->state = STATE_SETUP_FINISHED;
      pthread_cond_broadcast(&p->progress_cond);
@@@ -815,8 -808,6 +815,8 @@@ int ff_thread_get_buffer(AVCodecContex
  
      f->owner = avctx;
  
 +    ff_init_buffer_info(avctx, f);
 +
      if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
          f->thread_opaque = NULL;
          return avctx->get_buffer(avctx, f);
diff --combined libavcodec/version.h
index a2d96a4900fb672c851f75f1aef4b601a3ea1fe9,3f5c42e33e50f5791f63f42f26f7a9ac9fb6fbff..591d419d26a38e0595a6a87f88dbbf6e15b75f05
@@@ -1,19 -1,19 +1,19 @@@
  /*
   *
 - * 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
   */
  
@@@ -21,7 -21,7 +21,7 @@@
  #define AVCODEC_VERSION_H
  
  #define LIBAVCODEC_VERSION_MAJOR 53
- #define LIBAVCODEC_VERSION_MINOR 36
 -#define LIBAVCODEC_VERSION_MINOR 22
++#define LIBAVCODEC_VERSION_MINOR 37
  #define LIBAVCODEC_VERSION_MICRO  0
  
  #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
diff --combined libavcodec/wma.c
index 67599b7eab8fce263bc76af8416cfc2888827237,4cdffcd1013e9e17df085c23440ac8e17397e1ec..8f464619c212019d1bcaf3f9f906db692449d4cf
@@@ -1,21 -1,21 +1,21 @@@
  /*
   * WMA compatible codec
 - * Copyright (c) 2002-2007 The Libav Project
 + * Copyright (c) 2002-2007 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
   */
  
@@@ -137,6 -137,9 +137,9 @@@ int ff_wma_init(AVCodecContext *avctx, 
  
      /* compute MDCT block size */
      s->frame_len_bits = ff_wma_get_frame_len_bits(s->sample_rate, s->version, 0);
+     s->next_block_len_bits = s->frame_len_bits;
+     s->prev_block_len_bits = s->frame_len_bits;
+     s->block_len_bits      = s->frame_len_bits;
  
      s->frame_len = 1 << s->frame_len_bits;
      if (s->use_variable_block_len) {
index 83b921913508b05264ae75409dc37dec88b6368f,104bd7595f598d164f444e74817ebe7cf34cba58..a27c3b53d681fe012fcfe2c8c5a4e16f1a6cf79a
@@@ -3,20 -3,20 +3,20 @@@
   * Copyright (c) 2000, 2001 Fabrice Bellard
   * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
   *
 - * 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
   *
   * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
@@@ -31,7 -31,6 +31,7 @@@
  #include "libavcodec/ac3dec.h"
  #include "dsputil_mmx.h"
  #include "idct_xvid.h"
 +#include "diracdsp_mmx.h"
  
  //#undef NDEBUG
  //#include <assert.h>
@@@ -583,6 -582,28 +583,6 @@@ static void add_bytes_mmx(uint8_t *dst
          dst[i+0] += src[i+0];
  }
  
 -static void add_bytes_l2_mmx(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w){
 -    x86_reg i=0;
 -    __asm__ volatile(
 -        "jmp 2f                         \n\t"
 -        "1:                             \n\t"
 -        "movq   (%2, %0), %%mm0         \n\t"
 -        "movq  8(%2, %0), %%mm1         \n\t"
 -        "paddb  (%3, %0), %%mm0         \n\t"
 -        "paddb 8(%3, %0), %%mm1         \n\t"
 -        "movq %%mm0,  (%1, %0)          \n\t"
 -        "movq %%mm1, 8(%1, %0)          \n\t"
 -        "add $16, %0                    \n\t"
 -        "2:                             \n\t"
 -        "cmp %4, %0                     \n\t"
 -        " js 1b                         \n\t"
 -        : "+r" (i)
 -        : "r"(dst), "r"(src1), "r"(src2), "r"((x86_reg)w-15)
 -    );
 -    for(; i<w; i++)
 -        dst[i] = src1[i] + src2[i];
 -}
 -
  #if HAVE_7REGS
  static void add_hfyu_median_prediction_cmov(uint8_t *dst, const uint8_t *top, const uint8_t *diff, int w, int *left, int *left_top) {
      x86_reg w2 = -w;
@@@ -858,6 -879,80 +858,6 @@@ static void draw_edges_mmx(uint8_t *buf
      }
  }
  
 -#define PAETH(cpu, abs3)\
 -static void add_png_paeth_prediction_##cpu(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)\
 -{\
 -    x86_reg i = -bpp;\
 -    x86_reg end = w-3;\
 -    __asm__ volatile(\
 -        "pxor      %%mm7, %%mm7 \n"\
 -        "movd    (%1,%0), %%mm0 \n"\
 -        "movd    (%2,%0), %%mm1 \n"\
 -        "punpcklbw %%mm7, %%mm0 \n"\
 -        "punpcklbw %%mm7, %%mm1 \n"\
 -        "add       %4, %0 \n"\
 -        "1: \n"\
 -        "movq      %%mm1, %%mm2 \n"\
 -        "movd    (%2,%0), %%mm1 \n"\
 -        "movq      %%mm2, %%mm3 \n"\
 -        "punpcklbw %%mm7, %%mm1 \n"\
 -        "movq      %%mm2, %%mm4 \n"\
 -        "psubw     %%mm1, %%mm3 \n"\
 -        "psubw     %%mm0, %%mm4 \n"\
 -        "movq      %%mm3, %%mm5 \n"\
 -        "paddw     %%mm4, %%mm5 \n"\
 -        abs3\
 -        "movq      %%mm4, %%mm6 \n"\
 -        "pminsw    %%mm5, %%mm6 \n"\
 -        "pcmpgtw   %%mm6, %%mm3 \n"\
 -        "pcmpgtw   %%mm5, %%mm4 \n"\
 -        "movq      %%mm4, %%mm6 \n"\
 -        "pand      %%mm3, %%mm4 \n"\
 -        "pandn     %%mm3, %%mm6 \n"\
 -        "pandn     %%mm0, %%mm3 \n"\
 -        "movd    (%3,%0), %%mm0 \n"\
 -        "pand      %%mm1, %%mm6 \n"\
 -        "pand      %%mm4, %%mm2 \n"\
 -        "punpcklbw %%mm7, %%mm0 \n"\
 -        "movq      %6,    %%mm5 \n"\
 -        "paddw     %%mm6, %%mm0 \n"\
 -        "paddw     %%mm2, %%mm3 \n"\
 -        "paddw     %%mm3, %%mm0 \n"\
 -        "pand      %%mm5, %%mm0 \n"\
 -        "movq      %%mm0, %%mm3 \n"\
 -        "packuswb  %%mm3, %%mm3 \n"\
 -        "movd      %%mm3, (%1,%0) \n"\
 -        "add       %4, %0 \n"\
 -        "cmp       %5, %0 \n"\
 -        "jle 1b \n"\
 -        :"+r"(i)\
 -        :"r"(dst), "r"(top), "r"(src), "r"((x86_reg)bpp), "g"(end),\
 -         "m"(ff_pw_255)\
 -        :"memory"\
 -    );\
 -}
 -
 -#define ABS3_MMX2\
 -        "psubw     %%mm5, %%mm7 \n"\
 -        "pmaxsw    %%mm7, %%mm5 \n"\
 -        "pxor      %%mm6, %%mm6 \n"\
 -        "pxor      %%mm7, %%mm7 \n"\
 -        "psubw     %%mm3, %%mm6 \n"\
 -        "psubw     %%mm4, %%mm7 \n"\
 -        "pmaxsw    %%mm6, %%mm3 \n"\
 -        "pmaxsw    %%mm7, %%mm4 \n"\
 -        "pxor      %%mm7, %%mm7 \n"
 -
 -#define ABS3_SSSE3\
 -        "pabsw     %%mm3, %%mm3 \n"\
 -        "pabsw     %%mm4, %%mm4 \n"\
 -        "pabsw     %%mm5, %%mm5 \n"
 -
 -PAETH(mmx2, ABS3_MMX2)
 -#if HAVE_SSSE3
 -PAETH(ssse3, ABS3_SSSE3)
 -#endif
 -
  #define QPEL_V_LOW(m3,m4,m5,m6, pw_20, pw_3, rnd, in0, in1, in2, in7, out, OP)\
          "paddw " #m4 ", " #m3 "           \n\t" /* x1 */\
          "movq "MANGLE(ff_pw_20)", %%mm4   \n\t" /* 20 */\
@@@ -1865,84 -1960,6 +1865,84 @@@ void ff_avg_vc1_mspel_mc00_mmx2(uint8_
      avg_pixels8_mmx2(dst, src, stride, 8);
  }
  
 +/* only used in VP3/5/6 */
 +static void put_vp_no_rnd_pixels8_l2_mmx(uint8_t *dst, const uint8_t *a, const uint8_t *b, int stride, int h)
 +{
 +//    START_TIMER
 +    MOVQ_BFE(mm6);
 +    __asm__ volatile(
 +        "1:                             \n\t"
 +        "movq   (%1), %%mm0             \n\t"
 +        "movq   (%2), %%mm1             \n\t"
 +        "movq   (%1,%4), %%mm2          \n\t"
 +        "movq   (%2,%4), %%mm3          \n\t"
 +        PAVGBP_MMX_NO_RND(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
 +        "movq   %%mm4, (%3)             \n\t"
 +        "movq   %%mm5, (%3,%4)          \n\t"
 +
 +        "movq   (%1,%4,2), %%mm0        \n\t"
 +        "movq   (%2,%4,2), %%mm1        \n\t"
 +        "movq   (%1,%5), %%mm2          \n\t"
 +        "movq   (%2,%5), %%mm3          \n\t"
 +        "lea    (%1,%4,4), %1           \n\t"
 +        "lea    (%2,%4,4), %2           \n\t"
 +        PAVGBP_MMX_NO_RND(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
 +        "movq   %%mm4, (%3,%4,2)        \n\t"
 +        "movq   %%mm5, (%3,%5)          \n\t"
 +        "lea    (%3,%4,4), %3           \n\t"
 +        "subl   $4, %0                  \n\t"
 +        "jnz    1b                      \n\t"
 +        :"+r"(h), "+r"(a), "+r"(b), "+r"(dst)
 +        :"r"((x86_reg)stride), "r"((x86_reg)3L*stride)
 +        :"memory");
 +//    STOP_TIMER("put_vp_no_rnd_pixels8_l2_mmx")
 +}
 +static void put_vp_no_rnd_pixels16_l2_mmx(uint8_t *dst, const uint8_t *a, const uint8_t *b, int stride, int h)
 +{
 +    put_vp_no_rnd_pixels8_l2_mmx(dst, a, b, stride, h);
 +    put_vp_no_rnd_pixels8_l2_mmx(dst+8, a+8, b+8, stride, h);
 +}
 +
 +#if CONFIG_DIRAC_DECODER
 +#define DIRAC_PIXOP(OPNAME, EXT)\
 +void ff_ ## OPNAME ## _dirac_pixels8_ ## EXT(uint8_t *dst, const uint8_t *src[5], int stride, int h)\
 +{\
 +    OPNAME ## _pixels8_ ## EXT(dst, src[0], stride, h);\
 +}\
 +void ff_ ## OPNAME ## _dirac_pixels16_ ## EXT(uint8_t *dst, const uint8_t *src[5], int stride, int h)\
 +{\
 +    OPNAME ## _pixels16_ ## EXT(dst, src[0], stride, h);\
 +}\
 +void ff_ ## OPNAME ## _dirac_pixels32_ ## EXT(uint8_t *dst, const uint8_t *src[5], int stride, int h)\
 +{\
 +    OPNAME ## _pixels16_ ## EXT(dst   , src[0]   , stride, h);\
 +    OPNAME ## _pixels16_ ## EXT(dst+16, src[0]+16, stride, h);\
 +}
 +
 +DIRAC_PIXOP(put, mmx)
 +DIRAC_PIXOP(avg, mmx)
 +DIRAC_PIXOP(avg, mmx2)
 +
 +void ff_put_dirac_pixels16_sse2(uint8_t *dst, const uint8_t *src[5], int stride, int h)
 +{
 +    put_pixels16_sse2(dst, src[0], stride, h);
 +}
 +void ff_avg_dirac_pixels16_sse2(uint8_t *dst, const uint8_t *src[5], int stride, int h)
 +{
 +    avg_pixels16_sse2(dst, src[0], stride, h);
 +}
 +void ff_put_dirac_pixels32_sse2(uint8_t *dst, const uint8_t *src[5], int stride, int h)
 +{
 +    put_pixels16_sse2(dst   , src[0]   , stride, h);
 +    put_pixels16_sse2(dst+16, src[0]+16, stride, h);
 +}
 +void ff_avg_dirac_pixels32_sse2(uint8_t *dst, const uint8_t *src[5], int stride, int h)
 +{
 +    avg_pixels16_sse2(dst   , src[0]   , stride, h);
 +    avg_pixels16_sse2(dst+16, src[0]+16, stride, h);
 +}
 +#endif
 +
  /* XXX: those functions should be suppressed ASAP when all IDCTs are
     converted */
  #if CONFIG_GPL
@@@ -2535,12 -2552,10 +2535,12 @@@ void dsputil_init_mmx(DSPContext* c, AV
  #endif
  
          c->add_bytes= add_bytes_mmx;
 -        c->add_bytes_l2= add_bytes_l2_mmx;
  
          if (!high_bit_depth)
 -        c->draw_edges = draw_edges_mmx;
 +            c->draw_edges = draw_edges_mmx;
 +
 +        c->put_no_rnd_pixels_l2[0]= put_vp_no_rnd_pixels16_l2_mmx;
 +        c->put_no_rnd_pixels_l2[1]= put_vp_no_rnd_pixels8_l2_mmx;
  
          if (CONFIG_H263_DECODER || CONFIG_H263_ENCODER) {
              c->h263_v_loop_filter= h263_v_loop_filter_mmx;
                  c->add_hfyu_median_prediction = add_hfyu_median_prediction_cmov;
  #endif
  
 -            c->add_png_paeth_prediction= add_png_paeth_prediction_mmx2;
          } else if (HAVE_AMD3DNOW && (mm_flags & AV_CPU_FLAG_3DNOW)) {
              c->prefetch = prefetch_3dnow;
  
                  H264_QPEL_FUNCS_10(2, 0, ssse3_cache64)
                  H264_QPEL_FUNCS_10(3, 0, ssse3_cache64)
              }
 -#endif
 -            c->add_png_paeth_prediction= add_png_paeth_prediction_ssse3;
 -#if HAVE_YASM
              if (!high_bit_depth) {
              c->put_h264_chroma_pixels_tab[0]= ff_put_h264_chroma_mc8_ssse3_rnd;
              c->avg_h264_chroma_pixels_tab[0]= ff_avg_h264_chroma_mc8_ssse3_rnd;
  #if HAVE_YASM
              c->scalarproduct_float = ff_scalarproduct_float_sse;
              c->butterflies_float_interleave = ff_butterflies_float_interleave_sse;
+             if (!high_bit_depth)
+                 c->emulated_edge_mc = emulated_edge_mc_sse;
+             c->gmc = gmc_sse;
  #endif
          }
          if (HAVE_AMD3DNOW && (mm_flags & AV_CPU_FLAG_3DNOW))
                      c->apply_window_int16 = ff_apply_window_int16_sse2;
                  }
              }
-             if (!high_bit_depth)
-             c->emulated_edge_mc = emulated_edge_mc_sse;
-             c->gmc= gmc_sse;
  #endif
          }
          if (mm_flags & AV_CPU_FLAG_SSSE3) {
index 0e497c9cdaa50021b812cd422dab42a24e1cfb81,4d2fb6a373c6023ca62cf6f443761a10090e8e73..64c87226a77642d51d05ca7669138d716de93bba
@@@ -2,24 -2,24 +2,24 @@@
  ;* MMX optimized DSP utils
  ;* Copyright (c) 2008 Loren Merritt
  ;*
 -;* 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 "x86inc.asm"
 +%include "libavutil/x86/x86inc.asm"
  %include "x86util.asm"
  
  SECTION_RODATA
@@@ -497,14 -497,14 +497,14 @@@ cglobal scalarproduct_float_sse, 3,3,2
  ; ... and then the same for left/right extend also. See below for loop
  ; function implementations. Fast are fixed-width, slow is variable-width
  
- %macro EMU_EDGE_FUNC 1
+ %macro EMU_EDGE_FUNC 0
  %ifdef ARCH_X86_64
  %define w_reg r10
- cglobal emu_edge_core_%1, 6, 7, 1
+ cglobal emu_edge_core, 6, 7, 1
      mov        r11, r5          ; save block_h
  %else
  %define w_reg r6
- cglobal emu_edge_core_%1, 2, 7, 0
+ cglobal emu_edge_core, 2, 7, 0
      mov         r4, r4m         ; end_y
      mov         r5, r5m         ; block_h
  %endif
  ;            - if (%2 & 3 == 3)  fills 2 bytes into r6, and 1 into ebx
  ;            - else              fills remaining bytes into ebx
  ; writing data out is in the same way
- %macro READ_NUM_BYTES 3
+ %macro READ_NUM_BYTES 2
  %assign %%src_off 0 ; offset in source buffer
  %assign %%smidx   0 ; mmx register idx
  %assign %%sxidx   0 ; xmm register idx
  
- %ifnidn %3, mmx
+ %if cpuflag(sse)
  %rep %2/16
-     movdqu xmm %+ %%sxidx, [r1+%%src_off]
+     movups xmm %+ %%sxidx, [r1+%%src_off]
  %assign %%src_off %%src_off+16
  %assign %%sxidx   %%sxidx+1
  %endrep ; %2/16
- %endif ; !mmx
+ %endif
  
  %ifdef ARCH_X86_64
  %if (%2-%%src_off) == 8
  %endif ; (%2-%%src_off) == 1/2/3
  %endmacro ; READ_NUM_BYTES
  
- %macro WRITE_NUM_BYTES 3
+ %macro WRITE_NUM_BYTES 2
  %assign %%dst_off 0 ; offset in destination buffer
  %assign %%dmidx   0 ; mmx register idx
  %assign %%dxidx   0 ; xmm register idx
  
- %ifnidn %3, mmx
+ %if cpuflag(sse)
  %rep %2/16
-     movdqu [r0+%%dst_off], xmm %+ %%dxidx
+     movups [r0+%%dst_off], xmm %+ %%dxidx
  %assign %%dst_off %%dst_off+16
  %assign %%dxidx   %%dxidx+1
  %endrep ; %2/16
  ; those out into the destination buffer
  ; r0=buf,r1=src,r2=linesize,r3(64)/r3m(32)=start_x,r4=end_y,r5=block_h
  ; r6(eax/64)/r3(ebx/32)=val_reg
- %macro VERTICAL_EXTEND 1
+ %macro VERTICAL_EXTEND 0
  %assign %%n 1
  %rep 22
  ALIGN 128
      cmp      dword r3m, 0
      je .emuedge_copy_body_ %+ %%n %+ _loop
  %endif ; ARCH_X86_64/32
-     READ_NUM_BYTES  top,    %%n, %1          ; read bytes
+     READ_NUM_BYTES  top,    %%n              ; read bytes
  .emuedge_extend_top_ %+ %%n %+ _loop:        ; do {
-     WRITE_NUM_BYTES top,    %%n, %1          ;   write bytes
+     WRITE_NUM_BYTES top,    %%n              ;   write bytes
      add            r0 , r2                   ;   dst += linesize
  %ifdef ARCH_X86_64
      dec            r3d
  
      ; copy body pixels
  .emuedge_copy_body_ %+ %%n %+ _loop:         ; do {
-     READ_NUM_BYTES  body,   %%n, %1          ;   read bytes
-     WRITE_NUM_BYTES body,   %%n, %1          ;   write bytes
+     READ_NUM_BYTES  body,   %%n              ;   read bytes
+     WRITE_NUM_BYTES body,   %%n              ;   write bytes
      add            r0 , r2                   ;   dst += linesize
      add            r1 , r2                   ;   src += linesize
      dec            r4d
      test           r5 , r5                   ; if (!block_h)
      jz .emuedge_v_extend_end_ %+ %%n         ;   goto end
      sub            r1 , r2                   ; src -= linesize
-     READ_NUM_BYTES  bottom, %%n, %1          ; read bytes
+     READ_NUM_BYTES  bottom, %%n              ; read bytes
  .emuedge_extend_bottom_ %+ %%n %+ _loop:     ; do {
-     WRITE_NUM_BYTES bottom, %%n, %1          ;   write bytes
+     WRITE_NUM_BYTES bottom, %%n              ;   write bytes
      add            r0 , r2                   ;   dst += linesize
      dec            r5d
      jnz .emuedge_extend_bottom_ %+ %%n %+ _loop ; } while (--block_h)
  ; lowest two bytes of the register (so val*0x0101), and are splatted
  ; into each byte of mm0 as well if n_pixels >= 8
  
- %macro READ_V_PIXEL 3
+ %macro READ_V_PIXEL 2
      mov        vall, %2
      mov        valh, vall
  %if %1 >= 8
      movd        mm0, vald
- %ifidn %3, mmx
+ %if cpuflag(mmx2)
+     pshufw      mm0, mm0, 0
+ %else ; mmx
      punpcklwd   mm0, mm0
      punpckldq   mm0, mm0
- %else ; !mmx
-     pshufw      mm0, mm0, 0
- %endif ; mmx
+ %endif ; sse
  %endif ; %1 >= 8
  %endmacro
  
  %endmacro
  
  ; r0=buf+block_h*linesize, r1=start_x, r2=linesize, r5=block_h, r6/r3=val
- %macro LEFT_EXTEND 1
+ %macro LEFT_EXTEND 0
  %assign %%n 2
  %rep 11
  ALIGN 64
  .emuedge_extend_left_ %+ %%n:          ; do {
      sub         r0, r2                 ;   dst -= linesize
-     READ_V_PIXEL  %%n, [r0+r1], %1     ;   read pixels
+     READ_V_PIXEL  %%n, [r0+r1]         ;   read pixels
      WRITE_V_PIXEL %%n, r0              ;   write pixels
      dec         r5
      jnz .emuedge_extend_left_ %+ %%n   ; } while (--block_h)
  %endmacro ; LEFT_EXTEND
  
  ; r3/r0=buf+block_h*linesize, r2=linesize, r11/r5=block_h, r0/r6=end_x, r6/r3=val
- %macro RIGHT_EXTEND 1
+ %macro RIGHT_EXTEND 0
  %assign %%n 2
  %rep 11
  ALIGN 64
  .emuedge_extend_right_ %+ %%n:          ; do {
  %ifdef ARCH_X86_64
      sub        r3, r2                   ;   dst -= linesize
-     READ_V_PIXEL  %%n, [r3+w_reg-1], %1 ;   read pixels
+     READ_V_PIXEL  %%n, [r3+w_reg-1]     ;   read pixels
      WRITE_V_PIXEL %%n, r3+r4-%%n        ;   write pixels
      dec       r11
  %else ; ARCH_X86_32
      sub        r0, r2                   ;   dst -= linesize
-     READ_V_PIXEL  %%n, [r0+w_reg-1], %1 ;   read pixels
+     READ_V_PIXEL  %%n, [r0+w_reg-1]     ;   read pixels
      WRITE_V_PIXEL %%n, r0+r4-%%n        ;   write pixels
      dec     r5
  %endif ; ARCH_X86_64/32
  .%1_skip_%4_px:
  %endmacro
  
- %macro V_COPY_ROW 3
+ %macro V_COPY_ROW 2
  %ifidn %1, bottom
      sub         r1, linesize
  %endif
  .%1_copy_loop:
      xor    cnt_reg, cnt_reg
- %ifidn %3, mmx
+ %if notcpuflag(sse)
  %define linesize r2m
      V_COPY_NPX %1,  mm0, movq,    8, 0xFFFFFFF8
- %else ; !mmx
-     V_COPY_NPX %1, xmm0, movdqu, 16, 0xFFFFFFF0
+ %else ; sse
+     V_COPY_NPX %1, xmm0, movups, 16, 0xFFFFFFF0
  %ifdef ARCH_X86_64
  %define linesize r2
      V_COPY_NPX %1, rax , mov,     8
  %define linesize r2m
      V_COPY_NPX %1,  mm0, movq,    8
  %endif ; ARCH_X86_64/32
- %endif ; mmx
+ %endif ; sse
      V_COPY_NPX %1, vald, mov,     4
      V_COPY_NPX %1, valw, mov,     2
      V_COPY_NPX %1, vall, mov,     1
      jnz .%1_copy_loop
  %endmacro
  
- %macro SLOW_V_EXTEND 1
+ %macro SLOW_V_EXTEND 0
  .slow_v_extend_loop:
  ; r0=buf,r1=src,r2(64)/r2m(32)=linesize,r3(64)/r3m(32)=start_x,r4=end_y,r5=block_h
  ; r11(64)/r3(later-64)/r2(32)=cnt_reg,r6(64)/r3(32)=val_reg,r10(64)/r6(32)=w=end_x-start_x
      test        r3, r3
  %define cnt_reg r11
      jz .do_body_copy            ; if (!start_y) goto do_body_copy
-     V_COPY_ROW top, r3, %1
+     V_COPY_ROW top, r3
  %else
      cmp  dword r3m, 0
  %define cnt_reg r2
      je .do_body_copy            ; if (!start_y) goto do_body_copy
-     V_COPY_ROW top, dword r3m, %1
+     V_COPY_ROW top, dword r3m
  %endif
  
  .do_body_copy:
-     V_COPY_ROW body, r4, %1
+     V_COPY_ROW body, r4
  
  %ifdef ARCH_X86_64
      pop        r11              ; restore old value of block_h
  %else
      jz .skip_bottom_extend
  %endif
-     V_COPY_ROW bottom, r5, %1
+     V_COPY_ROW bottom, r5
  %ifdef ARCH_X86_32
  .skip_bottom_extend:
      mov         r2, r2m
      jmp .v_extend_end
  %endmacro
  
- %macro SLOW_LEFT_EXTEND 1
+ %macro SLOW_LEFT_EXTEND 0
  .slow_left_extend_loop:
  ; r0=buf+block_h*linesize,r2=linesize,r6(64)/r3(32)=val,r5=block_h,r4=cntr,r10/r6=start_x
      mov         r4, 8
      sub         r0, linesize
-     READ_V_PIXEL 8, [r0+w_reg], %1
+     READ_V_PIXEL 8, [r0+w_reg]
  .left_extend_8px_loop:
      movq [r0+r4-8], mm0
      add         r4, 8
      jmp .right_extend
  %endmacro
  
- %macro SLOW_RIGHT_EXTEND 1
+ %macro SLOW_RIGHT_EXTEND 0
  .slow_right_extend_loop:
  ; r3(64)/r0(32)=buf+block_h*linesize,r2=linesize,r4=block_w,r11(64)/r5(32)=block_h,
  ; r10(64)/r6(32)=end_x,r6/r3=val,r1=cntr
  %endif
      lea         r1, [r4-8]
      sub    buf_reg, linesize
-     READ_V_PIXEL 8, [buf_reg+w_reg-1], %1
+     READ_V_PIXEL 8, [buf_reg+w_reg-1]
  .right_extend_8px_loop:
      movq [buf_reg+r1], mm0
      sub         r1, 8
  %endmacro
  
  %macro emu_edge 1
- EMU_EDGE_FUNC     %1
- VERTICAL_EXTEND   %1
- LEFT_EXTEND       %1
- RIGHT_EXTEND      %1
- SLOW_V_EXTEND     %1
- SLOW_LEFT_EXTEND  %1
- SLOW_RIGHT_EXTEND %1
+ INIT_XMM %1
+ EMU_EDGE_FUNC
+ VERTICAL_EXTEND
+ LEFT_EXTEND
+ RIGHT_EXTEND
+ SLOW_V_EXTEND
+ SLOW_LEFT_EXTEND
+ SLOW_RIGHT_EXTEND
  %endmacro
  
  emu_edge sse
diff --combined libavformat/avformat.h
index e0e9c1f734fee1f2fbcce25cb353f0f4a1c3af0e,5276af1c15f64b6ce4febc7bd34b8c96549103f2..752645e2c58412ac49555b087ff6e528d9ce9a62
@@@ -1,26 -1,60 +1,60 @@@
  /*
   * 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_AVFORMAT_H
  #define AVFORMAT_AVFORMAT_H
  
+ /**
+  * @defgroup libavf I/O and Muxing/Demuxing Library
+  * @{
+  *
+  * @defgroup lavf_decoding Demuxing
+  * @{
+  * @}
+  *
+  * @defgroup lavf_encoding Muxing
+  * @{
+  * @}
+  *
+  * @defgroup lavf_proto I/O Read/Write
+  * @{
+  * @}
+  *
+  * @defgroup lavf_codec Demuxers
+  * @{
+  * @defgroup lavf_codec_native Native Demuxers
+  * @{
+  * @}
+  * @defgroup lavf_codec_wrappers External library wrappers
+  * @{
+  * @}
+  * @}
+  * @defgroup lavf_protos I/O Protocols
+  * @{
+  * @}
+  * @defgroup lavf_internal Internal
+  * @{
+  * @}
+  * @}
+  *
+  */
  
  /**
   * Return the LIBAVFORMAT_VERSION_INT constant.
@@@ -53,7 -87,7 +87,7 @@@ struct AVFormatContext
   * @defgroup metadata_api Public Metadata API
   * @{
   * The metadata API allows libavformat to export metadata tags to a client
 - * application using a sequence of key/value pairs. Like all strings in Libav,
 + * application using a sequence of key/value pairs. Like all strings in FFmpeg,
   * metadata must be stored as UTF-8 encoded Unicode. Note that metadata
   * exported by demuxers isn't checked to be valid UTF-8 in most cases.
   * Important concepts to keep in mind:
@@@ -275,9 -309,6 +309,9 @@@ typedef struct AVFormatParameters 
  #define AVFMT_NOBINSEARCH   0x2000 /**< Format does not allow to fallback to binary search via read_timestamp */
  #define AVFMT_NOGENSEARCH   0x4000 /**< Format does not allow to fallback to generic search */
  #define AVFMT_NO_BYTE_SEEK  0x8000 /**< Format does not allow seeking by bytes */
 +#define AVFMT_TS_NONSTRICT  0x8000000 /**< Format does not require strictly
 +                                           increasing timestamps, but they must
 +                                           still be monotonic */
  
  typedef struct AVOutputFormat {
      const char *name;
       * AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS
       */
      int flags;
 -    /**
 -     * Currently only used to set pixel format if not YUV420P.
 -     */
 -    int (*set_parameters)(struct AVFormatContext *, AVFormatParameters *);
 +
 +    void *dummy;
 +
      int (*interleave_packet)(struct AVFormatContext *, AVPacket *out,
                               AVPacket *in, int flush);
  
       */
      int (*query_codec)(enum CodecID id, int std_compliance);
  
 +    void (*get_output_timestamp)(struct AVFormatContext *s, int stream,
 +                                 int64_t *dts, int64_t *wall);
 +
      /* private fields */
      struct AVOutputFormat *next;
  } AVOutputFormat;
@@@ -472,12 -501,7 +506,12 @@@ enum AVStreamParseType 
  
  typedef struct AVIndexEntry {
      int64_t pos;
 -    int64_t timestamp;
 +    int64_t timestamp;        /**<
 +                               * Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are available
 +                               * when seeking to this entry. That means preferable PTS on keyframe based formats.
 +                               * But demuxers can choose to store a different timestamp, if it is more convenient for the implementation or nothing better
 +                               * is known
 +                               */
  #define AVINDEX_KEYFRAME 0x0001
      int flags:2;
      int size:30; //Yeah, trying to keep the size of this small to reduce memory requirements (it is 24 vs. 32 bytes due to possible 8-byte alignment).
@@@ -560,12 -584,10 +594,12 @@@ typedef struct AVStream 
  #endif
  
      /**
 -     * Decoding: pts of the first frame of the stream, in stream time base.
 +     * Decoding: pts of the first frame of the stream in presentation order, in stream time base.
       * Only set this if you are absolutely 100% sure that the value you set
       * it to really is the pts of the first frame.
       * This may be undefined (AV_NOPTS_VALUE).
 +     * @note The ASF header does NOT contain a correct start_time the ASF
 +     * demuxer must NOT set this.
       */
      int64_t start_time;
  
       */
      int codec_info_nb_frames;
  
 +    /**
 +     * Stream Identifier
 +     * This is the MPEG-TS stream identifier +1
 +     * 0 means unknown
 +     */
 +    int stream_identifier;
 +
      /**
       * Stream informations used internally by av_find_stream_info()
       */
          int64_t last_dts;
          int64_t duration_gcd;
          int duration_count;
 -        double duration_error[MAX_STD_TIMEBASES];
 +        double duration_error[2][2][MAX_STD_TIMEBASES];
          int64_t codec_info_duration;
          int nb_decoded_frames;
      } *info;
 +
 +    /**
 +     * flag to indicate that probing is requested
 +     * NOT PART OF PUBLIC API
 +     */
 +    int request_probe;
  #if !FF_API_REORDER_PRIVATE
      const uint8_t *cur_ptr;
      int cur_len;
@@@ -744,10 -753,6 +778,10 @@@ typedef struct AVProgram 
      unsigned int   *stream_index;
      unsigned int   nb_stream_indexes;
      AVDictionary *metadata;
 +
 +    int program_num;
 +    int pmt_pid;
 +    int pcr_pid;
  } AVProgram;
  
  #define AVFMTCTX_NOHEADER      0x0001 /**< signal that no header is present
@@@ -860,7 -865,7 +894,7 @@@ typedef struct AVFormatContext 
      /**
       * Decoding: total stream bitrate in bit/s, 0 if not
       * available. Never set it directly if the file_size and the
 -     * duration are known as Libav can compute it automatically.
 +     * duration are known as FFmpeg can compute it automatically.
       */
      int bit_rate;
  
  #endif
  #define AVFMT_FLAG_CUSTOM_IO    0x0080 ///< The caller has supplied a custom AVIOContext, don't avio_close() it.
  #define AVFMT_FLAG_DISCARD_CORRUPT  0x0100 ///< Discard frames marked corrupted
 +#define AVFMT_FLAG_MP4A_LATM    0x8000 ///< Enable RTP MP4A-LATM payload
 +#define AVFMT_FLAG_SORT_DTS    0x10000 ///< try to interleave outputted packets by dts (using this flag can slow demuxing down)
 +#define AVFMT_FLAG_PRIV_OPT    0x20000 ///< Enable use of private options by delaying codec open (this could be made default once all code is converted)
 +#define AVFMT_FLAG_KEEP_SIDE_DATA 0x40000 ///< Dont merge side data but keep it seperate.
  
  #if FF_API_LOOP_INPUT
      /**
       */
      AVIOInterruptCB interrupt_callback;
  
 +    /**
 +     * Transport stream id.
 +     * This will be moved into demuxer private options. Thus no API/ABI compatibility
 +     */
 +    int ts_id;
 +
 +
      /*****************************************************************
       * All fields below this line are not part of the public API. They
       * may not be used outside of libavformat and can be changed and
@@@ -1249,15 -1243,6 +1283,15 @@@ AVInputFormat *av_probe_input_format(AV
   */
  AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max);
  
 +/**
 + * Guess the file format.
 + *
 + * @param is_opened Whether the file is already opened; determines whether
 + *                  demuxers with or without AVFMT_NOFILE are probed.
 + * @param score_ret The score of the best detection.
 + */
 +AVInputFormat *av_probe_input_format3(AVProbeData *pd, int is_opened, int *score_ret);
 +
  /**
   * Probe a bytestream to determine the input format. Each time a probe returns
   * with a score that is too low, the probe buffer size is increased and another
@@@ -1328,8 -1313,6 +1362,8 @@@ attribute_deprecated int av_open_input_
   */
  int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options);
  
 +int av_demuxer_open(AVFormatContext *ic, AVFormatParameters *ap);
 +
  /**
   * Allocate an AVFormatContext.
   * avformat_free_context() can be used to free the context and everything
   */
  AVFormatContext *avformat_alloc_context(void);
  
 +#if FF_API_ALLOC_OUTPUT_CONTEXT
 +/**
 + * @deprecated deprecated in favor of avformat_alloc_output_context2()
 + */
 +attribute_deprecated
 +AVFormatContext *avformat_alloc_output_context(const char *format,
 +                                               AVOutputFormat *oformat,
 +                                               const char *filename);
 +#endif
 +
 +/**
 + * Allocate an AVFormatContext for an output format.
 + * avformat_free_context() can be used to free the context and
 + * everything allocated by the framework within it.
 + *
 + * @param *ctx is set to the created format context, or to NULL in
 + * case of failure
 + * @param oformat format to use for allocating the context, if NULL
 + * format_name and filename are used instead
 + * @param format_name the name of output format to use for allocating the
 + * context, if NULL filename is used instead
 + * @param filename the name of the filename to use for allocating the
 + * context, may be NULL
 + * @return >= 0 in case of success, a negative AVERROR code in case of
 + * failure
 + */
 +int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat,
 +                                   const char *format_name, const char *filename);
 +
  #if FF_API_FORMAT_PARAMETERS
  /**
   * Read packets of a media file to get stream information. This
@@@ -1409,18 -1363,6 +1443,18 @@@ int av_find_stream_info(AVFormatContex
   */
  int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);
  
 +/**
 + * Find the programs which belong to a given stream.
 + *
 + * @param ic    media file handle
 + * @param last  the last found program, the search will start after this
 + *              program, or from the beginning if it is NULL
 + * @param s     stream index
 + * @return the next program which belongs to s, NULL if no program is found or
 + *         the last program is not among the programs of ic.
 + */
 +AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s);
 +
  /**
   * Find the "best" stream in the file.
   * The best stream is determined according to various heuristics as the most
@@@ -1780,28 -1722,7 +1814,28 @@@ int av_interleave_packet_per_dts(AVForm
   */
  int av_write_trailer(AVFormatContext *s);
  
 +/**
 + * Get timing information for the data currently output.
 + * The exact meaning of "currently output" depends on the format.
 + * It is mostly relevant for devices that have an internal buffer and/or
 + * work in real time.
 + * @param s          media file handle
 + * @param stream     stream in the media file
 + * @param dts[out]   DTS of the last packet output for the stream, in stream
 + *                   time_base units
 + * @param wall[out]  absolute time when that packet whas output,
 + *                   in microsecond
 + * @return  0 if OK, AVERROR(ENOSYS) if the format does not support it
 + * Note: some formats or devices may not allow to measure dts and wall
 + * atomically.
 + */
 +int av_get_output_timestamp(struct AVFormatContext *s, int stream,
 +                            int64_t *dts, int64_t *wall);
 +
  #if FF_API_DUMP_FORMAT
 +/**
 + * @deprecated Deprecated in favor of av_dump_format().
 + */
  attribute_deprecated void dump_format(AVFormatContext *ic,
                                        int index,
                                        const char *url,
diff --combined libavformat/mov.c
index e9404276855308f1cc805b9ba68d035627ec9347,356a5e8ff41f4624324c18b23a9fcff3f09303c0..a30b6bb7d2106db572ca594490b2df57098b7d7d
@@@ -3,20 -3,20 +3,20 @@@
   * Copyright (c) 2001 Fabrice Bellard
   * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot 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
   */
  
  /*
   * First version by Francois Revol revol@free.fr
   * Seek function by Gael Chardon gael.dev@4now.net
-  *
-  * Features and limitations:
-  * - reads most of the QT files I have (at least the structure),
-  *   Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
-  * - the code is quite ugly... maybe I won't do it recursive next time :-)
-  *
-  * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
-  * when coding this :) (it's a writer anyway)
-  *
-  * Reference documents:
-  * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
-  * Apple:
-  *  http://developer.apple.com/documentation/QuickTime/QTFF/
-  *  http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
-  * QuickTime is a trademark of Apple (AFAIK :))
   */
  
  #include "qtpalette.h"
  #undef NDEBUG
  #include <assert.h>
  
- /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  /* those functions parse an atom */
- /* return code:
-   0: continue to parse next atom
-  <0: error occurred, exit
- */
  /* links atom IDs to parse functions */
  typedef struct MOVParseTableEntry {
      uint32_t type;
@@@ -87,11 -66,10 +66,11 @@@ static int mov_metadata_track_or_disc_n
  {
      char buf[16];
  
 -    short current, total;
 +    short current, total = 0;
      avio_rb16(pb); // unknown
      current = avio_rb16(pb);
 -    total = avio_rb16(pb);
 +    if (len >= 6)
 +        total = avio_rb16(pb);
      if (!total)
          snprintf(buf, sizeof(buf), "%d", current);
      else
@@@ -201,8 -179,6 +180,8 @@@ static int mov_read_udta_string(MOVCont
      case MKTAG(0xa9,'w','r','t'): key = "composer";  break;
      case MKTAG( 'c','p','r','t'):
      case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
 +    case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
 +    case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
      case MKTAG(0xa9,'c','m','t'):
      case MKTAG(0xa9,'i','n','f'): key = "comment";   break;
      case MKTAG(0xa9,'a','l','b'): key = "album";     break;
@@@ -327,7 -303,7 +306,7 @@@ static int mov_read_default(MOVContext 
  
      if (atom.size < 0)
          atom.size = INT64_MAX;
 -    while (total_size + 8 < atom.size && !pb->eof_reached) {
 +    while (total_size + 8 < atom.size && !url_feof(pb)) {
          int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
          a.size = atom.size;
          a.type=0;
@@@ -497,8 -473,6 +476,8 @@@ static int mov_read_hdlr(MOVContext *c
      AVStream *st;
      uint32_t type;
      uint32_t av_unused ctype;
 +    int title_size;
 +    char *title_str;
  
      if (c->fc->nb_streams < 1) // meta before first trak
          return 0;
      avio_rb32(pb); /* component flags */
      avio_rb32(pb); /* component flags mask */
  
 +    title_size = atom.size - 24;
 +    if (title_size > 0) {
 +        title_str = av_malloc(title_size + 1); /* Add null terminator */
 +        if (!title_str)
 +            return AVERROR(ENOMEM);
 +        avio_read(pb, title_str, title_size);
 +        title_str[title_size] = 0;
 +        av_dict_set(&st->metadata, "handler_name", title_str, 0);
 +        av_freep(&title_str);
 +    }
 +
      return 0;
  }
  
@@@ -808,7 -771,7 +787,7 @@@ static int mov_read_enda(MOVContext *c
          return 0;
      st = c->fc->streams[c->fc->nb_streams-1];
  
 -    little_endian = avio_rb16(pb);
 +    little_endian = avio_rb16(pb) & 0xFF;
      av_dlog(c->fc, "enda %d\n", little_endian);
      if (little_endian == 1) {
          switch (st->codec->codec_id) {
  }
  
  /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
 -static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 +static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
 +                              enum CodecID codec_id)
  {
      AVStream *st;
      uint64_t size;
      if (c->fc->nb_streams < 1) // will happen with jp2 files
          return 0;
      st= c->fc->streams[c->fc->nb_streams-1];
 +
 +    if (st->codec->codec_id != codec_id)
 +        return 0; /* unexpected codec_id - don't mess with extradata */
 +
      size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
      if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
          return -1;
      return 0;
  }
  
 +/* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
 +static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 +{
 +    return mov_read_extradata(c, pb, atom, CODEC_ID_ALAC);
 +}
 +
 +static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 +{
 +    return mov_read_extradata(c, pb, atom, CODEC_ID_AVS);
 +}
 +
 +static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 +{
 +    return mov_read_extradata(c, pb, atom, CODEC_ID_MJPEG);
 +}
 +
 +static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 +{
 +    return mov_read_extradata(c, pb, atom, CODEC_ID_JPEG2000);
 +}
 +
  static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  {
      AVStream *st;
@@@ -1154,9 -1091,6 +1133,9 @@@ int ff_mov_read_stsd_entries(MOVContex
                      color_index = 255;
                      color_dec = 256 / (color_count - 1);
                      for (j = 0; j < color_count; j++) {
 +                        if (id == CODEC_ID_CINEPAK){
 +                            r = g = b = color_count - 1 - color_index;
 +                        }else
                          r = g = b = color_index;
                          sc->palette[j] =
                              (r << 16) | (g << 8) | (b);
              st->codec->width = sc->width;
              st->codec->height = sc->height;
          } else {
 -            /* other codec type, just skip (rtp, mp4s, tmcd ...) */
 +            if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
 +                int val;
 +                avio_rb32(pb);       /* reserved */
 +                val = avio_rb32(pb); /* flags */
 +                if (val & 1)
 +                    st->codec->flags2 |= CODEC_FLAG2_DROP_FRAME_TIMECODE;
 +                avio_rb32(pb);
 +                avio_rb32(pb);
 +                st->codec->time_base.den = avio_r8(pb);
 +                st->codec->time_base.num = 1;
 +            }
 +            /* other codec type, just skip (rtp, mp4s, ...) */
              avio_skip(pb, size - (avio_tell(pb) - start_pos));
          }
          /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
              st->codec->sample_rate = AV_RB32(st->codec->extradata+32);
          }
          break;
 +    case CODEC_ID_AC3:
 +        st->need_parsing = AVSTREAM_PARSE_FULL;
 +        break;
 +    case CODEC_ID_MPEG1VIDEO:
 +        st->need_parsing = AVSTREAM_PARSE_FULL;
 +        break;
      default:
          break;
      }
@@@ -1582,8 -1499,8 +1561,8 @@@ static int mov_read_stts(MOVContext *c
      av_dlog(c->fc, "track[%i].stts.entries = %i\n",
              c->fc->nb_streams-1, entries);
  
 -    if (!entries || entries >= UINT_MAX / sizeof(*sc->stts_data))
 -        return AVERROR(EINVAL);
 +    if (entries >= UINT_MAX / sizeof(*sc->stts_data))
 +        return -1;
  
      sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
      if (!sc->stts_data)
  
          sample_count=avio_rb32(pb);
          sample_duration = avio_rb32(pb);
 +        /* sample_duration < 0 is invalid based on the spec */
 +        if (sample_duration < 0) {
 +            av_log(c->fc, AV_LOG_ERROR, "Invalid SampleDelta in STTS %d", sample_duration);
 +            sample_duration = 1;
 +        }
          sc->stts_data[i].count= sample_count;
          sc->stts_data[i].duration= sample_duration;
  
@@@ -1648,7 -1560,7 +1627,7 @@@ static int mov_read_ctts(MOVContext *c
  
          sc->ctts_data[i].count   = count;
          sc->ctts_data[i].duration= duration;
 -        if (duration < 0)
 +        if (duration < 0 && i+2<entries)
              sc->dts_shift = FFMAX(sc->dts_shift, -duration);
      }
  
@@@ -1670,13 -1582,12 +1649,13 @@@ static void mov_build_index(MOVContext 
      uint64_t stream_size = 0;
  
      /* adjust first dts according to edit list */
 -    if (sc->time_offset && mov->time_scale > 0) {
 -        if (sc->time_offset < 0)
 -            sc->time_offset = av_rescale(sc->time_offset, sc->time_scale, mov->time_scale);
 +    if ((sc->empty_duration || sc->start_time) && mov->time_scale > 0) {
 +        if (sc->empty_duration)
 +            sc->empty_duration = av_rescale(sc->empty_duration, sc->time_scale, mov->time_scale);
 +        sc->time_offset = sc->start_time - sc->empty_duration;
          current_dts = -sc->time_offset;
 -        if (sc->ctts_data && sc->stts_data && sc->stts_data[0].duration &&
 -            sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) {
 +        if (sc->ctts_data && sc->stts_data &&
 +            sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
              /* more than 16 frames delay, dts are likely wrong
                 this happens with files created by iMovie */
              sc->wrong_dts = 1;
      }
  }
  
 -static int mov_open_dref(AVIOContext **pb, char *src, MOVDref *ref,
 +static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref,
                           AVIOInterruptCB *int_cb)
  {
      /* try relative path, we do not try the absolute because it can leak information about our
         system to an attacker */
      if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
          char filename[1024];
 -        char *src_path;
 +        const char *src_path;
          int i, l;
  
          /* find a source dir */
@@@ -2064,10 -1975,6 +2043,10 @@@ static int mov_read_tkhd(MOVContext *c
      sc->width = width >> 16;
      sc->height = height >> 16;
  
 +    if (display_matrix[0][0] == -65536 && display_matrix[1][1] == -65536) {
 +         av_dict_set(&st->metadata, "rotate", "180", 0);
 +    }
 +
      // transform the display width/height according to the matrix
      // skip this if the display matrix is the default identity matrix
      // or if it is rotating the picture, ex iPhone 3GS
@@@ -2318,7 -2225,7 +2297,7 @@@ free_and_return
  static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  {
      MOVStreamContext *sc;
 -    int i, edit_count, version;
 +    int i, edit_count, version, edit_start_index = 0;
  
      if (c->fc->nb_streams < 1)
          return 0;
              time     = (int32_t)avio_rb32(pb); /* media time */
          }
          avio_rb32(pb); /* Media rate */
 -        if (i == 0 && time >= -1) {
 -            sc->time_offset = time != -1 ? time : -duration;
 -        }
 +        if (i == 0 && time == -1) {
 +            sc->empty_duration = duration;
 +            edit_start_index = 1;
 +        } else if (i == edit_start_index && time >= 0)
 +            sc->start_time = time;
      }
  
      if (edit_count > 1)
      return 0;
  }
  
 +static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 +{
 +    if (atom.size < 16)
 +        return AVERROR_INVALIDDATA;
 +    avio_skip(pb, 4);
 +    ff_mov_read_chan(c->fc, atom.size - 4, c->fc->streams[0]->codec);
 +    return 0;
 +}
 +
  static const MOVParseTableEntry mov_default_parse_table[] = {
 -{ MKTAG('a','v','s','s'), mov_read_extradata },
 +{ MKTAG('a','v','s','s'), mov_read_avss },
  { MKTAG('c','h','p','l'), mov_read_chpl },
  { MKTAG('c','o','6','4'), mov_read_stco },
  { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  { MKTAG('e','d','t','s'), mov_read_default },
  { MKTAG('e','l','s','t'), mov_read_elst },
  { MKTAG('e','n','d','a'), mov_read_enda },
 -{ MKTAG('f','i','e','l'), mov_read_extradata },
 +{ MKTAG('f','i','e','l'), mov_read_fiel },
  { MKTAG('f','t','y','p'), mov_read_ftyp },
  { MKTAG('g','l','b','l'), mov_read_glbl },
  { MKTAG('h','d','l','r'), mov_read_hdlr },
  { MKTAG('i','l','s','t'), mov_read_ilst },
 -{ MKTAG('j','p','2','h'), mov_read_extradata },
 +{ MKTAG('j','p','2','h'), mov_read_jp2h },
  { MKTAG('m','d','a','t'), mov_read_mdat },
  { MKTAG('m','d','h','d'), mov_read_mdhd },
  { MKTAG('m','d','i','a'), mov_read_default },
  { MKTAG('m','v','e','x'), mov_read_default },
  { MKTAG('m','v','h','d'), mov_read_mvhd },
  { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
 -{ MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
 +{ MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
  { MKTAG('a','v','c','C'), mov_read_glbl },
  { MKTAG('p','a','s','p'), mov_read_pasp },
  { MKTAG('s','t','b','l'), mov_read_default },
  { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  { MKTAG('w','f','e','x'), mov_read_wfex },
  { MKTAG('c','m','o','v'), mov_read_cmov },
 +{ MKTAG('c','h','a','n'), mov_read_chan },
  { 0, NULL }
  };
  
@@@ -2527,7 -2422,7 +2506,7 @@@ static void mov_read_chapters(AVFormatC
                  if (len == 1 || len == 2)
                      title[len] = 0;
                  else
 -                    avio_get_str(sc->pb, len - 2, title + 2, title_len - 2);
 +                    get_strz(sc->pb, title + 2, len - 1);
              }
          }
  
@@@ -2608,7 -2503,7 +2587,7 @@@ static int mov_read_packet(AVFormatCont
          mov->found_mdat = 0;
          if (s->pb->seekable||
              mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
 -            s->pb->eof_reached)
 +            url_feof(s->pb))
              return AVERROR_EOF;
          av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
          goto retry;
          }
  #if CONFIG_DV_DEMUXER
          if (mov->dv_demux && sc->dv_audio_container) {
 -            avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
 +            avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
              av_free(pkt->data);
              pkt->size = 0;
              ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
diff --combined libavutil/adler32.h
index 0b890bcc1100d34d06b035aa1e4c58f1b002dfdb,a8ff6f9d41c24814952b62b4b85c188c9fdb5278..e926ef6cc256ab86e5d03dead86699dc7db44787
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * copyright (c) 2006 Mans Rullgard
   *
 - * 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
   */
  
@@@ -25,6 -25,7 +25,7 @@@
  #include "attributes.h"
  
  /**
+  * @ingroup lavu_crypto
   * Calculate the Adler32 checksum of a buffer.
   *
   * Passing the return value to a subsequent av_adler32_update() call
diff --combined libavutil/aes.h
index 368f70cbbb6ec2e1e0dde0e7fca2f269f70bab0b,cf7b462092764561253adf176a3517c17aa553e8..bafa4cc3c4a03f33d46637f1c87ee001c140be63
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * copyright (c) 2007 Michael Niedermayer <michaelni@gmx.at>
   *
 - * 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 <stdint.h>
  
+ /**
+  * @defgroup lavu_aes AES
+  * @ingroup lavu_crypto
+  * @{
+  */
  extern const int av_aes_size;
  
  struct AVAES;
@@@ -44,4 -50,8 +50,8 @@@ int av_aes_init(struct AVAES *a, const 
   */
  void av_aes_crypt(struct AVAES *a, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt);
  
+ /**
+  * @}
+  */
  #endif /* AVUTIL_AES_H */
diff --combined libavutil/audioconvert.h
index 4e0e98c9b4efe582aa9b0ee403ae097a0d323191,1c5cfa0a8e63271152edba9d094fdd2765d6313c..766ef8b9da90de5a4475b98c43793859f3860258
@@@ -2,20 -2,20 +2,20 @@@
   * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
   * Copyright (c) 2008 Peter Ross
   *
 - * 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
   */
  
   * audio conversion routines
   */
  
- /* Audio channel masks */
+ /**
+  * @addtogroup lavu_audio
+  * @{
+  */
+ /**
+  * @defgroup channel_masks Audio channel masks
+  * @{
+  */
  #define AV_CH_FRONT_LEFT             0x00000001
  #define AV_CH_FRONT_RIGHT            0x00000002
  #define AV_CH_FRONT_CENTER           0x00000004
      to be the native codec channel order. */
  #define AV_CH_LAYOUT_NATIVE          0x8000000000000000LL
  
- /* Audio channel convenience macros */
+ /**
+  * @}
+  * @defgroup channel_mask_c Audio channel convenience macros
+  * @{
+  * */
  #define AV_CH_LAYOUT_MONO              (AV_CH_FRONT_CENTER)
  #define AV_CH_LAYOUT_STEREO            (AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT)
  #define AV_CH_LAYOUT_2_1               (AV_CH_LAYOUT_STEREO|AV_CH_BACK_CENTER)
  #define AV_CH_LAYOUT_7POINT1_WIDE      (AV_CH_LAYOUT_5POINT1_BACK|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER)
  #define AV_CH_LAYOUT_STEREO_DOWNMIX    (AV_CH_STEREO_LEFT|AV_CH_STEREO_RIGHT)
  
+ /**
+  * @}
+  */
  /**
   * Return a channel layout id that matches name, 0 if no match.
 + * name can be one or several of the following notations,
 + * separated by '+' or '|':
 + * - the name of an usual channel layout (mono, stereo, 4.0, quad, 5.0,
 + *   5.0(side), 5.1, 5.1(side), 7.1, 7.1(wide), downmix);
 + * - the name of a single channel (FL, FR, FC, LFE, BL, BR, FLC, FRC, BC,
 + *   SL, SR, TC, TFL, TFC, TFR, TBL, TBC, TBR, DL, DR);
 + * - a number of channels, in decimal, optionnally followed by 'c', yielding
 + *   the default channel layout for that number of channels (@see
 + *   av_get_default_channel_layout);
 + * - a channel layout mask, in hexadecimal starting with "0x" (see the
 + *   AV_CH_* macros).
 + + Example: "stereo+FC" = "2+FC" = "2c+1c" = "0x7"
   */
  int64_t av_get_channel_layout(const char *name);
  
@@@ -104,9 -108,8 +120,13 @@@ void av_get_channel_layout_string(char 
   */
  int av_get_channel_layout_nb_channels(int64_t channel_layout);
  
 +/**
 + * Return default channel layout for a given number of channels.
 + */
 +int64_t av_get_default_channel_layout(int nb_channels);
 +
+ /**
+  * @}
+  */
  #endif /* AVUTIL_AUDIOCONVERT_H */
diff --combined libavutil/avstring.h
index 4ea64e46a9b72c1935a6f552069b33ab9b541b6b,35b3d46c035060ba23cf46cc18487aae47f4da96..147d7acbe51d61ce3d3e47d1f487a4e29f9c7366
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * Copyright (c) 2007 Mans Rullgard
   *
 - * 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 <stddef.h>
  #include "attributes.h"
  
+ /**
+  * @addtogroup lavu_string
+  * @{
+  */
  /**
   * Return non-zero if pfx is a prefix of str. If it is, *ptr is set to
   * the address of the first character in str after the prefix.
@@@ -72,7 -77,7 +77,7 @@@ char *av_stristr(const char *haystack, 
   * @param size size of destination buffer
   * @return the length of src
   *
-  * WARNING: since the return value is the length of src, src absolutely
+  * @warning since the return value is the length of src, src absolutely
   * _must_ be a properly 0-terminated string, otherwise this will read beyond
   * the end of the buffer and possibly crash.
   */
@@@ -90,9 -95,9 +95,9 @@@ size_t av_strlcpy(char *dst, const cha
   * @param size size of destination buffer
   * @return the total length of src and dst
   *
-  * WARNING: since the return value use the length of src and dst, these absolutely
-  * _must_ be a properly 0-terminated strings, otherwise this will read beyond
-  * the end of the buffer and possibly crash.
+  * @warning since the return value use the length of src and dst, these
+  * absolutely _must_ be a properly 0-terminated strings, otherwise this
+  * will read beyond the end of the buffer and possibly crash.
   */
  size_t av_strlcat(char *dst, const char *src, size_t size);
  
   */
  size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) av_printf_format(3, 4);
  
 +/**
 + * Print arguments following specified format into a large enough auto
 + * allocated buffer. It is similar to GNU asprintf().
 + * @param fmt printf-compatible format string, specifying how the
 + *            following parameters are used.
 + * @return the allocated string
 + * @note You have to free the string yourself with av_free().
 + */
 +char *av_asprintf(const char *fmt, ...) av_printf_format(1, 2);
 +
  /**
   * Convert a number to a av_malloced string.
   */
@@@ -141,30 -136,6 +146,30 @@@ char *av_d2str(double d)
   */
  char *av_get_token(const char **buf, const char *term);
  
 +/**
 + * Split the string into several tokens which can be accessed by
 + * successive calls to av_strtok().
 + *
 + * A token is defined as a sequence of characters not belonging to the
 + * set specified in delim.
 + *
 + * On the first call to av_strtok(), s should point to the string to
 + * parse, and the value of saveptr is ignored. In subsequent calls, s
 + * should be NULL, and saveptr should be unchanged since the previous
 + * call.
 + *
 + * This function is similar to strtok_r() defined in POSIX.1.
 + *
 + * @param s the string to parse, may be NULL
 + * @param delim 0-terminated list of token delimiters, must be non-NULL
 + * @param saveptr user-provided pointer which points to stored
 + * information necessary for av_strtok() to continue scanning the same
 + * string. saveptr is updated to point to the next character after the
 + * first delimiter found, or to NULL if the string was terminated
 + * @return the found token, or NULL when no token is found
 + */
 +char *av_strtok(char *s, const char *delim, char **saveptr);
 +
  /**
   * Locale independent conversion of ASCII characters to upper case.
   */
@@@ -185,16 -156,20 +190,20 @@@ static inline int av_tolower(int c
      return c;
  }
  
 -/*
 +/**
   * Locale independent case-insensitive compare.
-  * Note: This means only ASCII-range characters are case-insensitive
+  * @note This means only ASCII-range characters are case-insensitive
   */
  int av_strcasecmp(const char *a, const char *b);
  
  /**
   * Locale independent case-insensitive compare.
-  * Note: This means only ASCII-range characters are case-insensitive
+  * @note This means only ASCII-range characters are case-insensitive
   */
  int av_strncasecmp(const char *a, const char *b, size_t n);
  
+ /**
+  * @}
+  */
  #endif /* AVUTIL_AVSTRING_H */
diff --combined libavutil/avutil.h
index 0fa46b823090654a8a33bfb2ef5503c5806d741e,5381a41d733ac1f9b7bb62340d1510aebc52f55f..a654f8b33229fdbf5f498622b2944ffda01f96dd
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
   *
 - * 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
   */
  
   * external API header
   */
  
+ /**
+  * @mainpage
+  *
+  * @section libav_intro Introduction
+  *
+  * This document describe the usage of the different libraries
+  * provided by Libav.
+  *
+  * @li @ref libavc "libavcodec" encoding/decoding library
+  * @li @subpage libavfilter graph based frame editing library
+  * @li @ref libavf "libavformat" I/O and muxing/demuxing library
+  * @li @ref lavu "libavutil" common utility library
+  * @li @subpage libpostproc post processing library
+  * @li @subpage libswscale  color conversion and scaling library
+  *
+  */
+ /**
+  * @defgroup lavu Common utility functions
+  *
+  * @brief
+  * libavutil contains the code shared across all the other Libav
+  * libraries
+  *
+  * @note In order to use the functions provided by avutil you must include
+  * the specific header.
+  *
+  * @{
+  *
+  * @defgroup lavu_crypto Crypto and Hashing
+  *
+  * @{
+  * @}
+  *
+  * @defgroup lavu_math Maths
+  * @{
+  *
+  * @}
+  *
+  * @defgroup lavu_string String Manipulation
+  *
+  * @{
+  *
+  * @}
+  *
+  * @defgroup lavu_mem Memory Management
+  *
+  * @{
+  *
+  * @}
+  *
+  * @defgroup lavu_data Data Structures
+  * @{
+  *
+  * @}
+  *
+  * @defgroup lavu_audio Audio related
+  *
+  * @{
+  *
+  * @}
+  *
+  * @defgroup lavu_error Error Codes
+  *
+  * @{
+  *
+  * @}
+  *
+  * @defgroup lavu_misc Other
+  *
+  * @{
+  *
+  * @defgroup lavu_internal Internal
+  *
+  * Not exported functions, for internal usage only
+  *
+  * @{
+  *
+  * @}
+  */
+ /**
+  * @defgroup preproc_misc Preprocessor String Macros
+  *
+  * String manipulation macros
+  *
+  * @{
+  */
  
  #define AV_STRINGIFY(s)         AV_TOSTRING(s)
  #define AV_TOSTRING(s) #s
  
  #define AV_PRAGMA(s) _Pragma(#s)
  
+ /**
+  * @}
+  */
+ /**
+  * @defgroup version_utils Library Version Macros
+  *
+  * Useful to check and match library version in order to maintain
+  * backward compatibility.
+  *
+  * @{
+  */
  #define AV_VERSION_INT(a, b, c) (a<<16 | b<<8 | c)
  #define AV_VERSION_DOT(a, b, c) a ##.## b ##.## c
  #define AV_VERSION(a, b, c) AV_VERSION_DOT(a, b, c)
  
+ /**
+  * @}
+  *
+  * @defgroup lavu_ver Version and Build diagnostics
+  *
+  * Macros and function useful to check at compiletime and at runtime
+  * which version of libavutil is in use.
+  *
+  * @{
+  */
  #define LIBAVUTIL_VERSION_MAJOR 51
 -#define LIBAVUTIL_VERSION_MINOR 16
 +#define LIBAVUTIL_VERSION_MINOR 26
  #define LIBAVUTIL_VERSION_MICRO  0
  
  #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
  #define LIBAVUTIL_IDENT         "Lavu" AV_STRINGIFY(LIBAVUTIL_VERSION)
  
  /**
+  * @}
+  *
+  * @defgroup depr_guards Deprecation guards
   * Those FF_API_* defines are not part of public API.
   * They may change, break or disappear at any time.
+  *
+  * They are used mostly internally to mark code that will be removed
+  * on the next major version.
+  *
+  * @{
   */
 +#ifndef FF_API_OLD_EVAL_NAMES
 +#define FF_API_OLD_EVAL_NAMES (LIBAVUTIL_VERSION_MAJOR < 52)
 +#endif
  #ifndef FF_API_GET_BITS_PER_SAMPLE_FMT
  #define FF_API_GET_BITS_PER_SAMPLE_FMT (LIBAVUTIL_VERSION_MAJOR < 52)
  #endif
  #define FF_API_OLD_AVOPTIONS            (LIBAVUTIL_VERSION_MAJOR < 52)
  #endif
  
+ /**
+  * @}
+  */
+ /**
+  * @addtogroup lavu_ver
+  * @{
+  */
  /**
   * Return the LIBAVUTIL_VERSION_INT constant.
   */
@@@ -88,22 -215,35 +218,41 @@@ const char *avutil_configuration(void)
   */
  const char *avutil_license(void);
  
+ /**
+  * @}
+  */
+ /**
+  * @addtogroup lavu_media Media Type
+  * @brief Media Type
+  */
  enum AVMediaType {
-     AVMEDIA_TYPE_UNKNOWN = -1,
+     AVMEDIA_TYPE_UNKNOWN = -1,  ///< Usually treated as AVMEDIA_TYPE_DATA
      AVMEDIA_TYPE_VIDEO,
      AVMEDIA_TYPE_AUDIO,
-     AVMEDIA_TYPE_DATA,
+     AVMEDIA_TYPE_DATA,          ///< Opaque data information usually continuous
      AVMEDIA_TYPE_SUBTITLE,
-     AVMEDIA_TYPE_ATTACHMENT,
+     AVMEDIA_TYPE_ATTACHMENT,    ///< Opaque data information usually sparse
      AVMEDIA_TYPE_NB
  };
  
 +/**
 + * Return a string describing the media_type enum, NULL if media_type
 + * is unknown.
 + */
 +const char *av_get_media_type_string(enum AVMediaType media_type);
 +
+ /**
+  * @defgroup lavu_const Constants
+  * @{
+  *
+  * @defgroup lavu_enc Encoding specific
+  *
+  * @note those definition should move to avcodec
+  * @{
+  */
  #define FF_LAMBDA_SHIFT 7
  #define FF_LAMBDA_SCALE (1<<FF_LAMBDA_SHIFT)
  #define FF_QP2LAMBDA 118 ///< factor to convert from H.263 QP to lambda
  
  #define FF_QUALITY_SCALE FF_LAMBDA_SCALE //FIXME maybe remove
  
+ /**
+  * @}
+  * @defgroup lavu_time Timestamp specific
+  *
+  * Libav internal timebase and timestamp definitions
+  *
+  * @{
+  */
+ /**
+  * @brief Undefined timestamp value
+  *
+  * Usually reported by demuxer that work on containers that do not provide
+  * either pts or dts.
+  */
  #define AV_NOPTS_VALUE          INT64_C(0x8000000000000000)
+ /**
+  * Internal time base represented as integer
+  */
  #define AV_TIME_BASE            1000000
+ /**
+  * Internal time base represented as fractional value
+  */
  #define AV_TIME_BASE_Q          (AVRational){1, AV_TIME_BASE}
  
+ /**
+  * @}
+  * @}
+  * @defgroup lavu_picture Image related
+  *
+  * AVPicture types, pixel formats and basic image planes manipulation.
+  *
+  * @{
+  */
  enum AVPictureType {
 -    AV_PICTURE_TYPE_I = 1, ///< Intra
 +    AV_PICTURE_TYPE_NONE = 0, ///< Undefined
 +    AV_PICTURE_TYPE_I,     ///< Intra
      AV_PICTURE_TYPE_P,     ///< Predicted
      AV_PICTURE_TYPE_B,     ///< Bi-dir predicted
      AV_PICTURE_TYPE_S,     ///< S(GMC)-VOP MPEG4
   */
  char av_get_picture_type_char(enum AVPictureType pict_type);
  
+ /**
+  * @}
+  */
 +/**
 + * Return x default pointer in case p is NULL.
 + */
 +static inline const void *av_x_if_null(const void *p, const void *x)
 +{
 +    return p ? p : x;
 +}
 +
  #include "common.h"
  #include "error.h"
 +#include "mathematics.h"
 +#include "rational.h"
 +#include "intfloat_readwrite.h"
 +#include "log.h"
 +#include "pixfmt.h"
  
+ /**
+  * @}
+  * @}
+  */
  #endif /* AVUTIL_AVUTIL_H */
diff --combined libavutil/base64.h
index 092980b09388cff64445bd7498b1e9f173580b3f,4750cf5c724aeea26a3940fb8ba9ffe6264507df..b095576130a2f0e07feb37a3b597dd2292284025
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * 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
   */
  
  
  #include <stdint.h>
  
+ /**
+  * @defgroup lavu_base64 Base64
+  * @ingroup lavu_crypto
+  * @{
+  */
  /**
   * Decode a base64-encoded string.
   *
@@@ -51,4 -58,8 +58,8 @@@ char *av_base64_encode(char *out, int o
   */
  #define AV_BASE64_SIZE(x)  (((x)+2) / 3 * 4 + 1)
  
+  /**
+   * @}
+   */
  #endif /* AVUTIL_BASE64_H */
diff --combined libavutil/dict.h
index 84f58ec47316e79ad839e760a11f7a96c0855d50,6e28b61406f782677f385af22a91fb6dd6ec7f83..2adf28c124977b334ede9c8468151444e7fe65fd
@@@ -1,38 -1,36 +1,42 @@@
  /*
   *
 - * 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
   */
  
  /**
   * @file
   * Public dictionary API.
 + * @deprecated
 + *  AVDictionary is provided for compatibility with libav. It is both in
 + *  implementation as well as API inefficient. It does not scale and is
 + *  extremely slow with large dictionaries.
 + *  It is recommended that new code uses our tree container from tree.c/h
 + *  where applicable, which uses AVL trees to achieve O(log n) performance.
   */
  
  #ifndef AVUTIL_DICT_H
  #define AVUTIL_DICT_H
  
  /**
-  * @defgroup dict_api Public Dictionary API
+  * @addtogroup lavu_dict AVDictionary
+  * @ingroup lavu_data
+  *
+  * @brief Simple key:value store
+  *
   * @{
   * Dictionaries are used for storing key:value pairs. To create
   * an AVDictionary, simply pass an address of a NULL pointer to
@@@ -58,7 -56,6 +62,6 @@@
   * av_dict_free(&d);
   * @endcode
   *
-  * @}
   */
  
  #define AV_DICT_MATCH_CASE      1
@@@ -117,4 -114,8 +120,8 @@@ void av_dict_copy(AVDictionary **dst, A
   */
  void av_dict_free(AVDictionary **m);
  
+ /**
+  * @}
+  */
  #endif // AVUTIL_DICT_H
diff --combined libavutil/error.h
index 47d366ebbcb1a82cefeeac2a5ecd920917b00cd8,8ed77342ef199b871182f7da07f33959652c9c23..fa18afcdf1544d91784bf0d2fff304cb143881f1
@@@ -1,18 -1,18 +1,18 @@@
  /*
 - * 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 <errno.h>
  #include "avutil.h"
  
+ /**
+  * @addtogroup lavu_error
+  *
+  * @{
+  */
  /* error handling */
  #if EDOM > 0
  #define AVERROR(e) (-(e))   ///< Returns a negative error code from a POSIX error code, to return from library functions.
@@@ -47,7 -54,7 +54,7 @@@
  #define AVERROR_INVALIDDATA        (-MKTAG( 'I','N','D','A')) ///< Invalid data found when processing input
  #define AVERROR_MUXER_NOT_FOUND    (-MKTAG(0xF8,'M','U','X')) ///< Muxer not found
  #define AVERROR_OPTION_NOT_FOUND   (-MKTAG(0xF8,'O','P','T')) ///< Option not found
 -#define AVERROR_PATCHWELCOME       (-MKTAG( 'P','A','W','E')) ///< Not yet implemented in Libav, patches welcome
 +#define AVERROR_PATCHWELCOME       (-MKTAG( 'P','A','W','E')) ///< Not yet implemented in FFmpeg, patches welcome
  #define AVERROR_PROTOCOL_NOT_FOUND (-MKTAG(0xF8,'P','R','O')) ///< Protocol not found
  #define AVERROR_STREAM_NOT_FOUND   (-MKTAG(0xF8,'S','T','R')) ///< Stream not found
  
@@@ -65,4 -72,8 +72,8 @@@
   */
  int av_strerror(int errnum, char *errbuf, size_t errbuf_size);
  
+ /**
+  * @}
+  */
  #endif /* AVUTIL_ERROR_H */
diff --combined libavutil/imgutils.h
index f976d210b9fceb2fea9a309056a3fe850bcbc8d3,3815a49ae4f1db60c5f123a0500a6e055f2fa2a8..9b53815fb66dd73d2581a8caef68bacd3ddbb9b3
@@@ -1,18 -1,18 +1,18 @@@
  /*
 - * 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
   */
  
@@@ -22,6 -22,9 +22,9 @@@
  /**
   * @file
   * misc image utilities
+  *
+  * @addtogroup lavu_picture
+  * @{
   */
  
  #include "avutil.h"
@@@ -127,4 -130,9 +130,9 @@@ int av_image_check_size(unsigned int w
  
  int ff_set_systematic_pal2(uint32_t pal[256], enum PixelFormat pix_fmt);
  
+ /**
+  * @}
+  */
  #endif /* AVUTIL_IMGUTILS_H */
diff --combined libavutil/intmath.h
index 8b400ef05464c92bdb345d3dcccd3f15cd65b104,e6a2e102c414fd648f4d38d04a3a9cef904b1aaa..0feedf8cfd4de66cdb65619b5fa70ab51f6eeb82
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * Copyright (c) 2010 Mans Rullgard <mans@mansr.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
   */
  
  #include "config.h"
  #include "attributes.h"
  
+ /**
+  * @addtogroup lavu_internal
+  * @{
+  */
  extern const uint32_t ff_inverse[257];
  
  #if   ARCH_ARM
@@@ -76,4 -81,7 +81,7 @@@ static inline av_const unsigned int ff_
      return b - (a < b * b);
  }
  
+ /**
+  * @}
+  */
  #endif /* AVUTIL_INTMATH_H */
diff --combined libavutil/lzo.h
index ae5fc53c61996e2bbc079bf4e909a5c83774f8f5,b4c71c0933e5c53431f55a70f1679528686c7844..1603d3b3bc79c4cf02f93b24db289a0db8bc2846
@@@ -2,26 -2,33 +2,33 @@@
   * LZO 1x decompression
   * copyright (c) 2006 Reimar Doeffinger
   *
 - * 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 AVUTIL_LZO_H
  #define AVUTIL_LZO_H
  
+ /**
+  * @defgroup lavu_lzo LZO
+  * @ingroup lavu_crypto
+  *
+  * @{
+  */
  #include <stdint.h>
  
  /** @name Error flags returned by av_lzo1x_decode
@@@ -63,4 -70,8 +70,8 @@@ int av_lzo1x_decode(void *out, int *out
   */
  void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
  
+ /**
+  * @}
+  */
  #endif /* AVUTIL_LZO_H */
diff --combined libavutil/mathematics.h
index 882a516393b6d8696f33eec662e20ff06dcce66b,0b072ebe63cfcda52b757737651ba4a1e0fab453..ad39e263cef859a5472a910851a579de1f4301c2
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
   *
 - * 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
   */
  
  #define INFINITY       (1.0/0.0)
  #endif
  
+ /**
+  * @addtogroup lavu_math
+  * @{
+  */
  enum AVRounding {
      AV_ROUND_ZERO     = 0, ///< Round toward zero.
      AV_ROUND_INF      = 1, ///< Round away from zero.
@@@ -109,4 -115,8 +115,8 @@@ int av_compare_ts(int64_t ts_a, AVRatio
   */
  int64_t av_compare_mod(uint64_t a, uint64_t b, uint64_t mod);
  
+ /**
+  * @}
+  */
  #endif /* AVUTIL_MATHEMATICS_H */
diff --combined libavutil/md5.h
index 969202a807a64da28f748cedf06845247c57e8d4,1412ee240112cdd1d38e48bda4e2c2db5bf82f4f..1333ab2ddfc33853a5693a0050b4c1ae26c9a9b1
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
   *
 - * 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 <stdint.h>
  
+ /**
+  * @defgroup lavu_md5 MD5
+  * @ingroup lavu_crypto
+  * @{
+  */
  extern const int av_md5_size;
  
  struct AVMD5;
@@@ -32,5 -38,9 +38,9 @@@ void av_md5_update(struct AVMD5 *ctx, c
  void av_md5_final(struct AVMD5 *ctx, uint8_t *dst);
  void av_md5_sum(uint8_t *dst, const uint8_t *src, const int len);
  
+ /**
+  * @}
+  */
  #endif /* AVUTIL_MD5_H */
  
diff --combined libavutil/mem.h
index 179e12f32f7734287dc71d571b441543266ab608,cd8490b2dae9e885f79921da9693c28f9ed20abd..f148c637bb61931f865e94d88ad2aa668b8f97e6
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
   *
 - * 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
   */
  
  #define AVUTIL_MEM_H
  
  #include "attributes.h"
 +#include "error.h"
  #include "avutil.h"
  
 -#if defined(__ICC) && _ICC < 1200 || defined(__SUNPRO_C)
+ /**
+  * @addtogroup lavu_mem
+  * @{
+  */
 +#if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C)
      #define DECLARE_ALIGNED(n,t,v)      t __attribute__ ((aligned (n))) v
      #define DECLARE_ASM_CONST(n,t,v)    const t __attribute__ ((aligned (n))) v
  #elif defined(__TI_COMPILER_VERSION__)
@@@ -87,16 -92,6 +93,16 @@@ void *av_malloc(size_t size) av_malloc_
   */
  void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
  
 +/**
 + * Allocate or reallocate a block of memory.
 + * This function does the same thing as av_realloc, except:
 + * - It takes two arguments and checks the result of the multiplication for
 + *   integer overflow.
 + * - It frees the input block in case of failure, thus avoiding the memory
 + *   leak with the classic "buf = realloc(buf); if (!buf) return -1;".
 + */
 +void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
 +
  /**
   * Free a memory block which has been allocated with av_malloc(z)() or
   * av_realloc().
@@@ -117,18 -112,6 +123,18 @@@ void av_free(void *ptr)
   */
  void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
  
 +/**
 + * Allocate a block of nmemb * size bytes with alignment suitable for all
 + * memory accesses (including vectors if available on the CPU) and
 + * zero all the bytes of the block.
 + * The allocation will fail if nmemb * size is greater than or equal
 + * to INT_MAX.
 + * @param nmemb
 + * @param size
 + * @return Pointer to the allocated block, NULL if it cannot be allocated.
 + */
 +void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib;
 +
  /**
   * Duplicate the string s.
   * @param s string to be duplicated
@@@ -146,28 -129,8 +152,32 @@@ char *av_strdup(const char *s) av_mallo
   */
  void av_freep(void *ptr);
  
 +/**
 + * Add an element to a dynamic array.
 + *
 + * @param tab_ptr Pointer to the array.
 + * @param nb_ptr  Pointer to the number of elements in the array.
 + * @param elem    Element to be added.
 + */
 +void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
 +
 +/**
 + * Multiply two size_t values checking for overflow.
 + * @return  0 if success, AVERROR(EINVAL) if overflow.
 + */
 +static inline int av_size_mult(size_t a, size_t b, size_t *r)
 +{
 +    size_t t = a * b;
 +    /* Hack inspired from glibc: only try the division if nelem and elsize
 +     * are both greater than sqrt(SIZE_MAX). */
 +    if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b)
 +        return AVERROR(EINVAL);
 +    *r = t;
 +    return 0;
 +}
 +
+ /**
+  * @}
+  */
  #endif /* AVUTIL_MEM_H */
diff --combined libavutil/opt.h
index f8a14e08760a00c109d9497f5953628d2b44d402,19549408e2e3df94e7059b24a25e367211920c60..a0730a7d3b0989a183f0294eca4503dd0c2c264c
@@@ -2,20 -2,20 +2,20 @@@
   * AVOptions
   * copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
   *
 - * 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 @@@
  
  /**
   * @defgroup avoptions AVOptions
+  * @ingroup lavu_data
   * @{
   * AVOptions provide a generic system to declare options on arbitrary structs
   * ("objects"). An option can have a help text, a type and a range of possible
   * filled with option as a parameter. This allows to set some options
   * that cannot be set otherwise, since e.g. the input file format is not known
   * before the file is actually opened.
-  * @}
   */
  
  enum AVOptionType{
@@@ -340,9 -340,9 +340,9 @@@ attribute_deprecated const AVOption *av
  attribute_deprecated const AVOption *av_set_q(void *obj, const char *name, AVRational n);
  attribute_deprecated const AVOption *av_set_int(void *obj, const char *name, int64_t n);
  
 -attribute_deprecated double av_get_double(void *obj, const char *name, const AVOption **o_out);
 -attribute_deprecated AVRational av_get_q(void *obj, const char *name, const AVOption **o_out);
 -attribute_deprecated int64_t av_get_int(void *obj, const char *name, const AVOption **o_out);
 +double av_get_double(void *obj, const char *name, const AVOption **o_out);
 +AVRational av_get_q(void *obj, const char *name, const AVOption **o_out);
 +int64_t av_get_int(void *obj, const char *name, const AVOption **o_out);
  attribute_deprecated const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len);
  attribute_deprecated const AVOption *av_next_option(void *obj, const AVOption *last);
  #endif
@@@ -376,7 -376,6 +376,7 @@@ void av_opt_set_defaults2(void *s, int 
   * key. ctx must be an AVClass context, storing is done using
   * AVOptions.
   *
 + * @param opts options string to parse, may be NULL
   * @param key_val_sep a 0-terminated list of characters used to
   * separate key from value
   * @param pairs_sep a 0-terminated list of characters used to separate
@@@ -585,6 -584,7 +585,7 @@@ int av_opt_get_int   (void *obj, const 
  int av_opt_get_double(void *obj, const char *name, int search_flags, double     *out_val);
  int av_opt_get_q     (void *obj, const char *name, int search_flags, AVRational *out_val);
  /**
+  * @}
   * @}
   */
  
diff --combined libavutil/pixfmt.h
index 37b694e7bf45d2f503632fd08584b0f734543aa7,39c608dde832530065bcd49add1b5e011c647b55..f711de78281c3bd9fdde89d4c6a35a582bb3989a
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
   *
 - * 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
   */
  
   * @file
   * pixel format definitions
   *
-  * @warning This file has to be considered an internal but installed
-  * header, so it should not be directly included in your projects.
   */
  
  #include "libavutil/avconfig.h"
  
  /**
-  * Pixel format. Notes:
+  * Pixel format.
   *
+  * @note
   * PIX_FMT_RGB32 is handled in an endian-specific manner. An RGBA
   * color is put together as:
   *  (A << 24) | (R << 16) | (G << 8) | B
   * This is stored as BGRA on little-endian CPU architectures and ARGB on
   * big-endian CPUs.
   *
+  * @par
   * When the pixel format is palettized RGB (PIX_FMT_PAL8), the palettized
   * image data is stored in AVFrame.data[0]. The palette is transported in
   * AVFrame.data[1], is 1024 bytes long (256 4-byte entries) and is
   * This is important as many custom PAL8 video codecs that were designed
   * to run on the IBM VGA graphics adapter use 6-bit palette components.
   *
+  * @par
   * For all the 8bit per pixel formats, an RGB32 palette is in data[1] like
   * for pal8. This palette is filled in automatically by the function
   * allocating the picture.
   *
-  * Note, make sure that all newly added big endian formats have pix_fmt&1==1
-  *       and that all newly added little endian formats have pix_fmt&1==0
-  *       this allows simpler detection of big vs little endian.
+  * @note
+  * make sure that all newly added big endian formats have pix_fmt&1==1
+  * and that all newly added little endian formats have pix_fmt&1==0
+  * this allows simpler detection of big vs little endian.
   */
  enum PixelFormat {
      PIX_FMT_NONE= -1,
      PIX_FMT_RGB444BE,  ///< packed RGB 4:4:4, 16bpp, (msb)4A 4R 4G 4B(lsb), big-endian, most significant bits to 0
      PIX_FMT_BGR444LE,  ///< packed BGR 4:4:4, 16bpp, (msb)4A 4B 4G 4R(lsb), little-endian, most significant bits to 1
      PIX_FMT_BGR444BE,  ///< packed BGR 4:4:4, 16bpp, (msb)4A 4B 4G 4R(lsb), big-endian, most significant bits to 1
 -    PIX_FMT_Y400A,     ///< 8bit gray, 8bit alpha
 +    PIX_FMT_GRAY8A,    ///< 8bit gray, 8bit alpha
      PIX_FMT_BGR48BE,   ///< packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as big-endian
      PIX_FMT_BGR48LE,   ///< packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as little-endian
 +
 +    //the following 10 formats have the disadvantage of needing 1 format for each bit depth, thus
 +    //If you want to support multiple bit depths, then using PIX_FMT_YUV420P16* with the bpp stored seperately
 +    //is better
      PIX_FMT_YUV420P9BE, ///< planar YUV 4:2:0, 13.5bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
      PIX_FMT_YUV420P9LE, ///< planar YUV 4:2:0, 13.5bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
      PIX_FMT_YUV420P10BE,///< planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
      PIX_FMT_YUV422P9BE, ///< planar YUV 4:2:2, 18bpp, (1 Cr & Cb sample per 2x1 Y samples), big-endian
      PIX_FMT_YUV422P9LE, ///< planar YUV 4:2:2, 18bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
      PIX_FMT_VDA_VLD,    ///< hardware decoding through VDA
 +
 +    PIX_FMT_RGBA64BE,  ///< packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is stored as big-endian
 +    PIX_FMT_RGBA64LE,  ///< packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is stored as little-endian
 +    PIX_FMT_BGRA64BE,  ///< packed RGBA 16:16:16:16, 64bpp, 16B, 16G, 16R, 16A, the 2-byte value for each R/G/B/A component is stored as big-endian
 +    PIX_FMT_BGRA64LE,  ///< packed RGBA 16:16:16:16, 64bpp, 16B, 16G, 16R, 16A, the 2-byte value for each R/G/B/A component is stored as little-endian
 +    PIX_FMT_GBR24P,    ///< planar GBR, 24bpp, 8G, 8B, 8R.
 +
      PIX_FMT_NB,        ///< number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of formats might differ between versions
  };
  
 +#define PIX_FMT_Y400A PIX_FMT_GRAY8A
 +
  #if AV_HAVE_BIGENDIAN
  #   define PIX_FMT_NE(be, le) PIX_FMT_##be
  #else
  #define PIX_FMT_YUV422P16 PIX_FMT_NE(YUV422P16BE, YUV422P16LE)
  #define PIX_FMT_YUV444P16 PIX_FMT_NE(YUV444P16BE, YUV444P16LE)
  
 +#define PIX_FMT_RGBA64 PIX_FMT_NE(RGBA64BE, RGBA64LE)
 +#define PIX_FMT_BGRA64 PIX_FMT_NE(BGRA64BE, BGRA64LE)
  #endif /* AVUTIL_PIXFMT_H */
diff --combined libavutil/random_seed.h
index bb957879be65ffbdf010ed2d43df328ad972e075,b1fad13d0757a04b151640146e2f4e5d2327b55b..0462a048e048317b0150aa3b800171a96a668e45
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.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
   */
  
  #define AVUTIL_RANDOM_SEED_H
  
  #include <stdint.h>
+ /**
+  * @addtogroup lavu_crypto
+  * @{
+  */
  
  /**
 - * Get random data.
 - *
 - * This function can be called repeatedly to generate more random bits
 - * as needed. It is generally quite slow, and usually used to seed a
 - * PRNG.  As it uses /dev/urandom and /dev/random, the quality of the
 - * returned random data depends on the platform.
 + * Get a seed to use in conjunction with random functions.
 + * This function tries to provide a good seed at a best effort bases.
 + * Its possible to call this function multiple times if more bits are needed.
 + * It can be quite slow, which is why it should only be used as seed for a faster
 + * PRNG. The quality of the seed depends on the platform.
   */
  uint32_t av_get_random_seed(void);
  
+ /**
+  * @}
+  */
  #endif /* AVUTIL_RANDOM_SEED_H */
diff --combined libavutil/rational.h
index 789e4aca2cc1b2270f5be2bdcb99e310f1fbcd01,0ec18ec9697a311ab23ce331db6ed4fd1a1a61a8..8c2bdb552901cb6e01caa6c3d061fe3fb8337475
@@@ -2,20 -2,20 +2,20 @@@
   * rational numbers
   * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
   *
 - * 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 <limits.h>
  #include "attributes.h"
  
+ /**
+  * @addtogroup lavu_math
+  * @{
+  */
  /**
   * rational number numerator/denominator
   */
@@@ -132,4 -137,8 +137,8 @@@ int av_nearer_q(AVRational q, AVRationa
   */
  int av_find_nearest_q_idx(AVRational q, const AVRational* q_list);
  
+ /**
+  * @}
+  */
  #endif /* AVUTIL_RATIONAL_H */
diff --combined libavutil/sha.h
index 543f5a194948a3596c10f1f8f3340693a58e9052,8350954c4b4897eac7f67472c536239bdf113d16..d891cae87f4f5843dcf2712aa07b4bb547e0a210
@@@ -1,20 -1,20 +1,20 @@@
  /*
   * Copyright (C) 2007 Michael Niedermayer <michaelni@gmx.at>
   *
 - * 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 <stdint.h>
  
+ /**
+  * @defgroup lavu_sha SHA
+  * @ingroup lavu_crypto
+  * @{
+  */
  extern const int av_sha_size;
  
  struct AVSHA;
@@@ -53,4 -59,8 +59,8 @@@ void av_sha_update(struct AVSHA* contex
   */
  void av_sha_final(struct AVSHA* context, uint8_t *digest);
  
+ /**
+  * @}
+  */
  #endif /* AVUTIL_SHA_H */
diff --combined libavutil/tree.h
index 8c7de2ffbf8bb35f6739e6a6a3404daf3e98549b,59ea01dbdb049b3b627806a0ada1cb29989a5af4..81610b6b791f0a870f8899d45051a3995140670a
@@@ -1,34 -1,44 +1,44 @@@
  /*
   * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
   *
 - * 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
   */
  
  /**
   * @file
   * A tree container.
-  * Insertion, removal, finding equal, largest which is smaller than and
-  * smallest which is larger than, all have O(log n) worst case complexity.
   * @author Michael Niedermayer <michaelni@gmx.at>
   */
  
  #ifndef AVUTIL_TREE_H
  #define AVUTIL_TREE_H
  
+ /**
+  * @addtogroup lavu_tree AVTree
+  * @ingroup lavu_data
+  *
+  * Low complexity tree container
+  *
+  * Insertion, removal, finding equal, largest which is smaller than and
+  * smallest which is larger than, all have O(log n) worst case complexity.
+  * @{
+  */
  struct AVTreeNode;
  extern const int av_tree_node_size;
  
@@@ -91,5 -101,8 +101,8 @@@ void av_tree_destroy(struct AVTreeNode 
   */
  void av_tree_enumerate(struct AVTreeNode *t, void *opaque, int (*cmp)(void *opaque, void *elem), int (*enu)(void *opaque, void *elem));
  
+ /**
+  * @}
+  */
  
  #endif /* AVUTIL_TREE_H */
index 4fd5d17d1791be88a4ce1777f0851f8169ebecd1,1a15c39e671f7e228888ee0a6185d0609622fc34..12f74f055de28b6334afb991b98dd2c89a3d64f9
@@@ -29,19 -29,27 +29,27 @@@ if [ -n "$do_mpeg2" ] ; the
  # mpeg2
  do_video_encoding mpeg2.mpg "-qscale 10 -vcodec mpeg2video -f mpeg1video"
  do_video_decoding
+ fi
  
+ if [ -n "$do_mpeg2_ivlc_qprd" ]; then
  # mpeg2 encoding intra vlc qprd
  do_video_encoding mpeg2ivlc-qprd.mpg "-vb 500k -bf 2 -trellis 1 -flags +qprd+mv0 -flags2 +ivlc -cmp 2 -subcmp 2 -mbd rd -vcodec mpeg2video -f mpeg2video"
  do_video_decoding
+ fi
  
+ if [ -n "$do_mpeg2_422" ]; then
  #mpeg2 4:2:2 encoding
  do_video_encoding mpeg2_422.mpg "-vb 1000k -bf 2 -trellis 1 -flags +qprd+mv0+ildct+ilme -flags2 +ivlc -mbd rd -vcodec mpeg2video -pix_fmt yuv422p -f mpeg2video"
  do_video_decoding
+ fi
  
+ if [ -n "$do_mpeg2_idct_int" ]; then
  # mpeg2
- do_video_encoding mpeg2.mpg "-qscale 10 -vcodec mpeg2video -idct int -dct int -f mpeg1video"
+ do_video_encoding mpeg2_idct_int.mpg "-qscale 10 -vcodec mpeg2video -idct int -dct int -f mpeg1video"
  do_video_decoding "-idct int"
+ fi
  
+ if [ -n "$do_mpeg2_ilace" ]; then
  # mpeg2 encoding interlaced
  do_video_encoding mpeg2i.mpg "-qscale 10 -vcodec mpeg2video -f mpeg1video -flags +ildct+ilme"
  do_video_decoding
@@@ -51,15 -59,12 +59,17 @@@ if [ -n "$do_mpeg2thread" ] ; the
  # mpeg2 encoding interlaced
  do_video_encoding mpeg2thread.mpg "-qscale 10 -vcodec mpeg2video -f mpeg1video -bf 2 -flags +ildct+ilme -threads 2"
  do_video_decoding
+ fi
  
+ if [ -n "$do_mpeg2thread_ilace" ]; then
  # mpeg2 encoding interlaced using intra vlc
  do_video_encoding mpeg2threadivlc.mpg "-qscale 10 -vcodec mpeg2video -f mpeg1video -bf 2 -flags +ildct+ilme -flags2 +ivlc -threads 2"
  do_video_decoding
 +
 +# mpeg2 encoding interlaced
 +file=${outfile}mpeg2reuse.mpg
 +do_avconv $file $DEC_OPTS -me_threshold 256 -i ${target_path}/${outfile}mpeg2thread.mpg $ENC_OPTS -same_quant -me_threshold 256 -mb_threshold 1024 -vcodec mpeg2video -f mpeg1video -bf 2 -flags +ildct+ilme -threads 4
 +do_video_decoding
  fi
  
  if [ -n "$do_msmpeg4v2" ] ; then
@@@ -72,11 -77,6 +82,11 @@@ do_video_encoding msmpeg4.avi "-qscale 
  do_video_decoding
  fi
  
 +if [ -n "$do_msvideo1" ] ; then
 +do_video_encoding msvideo1.avi "-an -vcodec msvideo1"
 +do_video_decoding "" "-pix_fmt yuv420p"
 +fi
 +
  if [ -n "$do_wmv1" ] ; then
  do_video_encoding wmv1.avi "-qscale 10 -an -vcodec wmv1"
  do_video_decoding
@@@ -112,11 -112,6 +122,11 @@@ do_video_encoding huffyuv.avi "-an -vco
  do_video_decoding "" "-strict -2 -pix_fmt yuv420p -sws_flags neighbor+bitexact"
  fi
  
 +if [ -n "$do_amv" ] ; then
 +do_video_encoding amv.avi "-an -vcodec amv"
 +do_video_decoding
 +fi
 +
  if [ -n "$do_rc" ] ; then
  do_video_encoding mpeg4-rc.avi "-b 400k -bf 2 -an -vcodec mpeg4"
  do_video_decoding
  if [ -n "$do_mpeg4adv" ] ; then
  do_video_encoding mpeg4-adv.avi "-qscale 9 -flags +mv4+part+aic -trellis 1 -mbd bits -ps 200 -an -vcodec mpeg4"
  do_video_decoding
+ fi
  
+ if [ -n "$do_mpeg4_qprd" ]; then
  do_video_encoding mpeg4-qprd.avi "-b 450k -bf 2 -trellis 1 -flags +mv4+qprd+mv0 -cmp 2 -subcmp 2 -mbd rd -an -vcodec mpeg4"
  do_video_decoding
+ fi
  
+ if [ -n "$do_mpeg4_adap" ]; then
  do_video_encoding mpeg4-adap.avi "-b 550k -bf 2 -flags +mv4+mv0 -trellis 1 -cmp 1 -subcmp 2 -mbd rd -scplx_mask 0.3 -an -vcodec mpeg4"
  do_video_decoding
+ fi
  
+ if [ -n "$do_mpeg4_qpel" ]; then
  do_video_encoding mpeg4-Q.avi "-qscale 7 -flags +mv4+qpel -mbd 2 -bf 2 -cmp 1 -subcmp 2 -an -vcodec mpeg4"
  do_video_decoding
  fi
@@@ -161,11 -162,6 +177,11 @@@ do_video_encoding mjpeg.avi "-qscale 9 
  do_video_decoding "" "-pix_fmt yuv420p"
  fi
  
 +if [ -n "$do_jpeg2000" ] ; then
 +do_video_encoding jpeg2000.avi "-qscale 7 -an -vcodec j2k -strict experimental -pix_fmt rgb24"
 +do_video_decoding "-vcodec j2k -strict experimental" "-pix_fmt yuv420p"
 +fi
 +
  if [ -n "$do_ljpeg" ] ; then
  do_video_encoding ljpeg.avi "-an -vcodec ljpeg -strict -1"
  do_video_decoding
@@@ -219,7 -215,9 +235,9 @@@ f
  if [ -n "$do_dv" ] ; then
  do_video_encoding dv.dv "-dct int -s pal -an"
  do_video_decoding "" "-s cif"
+ fi
  
+ if [ -n "$do_dv_411" ]; then
  do_video_encoding dv411.dv "-dct int -s pal -an -pix_fmt yuv411p -sws_flags area+accurate_rnd+bitexact"
  do_video_decoding "" "-s cif -sws_flags area+accurate_rnd+bitexact"
  fi
@@@ -250,11 -248,6 +268,11 @@@ do_video_encoding dnxhd-720p-10bit.dnxh
  do_video_decoding "" "-s cif -pix_fmt yuv420p"
  fi
  
 +if [ -n "$do_prores" ] ; then
 +do_video_encoding prores.mov "-vcodec prores"
 +do_video_decoding "" "-pix_fmt yuv420p"
 +fi
 +
  if [ -n "$do_svq1" ] ; then
  do_video_encoding svq1.mov "-an -vcodec svq1 -qscale 3 -pix_fmt yuv410p"
  do_video_decoding "" "-pix_fmt yuv420p"
@@@ -265,12 -258,6 +283,12 @@@ do_video_encoding flashsv.flv "-an -vco
  do_video_decoding "" "-pix_fmt yuv420p -sws_flags area+accurate_rnd+bitexact"
  fi
  
 +if [ -n "$do_flashsv2" ] ; then
 +do_video_encoding flashsv2.flv "-an -vcodec flashsv2 -sws_flags neighbor+full_chroma_int+accurate_rnd+bitexact -strict experimental"
 +do_video_encoding flashsv2I.flv "-an -vcodec flashsv2 -sws_flags neighbor+full_chroma_int+accurate_rnd+bitexact -strict experimental -g 1"
 +do_video_decoding "" "-pix_fmt yuv420p -sws_flags area+accurate_rnd+bitexact"
 +fi
 +
  if [ -n "$do_roq" ] ; then
  do_video_encoding roqav.roq "-vframes 5"
  do_video_decoding "" "-pix_fmt yuv420p"
@@@ -281,11 -268,6 +299,11 @@@ do_video_encoding qtrle.mov "-an -vcode
  do_video_decoding "" "-pix_fmt yuv420p"
  fi
  
 +if [ -n "$do_qtrlegray" ] ; then
 +do_video_encoding qtrlegray.mov "-an -vcodec qtrle -pix_fmt gray"
 +do_video_decoding "" "-pix_fmt yuv420p"
 +fi
 +
  if [ -n "$do_rgb" ] ; then
  do_video_encoding rgb.avi "-an -vcodec rawvideo -pix_fmt bgr24"
  do_video_decoding "" "-pix_fmt yuv420p"
@@@ -309,13 -291,8 +327,13 @@@ do_audio_encoding ac3.rm "-vn -acodec a
  #$tiny_psnr $pcm_dst $pcm_ref 2 1024
  fi
  
 +if [ -n "$do_g723_1" ] ; then
 +do_audio_encoding g723_1.tco "-b:a 6.3k -ac 1 -ar 8000 -acodec g723_1"
 +do_audio_decoding
 +fi
 +
  if [ -n "$do_g726" ] ; then
 -do_audio_encoding g726.wav "-b 32k -ac 1 -ar 8000 -acodec g726"
 +do_audio_encoding g726.wav "-b:a 32k -ac 1 -ar 8000 -acodec g726"
  do_audio_decoding
  fi
  
diff --combined tests/ref/vsynth1/dv
index 5b55c4f3579816cdedf9e3549be1e2a577723116,c309bb2aba934ed8e853815fea6c4e9c8d1d2d08..3e71feceeb2adb9f071b8a6c205a84ece857385c
@@@ -1,8 -1,4 +1,4 @@@
 -27ade3031b17214cf81c19cbf70f37d7 *./tests/data/vsynth1/dv.dv
 +4d572f758b55a1756adf9f54132f3b9e *./tests/data/vsynth1/dv.dv
  7200000 ./tests/data/vsynth1/dv.dv
  02ac7cdeab91d4d5621e7ce96dddc498 *./tests/data/dv.vsynth1.out.yuv
--stddev:    6.90 PSNR: 31.34 MAXDIFF:   76 bytes:  7603200/  7603200
- f179899efba432c6f01149c36c709092 *./tests/data/vsynth1/dv411.dv
- 7200000 ./tests/data/vsynth1/dv411.dv
- b6640a3a572353f51284acb746eb00c4 *./tests/data/dv.vsynth1.out.yuv
- stddev:   30.76 PSNR: 18.37 MAXDIFF:  205 bytes:  7603200/  7603200
++stddev:    6.90 PSNR: 31.34 MAXDIFF:   76 bytes:  7603200/  7603200
diff --combined tests/ref/vsynth1/dv_411
index 0000000000000000000000000000000000000000,841c3fd32689353e4e83e8999255462f85a21144..c45b593c02ccdbce57675a9065c82dca15db927d
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,4 +1,4 @@@
 -bd67f2431db160d4bb6dcd791cea6efd *./tests/data/vsynth1/dv411.dv
++f179899efba432c6f01149c36c709092 *./tests/data/vsynth1/dv411.dv
+ 7200000 ./tests/data/vsynth1/dv411.dv
+ b6640a3a572353f51284acb746eb00c4 *./tests/data/dv_411.vsynth1.out.yuv
+ stddev:   30.76 PSNR: 18.37 MAXDIFF:  205 bytes:  7603200/  7603200
index 470c2e91b2cdd50020c7977b0f06306f71b236a2,a44c00dd9198beca5a678e886b41bc105ed26024..b12997eaa7e9b00ff0741f6b4709d2794232824c
@@@ -1,12 -1,4 +1,4 @@@
  ecd183706688bd977c9994c3d1b23d61 *./tests/data/vsynth1/mpeg2thread.mpg
  801313 ./tests/data/vsynth1/mpeg2thread.mpg
  d1658911ca83f5616c1d32abc40750de *./tests/data/mpeg2thread.vsynth1.out.yuv
- stddev:    7.63 PSNR: 30.48 MAXDIFF:  110 bytes:  7603200/  7603200
- 23d600b026222253c2340e23300a4c02 *./tests/data/vsynth1/mpeg2threadivlc.mpg
- 791773 ./tests/data/vsynth1/mpeg2threadivlc.mpg
- d1658911ca83f5616c1d32abc40750de *./tests/data/mpeg2thread.vsynth1.out.yuv
--stddev:    7.63 PSNR: 30.48 MAXDIFF:  110 bytes:  7603200/  7603200
- d119fe917dd81d1ff758b4ce684a8d9d *./tests/data/vsynth1/mpeg2reuse.mpg
- 2074636 ./tests/data/vsynth1/mpeg2reuse.mpg
- 92ced6afe8c02304943c400cce51a5f4 *./tests/data/mpeg2thread.vsynth1.out.yuv
- stddev:    7.66 PSNR: 30.44 MAXDIFF:  111 bytes:  7603200/  7603200
++stddev:    7.63 PSNR: 30.48 MAXDIFF:  110 bytes:  7603200/  7603200
index 0000000000000000000000000000000000000000,0667b68fae91b48c27836ae075d743bbbad04ed7..f9c33395856299fb982e13936293eaf91f83e282
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,4 +1,8 @@@
+ 23d600b026222253c2340e23300a4c02 *./tests/data/vsynth1/mpeg2threadivlc.mpg
+ 791773 ./tests/data/vsynth1/mpeg2threadivlc.mpg
+ d1658911ca83f5616c1d32abc40750de *./tests/data/mpeg2thread_ilace.vsynth1.out.yuv
+ stddev:    7.63 PSNR: 30.48 MAXDIFF:  110 bytes:  7603200/  7603200
++d119fe917dd81d1ff758b4ce684a8d9d *./tests/data/vsynth1/mpeg2reuse.mpg
++2074636 ./tests/data/vsynth1/mpeg2reuse.mpg
++92ced6afe8c02304943c400cce51a5f4 *./tests/data/mpeg2thread_ilace.vsynth1.out.yuv
++stddev:    7.66 PSNR: 30.44 MAXDIFF:  111 bytes:  7603200/  7603200
diff --combined tests/ref/vsynth2/dv
index b05dc4fffe5862ff55e712a1501c30806aa0ca2f,6c010b9301ae1ab15e0b223088dc7af1b6416b09..613312d837da7bf7dce4bf91ed53340daa7f4fe7
@@@ -1,8 -1,4 +1,4 @@@
 -bfa766f89bfeabc0ae1044f3954bed52 *./tests/data/vsynth2/dv.dv
 +85b8d55b0b68bb3fc2e90babb580f9b7 *./tests/data/vsynth2/dv.dv
  7200000 ./tests/data/vsynth2/dv.dv
  7ec62bd3350a6848364669e6e1e4b9cc *./tests/data/dv.vsynth2.out.yuv
--stddev:    1.71 PSNR: 43.47 MAXDIFF:   33 bytes:  7603200/  7603200
- e428508f400327aeb96969c08fb9e1b5 *./tests/data/vsynth2/dv411.dv
- 7200000 ./tests/data/vsynth2/dv411.dv
- 7f9fa421028aabb11eaf4c6513a5a843 *./tests/data/dv.vsynth2.out.yuv
- stddev:   10.09 PSNR: 28.05 MAXDIFF:   60 bytes:  7603200/  7603200
++stddev:    1.71 PSNR: 43.47 MAXDIFF:   33 bytes:  7603200/  7603200
diff --combined tests/ref/vsynth2/dv_411
index 0000000000000000000000000000000000000000,2340ef0e7efb4182be7fe5e2f4dbcf9082762114..708ac889f687167bf919844ed349144f5aa296fa
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,4 +1,4 @@@
 -00a9d8683ac6826af41bcf7223fb0389 *./tests/data/vsynth2/dv411.dv
++e428508f400327aeb96969c08fb9e1b5 *./tests/data/vsynth2/dv411.dv
+ 7200000 ./tests/data/vsynth2/dv411.dv
+ 7f9fa421028aabb11eaf4c6513a5a843 *./tests/data/dv_411.vsynth2.out.yuv
+ stddev:   10.09 PSNR: 28.05 MAXDIFF:   60 bytes:  7603200/  7603200
index 3c9c0d016d5fd2f89727679f5184798a3d7de438,7d7ed218c673c6e0ece5f6fb00176464cb9de883..75cfc7522fd815b6796b9053fe7bfdfa484f1c64
@@@ -1,12 -1,4 +1,4 @@@
  889c754a42d7689b228853e1ece6d345 *./tests/data/vsynth2/mpeg2thread.mpg
  179650 ./tests/data/vsynth2/mpeg2thread.mpg
  8c6a7ed2eb73bd18fd2bb9829464100d *./tests/data/mpeg2thread.vsynth2.out.yuv
- stddev:    4.72 PSNR: 34.65 MAXDIFF:   72 bytes:  7603200/  7603200
- 10b900e32809758857c596d56746e00e *./tests/data/vsynth2/mpeg2threadivlc.mpg
- 178801 ./tests/data/vsynth2/mpeg2threadivlc.mpg
- 8c6a7ed2eb73bd18fd2bb9829464100d *./tests/data/mpeg2thread.vsynth2.out.yuv
--stddev:    4.72 PSNR: 34.65 MAXDIFF:   72 bytes:  7603200/  7603200
- 864d6bf2982a61e510003a518be65a2d *./tests/data/vsynth2/mpeg2reuse.mpg
- 383419 ./tests/data/vsynth2/mpeg2reuse.mpg
- bb20fa080cfd2b0a687ea7376ff4f902 *./tests/data/mpeg2thread.vsynth2.out.yuv
- stddev:    4.73 PSNR: 34.63 MAXDIFF:   72 bytes:  7603200/  7603200
++stddev:    4.72 PSNR: 34.65 MAXDIFF:   72 bytes:  7603200/  7603200
index 0000000000000000000000000000000000000000,1320db98ad1db5d0aea3cce6be73a793c391ce6f..f074d44cc360f596b96076d375efb1a04a17d143
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,4 +1,8 @@@
+ 10b900e32809758857c596d56746e00e *./tests/data/vsynth2/mpeg2threadivlc.mpg
+ 178801 ./tests/data/vsynth2/mpeg2threadivlc.mpg
+ 8c6a7ed2eb73bd18fd2bb9829464100d *./tests/data/mpeg2thread_ilace.vsynth2.out.yuv
+ stddev:    4.72 PSNR: 34.65 MAXDIFF:   72 bytes:  7603200/  7603200
++864d6bf2982a61e510003a518be65a2d *./tests/data/vsynth2/mpeg2reuse.mpg
++383419 ./tests/data/vsynth2/mpeg2reuse.mpg
++bb20fa080cfd2b0a687ea7376ff4f902 *./tests/data/mpeg2thread_ilace.vsynth2.out.yuv
++stddev:    4.73 PSNR: 34.63 MAXDIFF:   72 bytes:  7603200/  7603200