Merge remote-tracking branch 'qatar/master'
authorMichael Niedermayer <michaelni@gmx.at>
Tue, 27 Sep 2011 23:31:38 +0000 (01:31 +0200)
committerMichael Niedermayer <michaelni@gmx.at>
Wed, 28 Sep 2011 00:07:44 +0000 (02:07 +0200)
* qatar/master:
  avconv: use different variables for decoded and filtered frame.
  avconv: add support for copying attachments.
  matroskaenc: write attachments.
  matroskadec: export mimetype of attachments as metadata.
  avconv: factorize common code from new_*_stream()
  doc/avconv: expand documentation for some options.
  doc/avconv: document -timelimit.

Conflicts:
avconv.c
cmdutils.c
tests/codec-regression.sh

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
avconv.c
cmdutils.c
doc/avconv.texi
doc/avtools-common-opts.texi
ffmpeg.c
libavformat/matroskadec.c
libavformat/matroskaenc.c

diff --cc avconv.c
+++ b/avconv.c
@@@ -1675,15 -1608,17 +1676,16 @@@ static int output_packet(InputStream *i
                      pkt_pts = AV_NOPTS_VALUE;
  
                      ret = avcodec_decode_video2(ist->st->codec,
-                                                 &picture, &got_output, &avpkt);
-                     quality = same_quant ? picture.quality : 0;
+                                                 decoded_frame, &got_output, &avpkt);
+                     quality = same_quant ? decoded_frame->quality : 0;
                      if (ret < 0)
-                         return ret;
+                         goto fail;
                      if (!got_output) {
                          /* no picture yet */
+                         av_freep(&decoded_frame);
                          goto discard_packet;
                      }
-                     ist->next_pts = ist->pts = picture.best_effort_timestamp;
 -                    ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, decoded_frame->pkt_pts,
 -                                                                 decoded_frame->pkt_dts);
