Merge remote-tracking branch 'qatar/master'
authorMichael Niedermayer <michaelni@gmx.at>
Tue, 22 Nov 2011 01:54:50 +0000 (02:54 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Tue, 22 Nov 2011 01:54:50 +0000 (02:54 +0100)
* qatar/master:
  pthread: add some malloc failure checks
  avconv: cosmetics, reformat transcode_init()
  avconv: rename 'os' variable to 'oc'
  doc/filters: fix some typos.
  configure: add libpulse to help output

Conflicts:
avconv.c
configure

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
avconv.c
configure
ffmpeg.c
libavcodec/pthread.c

diff --cc avconv.c
+++ b/avconv.c
@@@ -2094,23 -2028,16 +2094,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(os->oformat->name, "avi")) {
-                 if(!copy_tb && av_q2d(icodec->time_base)*icodec->ticks_per_frame > 2*av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/500){
++            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(!(os->oformat->flags & AVFMT_VARIABLE_FPS)) {
++            } 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 (codec->sample_fmt == AV_SAMPLE_FMT_NONE)
                      codec->sample_fmt = icodec->sample_fmt;
                  choose_sample_fmt(ost->st, ost->enc);
 -                if (!codec->channels)
 +                if (!codec->channels) {
                      codec->channels = icodec->channels;
 -                codec->channel_layout = icodec->channel_layout;
 +                    codec->channel_layout = icodec->channel_layout;
 +                }
                  if (av_get_channel_layout_nb_channels(codec->channel_layout) != codec->channels)
                      codec->channel_layout = 0;
-                 ost->audio_resample = codec->sample_rate != icodec->sample_rate || audio_sync_method > 1;
-                 icodec->request_channels = codec->channels;
+                 ost->audio_resample       = codec-> sample_rate != icodec->sample_rate || audio_sync_method > 1;
+                 icodec->request_channels  = codec-> channels;
                  ost->resample_sample_fmt  = icodec->sample_fmt;
                  ost->resample_sample_rate = icodec->sample_rate;
                  ost->resample_channels    = icodec->channels;
                                        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->resample_width  = icodec->width;
-                 ost->resample_pix_fmt= icodec->pix_fmt;
+                 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};
                      ost->frame_rate = ost->enc->supported_framerates[idx];
                  }
                  codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num};
-                    && (video_sync_method==1 || (video_sync_method<0 && !(os->oformat->flags & AVFMT_VARIABLE_FPS)))){
-                     av_log(os, AV_LOG_WARNING, "Frame rate very high for a muxer not effciciently supporting it.\n"
 +                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)) {
              }
          }
          if(codec->codec_type == AVMEDIA_TYPE_VIDEO){
-             int size= codec->width * codec->height;
-             bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 1664);
 +            /* maximum video buffer size is 6-bytes per pixel, plus DPX header size */
 -            bit_buffer_size = FFMAX(bit_buffer_size, 6*size + 200);
+             int        size = codec->width * codec->height;
++            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(os->oformat->name, "rtp")) {
+         if (strcmp(oc->oformat->name, "rtp")) {
              want_sdp = 0;
          }
      }
diff --cc configure
+++ b/configure
@@@ -183,7 -177,8 +183,8 @@@ External library support
    --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]