++                    ist->next_pts = ist->pts = decoded_frame->best_effort_timestamp;
                      if (ist->st->codec->time_base.num != 0) {
                          int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
                          ist->next_pts += ((int64_t)AV_TIME_BASE *
  #if CONFIG_AVFILTER
              if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
                  ost->input_video_filter) {
-                 if (!picture.sample_aspect_ratio.num)
-                     picture.sample_aspect_ratio = ist->st->sample_aspect_ratio;
-                 picture.pts = ist->pts;
 -                AVRational sar;
 -                if (ist->st->sample_aspect_ratio.num)
 -                    sar = ist->st->sample_aspect_ratio;
 -                else
 -                    sar = ist->st->codec->sample_aspect_ratio;
 -                av_vsrc_buffer_add_frame(ost->input_video_filter, decoded_frame, ist->pts, sar);
++                if (!decoded_frame->sample_aspect_ratio.num)
++                    decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
++                decoded_frame->pts = ist->pts;
 +
-                 av_vsrc_buffer_add_frame(ost->input_video_filter, &picture, AV_VSRC_BUF_FLAG_OVERWRITE);
++                av_vsrc_buffer_add_frame(ost->input_video_filter, decoded_frame, AV_VSRC_BUF_FLAG_OVERWRITE);
+                 if (!(filtered_frame = avcodec_alloc_frame())) {
+                     ret = AVERROR(ENOMEM);
+                     goto fail;
+                 }
              }
              frame_available = ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
                  !ost->output_video_filter || avfilter_poll_frame(ost->output_video_filter->inputs[0]);
              while (frame_available) {
 -                AVRational ist_pts_tb;
 -                if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter)
 -                    get_filtered_video_frame(ost->output_video_filter, filtered_frame, &ost->picref, &ist_pts_tb);
 -                if (ost->picref)
 -                    ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
 +                if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter) {
 +                    AVRational ist_pts_tb = ost->output_video_filter->inputs[0]->time_base;
 +                    if (av_buffersink_get_buffer_ref(ost->output_video_filter, &ost->picref, 0) < 0)
 +                        goto cont;
 +                    if (ost->picref) {
-                         avfilter_fill_frame_from_video_buffer_ref(&picture, 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;
  
                      case AVMEDIA_TYPE_VIDEO:
  #if CONFIG_AVFILTER
                          if (ost->picref->video && !ost->frame_aspect_ratio)
 -                            ost->st->codec->sample_aspect_ratio = ost->picref->video->pixel_aspect;
 +                            ost->st->codec->sample_aspect_ratio = ost->picref->video->sample_aspect_ratio;
  #endif
-                         do_video_out(os, ost, ist, &picture, &frame_size,
-                                         same_quant ? quality : ost->st->codec->global_quality);
+                         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;
diff --cc cmdutils.c
@@@ -872,7 -866,7 +872,8 @@@ int check_stream_specifier(AVFormatCont
          case 'a': type = AVMEDIA_TYPE_AUDIO;    break;
          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;
diff --cc doc/avconv.texi
Simple merge
Simple merge
diff --cc ffmpeg.c
+++ b/ffmpeg.c
@@@ -1504,124 -1448,39 +1504,123 @@@ static void generate_silence(uint8_t *b
      memset(buf, fill_char, size);
  }
  
 -/* pkt = NULL means EOF (needed to flush decoder buffers) */
 -static int output_packet(InputStream *ist, int ist_index,
 -                         OutputStream **ost_table, int nb_ostreams,
 -                         const AVPacket *pkt)
 +static void flush_encoders(OutputStream *ost_table, int nb_ostreams)
  {
 -    AVFormatContext *os;
 -    OutputStream *ost;
 -    int ret, i;
 -    int got_output;
 -    AVFrame picture;
 -    void *buffer_to_free = NULL;
 -    static unsigned int samples_size= 0;
 -    AVSubtitle subtitle, *subtitle_to_free;
 -    int64_t pkt_pts = AV_NOPTS_VALUE;
 -#if CONFIG_AVFILTER
 -    int frame_available;
 -#endif
 -    float quality;
 +    int i, ret;
  
 -    AVPacket avpkt;
 -    int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt);
 +    for (i = 0; i < nb_ostreams; i++) {
 +        OutputStream   *ost = &ost_table[i];
 +        AVCodecContext *enc = ost->st->codec;
 +        AVFormatContext *os = output_files[ost->file_index].ctx;
  
 -    if(ist->next_pts == AV_NOPTS_VALUE)
 -        ist->next_pts= ist->pts;
 +        if (!ost->encoding_needed)
 +            continue;
  
 -    if (pkt == NULL) {
 -        /* EOF handling */
 -        av_init_packet(&avpkt);
 -        avpkt.data = NULL;
 -        avpkt.size = 0;
 -        goto handle_eof;
 -    } else {
 -        avpkt = *pkt;
 +        if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <=1)
 +            continue;
 +        if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE))
 +            continue;
 +
 +        for(;;) {
 +            AVPacket pkt;
 +            int fifo_bytes;
 +            av_init_packet(&pkt);
 +            pkt.stream_index= ost->index;
 +
 +            switch (ost->st->codec->codec_type) {
 +            case AVMEDIA_TYPE_AUDIO:
 +                fifo_bytes = av_fifo_size(ost->fifo);
 +                ret = 0;
 +                /* encode any samples remaining in fifo */
 +                if (fifo_bytes > 0) {
 +                    int osize = av_get_bytes_per_sample(enc->sample_fmt);
 +                    int fs_tmp = enc->frame_size;
 +
 +                    av_fifo_generic_read(ost->fifo, audio_buf, fifo_bytes, NULL);
 +                    if (enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
 +                        enc->frame_size = fifo_bytes / (osize * enc->channels);
 +                    } else { /* pad */
 +                        int frame_bytes = enc->frame_size*osize*enc->channels;
 +                        if (allocated_audio_buf_size < frame_bytes)
 +                            exit_program(1);
 +                        generate_silence(audio_buf+fifo_bytes, enc->sample_fmt, frame_bytes - fifo_bytes);
 +                    }
 +
 +                    ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, (short *)audio_buf);
 +                    pkt.duration = av_rescale((int64_t)enc->frame_size*ost->st->time_base.den,
 +                                              ost->st->time_base.num, enc->sample_rate);
 +                    enc->frame_size = fs_tmp;
 +                }
 +                if (ret <= 0) {
 +                    ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL);
 +                }
 +                if (ret < 0) {
 +                    av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
 +                    exit_program(1);
 +                }
 +                audio_size += ret;
 +                pkt.flags |= AV_PKT_FLAG_KEY;
 +                break;
 +            case AVMEDIA_TYPE_VIDEO:
 +                ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL);
 +                if (ret < 0) {
 +                    av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
 +                    exit_program(1);
 +                }
 +                video_size += ret;
 +                if(enc->coded_frame && enc->coded_frame->key_frame)
 +                    pkt.flags |= AV_PKT_FLAG_KEY;
 +                if (ost->logfile && enc->stats_out) {
 +                    fprintf(ost->logfile, "%s", enc->stats_out);
 +                }
 +                break;
 +            default:
 +                ret=-1;
 +            }
 +
 +            if (ret <= 0)
 +                break;
 +            pkt.data = bit_buffer;
 +            pkt.size = ret;
 +            if (enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
 +                pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
 +            write_frame(os, &pkt, ost->st->codec, ost->bitstream_filters);
 +        }
 +    }
 +}
 +
 +/* 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, 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;
 +
 +    AVPacket avpkt;
 +    int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt);
 +
 +    if(ist->next_pts == AV_NOPTS_VALUE)
 +        ist->next_pts= ist->pts;
 +
 +    if (pkt == NULL) {
 +        /* EOF handling */
 +        av_init_packet(&avpkt);
 +        avpkt.data = NULL;
 +        avpkt.size = 0;
 +        goto handle_eof;
 +    } else {
 +        avpkt = *pkt;
      }
  
      if(pkt->dts != AV_NOPTS_VALUE)
      while (avpkt.size > 0 || (!pkt && got_output)) {
          uint8_t *data_buf, *decoded_data_buf;
          int data_size, decoded_data_size;
++        AVFrame *decoded_frame, *filtered_frame;
      handle_eof:
          ist->pts= ist->next_pts;
  
 -        if(avpkt.size && avpkt.size != pkt->size &&
 -           ((!ist->showed_multi_packet_warning && verbose>0) || verbose>1)){
 -            fprintf(stderr, "Multiple frames in a packet from stream %d\n", pkt->stream_index);
 +        if(avpkt.size && avpkt.size != pkt->size)
 +            av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,
 +                   "Multiple frames in a packet from stream %d\n", pkt->stream_index);
              ist->showed_multi_packet_warning=1;
 -        }
  
          /* decode the packet if needed */
++        decoded_frame    = filtered_frame = NULL;
          decoded_data_buf = NULL; /* fail safe */
          decoded_data_size= 0;
          data_buf  = avpkt.data;
                  break;}
              case AVMEDIA_TYPE_VIDEO:
                      decoded_data_size = (ist->st->codec->width * ist->st->codec->height * 3) / 2;
--                    /* XXX: allocate picture correctly */
--                    avcodec_get_frame_defaults(&picture);
++                    if (!(decoded_frame = avcodec_alloc_frame()))
++                        return AVERROR(ENOMEM);
                      avpkt.pts = pkt_pts;
                      avpkt.dts = ist->pts;
                      pkt_pts = AV_NOPTS_VALUE;
  
                      ret = avcodec_decode_video2(ist->st->codec,
--                                                &picture, &got_output, &avpkt);
-                     quality = same_quant ? picture.quality : 0;
 -                    quality = same_quality ? picture.quality : 0;
++                                                decoded_frame, &got_output, &avpkt);
++                    quality = same_quant ? decoded_frame->quality : 0;
                      if (ret < 0)
--                        return ret;
++                        goto fail;
                      if (!got_output) {
                          /* no picture yet */
++                        av_freep(&decoded_frame);
                          goto discard_packet;
                      }
-                     ist->next_pts = ist->pts = picture.best_effort_timestamp;
 -                    ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, picture.pkt_pts, picture.pkt_dts);