diff --cc ffmpeg.c
+++ b/ffmpeg.c
@@@ -2037,154 -1858,296 +2037,156 @@@ static void print_sdp(OutputFile *outpu
      av_sdp_create(avc, n, sdp, sizeof(sdp));
      printf("SDP:\n%s\n", sdp);
      fflush(stdout);
 +    av_freep(&avc);
  }
  
 -static int copy_chapters(int infile, int outfile)
 +static int init_input_stream(int ist_index, OutputStream *output_streams, int nb_output_streams,
 +                             char *error, int error_len)
  {
 -    AVFormatContext *is = input_files[infile].ctx;
 -    AVFormatContext *os = output_files[outfile];
 -    int i;
 -
 -    for (i = 0; i < is->nb_chapters; i++) {
 -        AVChapter *in_ch = is->chapters[i], *out_ch;
 -        int64_t ts_off   = av_rescale_q(start_time - input_files[infile].ts_offset,
 -                                      AV_TIME_BASE_Q, in_ch->time_base);
 -        int64_t rt       = (recording_time == INT64_MAX) ? INT64_MAX :
 -                           av_rescale_q(recording_time, AV_TIME_BASE_Q, in_ch->time_base);
 -
 -
 -        if (in_ch->end < ts_off)
 -            continue;
 -        if (rt != INT64_MAX && in_ch->start > rt + ts_off)
 -            break;
 -
 -        out_ch = av_mallocz(sizeof(AVChapter));
 -        if (!out_ch)
 -            return AVERROR(ENOMEM);
 -
 -        out_ch->id        = in_ch->id;
 -        out_ch->time_base = in_ch->time_base;
 -        out_ch->start     = FFMAX(0,  in_ch->start - ts_off);
 -        out_ch->end       = FFMIN(rt, in_ch->end   - ts_off);
 -
 -        if (metadata_chapters_autocopy)
 -            av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
 -
 -        os->nb_chapters++;
 -        os->chapters = av_realloc(os->chapters, sizeof(AVChapter)*os->nb_chapters);
 -        if (!os->chapters)
 -            return AVERROR(ENOMEM);
 -        os->chapters[os->nb_chapters - 1] = out_ch;
 +    InputStream *ist = &input_streams[ist_index];
 +    if (ist->decoding_needed) {
 +        AVCodec *codec = ist->dec;
 +        if (!codec) {
 +            snprintf(error, error_len, "Decoder (codec %s) not found for input stream #%d:%d",
 +                    avcodec_get_name(ist->st->codec->codec_id), ist->file_index, ist->st->index);
 +            return AVERROR(EINVAL);
 +        }
 +        if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) {
 +            snprintf(error, error_len, "Error while opening decoder for input stream #%d:%d",
 +                    ist->file_index, ist->st->index);
 +            return AVERROR(EINVAL);
 +        }
 +        assert_codec_experimental(ist->st->codec, 0);
 +        assert_avoptions(ist->opts);
      }
 -    return 0;
 -}
  
 -static void parse_forced_key_frames(char *kf, OutputStream *ost,
 -                                    AVCodecContext *avctx)
 -{
 -    char *p;
 -    int n = 1, i;
 -    int64_t t;
 +    ist->pts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames*AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
 +    ist->next_pts = AV_NOPTS_VALUE;
 +    ist->is_start = 1;
  
 -    for (p = kf; *p; p++)
 -        if (*p == ',')
 -            n++;
 -    ost->forced_kf_count = n;
 -    ost->forced_kf_pts = av_malloc(sizeof(*ost->forced_kf_pts) * n);
 -    if (!ost->forced_kf_pts) {
 -        av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
 -        exit_program(1);
 -    }
 -    for (i = 0; i < n; i++) {
 -        p = i ? strchr(p, ',') + 1 : kf;
 -        t = parse_time_or_die("force_key_frames", p, 1);
 -        ost->forced_kf_pts[i] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
 -    }
 +    return 0;
  }
  
 -/*
 - * The following code is the main loop of the file converter
 - */
 -static int transcode(AVFormatContext **output_files,
 -                     int nb_output_files,
 -                     InputFile *input_files,
 -                     int nb_input_files,
 -                     StreamMap *stream_maps, int nb_stream_maps)
 +static int transcode_init(OutputFile *output_files, int nb_output_files,
 +                          InputFile  *input_files,  int nb_input_files)
  {
 -    int ret = 0, i, j, k, n, nb_ostreams = 0;
 -    AVFormatContext *is, *os;
 +    int ret = 0, i, j, k;
-     AVFormatContext *os;
++    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;
 -    }
 -    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;
 -        }
 +    /* 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();
      }
  
 -    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++) {
-         os = output_files[i].ctx;
-         if (!os->nb_streams && !(os->oformat->flags & AVFMT_NOSTREAMS)) {
-             av_dump_format(os, i, os->filename, 1);
++    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];
-         os = output_files[ost->file_index].ctx;
++        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;
  
-         codec = ost->st->codec;
 -        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(os->oformat->name, "avi")) {
-                 if(!copy_tb && av_q2d(icodec->time_base)*icodec->ticks_per_frame > 2*av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/500){
++            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(!(os->oformat->flags & AVFMT_VARIABLE_FPS)) {
++            } 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:
          } 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;
-                 ost->audio_resample |=    codec->sample_fmt     != icodec->sample_fmt
-                                        || codec->channel_layout != icodec->channel_layout;
--                icodec->request_channels = codec->channels;
 -                ist->decoding_needed = 1;
 -                ost->encoding_needed = 1;
++
++                ost->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;
                                        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};
                      ost->frame_rate = ost->enc->supported_framerates[idx];
                  }
                  codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num};
-                    && (video_sync_method==1 || (video_sync_method<0 && !(os->oformat->flags & AVFMT_VARIABLE_FPS)))){
-                     av_log(os, AV_LOG_WARNING, "Frame rate very high for a muxer not effciciently supporting it.\n"
 +                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)) {
              }
          }
          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);
++            int size        = codec->width * codec->height;
++            bit_buffer_size = FFMAX(bit_buffer_size, 7*size + 10000);
          }
      }
  
      }
  
      /* 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;
              }
              assert_avoptions(ost->opts);
              if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
                  av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
 -                                             "It takes bits/s as argument, not kbits/s\n");
 +                                             " It takes bits/s as argument, not kbits/s\n");
              extra_size += ost->st->codec->extradata_size;
 -        }
 -    }
 -
 -    /* open each decoder */
 -    for (i = 0; i < nb_input_streams; i++) {
 -        ist = &input_streams[i];
 -        if (ist->decoding_needed) {
 -            AVCodec *codec = ist->dec;
 -            if (!codec)
 -                codec = avcodec_find_decoder(ist->st->codec->codec_id);
 -            if (!codec) {
 -                snprintf(error, sizeof(error), "Decoder (codec id %d) not found for input stream #%d.%d",
 -                        ist->st->codec->codec_id, ist->file_index, ist->st->index);
 -                ret = AVERROR(EINVAL);
 -                goto dump_format;
 -            }
 -
 -            /* update requested sample format for the decoder based on the
 -               corresponding encoder sample format */
 -            for (j = 0; j < nb_ostreams; j++) {
 -                ost = ost_table[j];
 -                if (ost->source_index == i) {
 -                    update_sample_fmt(ist->st->codec, codec, ost->st->codec);
 -                    break;
 -                }
 -            }
 -
 -            if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) {
 -                snprintf(error, sizeof(error), "Error while opening decoder for input stream #%d.%d",
 -                        ist->file_index, ist->st->index);
 -                ret = AVERROR(EINVAL);
 -                goto dump_format;
 -            }
 -            assert_codec_experimental(ist->st->codec, 0);
 -            assert_avoptions(ost->opts);
 -        }
 -    }
 -
 -    /* init pts */
 -    for (i = 0; i < nb_input_streams; i++) {
 -        AVStream *st;
 -        ist = &input_streams[i];
 -        st= ist->st;
 -        ist->pts = st->avg_frame_rate.num ? - st->codec->has_b_frames*AV_TIME_BASE / av_q2d(st->avg_frame_rate) : 0;
 -        ist->next_pts = AV_NOPTS_VALUE;
 -        init_pts_correction(&ist->pts_ctx);
 -        ist->is_start = 1;
 -    }
 -
 -    /* set meta data information from input file if required */
 -    for (i=0;i<nb_meta_data_maps;i++) {
 -        AVFormatContext *files[2];
 -        AVDictionary    **meta[2];
 -        int j;
 -
 -#define METADATA_CHECK_INDEX(index, nb_elems, desc)\
 -        if ((index) < 0 || (index) >= (nb_elems)) {\
 -            snprintf(error, sizeof(error), "Invalid %s index %d while processing metadata maps\n",\
 -                     (desc), (index));\
 -            ret = AVERROR(EINVAL);\
 -            goto dump_format;\
 -        }
 -
 -        int out_file_index = meta_data_maps[i][0].file;
 -        int in_file_index = meta_data_maps[i][1].file;
 -        if (in_file_index < 0 || out_file_index < 0)
 -            continue;
 -        METADATA_CHECK_INDEX(out_file_index, nb_output_files, "output file")
 -        METADATA_CHECK_INDEX(in_file_index, nb_input_files, "input file")
 -
 -        files[0] = output_files[out_file_index];
 -        files[1] = input_files[in_file_index].ctx;
 -
 -        for (j = 0; j < 2; j++) {
 -            MetadataMap *map = &meta_data_maps[i][j];
  
 -            switch (map->type) {
 -            case 'g':
 -                meta[j] = &files[j]->metadata;
 -                break;
 -            case 's':
 -                METADATA_CHECK_INDEX(map->index, files[j]->nb_streams, "stream")
 -                meta[j] = &files[j]->streams[map->index]->metadata;
 -                break;
 -            case 'c':
 -                METADATA_CHECK_INDEX(map->index, files[j]->nb_chapters, "chapter")
 -                meta[j] = &files[j]->chapters[map->index]->metadata;
 -                break;
 -            case 'p':
 -                METADATA_CHECK_INDEX(map->index, files[j]->nb_programs, "program")
 -                meta[j] = &files[j]->programs[map->index]->metadata;
 -                break;
 -            }
 +            if (ost->st->codec->me_threshold)
 +                input_streams[ost->source_index].st->codec->debug |= FF_DEBUG_MV;
          }
 -
 -        av_dict_copy(meta[0], *meta[1], AV_DICT_DONT_OVERWRITE);
 -    }
 -
 -    /* copy global metadata by default */
 -    if (metadata_global_autocopy) {
 -
 -        for (i = 0; i < nb_output_files; i++)
 -            av_dict_copy(&output_files[i]->metadata, input_files[0].ctx->metadata,
 -                         AV_DICT_DONT_OVERWRITE);
      }
  
 -    /* copy chapters according to chapter maps */
 -    for (i = 0; i < nb_chapter_maps; i++) {
 -        int infile  = chapter_maps[i].in_file;
 -        int outfile = chapter_maps[i].out_file;
 -
 -        if (infile < 0 || outfile < 0)
 -            continue;
 -        if (infile >= nb_input_files) {
 -            snprintf(error, sizeof(error), "Invalid input file index %d in chapter mapping.\n", infile);
 -            ret = AVERROR(EINVAL);
 -            goto dump_format;
 -        }
 -        if (outfile >= nb_output_files) {
 -            snprintf(error, sizeof(error), "Invalid output file index %d in chapter mapping.\n",outfile);
 -            ret = AVERROR(EINVAL);
 +    /* init input streams */
 +    for (i = 0; i < nb_input_streams; i++)
 +        if ((ret = init_input_stream(i, output_streams, nb_output_streams, error, sizeof(error))) < 0)
              goto dump_format;
 -        }
 -        copy_chapters(infile, outfile);
 -    }
  
 -    /* copy chapters from the first input file that has them*/
 -    if (!nb_chapter_maps)
 -        for (i = 0; i < nb_input_files; i++) {
 -            if (!input_files[i].ctx->nb_chapters)
 -                continue;
 +    /* discard unused programs */
 +    for (i = 0; i < nb_input_files; i++) {
 +        InputFile *ifile = &input_files[i];
 +        for (j = 0; j < ifile->ctx->nb_programs; j++) {
 +            AVProgram *p = ifile->ctx->programs[j];
 +            int discard  = AVDISCARD_ALL;
  
 -            for (j = 0; j < nb_output_files; j++)
 -                if ((ret = copy_chapters(i, j)) < 0)
 -                    goto dump_format;
 -            break;
 +            for (k = 0; k < p->nb_stream_indexes; k++)
 +                if (!input_streams[ifile->ist_index + p->stream_index[k]].discard) {
 +                    discard = AVDISCARD_DEFAULT;
 +                    break;
 +                }
 +            p->discard = discard;
          }
 +    }
  
      /* open files and write file headers */
 -    for(i=0;i<nb_output_files;i++) {
 -        os = output_files[i];
 -        if (avformat_write_header(os, &output_opts[i]) < 0) {
 +    for (i = 0; i < nb_output_files; i++) {
-         os = output_files[i].ctx;
-         os->interrupt_callback = int_cb;
-         if (avformat_write_header(os, &output_files[i].opts) < 0) {
++        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(os->oformat->name, "rtp")) {
++        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 */
Simple merge