++                    ist->next_pts = ist->pts = decoded_frame->best_effort_timestamp;
                      if (ist->st->codec->time_base.num != 0) {
                          int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
                          ist->next_pts += ((int64_t)AV_TIME_BASE *
                      }
                      avpkt.size = 0;
                      buffer_to_free = NULL;
--                    pre_process_video_frame(ist, (AVPicture *)&picture, &buffer_to_free);
++                    pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
                      break;
              case AVMEDIA_TYPE_SUBTITLE:
                  ret = avcodec_decode_subtitle2(ist->st->codec,
          }
  
  #if CONFIG_AVFILTER
 -        if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
 -            for (i = 0; i < nb_ostreams; i++) {
 -                ost = ost_table[i];
 +        if(ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
 +        for(i=0;i<nb_ostreams;i++) {
 +            OutputFile *of = &output_files[ost_table[i].file_index];
 +            if (of->start_time == 0 || ist->pts >= of->start_time) {
 +                ost = &ost_table[i];
                  if (ost->input_video_filter && ost->source_index == ist_index) {
-                     if (!picture.sample_aspect_ratio.num)
-                         picture.sample_aspect_ratio = ist->st->sample_aspect_ratio;
-                     picture.pts = ist->pts;
 -                    AVRational sar;
 -                    if (ist->st->sample_aspect_ratio.num)
 -                        sar = ist->st->sample_aspect_ratio;
 -                    else
 -                        sar = ist->st->codec->sample_aspect_ratio;
 -                    // add it to be filtered
 -                    av_vsrc_buffer_add_frame(ost->input_video_filter, &picture,
 -                                             ist->pts,
 -                                             sar);
++                if (!decoded_frame->sample_aspect_ratio.num)
++                    decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
++                decoded_frame->pts = ist->pts;
 +
-                     av_vsrc_buffer_add_frame(ost->input_video_filter, &picture, AV_VSRC_BUF_FLAG_OVERWRITE);
++                av_vsrc_buffer_add_frame(ost->input_video_filter, decoded_frame, AV_VSRC_BUF_FLAG_OVERWRITE);
                  }
              }
          }
          }
          /* if output time reached then transcode raw format,
             encode packets and output them */
 -        if (start_time == 0 || ist->pts >= start_time)
 -            for(i=0;i<nb_ostreams;i++) {
 -                int frame_size;
 +        for (i = 0; i < nb_ostreams; i++) {
 +            OutputFile *of = &output_files[ost_table[i].file_index];
 +            int frame_size;
 +
 +            ost = &ost_table[i];
 +            if (ost->source_index != ist_index)
 +                continue;
 +
 +            if (of->start_time && ist->pts < of->start_time)
 +                continue;
 +
 +            if (of->recording_time != INT64_MAX &&
 +                av_compare_ts(ist->pts, AV_TIME_BASE_Q, of->recording_time + of->start_time,
 +                              (AVRational){1, 1000000}) >= 0) {
 +                ost->is_past_recording_time = 1;
 +                continue;
 +            }
  
 -                ost = ost_table[i];
 -                if (ost->source_index == ist_index) {
  #if CONFIG_AVFILTER
 -                frame_available = ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
 -                    !ost->output_video_filter || avfilter_poll_frame(ost->output_video_filter->inputs[0]);
 -                while (frame_available) {
 -                    AVRational ist_pts_tb;
 -                    if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter)
 -                        get_filtered_video_frame(ost->output_video_filter, &picture, &ost->picref, &ist_pts_tb);
 -                    if (ost->picref)
 +            frame_available = ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
 +                !ost->output_video_filter || avfilter_poll_frame(ost->output_video_filter->inputs[0]);
 +            while (frame_available) {
 +                if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter) {
 +                    AVRational ist_pts_tb = ost->output_video_filter->inputs[0]->time_base;
 +                    if (av_buffersink_get_buffer_ref(ost->output_video_filter, &ost->picref, 0) < 0)
 +                        goto cont;
++                    if (!filtered_frame && !(filtered_frame = avcodec_alloc_frame())) {
++                        ret = AVERROR(ENOMEM);
++                        goto fail;
++                    }
++                    *filtered_frame= *decoded_frame; //for me_threshold
 +                    if (ost->picref) {
-                         avfilter_fill_frame_from_video_buffer_ref(&picture, ost->picref);
++                        avfilter_fill_frame_from_video_buffer_ref(filtered_frame, ost->picref);
                          ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
 +                    }
 +                }
++#else
++                filtered_frame = decoded_frame;
  #endif
 -                    os = output_files[ost->file_index];
 -
 -                    /* set the input output pts pairs */
 -                    //ost->sync_ipts = (double)(ist->pts + input_files[ist->file_index].ts_offset - start_time)/ AV_TIME_BASE;
 -
 -                    if (ost->encoding_needed) {
 -                        av_assert0(ist->decoding_needed);
 -                        switch(ost->st->codec->codec_type) {
 -                        case AVMEDIA_TYPE_AUDIO:
 -                            do_audio_out(os, ost, ist, decoded_data_buf, decoded_data_size);
 -                            break;
 -                        case AVMEDIA_TYPE_VIDEO:
 +                os = output_files[ost->file_index].ctx;
 +
 +                /* set the input output pts pairs */
 +                //ost->sync_ipts = (double)(ist->pts + input_files[ist->file_index].ts_offset - start_time)/ AV_TIME_BASE;
 +
 +                if (ost->encoding_needed) {
 +                    av_assert0(ist->decoding_needed);
 +                    switch(ost->st->codec->codec_type) {
 +                    case AVMEDIA_TYPE_AUDIO:
 +                        do_audio_out(os, ost, ist, decoded_data_buf, decoded_data_size);
 +                        break;
 +                    case AVMEDIA_TYPE_VIDEO:
  #if CONFIG_AVFILTER
 -                            if (ost->picref->video && !ost->frame_aspect_ratio)
 -                                ost->st->codec->sample_aspect_ratio = ost->picref->video->pixel_aspect;
 +                        if (ost->picref->video && !ost->frame_aspect_ratio)
 +                            ost->st->codec->sample_aspect_ratio = ost->picref->video->sample_aspect_ratio;
  #endif
-                         do_video_out(os, ost, ist, &picture, &frame_size,
-                                         same_quant ? quality : ost->st->codec->global_quality);
 -                            do_video_out(os, ost, ist, &picture, &frame_size,
 -                                         same_quality ? quality : ost->st->codec->global_quality);
 -                            if (vstats_filename && frame_size)
 -                                do_video_stats(os, ost, frame_size);
 -                            break;
 -                        case AVMEDIA_TYPE_SUBTITLE:
 -                            do_subtitle_out(os, ost, ist, &subtitle,
 -                                            pkt->pts);
 -                            break;
 -                        default:
 -                            abort();
 -                        }
 -                    } else {
 -                        AVFrame avframe; //FIXME/XXX remove this
 -                        AVPacket opkt;
 -                        int64_t ost_tb_start_time= av_rescale_q(start_time, AV_TIME_BASE_Q, ost->st->time_base);
 -
 -                        av_init_packet(&opkt);
++                        do_video_out(os, ost, ist, filtered_frame, &frame_size,
++                                     same_quant ? quality : ost->st->codec->global_quality);
 +                        if (vstats_filename && frame_size)
 +                            do_video_stats(os, ost, frame_size);
 +                        break;
 +                    case AVMEDIA_TYPE_SUBTITLE:
 +                        do_subtitle_out(os, ost, ist, &subtitle,
 +                                        pkt->pts);
 +                        break;
 +                    default:
 +                        abort();
 +                    }
 +                } else {
 +                    AVPicture pict;
 +                    AVPacket opkt;
 +                    int64_t ost_tb_start_time= av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base);
 +                    av_init_packet(&opkt);
  
 -                        if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) && !copy_initial_nonkeyframes)
 +                    if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) && !copy_initial_nonkeyframes)
  #if !CONFIG_AVFILTER
 -                            continue;
 +                        continue;
  #else
 -                            goto cont;
 +                        goto cont;
  #endif
  
 -                        /* no reencoding needed : output the packet directly */
 -                        /* force the input stream PTS */
 +                    /* no reencoding needed : output the packet directly */
 +                    /* force the input stream PTS */
  
 -                        avcodec_get_frame_defaults(&avframe);
 -                        ost->st->codec->coded_frame= &avframe;
 -                        avframe.key_frame = pkt->flags & AV_PKT_FLAG_KEY;
 +                    if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
 +                        audio_size += data_size;
 +                    else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
 +                        video_size += data_size;
 +                        ost->sync_opts++;
 +                    }
  
 -                        if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
 -                            audio_size += data_size;
 -                        else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
 -                            video_size += data_size;
 -                            ost->sync_opts++;
 -                        }
 +                    opkt.stream_index= ost->index;
 +                    if(pkt->pts != AV_NOPTS_VALUE)
 +                        opkt.pts= av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;
 +                    else
 +                        opkt.pts= AV_NOPTS_VALUE;
  
 -                        opkt.stream_index= ost->index;
 -                        if(pkt->pts != AV_NOPTS_VALUE)
 -                            opkt.pts= av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;
 -                        else
 -                            opkt.pts= AV_NOPTS_VALUE;
 -
 -                        if (pkt->dts == AV_NOPTS_VALUE)
 -                            opkt.dts = av_rescale_q(ist->pts, AV_TIME_BASE_Q, ost->st->time_base);
 -                        else
 -                            opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);
 -                        opkt.dts -= ost_tb_start_time;
 -
 -                        opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base);
 -                        opkt.flags= pkt->flags;
 -
 -                        //FIXME remove the following 2 lines they shall be replaced by the bitstream filters
 -                        if(   ost->st->codec->codec_id != CODEC_ID_H264
 -                           && ost->st->codec->codec_id != CODEC_ID_MPEG1VIDEO
 -                           && ost->st->codec->codec_id != CODEC_ID_MPEG2VIDEO
 -                           ) {
 -                            if(av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, data_buf, data_size, pkt->flags & AV_PKT_FLAG_KEY))
 -                                opkt.destruct= av_destruct_packet;
 -                        } else {
 -                            opkt.data = data_buf;
 -                            opkt.size = data_size;
 -                        }
 +                    if (pkt->dts == AV_NOPTS_VALUE)
 +                        opkt.dts = av_rescale_q(ist->pts, AV_TIME_BASE_Q, ost->st->time_base);
 +                    else
 +                        opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);
 +                    opkt.dts -= ost_tb_start_time;
 +
 +                    opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base);
 +                    opkt.flags= pkt->flags;
 +
 +                    //FIXME remove the following 2 lines they shall be replaced by the bitstream filters
 +                    if(   ost->st->codec->codec_id != CODEC_ID_H264
 +                       && ost->st->codec->codec_id != CODEC_ID_MPEG1VIDEO
 +                       && ost->st->codec->codec_id != CODEC_ID_MPEG2VIDEO
 +                       ) {
 +                        if(av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, data_buf, data_size, pkt->flags & AV_PKT_FLAG_KEY))
 +                            opkt.destruct= av_destruct_packet;
 +                    } else {
 +                        opkt.data = data_buf;
 +                        opkt.size = data_size;
 +                    }
  
 -                        write_frame(os, &opkt, ost->st->codec, ost->bitstream_filters);
 -                        ost->st->codec->frame_number++;
 -                        ost->frame_number++;
 -                        av_free_packet(&opkt);
 +                    if (os->oformat->flags & AVFMT_RAWPICTURE) {
 +                        /* store AVPicture in AVPacket, as expected by the output format */
 +                        avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height);
 +                        opkt.data = (uint8_t *)&pict;
 +                        opkt.size = sizeof(AVPicture);
 +                        opkt.flags |= AV_PKT_FLAG_KEY;
                      }
 -#if CONFIG_AVFILTER
 -                    cont:
 -                    frame_available = (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
 -                                       ost->output_video_filter && avfilter_poll_frame(ost->output_video_filter->inputs[0]);
 -                    if (ost->picref)
 -                        avfilter_unref_buffer(ost->picref);
 -                }
 -#endif
 +                    write_frame(os, &opkt, ost->st->codec, ost->bitstream_filters);
 +                    ost->st->codec->frame_number++;
 +                    ost->frame_number++;
 +                    av_free_packet(&opkt);
                  }
 +#if CONFIG_AVFILTER
 +                cont:
 +                frame_available = (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
 +                                   ost->output_video_filter && avfilter_poll_frame(ost->output_video_filter->inputs[0]);
 +                avfilter_unref_buffer(ost->picref);
              }
++            av_freep(&filtered_frame);
 +#endif
 +        }
  
++fail:
          av_free(buffer_to_free);
          /* XXX: allocate the subtitles in the codec ? */
          if (subtitle_to_free) {
              avsubtitle_free(subtitle_to_free);
              subtitle_to_free = NULL;
          }
++        av_freep(&decoded_frame);
++        if (ret < 0)
++            return ret;
      }
   discard_packet:
 -    if (pkt == NULL) {
 -        /* EOF handling */
 -
 -        for(i=0;i<nb_ostreams;i++) {
 -            ost = ost_table[i];
 -            if (ost->source_index == ist_index) {
 -                AVCodecContext *enc= ost->st->codec;
 -                os = output_files[ost->file_index];
 -
 -                if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <=1)
 -                    continue;
 -                if(ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE))
 -                    continue;
 -
 -                if (ost->encoding_needed) {
 -                    for(;;) {
 -                        AVPacket pkt;
 -                        int fifo_bytes;
 -                        av_init_packet(&pkt);
 -                        pkt.stream_index= ost->index;
 -
 -                        switch(ost->st->codec->codec_type) {
 -                        case AVMEDIA_TYPE_AUDIO:
 -                            fifo_bytes = av_fifo_size(ost->fifo);
 -                            ret = 0;
 -                            /* encode any samples remaining in fifo */
 -                            if (fifo_bytes > 0) {
 -                                int osize = av_get_bytes_per_sample(enc->sample_fmt);
 -                                int fs_tmp = enc->frame_size;
 -
 -                                av_fifo_generic_read(ost->fifo, audio_buf, fifo_bytes, NULL);
 -                                if (enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
 -                                    enc->frame_size = fifo_bytes / (osize * enc->channels);
 -                                } else { /* pad */
 -                                    int frame_bytes = enc->frame_size*osize*enc->channels;
 -                                    if (allocated_audio_buf_size < frame_bytes)
 -                                        exit_program(1);
 -                                    generate_silence(audio_buf+fifo_bytes, enc->sample_fmt, frame_bytes - fifo_bytes);
 -                                }
 -
 -                                ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, (short *)audio_buf);
 -                                pkt.duration = av_rescale((int64_t)enc->frame_size*ost->st->time_base.den,
 -                                                          ost->st->time_base.num, enc->sample_rate);
 -                                enc->frame_size = fs_tmp;
 -                            }
 -                            if(ret <= 0) {
 -                                ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL);
 -                            }
 -                            if (ret < 0) {
 -                                fprintf(stderr, "Audio encoding failed\n");
 -                                exit_program(1);
 -                            }
 -                            audio_size += ret;
 -                            pkt.flags |= AV_PKT_FLAG_KEY;
 -                            break;
 -                        case AVMEDIA_TYPE_VIDEO:
 -                            ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL);
 -                            if (ret < 0) {
 -                                fprintf(stderr, "Video encoding failed\n");
 -                                exit_program(1);
 -                            }
 -                            video_size += ret;
 -                            if(enc->coded_frame && enc->coded_frame->key_frame)
 -                                pkt.flags |= AV_PKT_FLAG_KEY;
 -                            if (ost->logfile && enc->stats_out) {
 -                                fprintf(ost->logfile, "%s", enc->stats_out);
 -                            }
 -                            break;
 -                        default:
 -                            ret=-1;
 -                        }
 -
 -                        if(ret<=0)
 -                            break;
 -                        pkt.data= bit_buffer;
 -                        pkt.size= ret;
 -                        if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
 -                            pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
 -                        write_frame(os, &pkt, ost->st->codec, ost->bitstream_filters);
 -                    }
 -                }
 -            }
 -        }
 -    }
  
      return 0;
  }
@@@ -2082,6 -2150,6 +2096,7 @@@ static int transcode_init(OutputFile *o
                  codec->height = icodec->height;
                  break;
              case AVMEDIA_TYPE_DATA:
++            case AVMEDIA_TYPE_ATTACHMENT:
                  break;
              default:
                  abort();
@@@ -3110,156 -3350,108 +3125,155 @@@ static int opt_input_file(OptionsContex
      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, AVCodecContext *avctx)
  {
 -    int has_video, has_audio, has_subtitle, has_data, i, j;
 -    AVFormatContext *ic;
 -
 -    has_video = 0;
 -    has_audio = 0;
 -    has_subtitle = 0;
 -    has_data = 0;
 +    char *p;
 +    int n = 1, i;
 +    int64_t t;
  
 -    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();
 -            }
 -        }
 +    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);
      }
 -    *has_video_ptr = has_video;
 -    *has_audio_ptr = has_audio;
 -    *has_subtitle_ptr = has_subtitle;
 -    *has_data_ptr = has_data;
  }
  
 -static void new_video_stream(AVFormatContext *oc, int file_idx)
 +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 = av_new_stream(oc, oc->nb_streams < o->nb_streamid_map ? o->streamid_map[oc->nb_streams] : 0);
 +    int idx      = oc->nb_streams - 1;
 +    int64_t max_frames = INT64_MAX;
 +    char *bsf = NULL, *next, *codec_tag = NULL;
 +    AVBitStreamFilterContext *bsfc, *bsfc_prev = NULL;
 +    double qscale = -1;
  
 -    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);
 +    }
 +
 +    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;
 +    ost->enc = choose_codec(o, oc, st, type);
 +    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(max_frames, i64, max_frames, oc, st);
 +    ost->max_frames = max_frames;
 +
 +    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;
 +
 +        bsfc_prev = bsfc;
 +        bsf       = next;
      }
  
 -    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
 +    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;
 +    }
 +
 +    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;
      }
  
 -    ost->bitstream_filters = video_bitstream_filters;
 -    video_bitstream_filters= NULL;
++    if (oc->oformat->flags & AVFMT_GLOBALHEADER)
++        st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
 -    st->codec->thread_count= thread_count;
 +    ost->sws_flags = av_get_int(sws_opts, "sws_flags", NULL);
 +    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(oc->oformat->flags & AVFMT_GLOBALHEADER) {
-         video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
-     }
 -    if(video_codec_tag)
 -        video_enc->codec_tag= video_codec_tag;
 +    if (!st->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, force_fps = 0, top_field_first = -1;
  
 -    if(oc->oformat->flags & AVFMT_GLOBALHEADER) {
 -        video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
 -    }
 +        MATCH_PER_STREAM_OPT(frame_rates, str, frame_rate, oc, st);
 +        if (frame_rate && av_parse_video_rate(&ost->frame_rate, frame_rate) < 0) {
 +            av_log(NULL, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);
 +            exit_program(1);
 +        }
  
 -    video_enc->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;
 -        int i;
 +        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);
 +        }
  
 -        if (frame_rate.num)
 -            ost->frame_rate = frame_rate;
 -        video_enc->codec_id = codec_id;
 +        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)
@@@ -3351,49 -3547,76 +3365,47 @@@ static OutputStream *new_audio_stream(O
      audio_enc = st->codec;
      audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
  
-     if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
-         audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
-     }
 -    if(audio_codec_tag)
 -        audio_enc->codec_tag= audio_codec_tag;
 +    if (!st->stream_copy) {
 +        char *sample_fmt = NULL;
  
 -    if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
 -        audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
 -    }
 -    if (audio_stream_copy) {
 -        st->stream_copy = 1;
 -    } else {
 -        audio_enc->codec_id = codec_id;
 +        MATCH_PER_STREAM_OPT(audio_channels, i, audio_enc->channels, oc, st);
  
 -        if (audio_qscale > QSCALE_NONE) {
 -            audio_enc->flags |= CODEC_FLAG_QSCALE;
 -            audio_enc->global_quality = FF_QP2LAMBDA * audio_qscale;
 +        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);
          }
 -        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);
 +
 +        MATCH_PER_STREAM_OPT(audio_sample_rate, i, audio_enc->sample_rate, oc, st);
      }
  
 -    /* 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");
 -        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;
 +    ost = new_output_stream(o, oc, AVMEDIA_TYPE_DATA);
 +    st  = ost->st;
-     data_enc = st->codec;
 +    if (!st->stream_copy) {
 +        av_log(NULL, AV_LOG_FATAL, "Data stream encoding not supported yet (only streamcopy)\n");
 +        exit_program(1);
      }
  
-     if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
-         data_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
-     }
 -    data_disable = 0;
 -    av_freep(&data_codec_name);
 -    data_stream_copy = 0;
++    return ost;
++}
 +
++static OutputStream *new_attachment_stream(OptionsContext *o, AVFormatContext *oc)
++{
++    OutputStream *ost = new_output_stream(o, oc, AVMEDIA_TYPE_ATTACHMENT);
++    ost->st->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;
      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;
 -
--    if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
--        subtitle_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
 -    }
 -    if (subtitle_stream_copy) {
 -        st->stream_copy = 1;
 -    } else {
 -        subtitle_enc->codec_id = codec_id;
 -    }
 -
 -    if (subtitle_language) {
 -        av_dict_set(&st->metadata, "language", subtitle_language, 0);
 -        av_freep(&subtitle_language);
 -    }
 -
 -    subtitle_disable = 0;
 -    av_freep(&subtitle_codec_name);
 -    subtitle_stream_copy = 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);
--    }
 -    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;
 +    return ost;
  }
  
  /* arg format is "output-stream-index:streamid-value". */
@@@ -3536,86 -3748,44 +3544,87 @@@ static void opt_output_file(void *optct
              print_error(filename, err);
              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 {
 -        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);
 +        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];
 +            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;
 +        }
      }
  
 -    av_dict_copy(&output_opts[nb_output_files], format_opts, 0);
 -    output_files[nb_output_files++] = oc;
 +    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) {
Simple merge
Simple merge