build: Move cli tool sources to a separate subdirectory
authorDiego Biurrun <diego@biurrun.de>
Wed, 4 Jan 2017 14:09:29 +0000 (15:09 +0100)
committerDiego Biurrun <diego@biurrun.de>
Tue, 21 Feb 2017 15:10:51 +0000 (16:10 +0100)
This unclutters the top-level directory and groups related files together.

28 files changed:
Makefile
avconv.c [deleted file]
avconv.h [deleted file]
avconv_dxva2.c [deleted file]
avconv_filter.c [deleted file]
avconv_opt.c [deleted file]
avconv_qsv.c [deleted file]
avconv_vaapi.c [deleted file]
avconv_vda.c [deleted file]
avconv_vdpau.c [deleted file]
avplay.c [deleted file]
avprobe.c [deleted file]
avtools/Makefile [new file with mode: 0644]
avtools/avconv.c [new file with mode: 0644]
avtools/avconv.h [new file with mode: 0644]
avtools/avconv_dxva2.c [new file with mode: 0644]
avtools/avconv_filter.c [new file with mode: 0644]
avtools/avconv_opt.c [new file with mode: 0644]
avtools/avconv_qsv.c [new file with mode: 0644]
avtools/avconv_vaapi.c [new file with mode: 0644]
avtools/avconv_vda.c [new file with mode: 0644]
avtools/avconv_vdpau.c [new file with mode: 0644]
avtools/avplay.c [new file with mode: 0644]
avtools/avprobe.c [new file with mode: 0644]
avtools/cmdutils.c [new file with mode: 0644]
avtools/cmdutils.h [new file with mode: 0644]
cmdutils.c [deleted file]
cmdutils.h [deleted file]

index 2932e6e..a3cd084 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -69,25 +69,6 @@ COMPILE_HOSTC = $(call COMPILE,HOSTCC)
 
 %.c %.h %.pc %.ver %.version: TAG = GEN
 
-AVPROGS-$(CONFIG_AVCONV)   += avconv
-AVPROGS-$(CONFIG_AVPLAY)   += avplay
-AVPROGS-$(CONFIG_AVPROBE)  += avprobe
-
-AVPROGS    := $(AVPROGS-yes:%=%$(EXESUF))
-PROGS      += $(AVPROGS)
-
-AVBASENAMES = avconv avplay avprobe
-ALLAVPROGS  = $(AVBASENAMES:%=%$(EXESUF))
-
-$(foreach prog,$(AVBASENAMES),$(eval OBJS-$(prog) += cmdutils.o))
-
-OBJS-avconv                   += avconv_opt.o avconv_filter.o
-OBJS-avconv-$(CONFIG_LIBMFX)  += avconv_qsv.o
-OBJS-avconv-$(CONFIG_VAAPI)   += avconv_vaapi.o
-OBJS-avconv-$(CONFIG_VDA)     += avconv_vda.o
-OBJS-avconv-$(HAVE_DXVA2_LIB) += avconv_dxva2.o
-OBJS-avconv-$(HAVE_VDPAU_X11) += avconv_vdpau.o
-
 TESTTOOLS   = audiogen videogen rotozoom tiny_psnr base64
 HOSTPROGS  := $(TESTTOOLS:%=tests/%) doc/print_options
 
@@ -115,8 +96,6 @@ FF_EXTRALIBS := $(FFEXTRALIBS)
 FF_DEP_LIBS  := $(DEP_LIBS)
 FF_STATIC_DEP_LIBS := $(STATIC_DEP_LIBS)
 
-all: $(AVPROGS)
-
 $(TOOLS): %$(EXESUF): %.o
        $(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $^ $(EXTRALIBS) $(ELIBS)
 
@@ -153,21 +132,11 @@ endef
 
 $(foreach D,$(FFLIBS),$(eval $(call DOSUBDIR,lib$(D))))
 
+include $(SRC_PATH)/avtools/Makefile
 include $(SRC_PATH)/doc/Makefile
 include $(SRC_PATH)/doc/examples/Makefile
 
-define DOPROG
-OBJS-$(1) += $(1).o $(OBJS-$(1)-yes)
-$(1)$(EXESUF): $$(OBJS-$(1))
-$$(OBJS-$(1)): CFLAGS  += $(CFLAGS-$(1))
-$(1)$(EXESUF): LDFLAGS += $(LDFLAGS-$(1))
-$(1)$(EXESUF): FF_EXTRALIBS += $(EXTRALIBS-$(1))
--include $$(OBJS-$(1):.o=.d)
-endef
-
-$(foreach P,$(AVPROGS-yes),$(eval $(call DOPROG,$(P))))
-
-$(PROGS): %$(EXESUF): %.o $(FF_DEP_LIBS)
+$(PROGS): %$(EXESUF): $(FF_DEP_LIBS)
        $(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $(OBJS-$*) $(FF_EXTRALIBS)
 
 VERSION_SH  = $(SRC_PATH)/avbuild/version.sh
@@ -176,7 +145,7 @@ GIT_LOG     = $(SRC_PATH)/.git/logs/HEAD
 .version: $(wildcard $(GIT_LOG)) $(VERSION_SH) avbuild/config.mak
 .version: M=@
 
-cmdutils.o libavutil/utils.o: avversion.h
+libavutil/utils.o: avversion.h
 avversion.h .version:
        $(M)$(VERSION_SH) $(SRC_PATH) avversion.h $(EXTRA_VERSION)
        $(Q)touch .version
@@ -184,35 +153,20 @@ avversion.h .version:
 # force version.sh to run whenever version might have changed
 -include .version
 
-ifdef AVPROGS
-install: install-progs install-data
-endif
-
 install: install-libs install-headers
 
 install-libs: install-libs-yes
 
-install-progs-yes:
-install-progs-$(CONFIG_SHARED): install-libs
-
-install-progs: install-progs-yes $(AVPROGS)
-       $(Q)mkdir -p "$(BINDIR)"
-       $(INSTALL) -c -m 755 $(AVPROGS) "$(BINDIR)"
-
 install-data: $(DATA_FILES)
        $(Q)mkdir -p "$(DATADIR)"
        $(INSTALL) -m 644 $(DATA_FILES) "$(DATADIR)"
 
-uninstall: uninstall-libs uninstall-headers uninstall-progs uninstall-data
-
-uninstall-progs:
-       $(RM) $(addprefix "$(BINDIR)/", $(ALLAVPROGS))
+uninstall: uninstall-libs uninstall-headers uninstall-data
 
 uninstall-data:
        $(RM) -r "$(DATADIR)"
 
 clean::
-       $(RM) $(ALLAVPROGS)
        $(RM) $(CLEANSUFFIXES)
        $(RM) -rf coverage.info lcov
 
diff --git a/avconv.c b/avconv.c
deleted file mode 100644 (file)
index 5c36761..0000000
--- a/avconv.c
+++ /dev/null
@@ -1,2913 +0,0 @@
-/*
- * avconv main
- * Copyright (c) 2000-2011 The Libav developers
- *
- * This file is part of Libav.
- *
- * Libav is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Libav is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "config.h"
-#include <ctype.h>
-#include <string.h>
-#include <math.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <signal.h>
-#include <limits.h>
-#include <stdint.h>
-
-#include "libavformat/avformat.h"
-#include "libavdevice/avdevice.h"
-#include "libswscale/swscale.h"
-#include "libavresample/avresample.h"
-#include "libavutil/opt.h"
-#include "libavutil/channel_layout.h"
-#include "libavutil/parseutils.h"
-#include "libavutil/samplefmt.h"
-#include "libavutil/fifo.h"
-#include "libavutil/hwcontext.h"
-#include "libavutil/internal.h"
-#include "libavutil/intreadwrite.h"
-#include "libavutil/dict.h"
-#include "libavutil/mathematics.h"
-#include "libavutil/pixdesc.h"
-#include "libavutil/avstring.h"
-#include "libavutil/libm.h"
-#include "libavutil/imgutils.h"
-#include "libavutil/time.h"
-#include "libavformat/os_support.h"
-
-# include "libavfilter/avfilter.h"
-# include "libavfilter/buffersrc.h"
-# include "libavfilter/buffersink.h"
-
-#if HAVE_SYS_RESOURCE_H
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/resource.h>
-#elif HAVE_GETPROCESSTIMES
-#include <windows.h>
-#endif
-#if HAVE_GETPROCESSMEMORYINFO
-#include <windows.h>
-#include <psapi.h>
-#endif
-
-#if HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
-
-#if HAVE_PTHREADS
-#include <pthread.h>
-#endif
-
-#include <time.h>
-
-#include "avconv.h"
-#include "cmdutils.h"
-
-#include "libavutil/avassert.h"
-
-const char program_name[] = "avconv";
-const int program_birth_year = 2000;
-
-static FILE *vstats_file;
-
-static int nb_frames_drop = 0;
-
-static int want_sdp = 1;
-
-#if HAVE_PTHREADS
-/* signal to input threads that they should exit; set by the main thread */
-static int transcoding_finished;
-#endif
-
-InputStream **input_streams = NULL;
-int        nb_input_streams = 0;
-InputFile   **input_files   = NULL;
-int        nb_input_files   = 0;
-
-OutputStream **output_streams = NULL;
-int         nb_output_streams = 0;
-OutputFile   **output_files   = NULL;
-int         nb_output_files   = 0;
-
-FilterGraph **filtergraphs;
-int        nb_filtergraphs;
-
-static void term_exit(void)
-{
-    av_log(NULL, AV_LOG_QUIET, "");
-}
-
-static volatile int received_sigterm = 0;
-static volatile int received_nb_signals = 0;
-
-static void
-sigterm_handler(int sig)
-{
-    received_sigterm = sig;
-    received_nb_signals++;
-    term_exit();
-}
-
-static void term_init(void)
-{
-    signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).    */
-    signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
-#ifdef SIGXCPU
-    signal(SIGXCPU, sigterm_handler);
-#endif
-}
-
-static int decode_interrupt_cb(void *ctx)
-{
-    return received_nb_signals > 1;
-}
-
-const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
-
-static void avconv_cleanup(int ret)
-{
-    int i, j;
-
-    for (i = 0; i < nb_filtergraphs; i++) {
-        FilterGraph *fg = filtergraphs[i];
-        avfilter_graph_free(&fg->graph);
-        for (j = 0; j < fg->nb_inputs; j++) {
-            while (av_fifo_size(fg->inputs[j]->frame_queue)) {
-                AVFrame *frame;
-                av_fifo_generic_read(fg->inputs[j]->frame_queue, &frame,
-                                     sizeof(frame), NULL);
-                av_frame_free(&frame);
-            }
-            av_fifo_free(fg->inputs[j]->frame_queue);
-            av_buffer_unref(&fg->inputs[j]->hw_frames_ctx);
-            av_freep(&fg->inputs[j]->name);
-            av_freep(&fg->inputs[j]);
-        }
-        av_freep(&fg->inputs);
-        for (j = 0; j < fg->nb_outputs; j++) {
-            av_freep(&fg->outputs[j]->name);
-            av_freep(&fg->outputs[j]->formats);
-            av_freep(&fg->outputs[j]->channel_layouts);
-            av_freep(&fg->outputs[j]->sample_rates);
-            av_freep(&fg->outputs[j]);
-        }
-        av_freep(&fg->outputs);
-        av_freep(&fg->graph_desc);
-
-        av_freep(&filtergraphs[i]);
-    }
-    av_freep(&filtergraphs);
-
-    /* close files */
-    for (i = 0; i < nb_output_files; i++) {
-        OutputFile *of = output_files[i];
-        AVFormatContext *s = of->ctx;
-        if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE) && s->pb)
-            avio_close(s->pb);
-        avformat_free_context(s);
-        av_dict_free(&of->opts);
-
-        av_freep(&output_files[i]);
-    }
-    for (i = 0; i < nb_output_streams; i++) {
-        OutputStream *ost = output_streams[i];
-
-        for (j = 0; j < ost->nb_bitstream_filters; j++)
-            av_bsf_free(&ost->bsf_ctx[j]);
-        av_freep(&ost->bsf_ctx);
-
-        av_frame_free(&ost->filtered_frame);
-
-        av_parser_close(ost->parser);
-        avcodec_free_context(&ost->parser_avctx);
-
-        av_freep(&ost->forced_keyframes);
-        av_freep(&ost->avfilter);
-        av_freep(&ost->logfile_prefix);
-
-        avcodec_free_context(&ost->enc_ctx);
-
-        if (ost->muxing_queue) {
-            while (av_fifo_size(ost->muxing_queue)) {
-                AVPacket pkt;
-                av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL);
-                av_packet_unref(&pkt);
-            }
-            av_fifo_free(ost->muxing_queue);
-        }
-        av_freep(&output_streams[i]);
-    }
-    for (i = 0; i < nb_input_files; i++) {
-        avformat_close_input(&input_files[i]->ctx);
-        av_freep(&input_files[i]);
-    }
-    for (i = 0; i < nb_input_streams; i++) {
-        InputStream *ist = input_streams[i];
-
-        av_frame_free(&ist->decoded_frame);
-        av_frame_free(&ist->filter_frame);
-        av_dict_free(&ist->decoder_opts);
-        av_freep(&ist->filters);
-        av_freep(&ist->hwaccel_device);
-
-        avcodec_free_context(&ist->dec_ctx);
-
-        av_freep(&input_streams[i]);
-    }
-
-    if (vstats_file)
-        fclose(vstats_file);
-    av_free(vstats_filename);
-
-    av_freep(&input_streams);
-    av_freep(&input_files);
-    av_freep(&output_streams);
-    av_freep(&output_files);
-
-    uninit_opts();
-
-    avformat_network_deinit();
-
-    if (received_sigterm) {
-        av_log(NULL, AV_LOG_INFO, "Received signal %d: terminating.\n",
-               (int) received_sigterm);
-        exit (255);
-    }
-}
-
-void assert_avoptions(AVDictionary *m)
-{
-    AVDictionaryEntry *t;
-    if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
-        av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
-        exit_program(1);
-    }
-}
-
-static void abort_codec_experimental(AVCodec *c, int encoder)
-{
-    const char *codec_string = encoder ? "encoder" : "decoder";
-    AVCodec *codec;
-    av_log(NULL, AV_LOG_FATAL, "%s '%s' is experimental and might produce bad "
-            "results.\nAdd '-strict experimental' if you want to use it.\n",
-            codec_string, c->name);
-    codec = encoder ? avcodec_find_encoder(c->id) : avcodec_find_decoder(c->id);
-    if (!(codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
-        av_log(NULL, AV_LOG_FATAL, "Or use the non experimental %s '%s'.\n",
-               codec_string, codec->name);
-    exit_program(1);
-}
-
-static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
-{
-    AVFormatContext *s = of->ctx;
-    AVStream *st = ost->st;
-    int ret;
-
-    if (!of->header_written) {
-        AVPacket tmp_pkt;
-        /* the muxer is not initialized yet, buffer the packet */
-        if (!av_fifo_space(ost->muxing_queue)) {
-            int new_size = FFMIN(2 * av_fifo_size(ost->muxing_queue),
-                                 ost->max_muxing_queue_size);
-            if (new_size <= av_fifo_size(ost->muxing_queue)) {
-                av_log(NULL, AV_LOG_ERROR,
-                       "Too many packets buffered for output stream %d:%d.\n",
-                       ost->file_index, ost->st->index);
-                exit_program(1);
-            }
-            ret = av_fifo_realloc2(ost->muxing_queue, new_size);
-            if (ret < 0)
-                exit_program(1);
-        }
-        av_packet_move_ref(&tmp_pkt, pkt);
-        av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL);
-        return;
-    }
-
-    /*
-     * Audio encoders may split the packets --  #frames in != #packets out.
-     * But there is no reordering, so we can limit the number of output packets
-     * by simply dropping them here.
-     * Counting encoded video frames needs to be done separately because of
-     * reordering, see do_video_out()
-     */
-    if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) {
-        if (ost->frame_number >= ost->max_frames) {
-            av_packet_unref(pkt);
-            return;
-        }
-        ost->frame_number++;
-    }
-    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
-        uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_FACTOR,
-                                              NULL);
-        ost->quality = sd ? *(int *)sd : -1;
-
-        if (ost->frame_rate.num) {
-            pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate),
-                                         ost->mux_timebase);
-        }
-    }
-
-    av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base);
-
-    if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
-        ost->last_mux_dts != AV_NOPTS_VALUE &&
-        pkt->dts < ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT)) {
-        av_log(NULL, AV_LOG_WARNING, "Non-monotonous DTS in output stream "
-               "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
-               ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts);
-        if (exit_on_error) {
-            av_log(NULL, AV_LOG_FATAL, "aborting.\n");
-            exit_program(1);
-        }
-        av_log(NULL, AV_LOG_WARNING, "changing to %"PRId64". This may result "
-               "in incorrect timestamps in the output file.\n",
-               ost->last_mux_dts + 1);
-        pkt->dts = ost->last_mux_dts + 1;
-        if (pkt->pts != AV_NOPTS_VALUE)
-            pkt->pts = FFMAX(pkt->pts, pkt->dts);
-    }
-    ost->last_mux_dts = pkt->dts;
-
-    ost->data_size += pkt->size;
-    ost->packets_written++;
-
-    pkt->stream_index = ost->index;
-
-    ret = av_interleaved_write_frame(s, pkt);
-    if (ret < 0) {
-        print_error("av_interleaved_write_frame()", ret);
-        exit_program(1);
-    }
-}
-
-static void output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
-{
-    int ret = 0;
-
-    /* apply the output bitstream filters, if any */
-    if (ost->nb_bitstream_filters) {
-        int idx;
-
-        ret = av_bsf_send_packet(ost->bsf_ctx[0], pkt);
-        if (ret < 0)
-            goto finish;
-
-        idx = 1;
-        while (idx) {
-            /* get a packet from the previous filter up the chain */
-            ret = av_bsf_receive_packet(ost->bsf_ctx[idx - 1], pkt);
-            if (ret == AVERROR(EAGAIN)) {
-                ret = 0;
-                idx--;
-                continue;
-            } else if (ret < 0)
-                goto finish;
-
-            /* send it to the next filter down the chain or to the muxer */
-            if (idx < ost->nb_bitstream_filters) {
-                ret = av_bsf_send_packet(ost->bsf_ctx[idx], pkt);
-                if (ret < 0)
-                    goto finish;
-                idx++;
-            } else
-                write_packet(of, pkt, ost);
-        }
-    } else
-        write_packet(of, pkt, ost);
-
-finish:
-    if (ret < 0 && ret != AVERROR_EOF) {
-        av_log(NULL, AV_LOG_FATAL, "Error applying bitstream filters to an output "
-               "packet for stream #%d:%d.\n", ost->file_index, ost->index);
-        exit_program(1);
-    }
-}
-
-static int check_recording_time(OutputStream *ost)
-{
-    OutputFile *of = output_files[ost->file_index];
-
-    if (of->recording_time != INT64_MAX &&
-        av_compare_ts(ost->sync_opts - ost->first_pts, ost->enc_ctx->time_base, of->recording_time,
-                      AV_TIME_BASE_Q) >= 0) {
-        ost->finished = 1;
-        return 0;
-    }
-    return 1;
-}
-
-static void do_audio_out(OutputFile *of, OutputStream *ost,
-                         AVFrame *frame)
-{
-    AVCodecContext *enc = ost->enc_ctx;
-    AVPacket pkt;
-    int ret;
-
-    av_init_packet(&pkt);
-    pkt.data = NULL;
-    pkt.size = 0;
-
-    if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
-        frame->pts = ost->sync_opts;
-    ost->sync_opts = frame->pts + frame->nb_samples;
-
-    ost->samples_encoded += frame->nb_samples;
-    ost->frames_encoded++;
-
-    ret = avcodec_send_frame(enc, frame);
-    if (ret < 0)
-        goto error;
-
-    while (1) {
-        ret = avcodec_receive_packet(enc, &pkt);
-        if (ret == AVERROR(EAGAIN))
-            break;
-        if (ret < 0)
-            goto error;
-
-        output_packet(of, &pkt, ost);
-    }
-
-    return;
-error:
-    av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
-    exit_program(1);
-}
-
-static void do_subtitle_out(OutputFile *of,
-                            OutputStream *ost,
-                            InputStream *ist,
-                            AVSubtitle *sub,
-                            int64_t pts)
-{
-    static uint8_t *subtitle_out = NULL;
-    int subtitle_out_max_size = 1024 * 1024;
-    int subtitle_out_size, nb, i;
-    AVCodecContext *enc;
-    AVPacket pkt;
-
-    if (pts == AV_NOPTS_VALUE) {
-        av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
-        if (exit_on_error)
-            exit_program(1);
-        return;
-    }
-
-    enc = ost->enc_ctx;
-
-    if (!subtitle_out) {
-        subtitle_out = av_malloc(subtitle_out_max_size);
-    }
-
-    /* Note: DVB subtitle need one packet to draw them and one other
-       packet to clear them */
-    /* XXX: signal it in the codec context ? */
-    if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
-        nb = 2;
-    else
-        nb = 1;
-
-    for (i = 0; i < nb; i++) {
-        ost->sync_opts = av_rescale_q(pts, ist->st->time_base, enc->time_base);
-        if (!check_recording_time(ost))
-            return;
-
-        sub->pts = av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q);
-        // start_display_time is required to be 0
-        sub->pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
-        sub->end_display_time  -= sub->start_display_time;
-        sub->start_display_time = 0;
-
-        ost->frames_encoded++;
-
-        subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
-                                                    subtitle_out_max_size, sub);
-        if (subtitle_out_size < 0) {
-            av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
-            exit_program(1);
-        }
-
-        av_init_packet(&pkt);
-        pkt.data = subtitle_out;
-        pkt.size = subtitle_out_size;
-        pkt.pts  = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->mux_timebase);
-        if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
-            /* XXX: the pts correction is handled here. Maybe handling
-               it in the codec would be better */
-            if (i == 0)
-                pkt.pts += 90 * sub->start_display_time;
-            else
-                pkt.pts += 90 * sub->end_display_time;
-        }
-        output_packet(of, &pkt, ost);
-    }
-}
-
-static void do_video_out(OutputFile *of,
-                         OutputStream *ost,
-                         AVFrame *in_picture,
-                         int *frame_size)
-{
-    int ret, format_video_sync;
-    AVPacket pkt;
-    AVCodecContext *enc = ost->enc_ctx;
-
-    *frame_size = 0;
-
-    format_video_sync = video_sync_method;
-    if (format_video_sync == VSYNC_AUTO)
-        format_video_sync = (of->ctx->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH :
-                            (of->ctx->oformat->flags & AVFMT_VARIABLE_FPS) ? VSYNC_VFR : VSYNC_CFR;
-    if (format_video_sync != VSYNC_PASSTHROUGH &&
-        ost->frame_number &&
-        in_picture->pts != AV_NOPTS_VALUE &&
-        in_picture->pts < ost->sync_opts) {
-        nb_frames_drop++;
-        av_log(NULL, AV_LOG_WARNING,
-               "*** dropping frame %d from stream %d at ts %"PRId64"\n",
-               ost->frame_number, ost->st->index, in_picture->pts);
-        return;
-    }
-
-    if (in_picture->pts == AV_NOPTS_VALUE)
-        in_picture->pts = ost->sync_opts;
-    ost->sync_opts = in_picture->pts;
-
-
-    if (!ost->frame_number)
-        ost->first_pts = in_picture->pts;
-
-    av_init_packet(&pkt);
-    pkt.data = NULL;
-    pkt.size = 0;
-
-    if (ost->frame_number >= ost->max_frames)
-        return;
-
-    if (enc->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) &&
-        ost->top_field_first >= 0)
-        in_picture->top_field_first = !!ost->top_field_first;
-
-    in_picture->quality = enc->global_quality;
-    in_picture->pict_type = 0;
-    if (ost->forced_kf_index < ost->forced_kf_count &&
-        in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
-        in_picture->pict_type = AV_PICTURE_TYPE_I;
-        ost->forced_kf_index++;
-    }
-
-    ost->frames_encoded++;
-
-    ret = avcodec_send_frame(enc, in_picture);
-    if (ret < 0)
-        goto error;
-
-    /*
-     * For video, there may be reordering, so we can't throw away frames on
-     * encoder flush, we need to limit them here, before they go into encoder.
-     */
-    ost->frame_number++;
-
-    while (1) {
-        ret = avcodec_receive_packet(enc, &pkt);
-        if (ret == AVERROR(EAGAIN))
-            break;
-        if (ret < 0)
-            goto error;
-
-        output_packet(of, &pkt, ost);
-        *frame_size = pkt.size;
-
-        /* if two pass, output log */
-        if (ost->logfile && enc->stats_out) {
-            fprintf(ost->logfile, "%s", enc->stats_out);
-        }
-
-        ost->sync_opts++;
-    }
-
-    return;
-error:
-    av_assert0(ret != AVERROR(EAGAIN) && ret != AVERROR_EOF);
-    av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
-    exit_program(1);
-}
-
-#if FF_API_CODED_FRAME && FF_API_ERROR_FRAME
-static double psnr(double d)
-{
-    return -10.0 * log(d) / log(10.0);
-}
-#endif
-
-static void do_video_stats(OutputStream *ost, int frame_size)
-{
-    AVCodecContext *enc;
-    int frame_number;
-    double ti1, bitrate, avg_bitrate;
-
-    /* this is executed just the first time do_video_stats is called */
-    if (!vstats_file) {
-        vstats_file = fopen(vstats_filename, "w");
-        if (!vstats_file) {
-            perror("fopen");
-            exit_program(1);
-        }
-    }
-
-    enc = ost->enc_ctx;
-    if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
-        frame_number = ost->frame_number;
-        fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
-                ost->quality / (float)FF_QP2LAMBDA);
-
-#if FF_API_CODED_FRAME && FF_API_ERROR_FRAME
-FF_DISABLE_DEPRECATION_WARNINGS
-        if (enc->flags & AV_CODEC_FLAG_PSNR)
-            fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
-FF_ENABLE_DEPRECATION_WARNINGS
-#endif
-
-        fprintf(vstats_file,"f_size= %6d ", frame_size);
-        /* compute pts value */
-        ti1 = ost->sync_opts * av_q2d(enc->time_base);
-        if (ti1 < 0.01)
-            ti1 = 0.01;
-
-        bitrate     = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
-        avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
-        fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
-               (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
-#if FF_API_CODED_FRAME
-FF_DISABLE_DEPRECATION_WARNINGS
-        fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type));
-FF_ENABLE_DEPRECATION_WARNINGS
-#endif
-    }
-}
-
-static int init_output_stream(OutputStream *ost, char *error, int error_len);
-
-/*
- * Read one frame for lavfi output for ost and encode it.
- */
-static int poll_filter(OutputStream *ost)
-{
-    OutputFile    *of = output_files[ost->file_index];
-    AVFrame *filtered_frame = NULL;
-    int frame_size, ret;
-
-    if (!ost->filtered_frame && !(ost->filtered_frame = av_frame_alloc())) {
-        return AVERROR(ENOMEM);
-    }
-    filtered_frame = ost->filtered_frame;
-
-    if (!ost->initialized) {
-        char error[1024];
-        ret = init_output_stream(ost, error, sizeof(error));
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_ERROR, "Error initializing output stream %d:%d -- %s\n",
-                   ost->file_index, ost->index, error);
-            exit_program(1);
-        }
-    }
-
-    if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
-        !(ost->enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
-        ret = av_buffersink_get_samples(ost->filter->filter, filtered_frame,
-                                         ost->enc_ctx->frame_size);
-    else
-        ret = av_buffersink_get_frame(ost->filter->filter, filtered_frame);
-
-    if (ret < 0)
-        return ret;
-
-    if (filtered_frame->pts != AV_NOPTS_VALUE) {
-        int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
-        filtered_frame->pts = av_rescale_q(filtered_frame->pts,
-                                           ost->filter->filter->inputs[0]->time_base,
-                                           ost->enc_ctx->time_base) -
-                              av_rescale_q(start_time,
-                                           AV_TIME_BASE_Q,
-                                           ost->enc_ctx->time_base);
-    }
-
-    switch (ost->filter->filter->inputs[0]->type) {
-    case AVMEDIA_TYPE_VIDEO:
-        if (!ost->frame_aspect_ratio)
-            ost->enc_ctx->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
-
-        do_video_out(of, ost, filtered_frame, &frame_size);
-        if (vstats_filename && frame_size)
-            do_video_stats(ost, frame_size);
-        break;
-    case AVMEDIA_TYPE_AUDIO:
-        do_audio_out(of, ost, filtered_frame);
-        break;
-    default:
-        // TODO support subtitle filters
-        av_assert0(0);
-    }
-
-    av_frame_unref(filtered_frame);
-
-    return 0;
-}
-
-static void finish_output_stream(OutputStream *ost)
-{
-    OutputFile *of = output_files[ost->file_index];
-    int i;
-
-    ost->finished = 1;
-
-    if (of->shortest) {
-        for (i = 0; i < of->ctx->nb_streams; i++)
-            output_streams[of->ost_index + i]->finished = 1;
-    }
-}
-
-/*
- * Read as many frames from possible from lavfi and encode them.
- *
- * Always read from the active stream with the lowest timestamp. If no frames
- * are available for it then return EAGAIN and wait for more input. This way we
- * can use lavfi sources that generate unlimited amount of frames without memory
- * usage exploding.
- */
-static int poll_filters(void)
-{
-    int i, ret = 0;
-
-    while (ret >= 0 && !received_sigterm) {
-        OutputStream *ost = NULL;
-        int64_t min_pts = INT64_MAX;
-
-        /* choose output stream with the lowest timestamp */
-        for (i = 0; i < nb_output_streams; i++) {
-            int64_t pts = output_streams[i]->sync_opts;
-
-            if (output_streams[i]->filter && !output_streams[i]->filter->graph->graph &&
-                !output_streams[i]->filter->graph->nb_inputs) {
-                ret = configure_filtergraph(output_streams[i]->filter->graph);
-                if (ret < 0) {
-                    av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
-                    return ret;
-                }
-            }
-
-            if (!output_streams[i]->filter || output_streams[i]->finished ||
-                !output_streams[i]->filter->graph->graph)
-                continue;
-
-            pts = av_rescale_q(pts, output_streams[i]->enc_ctx->time_base,
-                               AV_TIME_BASE_Q);
-            if (pts < min_pts) {
-                min_pts = pts;
-                ost = output_streams[i];
-            }
-        }
-
-        if (!ost)
-            break;
-
-        ret = poll_filter(ost);
-
-        if (ret == AVERROR_EOF) {
-            finish_output_stream(ost);
-            ret = 0;
-        } else if (ret == AVERROR(EAGAIN))
-            return 0;
-    }
-
-    return ret;
-}
-
-static void print_final_stats(int64_t total_size)
-{
-    uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
-    uint64_t data_size = 0;
-    float percent = -1.0;
-    int i, j;
-
-    for (i = 0; i < nb_output_streams; i++) {
-        OutputStream *ost = output_streams[i];
-        switch (ost->enc_ctx->codec_type) {
-            case AVMEDIA_TYPE_VIDEO: video_size += ost->data_size; break;
-            case AVMEDIA_TYPE_AUDIO: audio_size += ost->data_size; break;
-            default:                 other_size += ost->data_size; break;
-        }
-        extra_size += ost->enc_ctx->extradata_size;
-        data_size  += ost->data_size;
-    }
-
-    if (data_size && total_size >= data_size)
-        percent = 100.0 * (total_size - data_size) / data_size;
-
-    av_log(NULL, AV_LOG_INFO, "\n");
-    av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ",
-           video_size / 1024.0,
-           audio_size / 1024.0,
-           other_size / 1024.0,
-           extra_size / 1024.0);
-    if (percent >= 0.0)
-        av_log(NULL, AV_LOG_INFO, "%f%%", percent);
-    else
-        av_log(NULL, AV_LOG_INFO, "unknown");
-    av_log(NULL, AV_LOG_INFO, "\n");
-
-    /* print verbose per-stream stats */
-    for (i = 0; i < nb_input_files; i++) {
-        InputFile *f = input_files[i];
-        uint64_t total_packets = 0, total_size = 0;
-
-        av_log(NULL, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
-               i, f->ctx->filename);
-
-        for (j = 0; j < f->nb_streams; j++) {
-            InputStream *ist = input_streams[f->ist_index + j];
-            enum AVMediaType type = ist->dec_ctx->codec_type;
-
-            total_size    += ist->data_size;
-            total_packets += ist->nb_packets;
-
-            av_log(NULL, AV_LOG_VERBOSE, "  Input stream #%d:%d (%s): ",
-                   i, j, media_type_string(type));
-            av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
-                   ist->nb_packets, ist->data_size);
-
-            if (ist->decoding_needed) {
-                av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames decoded",
-                       ist->frames_decoded);
-                if (type == AVMEDIA_TYPE_AUDIO)
-                    av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
-                av_log(NULL, AV_LOG_VERBOSE, "; ");
-            }
-
-            av_log(NULL, AV_LOG_VERBOSE, "\n");
-        }
-
-        av_log(NULL, AV_LOG_VERBOSE, "  Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
-               total_packets, total_size);
-    }
-
-    for (i = 0; i < nb_output_files; i++) {
-        OutputFile *of = output_files[i];
-        uint64_t total_packets = 0, total_size = 0;
-
-        av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
-               i, of->ctx->filename);
-
-        for (j = 0; j < of->ctx->nb_streams; j++) {
-            OutputStream *ost = output_streams[of->ost_index + j];
-            enum AVMediaType type = ost->enc_ctx->codec_type;
-
-            total_size    += ost->data_size;
-            total_packets += ost->packets_written;
-
-            av_log(NULL, AV_LOG_VERBOSE, "  Output stream #%d:%d (%s): ",
-                   i, j, media_type_string(type));
-            if (ost->encoding_needed) {
-                av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
-                       ost->frames_encoded);
-                if (type == AVMEDIA_TYPE_AUDIO)
-                    av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
-                av_log(NULL, AV_LOG_VERBOSE, "; ");
-            }
-
-            av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
-                   ost->packets_written, ost->data_size);
-
-            av_log(NULL, AV_LOG_VERBOSE, "\n");
-        }
-
-        av_log(NULL, AV_LOG_VERBOSE, "  Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
-               total_packets, total_size);
-    }
-}
-
-static void print_report(int is_last_report, int64_t timer_start)
-{
-    char buf[1024];
-    OutputStream *ost;
-    AVFormatContext *oc;
-    int64_t total_size = 0;
-    AVCodecContext *enc;
-    int frame_number, vid, i;
-    double bitrate, ti1, pts;
-    static int64_t last_time = -1;
-    static int qp_histogram[52];
-
-    if (!print_stats && !is_last_report)
-        return;
-
-    if (!is_last_report) {
-        int64_t cur_time;
-        /* display the report every 0.5 seconds */
-        cur_time = av_gettime_relative();
-        if (last_time == -1) {
-            last_time = cur_time;
-            return;
-        }
-        if ((cur_time - last_time) < 500000)
-            return;
-        last_time = cur_time;
-    }
-
-
-    oc = output_files[0]->ctx;
-    if (oc->pb) {
-        total_size = avio_size(oc->pb);
-        if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too
-            total_size = avio_tell(oc->pb);
-        if (total_size < 0) {
-            char errbuf[128];
-            av_strerror(total_size, errbuf, sizeof(errbuf));
-            av_log(NULL, AV_LOG_VERBOSE, "Bitrate not available, "
-                   "avio_tell() failed: %s\n", errbuf);
-            total_size = 0;
-        }
-    }
-
-    buf[0] = '\0';
-    ti1 = 1e10;
-    vid = 0;
-    for (i = 0; i < nb_output_streams; i++) {
-        float q = -1;
-        ost = output_streams[i];
-        enc = ost->enc_ctx;
-        if (!ost->stream_copy)
-            q = ost->quality / (float) FF_QP2LAMBDA;
-
-        if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
-            snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q);
-        }
-        if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
-            float t = (av_gettime_relative() - timer_start) / 1000000.0;
-
-            frame_number = ost->frame_number;
-            snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3d q=%3.1f ",
-                     frame_number, (t > 1) ? (int)(frame_number / t + 0.5) : 0, q);
-            if (is_last_report)
-                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
-            if (qp_hist) {
-                int j;
-                int qp = lrintf(q);
-                if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
-                    qp_histogram[qp]++;
-                for (j = 0; j < 32; j++)
-                    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log2(qp_histogram[j] + 1)));
-            }
-
-#if FF_API_CODED_FRAME && FF_API_ERROR_FRAME
-FF_DISABLE_DEPRECATION_WARNINGS
-            if (enc->flags & AV_CODEC_FLAG_PSNR) {
-                int j;
-                double error, error_sum = 0;
-                double scale, scale_sum = 0;
-                char type[3] = { 'Y','U','V' };
-                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
-                for (j = 0; j < 3; j++) {
-                    if (is_last_report) {
-                        error = enc->error[j];
-                        scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
-                    } else {
-                        error = enc->coded_frame->error[j];
-                        scale = enc->width * enc->height * 255.0 * 255.0;
-                    }
-                    if (j)
-                        scale /= 4;
-                    error_sum += error;
-                    scale_sum += scale;
-                    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error / scale));
-                }
-                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum / scale_sum));
-            }
-FF_ENABLE_DEPRECATION_WARNINGS
-#endif
-            vid = 1;
-        }
-        /* compute min output value */
-        pts = (double)ost->last_mux_dts * av_q2d(ost->st->time_base);
-        if ((pts < ti1) && (pts > 0))
-            ti1 = pts;
-    }
-    if (ti1 < 0.01)
-        ti1 = 0.01;
-
-    bitrate = (double)(total_size * 8) / ti1 / 1000.0;
-
-    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
-            "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
-            (double)total_size / 1024, ti1, bitrate);
-
-    if (nb_frames_drop)
-        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " drop=%d",
-                 nb_frames_drop);
-
-    av_log(NULL, AV_LOG_INFO, "%s    \r", buf);
-
-    fflush(stderr);
-
-    if (is_last_report)
-        print_final_stats(total_size);
-
-}
-
-static void flush_encoders(void)
-{
-    int i, ret;
-
-    for (i = 0; i < nb_output_streams; i++) {
-        OutputStream   *ost = output_streams[i];
-        AVCodecContext *enc = ost->enc_ctx;
-        OutputFile      *of = output_files[ost->file_index];
-        int stop_encoding = 0;
-
-        if (!ost->encoding_needed)
-            continue;
-
-        if (enc->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
-            continue;
-
-        if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
-            continue;
-
-        avcodec_send_frame(enc, NULL);
-
-        for (;;) {
-            const char *desc = NULL;
-
-            switch (enc->codec_type) {
-            case AVMEDIA_TYPE_AUDIO:
-                desc   = "Audio";
-                break;
-            case AVMEDIA_TYPE_VIDEO:
-                desc   = "Video";
-                break;
-            default:
-                av_assert0(0);
-            }
-
-            if (1) {
-                AVPacket pkt;
-                av_init_packet(&pkt);
-                pkt.data = NULL;
-                pkt.size = 0;
-
-                ret = avcodec_receive_packet(enc, &pkt);
-                if (ret < 0 && ret != AVERROR_EOF) {
-                    av_log(NULL, AV_LOG_FATAL, "%s encoding failed\n", desc);
-                    exit_program(1);
-                }
-                if (ost->logfile && enc->stats_out) {
-                    fprintf(ost->logfile, "%s", enc->stats_out);
-                }
-                if (ret == AVERROR_EOF) {
-                    stop_encoding = 1;
-                    break;
-                }
-                output_packet(of, &pkt, ost);
-            }
-
-            if (stop_encoding)
-                break;
-        }
-    }
-}
-
-/*
- * Check whether a packet from ist should be written into ost at this time
- */
-static int check_output_constraints(InputStream *ist, OutputStream *ost)
-{
-    OutputFile *of = output_files[ost->file_index];
-    int ist_index  = input_files[ist->file_index]->ist_index + ist->st->index;
-
-    if (ost->source_index != ist_index)
-        return 0;
-
-    if (of->start_time != AV_NOPTS_VALUE && ist->last_dts < of->start_time)
-        return 0;
-
-    return 1;
-}
-
-static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
-{
-    OutputFile *of = output_files[ost->file_index];
-    InputFile   *f = input_files [ist->file_index];
-    int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
-    int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase);
-    AVPacket opkt;
-
-    av_init_packet(&opkt);
-
-    if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
-        !ost->copy_initial_nonkeyframes)
-        return;
-
-    if (of->recording_time != INT64_MAX &&
-        ist->last_dts >= of->recording_time + start_time) {
-        ost->finished = 1;
-        return;
-    }
-
-    if (f->recording_time != INT64_MAX) {
-        start_time = f->ctx->start_time;
-        if (f->start_time != AV_NOPTS_VALUE)
-            start_time += f->start_time;
-        if (ist->last_dts >= f->recording_time + start_time) {
-            ost->finished = 1;
-            return;
-        }
-    }
-
-    /* force the input stream PTS */
-    if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
-        ost->sync_opts++;
-
-    if (pkt->pts != AV_NOPTS_VALUE)
-        opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->mux_timebase) - ost_tb_start_time;
-    else
-        opkt.pts = AV_NOPTS_VALUE;
-
-    if (pkt->dts == AV_NOPTS_VALUE)
-        opkt.dts = av_rescale_q(ist->last_dts, AV_TIME_BASE_Q, ost->mux_timebase);
-    else
-        opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->mux_timebase);
-    opkt.dts -= ost_tb_start_time;
-
-    opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->mux_timebase);
-    opkt.flags    = pkt->flags;
-
-    // FIXME remove the following 2 lines they shall be replaced by the bitstream filters
-    if (  ost->enc_ctx->codec_id != AV_CODEC_ID_H264
-       && ost->enc_ctx->codec_id != AV_CODEC_ID_MPEG1VIDEO
-       && ost->enc_ctx->codec_id != AV_CODEC_ID_MPEG2VIDEO
-       && ost->enc_ctx->codec_id != AV_CODEC_ID_VC1
-       ) {
-        if (av_parser_change(ost->parser, ost->parser_avctx,
-                             &opkt.data, &opkt.size,
-                             pkt->data, pkt->size,
-                             pkt->flags & AV_PKT_FLAG_KEY)) {
-            opkt.buf = av_buffer_create(opkt.data, opkt.size, av_buffer_default_free, NULL, 0);
-            if (!opkt.buf)
-                exit_program(1);
-        }
-    } else {
-        opkt.data = pkt->data;
-        opkt.size = pkt->size;
-    }
-
-    output_packet(of, &opkt, ost);
-}
-
-static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame)
-{
-    FilterGraph *fg = ifilter->graph;
-    int need_reinit, ret, i;
-
-    /* determine if the parameters for this input changed */
-    need_reinit = ifilter->format != frame->format;
-    if (!!ifilter->hw_frames_ctx != !!frame->hw_frames_ctx ||
-        (ifilter->hw_frames_ctx && ifilter->hw_frames_ctx->data != frame->hw_frames_ctx->data))
-        need_reinit = 1;
-
-    switch (ifilter->ist->st->codecpar->codec_type) {
-    case AVMEDIA_TYPE_AUDIO:
-        need_reinit |= ifilter->sample_rate    != frame->sample_rate ||
-                       ifilter->channel_layout != frame->channel_layout;
-        break;
-    case AVMEDIA_TYPE_VIDEO:
-        need_reinit |= ifilter->width  != frame->width ||
-                       ifilter->height != frame->height;
-        break;
-    }
-
-    if (need_reinit) {
-        ret = ifilter_parameters_from_frame(ifilter, frame);
-        if (ret < 0)
-            return ret;
-    }
-
-    /* (re)init the graph if possible, otherwise buffer the frame and return */
-    if (need_reinit || !fg->graph) {
-        for (i = 0; i < fg->nb_inputs; i++) {
-            if (fg->inputs[i]->format < 0) {
-                AVFrame *tmp = av_frame_clone(frame);
-                if (!tmp)
-                    return AVERROR(ENOMEM);
-                av_frame_unref(frame);
-
-                if (!av_fifo_space(ifilter->frame_queue)) {
-                    ret = av_fifo_realloc2(ifilter->frame_queue, 2 * av_fifo_size(ifilter->frame_queue));
-                    if (ret < 0)
-                        return ret;
-                }
-                av_fifo_generic_write(ifilter->frame_queue, &tmp, sizeof(tmp), NULL);
-                return 0;
-            }
-        }
-
-        ret = poll_filters();
-        if (ret < 0 && ret != AVERROR_EOF) {
-            char errbuf[128];
-            av_strerror(ret, errbuf, sizeof(errbuf));
-
-            av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", errbuf);
-            return ret;
-        }
-
-        ret = configure_filtergraph(fg);
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
-            return ret;
-        }
-    }
-
-    ret = av_buffersrc_add_frame(ifilter->filter, frame);
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_ERROR, "Error while filtering\n");
-        return ret;
-    }
-
-    return 0;
-}
-
-static int ifilter_send_eof(InputFilter *ifilter)
-{
-    int i, j, ret;
-
-    ifilter->eof = 1;
-
-    if (ifilter->filter) {
-        ret = av_buffersrc_add_frame(ifilter->filter, NULL);
-        if (ret < 0)
-            return ret;
-    } else {
-        // the filtergraph was never configured
-        FilterGraph *fg = ifilter->graph;
-        for (i = 0; i < fg->nb_inputs; i++)
-            if (!fg->inputs[i]->eof)
-                break;
-        if (i == fg->nb_inputs) {
-            // All the input streams have finished without the filtergraph
-            // ever being configured.
-            // Mark the output streams as finished.
-            for (j = 0; j < fg->nb_outputs; j++)
-                finish_output_stream(fg->outputs[j]->ost);
-        }
-    }
-
-    return 0;
-}
-
-// This does not quite work like avcodec_decode_audio4/avcodec_decode_video2.
-// There is the following difference: if you got a frame, you must call
-// it again with pkt=NULL. pkt==NULL is treated differently from pkt.size==0
-// (pkt==NULL means get more output, pkt.size==0 is a flush/drain packet)
-static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
-{
-    int ret;
-
-    *got_frame = 0;
-
-    if (pkt) {
-        ret = avcodec_send_packet(avctx, pkt);
-        // In particular, we don't expect AVERROR(EAGAIN), because we read all
-        // decoded frames with avcodec_receive_frame() until done.
-        if (ret < 0)
-            return ret == AVERROR_EOF ? 0 : ret;
-    }
-
-    ret = avcodec_receive_frame(avctx, frame);
-    if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
-        return ret;
-    if (ret >= 0)
-        *got_frame = 1;
-
-    return 0;
-}
-
-int guess_input_channel_layout(InputStream *ist)
-{
-    AVCodecContext *dec = ist->dec_ctx;
-
-    if (!dec->channel_layout) {
-        char layout_name[256];
-
-        dec->channel_layout = av_get_default_channel_layout(dec->channels);
-        if (!dec->channel_layout)
-            return 0;
-        av_get_channel_layout_string(layout_name, sizeof(layout_name),
-                                     dec->channels, dec->channel_layout);
-        av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for Input Stream "
-               "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
-    }
-    return 1;
-}
-
-static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output,
-                        int *decode_failed)
-{
-    AVFrame *decoded_frame, *f;
-    AVCodecContext *avctx = ist->dec_ctx;
-    int i, ret, err = 0;
-
-    if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
-        return AVERROR(ENOMEM);
-    if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
-        return AVERROR(ENOMEM);
-    decoded_frame = ist->decoded_frame;
-
-    ret = decode(avctx, decoded_frame, got_output, pkt);
-    if (ret < 0)
-        *decode_failed = 1;
-    if (!*got_output || ret < 0)
-        return ret;
-
-    ist->samples_decoded += decoded_frame->nb_samples;
-    ist->frames_decoded++;
-
-    /* if the decoder provides a pts, use it instead of the last packet pts.
-       the decoder could be delaying output by a packet or more. */
-    if (decoded_frame->pts != AV_NOPTS_VALUE)
-        ist->next_dts = av_rescale_q(decoded_frame->pts, ist->st->time_base, AV_TIME_BASE_Q);
-    else if (pkt && pkt->pts != AV_NOPTS_VALUE) {
-        decoded_frame->pts = pkt->pts;
-    }
-
-    if (decoded_frame->pts != AV_NOPTS_VALUE)
-        decoded_frame->pts = av_rescale_q(decoded_frame->pts,
-                                          ist->st->time_base,
-                                          (AVRational){1, avctx->sample_rate});
-    ist->nb_samples = decoded_frame->nb_samples;
-    for (i = 0; i < ist->nb_filters; i++) {
-        if (i < ist->nb_filters - 1) {
-            f = ist->filter_frame;
-            err = av_frame_ref(f, decoded_frame);
-            if (err < 0)
-                break;
-        } else
-            f = decoded_frame;
-
-        err = ifilter_send_frame(ist->filters[i], f);
-        if (err < 0)
-            break;
-    }
-
-    av_frame_unref(ist->filter_frame);
-    av_frame_unref(decoded_frame);
-    return err < 0 ? err : ret;
-}
-
-static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output,
-                        int *decode_failed)
-{
-    AVFrame *decoded_frame, *f;
-    int i, ret = 0, err = 0;
-
-    if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
-        return AVERROR(ENOMEM);
-    if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
-        return AVERROR(ENOMEM);
-    decoded_frame = ist->decoded_frame;
-
-    ret = decode(ist->dec_ctx, decoded_frame, got_output, pkt);
-    if (ret < 0)
-        *decode_failed = 1;
-    if (!*got_output || ret < 0)
-        return ret;
-
-    ist->frames_decoded++;
-
-    if (ist->hwaccel_retrieve_data && decoded_frame->format == ist->hwaccel_pix_fmt) {
-        err = ist->hwaccel_retrieve_data(ist->dec_ctx, decoded_frame);
-        if (err < 0)
-            goto fail;
-    }
-    ist->hwaccel_retrieved_pix_fmt = decoded_frame->format;
-
-    decoded_frame->pts = guess_correct_pts(&ist->pts_ctx, decoded_frame->pts,
-                                           decoded_frame->pkt_dts);
-    if (ist->framerate.num)
-        decoded_frame->pts = ist->cfr_next_pts++;
-
-    if (ist->st->sample_aspect_ratio.num)
-        decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
-
-    for (i = 0; i < ist->nb_filters; i++) {
-        if (i < ist->nb_filters - 1) {
-            f = ist->filter_frame;
-            err = av_frame_ref(f, decoded_frame);
-            if (err < 0)
-                break;
-        } else
-            f = decoded_frame;
-
-        err = ifilter_send_frame(ist->filters[i], f);
-        if (err < 0)
-            break;
-    }
-
-fail:
-    av_frame_unref(ist->filter_frame);
-    av_frame_unref(decoded_frame);
-    return err < 0 ? err : ret;
-}
-
-static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
-                               int *decode_failed)
-{
-    AVSubtitle subtitle;
-    int i, ret = avcodec_decode_subtitle2(ist->dec_ctx,
-                                          &subtitle, got_output, pkt);
-    if (ret < 0) {
-        *decode_failed = 1;
-        return ret;
-    }
-    if (!*got_output)
-        return ret;
-
-    ist->frames_decoded++;
-
-    for (i = 0; i < nb_output_streams; i++) {
-        OutputStream *ost = output_streams[i];
-
-        if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
-            continue;
-
-        do_subtitle_out(output_files[ost->file_index], ost, ist, &subtitle, pkt->pts);
-    }
-
-    avsubtitle_free(&subtitle);
-    return ret;
-}
-
-static int send_filter_eof(InputStream *ist)
-{
-    int i, ret;
-    for (i = 0; i < ist->nb_filters; i++) {
-        ret = ifilter_send_eof(ist->filters[i]);
-        if (ret < 0)
-            return ret;
-    }
-    return 0;
-}
-
-/* pkt = NULL means EOF (needed to flush decoder buffers) */
-static void process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
-{
-    int i;
-    int repeating = 0;
-    AVPacket avpkt;
-
-    if (ist->next_dts == AV_NOPTS_VALUE)
-        ist->next_dts = ist->last_dts;
-
-    if (!pkt) {
-        /* EOF handling */
-        av_init_packet(&avpkt);
-        avpkt.data = NULL;
-        avpkt.size = 0;
-    } else {
-        avpkt = *pkt;
-    }
-
-    if (pkt && pkt->dts != AV_NOPTS_VALUE)
-        ist->next_dts = ist->last_dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
-
-    // while we have more to decode or while the decoder did output something on EOF
-    while (ist->decoding_needed && (!pkt || avpkt.size > 0)) {
-        int ret = 0;
-        int got_output = 0;
-        int decode_failed = 0;
-
-        if (!repeating)
-            ist->last_dts = ist->next_dts;
-
-        switch (ist->dec_ctx->codec_type) {
-        case AVMEDIA_TYPE_AUDIO:
-            ret = decode_audio    (ist, repeating ? NULL : &avpkt, &got_output,
-                                   &decode_failed);
-            break;
-        case AVMEDIA_TYPE_VIDEO:
-            ret = decode_video    (ist, repeating ? NULL : &avpkt, &got_output,
-                                   &decode_failed);
-            if (repeating && !got_output)
-                ;
-            else if (pkt && pkt->duration)
-                ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
-            else if (ist->st->avg_frame_rate.num)
-                ist->next_dts += av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate),
-                                              AV_TIME_BASE_Q);
-            else if (ist->dec_ctx->framerate.num != 0) {
-                int ticks      = ist->st->parser ? ist->st->parser->repeat_pict + 1 :
-                                                   ist->dec_ctx->ticks_per_frame;
-                ist->next_dts += av_rescale_q(ticks, ist->dec_ctx->framerate, AV_TIME_BASE_Q);
-            }
-            break;
-        case AVMEDIA_TYPE_SUBTITLE:
-            if (repeating)
-                break;
-            ret = transcode_subtitles(ist, &avpkt, &got_output, &decode_failed);
-            break;
-        default:
-            return;
-        }
-
-        if (ret < 0) {
-            if (decode_failed) {
-                av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d\n",
-                       ist->file_index, ist->st->index);
-            } else {
-                av_log(NULL, AV_LOG_FATAL, "Error while processing the decoded "
-                       "data for stream #%d:%d\n", ist->file_index, ist->st->index);
-            }
-            if (!decode_failed || exit_on_error)
-                exit_program(1);
-            break;
-        }
-
-        if (!got_output)
-            break;
-
-        repeating = 1;
-    }
-
-    /* after flushing, send an EOF on all the filter inputs attached to the stream */
-    /* except when looping we need to flush but not to send an EOF */
-    if (!pkt && ist->decoding_needed && !no_eof) {
-        int ret = send_filter_eof(ist);
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n");
-            exit_program(1);
-        }
-    }
-
-    /* handle stream copy */
-    if (!ist->decoding_needed) {
-        ist->last_dts = ist->next_dts;
-        switch (ist->dec_ctx->codec_type) {
-        case AVMEDIA_TYPE_AUDIO:
-            ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->frame_size) /
-                             ist->dec_ctx->sample_rate;
-            break;
-        case AVMEDIA_TYPE_VIDEO:
-            if (ist->dec_ctx->framerate.num != 0) {
-                int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame;
-                ist->next_dts += ((int64_t)AV_TIME_BASE *
-                                  ist->dec_ctx->framerate.den * ticks) /
-                                  ist->dec_ctx->framerate.num;
-            }
-            break;
-        }
-    }
-    for (i = 0; pkt && i < nb_output_streams; i++) {
-        OutputStream *ost = output_streams[i];
-
-        if (!check_output_constraints(ist, ost) || ost->encoding_needed)
-            continue;
-
-        do_streamcopy(ist, ost, pkt);
-    }
-
-    return;
-}
-
-static void print_sdp(void)
-{
-    char sdp[16384];
-    int i;
-    AVFormatContext **avc;
-
-    for (i = 0; i < nb_output_files; i++) {
-        if (!output_files[i]->header_written)
-            return;
-    }
-
-    avc = av_malloc(sizeof(*avc) * nb_output_files);
-    if (!avc)
-        exit_program(1);
-    for (i = 0; i < nb_output_files; i++)
-        avc[i] = output_files[i]->ctx;
-
-    av_sdp_create(avc, nb_output_files, sdp, sizeof(sdp));
-    printf("SDP:\n%s\n", sdp);
-    fflush(stdout);
-    av_freep(&avc);
-}
-
-static const HWAccel *get_hwaccel(enum AVPixelFormat pix_fmt)
-{
-    int i;
-    for (i = 0; hwaccels[i].name; i++)
-        if (hwaccels[i].pix_fmt == pix_fmt)
-            return &hwaccels[i];
-    return NULL;
-}
-
-static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
-{
-    InputStream *ist = s->opaque;
-    const enum AVPixelFormat *p;
-    int ret;
-
-    for (p = pix_fmts; *p != -1; p++) {
-        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
-        const HWAccel *hwaccel;
-
-        if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
-            break;
-
-        hwaccel = get_hwaccel(*p);
-        if (!hwaccel ||
-            (ist->active_hwaccel_id && ist->active_hwaccel_id != hwaccel->id) ||
-            (ist->hwaccel_id != HWACCEL_AUTO && ist->hwaccel_id != hwaccel->id))
-            continue;
-
-        ret = hwaccel->init(s);
-        if (ret < 0) {
-            if (ist->hwaccel_id == hwaccel->id) {
-                av_log(NULL, AV_LOG_FATAL,
-                       "%s hwaccel requested for input stream #%d:%d, "
-                       "but cannot be initialized.\n", hwaccel->name,
-                       ist->file_index, ist->st->index);
-                return AV_PIX_FMT_NONE;
-            }
-            continue;
-        }
-
-        if (ist->hw_frames_ctx) {
-            s->hw_frames_ctx = av_buffer_ref(ist->hw_frames_ctx);
-            if (!s->hw_frames_ctx)
-                return AV_PIX_FMT_NONE;
-        }
-
-        ist->active_hwaccel_id = hwaccel->id;
-        ist->hwaccel_pix_fmt   = *p;
-        break;
-    }
-
-    return *p;
-}
-
-static int get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
-{
-    InputStream *ist = s->opaque;
-
-    if (ist->hwaccel_get_buffer && frame->format == ist->hwaccel_pix_fmt)
-        return ist->hwaccel_get_buffer(s, frame, flags);
-
-    return avcodec_default_get_buffer2(s, frame, flags);
-}
-
-static int init_input_stream(int ist_index, char *error, int error_len)
-{
-    int ret;
-    InputStream *ist = input_streams[ist_index];
-
-    if (ist->decoding_needed) {
-        AVCodec *codec = ist->dec;
-        if (!codec) {
-            snprintf(error, error_len, "Decoder (codec id %d) not found for input stream #%d:%d",
-                    ist->dec_ctx->codec_id, ist->file_index, ist->st->index);
-            return AVERROR(EINVAL);
-        }
-
-        ist->dec_ctx->opaque                = ist;
-        ist->dec_ctx->get_format            = get_format;
-        ist->dec_ctx->get_buffer2           = get_buffer;
-        ist->dec_ctx->thread_safe_callbacks = 1;
-
-        av_opt_set_int(ist->dec_ctx, "refcounted_frames", 1, 0);
-
-        if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
-            av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
-        if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
-            char errbuf[128];
-            if (ret == AVERROR_EXPERIMENTAL)
-                abort_codec_experimental(codec, 0);
-
-            av_strerror(ret, errbuf, sizeof(errbuf));
-
-            snprintf(error, error_len,
-                     "Error while opening decoder for input stream "
-                     "#%d:%d : %s",
-                     ist->file_index, ist->st->index, errbuf);
-            return ret;
-        }
-        assert_avoptions(ist->decoder_opts);
-    }
-
-    ist->last_dts = ist->st->avg_frame_rate.num ? - ist->dec_ctx->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
-    ist->next_dts = AV_NOPTS_VALUE;
-    init_pts_correction(&ist->pts_ctx);
-
-    return 0;
-}
-
-static InputStream *get_input_stream(OutputStream *ost)
-{
-    if (ost->source_index >= 0)
-        return input_streams[ost->source_index];
-
-    if (ost->filter) {
-        FilterGraph *fg = ost->filter->graph;
-        int i;
-
-        for (i = 0; i < fg->nb_inputs; i++)
-            if (fg->inputs[i]->ist->dec_ctx->codec_type == ost->enc_ctx->codec_type)
-                return fg->inputs[i]->ist;
-    }
-
-    return NULL;
-}
-
-/* open the muxer when all the streams are initialized */
-static int check_init_output_file(OutputFile *of, int file_index)
-{
-    int ret, i;
-
-    for (i = 0; i < of->ctx->nb_streams; i++) {
-        OutputStream *ost = output_streams[of->ost_index + i];
-        if (!ost->initialized)
-            return 0;
-    }
-
-    of->ctx->interrupt_callback = int_cb;
-
-    ret = avformat_write_header(of->ctx, &of->opts);
-    if (ret < 0) {
-        char errbuf[128];
-
-        av_strerror(ret, errbuf, sizeof(errbuf));
-
-        av_log(NULL, AV_LOG_ERROR,
-               "Could not write header for output file #%d "
-               "(incorrect codec parameters ?): %s",
-               file_index, errbuf);
-        return ret;
-    }
-    assert_avoptions(of->opts);
-    of->header_written = 1;
-
-    av_dump_format(of->ctx, file_index, of->ctx->filename, 1);
-
-    if (want_sdp)
-        print_sdp();
-
-    /* flush the muxing queues */
-    for (i = 0; i < of->ctx->nb_streams; i++) {
-        OutputStream *ost = output_streams[of->ost_index + i];
-
-        while (av_fifo_size(ost->muxing_queue)) {
-            AVPacket pkt;
-            av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL);
-            write_packet(of, &pkt, ost);
-        }
-    }
-
-    return 0;
-}
-
-static int init_output_bsfs(OutputStream *ost)
-{
-    AVBSFContext *ctx;
-    int i, ret;
-
-    if (!ost->nb_bitstream_filters)
-        return 0;
-
-    for (i = 0; i < ost->nb_bitstream_filters; i++) {
-        ctx = ost->bsf_ctx[i];
-
-        ret = avcodec_parameters_copy(ctx->par_in,
-                                      i ? ost->bsf_ctx[i - 1]->par_out : ost->st->codecpar);
-        if (ret < 0)
-            return ret;
-
-        ctx->time_base_in = i ? ost->bsf_ctx[i - 1]->time_base_out : ost->st->time_base;
-
-        ret = av_bsf_init(ctx);
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
-                   ctx->filter->name);
-            return ret;
-        }
-    }
-
-    ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
-    if (ret < 0)
-        return ret;
-
-    ost->st->time_base = ctx->time_base_out;
-
-    return 0;
-}
-
-static int init_output_stream_streamcopy(OutputStream *ost)
-{
-    OutputFile *of = output_files[ost->file_index];
-    InputStream *ist = get_input_stream(ost);
-    AVCodecParameters *par_dst = ost->st->codecpar;
-    AVCodecParameters *par_src = ist->st->codecpar;
-    AVRational sar;
-    uint32_t codec_tag = par_dst->codec_tag;
-    int i, ret;
-
-    if (!codec_tag) {
-        if (!of->ctx->oformat->codec_tag ||
-             av_codec_get_id (of->ctx->oformat->codec_tag, par_src->codec_tag) == par_src->codec_id ||
-             av_codec_get_tag(of->ctx->oformat->codec_tag, par_src->codec_id) <= 0)
-            codec_tag = par_src->codec_tag;
-    }
-
-    ret = avcodec_parameters_copy(par_dst, par_src);
-    if (ret < 0)
-        return ret;
-
-    par_dst->codec_tag = codec_tag;
-
-    ost->st->disposition = ist->st->disposition;
-
-    ost->st->time_base = ist->st->time_base;
-
-    if (ost->bitrate_override)
-        par_dst->bit_rate = ost->bitrate_override;
-
-    if (ist->st->nb_side_data) {
-        ost->st->side_data = av_realloc_array(NULL, ist->st->nb_side_data,
-                                              sizeof(*ist->st->side_data));
-        if (!ost->st->side_data)
-            return AVERROR(ENOMEM);
-
-        for (i = 0; i < ist->st->nb_side_data; i++) {
-            const AVPacketSideData *sd_src = &ist->st->side_data[i];
-            AVPacketSideData *sd_dst = &ost->st->side_data[i];
-
-            sd_dst->data = av_malloc(sd_src->size);
-            if (!sd_dst->data)
-                return AVERROR(ENOMEM);
-            memcpy(sd_dst->data, sd_src->data, sd_src->size);
-            sd_dst->size = sd_src->size;
-            sd_dst->type = sd_src->type;
-            ost->st->nb_side_data++;
-        }
-    }
-
-    ost->parser = av_parser_init(par_dst->codec_id);
-    ost->parser_avctx = avcodec_alloc_context3(NULL);
-    if (!ost->parser_avctx)
-        return AVERROR(ENOMEM);
-
-    if (par_dst->codec_type == AVMEDIA_TYPE_VIDEO) {
-        if (ost->frame_aspect_ratio)
-            sar = av_d2q(ost->frame_aspect_ratio * par_dst->height / par_dst->width, 255);
-        else if (ist->st->sample_aspect_ratio.num)
-            sar = ist->st->sample_aspect_ratio;
-        else
-            sar = par_src->sample_aspect_ratio;
-        ost->st->sample_aspect_ratio = par_dst->sample_aspect_ratio = sar;
-    }
-
-    return 0;
-}
-
-static void set_encoder_id(OutputFile *of, OutputStream *ost)
-{
-    AVDictionaryEntry *e;
-
-    uint8_t *encoder_string;
-    int encoder_string_len;
-    int format_flags = 0;
-
-    e = av_dict_get(of->opts, "fflags", NULL, 0);
-    if (e) {
-        const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0);
-        if (!o)
-            return;
-        av_opt_eval_flags(of->ctx, o, e->value, &format_flags);
-    }
-
-    encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2;
-    encoder_string     = av_mallocz(encoder_string_len);
-    if (!encoder_string)
-        exit_program(1);
-
-    if (!(format_flags & AVFMT_FLAG_BITEXACT))
-        av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
-    av_strlcat(encoder_string, ost->enc->name, encoder_string_len);
-    av_dict_set(&ost->st->metadata, "encoder",  encoder_string,
-                AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
-}
-
-static void parse_forced_key_frames(char *kf, OutputStream *ost,
-                                    AVCodecContext *avctx)
-{
-    char *p;
-    int n = 1, i;
-    int64_t t;
-
-    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);
-    }
-
-    p = kf;
-    for (i = 0; i < n; i++) {
-        char *next = strchr(p, ',');
-
-        if (next)
-            *next++ = 0;
-
-        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);
-
-        p = next;
-    }
-}
-
-static int init_output_stream_encode(OutputStream *ost)
-{
-    InputStream *ist = get_input_stream(ost);
-    AVCodecContext *enc_ctx = ost->enc_ctx;
-    AVCodecContext *dec_ctx = NULL;
-
-    set_encoder_id(output_files[ost->file_index], ost);
-
-    if (ist) {
-        ost->st->disposition = ist->st->disposition;
-
-        dec_ctx = ist->dec_ctx;
-
-        enc_ctx->bits_per_raw_sample    = dec_ctx->bits_per_raw_sample;
-        enc_ctx->chroma_sample_location = dec_ctx->chroma_sample_location;
-    }
-
-    switch (enc_ctx->codec_type) {
-    case AVMEDIA_TYPE_AUDIO:
-        enc_ctx->sample_fmt     = ost->filter->filter->inputs[0]->format;
-        enc_ctx->sample_rate    = ost->filter->filter->inputs[0]->sample_rate;
-        enc_ctx->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
-        enc_ctx->channels       = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);
-        enc_ctx->time_base      = (AVRational){ 1, enc_ctx->sample_rate };
-        break;
-    case AVMEDIA_TYPE_VIDEO:
-        enc_ctx->time_base = ost->filter->filter->inputs[0]->time_base;
-
-        enc_ctx->width  = ost->filter->filter->inputs[0]->w;
-        enc_ctx->height = ost->filter->filter->inputs[0]->h;
-        enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
-            ost->frame_aspect_ratio ? // overridden by the -aspect cli option
-            av_d2q(ost->frame_aspect_ratio * enc_ctx->height/enc_ctx->width, 255) :
-            ost->filter->filter->inputs[0]->sample_aspect_ratio;
-        enc_ctx->pix_fmt = ost->filter->filter->inputs[0]->format;
-
-        enc_ctx->framerate = ost->frame_rate;
-
-        ost->st->avg_frame_rate = ost->frame_rate;
-
-        if (dec_ctx &&
-            (enc_ctx->width   != dec_ctx->width  ||
-             enc_ctx->height  != dec_ctx->height ||
-             enc_ctx->pix_fmt != dec_ctx->pix_fmt)) {
-            enc_ctx->bits_per_raw_sample = 0;
-        }
-
-        if (ost->forced_keyframes)
-            parse_forced_key_frames(ost->forced_keyframes, ost,
-                                    ost->enc_ctx);
-        break;
-    case AVMEDIA_TYPE_SUBTITLE:
-        enc_ctx->time_base = (AVRational){1, 1000};
-        break;
-    default:
-        abort();
-        break;
-    }
-
-    return 0;
-}
-
-static int init_output_stream(OutputStream *ost, char *error, int error_len)
-{
-    int ret = 0;
-
-    if (ost->encoding_needed) {
-        AVCodec      *codec = ost->enc;
-        AVCodecContext *dec = NULL;
-        InputStream *ist;
-
-        ret = init_output_stream_encode(ost);
-        if (ret < 0)
-            return ret;
-
-        if ((ist = get_input_stream(ost)))
-            dec = ist->dec_ctx;
-        if (dec && dec->subtitle_header) {
-            ost->enc_ctx->subtitle_header = av_malloc(dec->subtitle_header_size);
-            if (!ost->enc_ctx->subtitle_header)
-                return AVERROR(ENOMEM);
-            memcpy(ost->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
-            ost->enc_ctx->subtitle_header_size = dec->subtitle_header_size;
-        }
-        if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
-            av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
-
-        if (ost->filter && ost->filter->filter->inputs[0]->hw_frames_ctx &&
-            ((AVHWFramesContext*)ost->filter->filter->inputs[0]->hw_frames_ctx->data)->format ==
-            ost->filter->filter->inputs[0]->format) {
-            ost->enc_ctx->hw_frames_ctx = av_buffer_ref(ost->filter->filter->inputs[0]->hw_frames_ctx);
-            if (!ost->enc_ctx->hw_frames_ctx)
-                return AVERROR(ENOMEM);
-        }
-
-        if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {
-            if (ret == AVERROR_EXPERIMENTAL)
-                abort_codec_experimental(codec, 1);
-            snprintf(error, error_len,
-                     "Error while opening encoder for output stream #%d:%d - "
-                     "maybe incorrect parameters such as bit_rate, rate, width or height",
-                    ost->file_index, ost->index);
-            return ret;
-        }
-        assert_avoptions(ost->encoder_opts);
-        if (ost->enc_ctx->bit_rate && ost->enc_ctx->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");
-
-        ret = avcodec_parameters_from_context(ost->st->codecpar, ost->enc_ctx);
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_FATAL,
-                   "Error initializing the output stream codec context.\n");
-            exit_program(1);
-        }
-
-        if (ost->enc_ctx->nb_coded_side_data) {
-            int i;
-
-            ost->st->side_data = av_realloc_array(NULL, ost->enc_ctx->nb_coded_side_data,
-                                                  sizeof(*ost->st->side_data));
-            if (!ost->st->side_data)
-                return AVERROR(ENOMEM);
-
-            for (i = 0; i < ost->enc_ctx->nb_coded_side_data; i++) {
-                const AVPacketSideData *sd_src = &ost->enc_ctx->coded_side_data[i];
-                AVPacketSideData *sd_dst = &ost->st->side_data[i];
-
-                sd_dst->data = av_malloc(sd_src->size);
-                if (!sd_dst->data)
-                    return AVERROR(ENOMEM);
-                memcpy(sd_dst->data, sd_src->data, sd_src->size);
-                sd_dst->size = sd_src->size;
-                sd_dst->type = sd_src->type;
-                ost->st->nb_side_data++;
-            }
-        }
-
-        ost->st->time_base = ost->enc_ctx->time_base;
-    } else if (ost->stream_copy) {
-        ret = init_output_stream_streamcopy(ost);
-        if (ret < 0)
-            return ret;
-
-        /*
-         * FIXME: will the codec context used by the parser during streamcopy
-         * This should go away with the new parser API.
-         */
-        ret = avcodec_parameters_to_context(ost->parser_avctx, ost->st->codecpar);
-        if (ret < 0)
-            return ret;
-    }
-
-    /* initialize bitstream filters for the output stream
-     * needs to be done here, because the codec id for streamcopy is not
-     * known until now */
-    ret = init_output_bsfs(ost);
-    if (ret < 0)
-        return ret;
-
-    ost->mux_timebase = ost->st->time_base;
-
-    ost->initialized = 1;
-
-    ret = check_init_output_file(output_files[ost->file_index], ost->file_index);
-    if (ret < 0)
-        return ret;
-
-    return ret;
-}
-
-static int transcode_init(void)
-{
-    int ret = 0, i, j, k;
-    OutputStream *ost;
-    InputStream *ist;
-    char error[1024];
-
-    /* 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_relative();
-    }
-
-    /* init input streams */
-    for (i = 0; i < nb_input_streams; i++)
-        if ((ret = init_input_stream(i, error, sizeof(error))) < 0)
-            goto dump_format;
-
-    /* open each encoder */
-    for (i = 0; i < nb_output_streams; i++) {
-        // skip streams fed from filtergraphs until we have a frame for them
-        if (output_streams[i]->filter)
-            continue;
-
-        ret = init_output_stream(output_streams[i], error, sizeof(error));
-        if (ret < 0)
-            goto dump_format;
-    }
-
-
-    /* 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 (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;
-        }
-    }
-
- dump_format:
-    /* dump the stream mapping */
-    av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
-    for (i = 0; i < nb_input_streams; i++) {
-        ist = input_streams[i];
-
-        for (j = 0; j < ist->nb_filters; j++) {
-            if (!filtergraph_is_simple(ist->filters[j]->graph)) {
-                av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d (%s) -> %s",
-                       ist->file_index, ist->st->index, ist->dec ? ist->dec->name : "?",
-                       ist->filters[j]->name);
-                if (nb_filtergraphs > 1)
-                    av_log(NULL, AV_LOG_INFO, " (graph %d)", ist->filters[j]->graph->index);
-                av_log(NULL, AV_LOG_INFO, "\n");
-            }
-        }
-    }
-
-    for (i = 0; i < nb_output_streams; i++) {
-        ost = output_streams[i];
-
-        if (ost->attachment_filename) {
-            /* an attached file */
-            av_log(NULL, AV_LOG_INFO, "  File %s -> Stream #%d:%d\n",
-                   ost->attachment_filename, ost->file_index, ost->index);
-            continue;
-        }
-
-        if (ost->filter && !filtergraph_is_simple(ost->filter->graph)) {
-            /* output from a complex graph */
-            av_log(NULL, AV_LOG_INFO, "  %s", ost->filter->name);
-            if (nb_filtergraphs > 1)
-                av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);
-
-            av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index,
-                   ost->index, ost->enc ? ost->enc->name : "?");
-            continue;
-        }
-
-        av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d -> #%d:%d",
-               input_streams[ost->source_index]->file_index,
-               input_streams[ost->source_index]->st->index,
-               ost->file_index,
-               ost->index);
-        if (ost->sync_ist != input_streams[ost->source_index])
-            av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]",
-                   ost->sync_ist->file_index,
-                   ost->sync_ist->st->index);
-        if (ost->stream_copy)
-            av_log(NULL, AV_LOG_INFO, " (copy)");
-        else {
-            const AVCodec *in_codec    = input_streams[ost->source_index]->dec;
-            const AVCodec *out_codec   = ost->enc;
-            const char *decoder_name   = "?";
-            const char *in_codec_name  = "?";
-            const char *encoder_name   = "?";
-            const char *out_codec_name = "?";
-            const AVCodecDescriptor *desc;
-
-            if (in_codec) {
-                decoder_name  = in_codec->name;
-                desc = avcodec_descriptor_get(in_codec->id);
-                if (desc)
-                    in_codec_name = desc->name;
-                if (!strcmp(decoder_name, in_codec_name))
-                    decoder_name = "native";
-            }
-
-            if (out_codec) {
-                encoder_name   = out_codec->name;
-                desc = avcodec_descriptor_get(out_codec->id);
-                if (desc)
-                    out_codec_name = desc->name;
-                if (!strcmp(encoder_name, out_codec_name))
-                    encoder_name = "native";
-            }
-
-            av_log(NULL, AV_LOG_INFO, " (%s (%s) -> %s (%s))",
-                   in_codec_name, decoder_name,
-                   out_codec_name, encoder_name);
-        }
-        av_log(NULL, AV_LOG_INFO, "\n");
-    }
-
-    if (ret) {
-        av_log(NULL, AV_LOG_ERROR, "%s\n", error);
-        return ret;
-    }
-
-    return 0;
-}
-
-/* Return 1 if there remain streams where more output is wanted, 0 otherwise. */
-static int need_output(void)
-{
-    int i;
-
-    for (i = 0; i < nb_output_streams; i++) {
-        OutputStream *ost    = output_streams[i];
-        OutputFile *of       = output_files[ost->file_index];
-        AVFormatContext *os  = output_files[ost->file_index]->ctx;
-
-        if (ost->finished ||
-            (os->pb && avio_tell(os->pb) >= of->limit_filesize))
-            continue;
-        if (ost->frame_number >= ost->max_frames) {
-            int j;
-            for (j = 0; j < of->ctx->nb_streams; j++)
-                output_streams[of->ost_index + j]->finished = 1;
-            continue;
-        }
-
-        return 1;
-    }
-
-    return 0;
-}
-
-static InputFile *select_input_file(void)
-{
-    InputFile *ifile = NULL;
-    int64_t ipts_min = INT64_MAX;
-    int i;
-
-    for (i = 0; i < nb_input_streams; i++) {
-        InputStream *ist = input_streams[i];
-        int64_t ipts     = ist->last_dts;
-
-        if (ist->discard || input_files[ist->file_index]->eagain)
-            continue;
-        if (!input_files[ist->file_index]->eof_reached) {
-            if (ipts < ipts_min) {
-                ipts_min = ipts;
-                ifile    = input_files[ist->file_index];
-            }
-        }
-    }
-
-    return ifile;
-}
-
-#if HAVE_PTHREADS
-static void *input_thread(void *arg)
-{
-    InputFile *f = arg;
-    int ret = 0;
-
-    while (!transcoding_finished && ret >= 0) {
-        AVPacket pkt;
-        ret = av_read_frame(f->ctx, &pkt);
-
-        if (ret == AVERROR(EAGAIN)) {
-            av_usleep(10000);
-            ret = 0;
-            continue;
-        } else if (ret < 0)
-            break;
-
-        pthread_mutex_lock(&f->fifo_lock);
-        while (!av_fifo_space(f->fifo))
-            pthread_cond_wait(&f->fifo_cond, &f->fifo_lock);
-
-        av_fifo_generic_write(f->fifo, &pkt, sizeof(pkt), NULL);
-
-        pthread_mutex_unlock(&f->fifo_lock);
-    }
-
-    f->finished = 1;
-    return NULL;
-}
-
-static void free_input_threads(void)
-{
-    int i;
-
-    if (nb_input_files == 1)
-        return;
-
-    transcoding_finished = 1;
-
-    for (i = 0; i < nb_input_files; i++) {
-        InputFile *f = input_files[i];
-        AVPacket pkt;
-
-        if (!f->fifo || f->joined)
-            continue;
-
-        pthread_mutex_lock(&f->fifo_lock);
-        while (av_fifo_size(f->fifo)) {
-            av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL);
-            av_packet_unref(&pkt);
-        }
-        pthread_cond_signal(&f->fifo_cond);
-        pthread_mutex_unlock(&f->fifo_lock);
-
-        pthread_join(f->thread, NULL);
-        f->joined = 1;
-
-        while (av_fifo_size(f->fifo)) {
-            av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL);
-            av_packet_unref(&pkt);
-        }
-        av_fifo_free(f->fifo);
-    }
-}
-
-static int init_input_threads(void)
-{
-    int i, ret;
-
-    if (nb_input_files == 1)
-        return 0;
-
-    for (i = 0; i < nb_input_files; i++) {
-        InputFile *f = input_files[i];
-
-        if (!(f->fifo = av_fifo_alloc(8*sizeof(AVPacket))))
-            return AVERROR(ENOMEM);
-
-        pthread_mutex_init(&f->fifo_lock, NULL);
-        pthread_cond_init (&f->fifo_cond, NULL);
-
-        if ((ret = pthread_create(&f->thread, NULL, input_thread, f)))
-            return AVERROR(ret);
-    }
-    return 0;
-}
-
-static int get_input_packet_mt(InputFile *f, AVPacket *pkt)
-{
-    int ret = 0;
-
-    pthread_mutex_lock(&f->fifo_lock);
-
-    if (av_fifo_size(f->fifo)) {
-        av_fifo_generic_read(f->fifo, pkt, sizeof(*pkt), NULL);
-        pthread_cond_signal(&f->fifo_cond);
-    } else {
-        if (f->finished)
-            ret = AVERROR_EOF;
-        else
-            ret = AVERROR(EAGAIN);
-    }
-
-    pthread_mutex_unlock(&f->fifo_lock);
-
-    return ret;
-}
-#endif
-
-static int get_input_packet(InputFile *f, AVPacket *pkt)
-{
-    if (f->rate_emu) {
-        int i;
-        for (i = 0; i < f->nb_streams; i++) {
-            InputStream *ist = input_streams[f->ist_index + i];
-            int64_t pts = av_rescale(ist->last_dts, 1000000, AV_TIME_BASE);
-            int64_t now = av_gettime_relative() - ist->start;
-            if (pts > now)
-                return AVERROR(EAGAIN);
-        }
-    }
-
-#if HAVE_PTHREADS
-    if (nb_input_files > 1)
-        return get_input_packet_mt(f, pkt);
-#endif
-    return av_read_frame(f->ctx, pkt);
-}
-
-static int got_eagain(void)
-{
-    int i;
-    for (i = 0; i < nb_input_files; i++)
-        if (input_files[i]->eagain)
-            return 1;
-    return 0;
-}
-
-static void reset_eagain(void)
-{
-    int i;
-    for (i = 0; i < nb_input_files; i++)
-        input_files[i]->eagain = 0;
-}
-
-// set duration to max(tmp, duration) in a proper time base and return duration's time_base
-static AVRational duration_max(int64_t tmp, int64_t *duration, AVRational tmp_time_base,
-                                AVRational time_base)
-{
-    int ret;
-
-    if (!*duration) {
-        *duration = tmp;
-        return tmp_time_base;
-    }
-
-    ret = av_compare_ts(*duration, time_base, tmp, tmp_time_base);
-    if (ret < 0) {
-        *duration = tmp;
-        return tmp_time_base;
-    }
-
-    return time_base;
-}
-
-static int seek_to_start(InputFile *ifile, AVFormatContext *is)
-{
-    InputStream *ist;
-    AVCodecContext *avctx;
-    int i, ret, has_audio = 0;
-    int64_t duration = 0;
-
-    ret = av_seek_frame(is, -1, is->start_time, 0);
-    if (ret < 0)
-        return ret;
-
-    for (i = 0; i < ifile->nb_streams; i++) {
-        ist   = input_streams[ifile->ist_index + i];
-        avctx = ist->dec_ctx;
-
-        // flush decoders
-        if (ist->decoding_needed) {
-            process_input_packet(ist, NULL, 1);
-            avcodec_flush_buffers(avctx);
-        }
-
-        /* duration is the length of the last frame in a stream
-         * when audio stream is present we don't care about
-         * last video frame length because it's not defined exactly */
-        if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->nb_samples)
-            has_audio = 1;
-    }
-
-    for (i = 0; i < ifile->nb_streams; i++) {
-        ist   = input_streams[ifile->ist_index + i];
-        avctx = ist->dec_ctx;
-
-        if (has_audio) {
-            if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->nb_samples) {
-                AVRational sample_rate = {1, avctx->sample_rate};
-
-                duration = av_rescale_q(ist->nb_samples, sample_rate, ist->st->time_base);
-            } else
-                continue;
-        } else {
-            if (ist->framerate.num) {
-                duration = av_rescale_q(1, ist->framerate, ist->st->time_base);
-            } else if (ist->st->avg_frame_rate.num) {
-                duration = av_rescale_q(1, ist->st->avg_frame_rate, ist->st->time_base);
-            } else duration = 1;
-        }
-        if (!ifile->duration)
-            ifile->time_base = ist->st->time_base;
-        /* the total duration of the stream, max_pts - min_pts is
-         * the duration of the stream without the last frame */
-        duration += ist->max_pts - ist->min_pts;
-        ifile->time_base = duration_max(duration, &ifile->duration, ist->st->time_base,
-                                        ifile->time_base);
-    }
-
-    if (ifile->loop > 0)
-        ifile->loop--;
-
-    return ret;
-}
-
-/*
- * Read one packet from an input file and send it for
- * - decoding -> lavfi (audio/video)
- * - decoding -> encoding -> muxing (subtitles)
- * - muxing (streamcopy)
- *
- * Return
- * - 0 -- one packet was read and processed
- * - AVERROR(EAGAIN) -- no packets were available for selected file,
- *   this function should be called again
- * - AVERROR_EOF -- this function should not be called again
- */
-static int process_input(void)
-{
-    InputFile *ifile;
-    AVFormatContext *is;
-    InputStream *ist;
-    AVPacket pkt;
-    int ret, i, j;
-    int64_t duration;
-
-    /* select the stream that we must read now */
-    ifile = select_input_file();
-    /* if none, if is finished */
-    if (!ifile) {
-        if (got_eagain()) {
-            reset_eagain();
-            av_usleep(10000);
-            return AVERROR(EAGAIN);
-        }
-        av_log(NULL, AV_LOG_VERBOSE, "No more inputs to read from.\n");
-        return AVERROR_EOF;
-    }
-
-    is  = ifile->ctx;
-    ret = get_input_packet(ifile, &pkt);
-
-    if (ret == AVERROR(EAGAIN)) {
-        ifile->eagain = 1;
-        return ret;
-    }
-    if (ret < 0 && ifile->loop) {
-        if ((ret = seek_to_start(ifile, is)) < 0)
-            return ret;
-        ret = get_input_packet(ifile, &pkt);
-    }
-    if (ret < 0) {
-        if (ret != AVERROR_EOF) {
-            print_error(is->filename, ret);
-            if (exit_on_error)
-                exit_program(1);
-        }
-        ifile->eof_reached = 1;
-
-        for (i = 0; i < ifile->nb_streams; i++) {
-            ist = input_streams[ifile->ist_index + i];
-            if (ist->decoding_needed)
-                process_input_packet(ist, NULL, 0);
-
-            /* mark all outputs that don't go through lavfi as finished */
-            for (j = 0; j < nb_output_streams; j++) {
-                OutputStream *ost = output_streams[j];
-
-                if (ost->source_index == ifile->ist_index + i &&
-                    (ost->stream_copy || ost->enc->type == AVMEDIA_TYPE_SUBTITLE))
-                    finish_output_stream(ost);
-            }
-        }
-
-        return AVERROR(EAGAIN);
-    }
-
-    reset_eagain();
-
-    if (do_pkt_dump) {
-        av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump,
-                         is->streams[pkt.stream_index]);
-    }
-    /* the following test is needed in case new streams appear
-       dynamically in stream : we ignore them */
-    if (pkt.stream_index >= ifile->nb_streams)
-        goto discard_packet;
-
-    ist = input_streams[ifile->ist_index + pkt.stream_index];
-
-    ist->data_size += pkt.size;
-    ist->nb_packets++;
-
-    if (ist->discard)
-        goto discard_packet;
-
-    /* add the stream-global side data to the first packet */
-    if (ist->nb_packets == 1)
-        for (i = 0; i < ist->st->nb_side_data; i++) {
-            AVPacketSideData *src_sd = &ist->st->side_data[i];
-            uint8_t *dst_data;
-
-            if (av_packet_get_side_data(&pkt, src_sd->type, NULL))
-                continue;
-            if (ist->autorotate && src_sd->type == AV_PKT_DATA_DISPLAYMATRIX)
-                continue;
-
-            dst_data = av_packet_new_side_data(&pkt, src_sd->type, src_sd->size);
-            if (!dst_data)
-                exit_program(1);
-
-            memcpy(dst_data, src_sd->data, src_sd->size);
-        }
-
-    if (pkt.dts != AV_NOPTS_VALUE)
-        pkt.dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
-    if (pkt.pts != AV_NOPTS_VALUE)
-        pkt.pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
-
-    if (pkt.pts != AV_NOPTS_VALUE)
-        pkt.pts *= ist->ts_scale;
-    if (pkt.dts != AV_NOPTS_VALUE)
-        pkt.dts *= ist->ts_scale;
-
-    if ((ist->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
-         ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) &&
-        pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE &&
-        (is->iformat->flags & AVFMT_TS_DISCONT)) {
-        int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
-        int64_t delta   = pkt_dts - ist->next_dts;
-
-        if ((FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE || pkt_dts + 1 < ist->last_dts) && !copy_ts) {
-            ifile->ts_offset -= delta;
-            av_log(NULL, AV_LOG_DEBUG,
-                   "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
-                   delta, ifile->ts_offset);
-            pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
-            if (pkt.pts != AV_NOPTS_VALUE)
-                pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
-        }
-    }
-    duration = av_rescale_q(ifile->duration, ifile->time_base, ist->st->time_base);
-    if (pkt.pts != AV_NOPTS_VALUE) {
-        pkt.pts += duration;
-        ist->max_pts = FFMAX(pkt.pts, ist->max_pts);
-        ist->min_pts = FFMIN(pkt.pts, ist->min_pts);
-    }
-
-    if (pkt.dts != AV_NOPTS_VALUE)
-        pkt.dts += duration;
-
-    process_input_packet(ist, &pkt, 0);
-
-discard_packet:
-    av_packet_unref(&pkt);
-
-    return 0;
-}
-
-/*
- * The following code is the main loop of the file converter
- */
-static int transcode(void)
-{
-    int ret, i, need_input = 1;
-    AVFormatContext *os;
-    OutputStream *ost;
-    InputStream *ist;
-    int64_t timer_start;
-
-    ret = transcode_init();
-    if (ret < 0)
-        goto fail;
-
-    av_log(NULL, AV_LOG_INFO, "Press ctrl-c to stop encoding\n");
-    term_init();
-
-    timer_start = av_gettime_relative();
-
-#if HAVE_PTHREADS
-    if ((ret = init_input_threads()) < 0)
-        goto fail;
-#endif
-
-    while (!received_sigterm) {
-        /* check if there's any stream where output is still needed */
-        if (!need_output()) {
-            av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");
-            break;
-        }
-
-        /* read and process one input packet if needed */
-        if (need_input) {
-            ret = process_input();
-            if (ret == AVERROR_EOF)
-                need_input = 0;
-        }
-
-        ret = poll_filters();
-        if (ret < 0 && ret != AVERROR_EOF) {
-            char errbuf[128];
-            av_strerror(ret, errbuf, sizeof(errbuf));
-
-            av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", errbuf);
-            break;
-        }
-
-        /* dump report by using the output first video and audio streams */
-        print_report(0, timer_start);
-    }
-#if HAVE_PTHREADS
-    free_input_threads();
-#endif
-
-    /* at the end of stream, we must flush the decoder buffers */
-    for (i = 0; i < nb_input_streams; i++) {
-        ist = input_streams[i];
-        if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {
-            process_input_packet(ist, NULL, 0);
-        }
-    }
-    poll_filters();
-    flush_encoders();
-
-    term_exit();
-
-    /* write the trailer if needed and close file */
-    for (i = 0; i < nb_output_files; i++) {
-        os = output_files[i]->ctx;
-        if (!output_files[i]->header_written) {
-            av_log(NULL, AV_LOG_ERROR,
-                   "Nothing was written into output file %d (%s), because "
-                   "at least one of its streams received no packets.\n",
-                   i, os->filename);
-            continue;
-        }
-        av_write_trailer(os);
-    }
-
-    /* dump report by using the first video and audio streams */
-    print_report(1, timer_start);
-
-    /* close each encoder */
-    for (i = 0; i < nb_output_streams; i++) {
-        ost = output_streams[i];
-        if (ost->encoding_needed) {
-            av_freep(&ost->enc_ctx->stats_in);
-        }
-    }
-
-    /* close each decoder */
-    for (i = 0; i < nb_input_streams; i++) {
-        ist = input_streams[i];
-        if (ist->decoding_needed) {
-            avcodec_close(ist->dec_ctx);
-            if (ist->hwaccel_uninit)
-                ist->hwaccel_uninit(ist->dec_ctx);
-        }
-    }
-
-    av_buffer_unref(&hw_device_ctx);
-
-    /* finished ! */
-    ret = 0;
-
- fail:
-#if HAVE_PTHREADS
-    free_input_threads();
-#endif
-
-    if (output_streams) {
-        for (i = 0; i < nb_output_streams; i++) {
-            ost = output_streams[i];
-            if (ost) {
-                if (ost->logfile) {
-                    fclose(ost->logfile);
-                    ost->logfile = NULL;
-                }
-                av_free(ost->forced_kf_pts);
-                av_dict_free(&ost->encoder_opts);
-                av_dict_free(&ost->resample_opts);
-            }
-        }
-    }
-    return ret;
-}
-
-static int64_t getutime(void)
-{
-#if HAVE_GETRUSAGE
-    struct rusage rusage;
-
-    getrusage(RUSAGE_SELF, &rusage);
-    return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
-#elif HAVE_GETPROCESSTIMES
-    HANDLE proc;
-    FILETIME c, e, k, u;
-    proc = GetCurrentProcess();
-    GetProcessTimes(proc, &c, &e, &k, &u);
-    return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
-#else
-    return av_gettime_relative();
-#endif
-}
-
-static int64_t getmaxrss(void)
-{
-#if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
-    struct rusage rusage;
-    getrusage(RUSAGE_SELF, &rusage);
-    return (int64_t)rusage.ru_maxrss * 1024;
-#elif HAVE_GETPROCESSMEMORYINFO
-    HANDLE proc;
-    PROCESS_MEMORY_COUNTERS memcounters;
-    proc = GetCurrentProcess();
-    memcounters.cb = sizeof(memcounters);
-    GetProcessMemoryInfo(proc, &memcounters, sizeof(memcounters));
-    return memcounters.PeakPagefileUsage;
-#else
-    return 0;
-#endif
-}
-
-int main(int argc, char **argv)
-{
-    int i, ret;
-    int64_t ti;
-
-    register_exit(avconv_cleanup);
-
-    av_log_set_flags(AV_LOG_SKIP_REPEATED);
-    parse_loglevel(argc, argv, options);
-
-    avcodec_register_all();
-#if CONFIG_AVDEVICE
-    avdevice_register_all();
-#endif
-    avfilter_register_all();
-    av_register_all();
-    avformat_network_init();
-
-    show_banner();
-
-    /* parse options and open all input/output files */
-    ret = avconv_parse_options(argc, argv);
-    if (ret < 0)
-        exit_program(1);
-
-    if (nb_output_files <= 0 && nb_input_files == 0) {
-        show_usage();
-        av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
-        exit_program(1);
-    }
-
-    /* file converter / grab */
-    if (nb_output_files <= 0) {
-        fprintf(stderr, "At least one output file must be specified\n");
-        exit_program(1);
-    }
-
-    for (i = 0; i < nb_output_files; i++) {
-        if (strcmp(output_files[i]->ctx->oformat->name, "rtp"))
-            want_sdp = 0;
-    }
-
-    ti = getutime();
-    if (transcode() < 0)
-        exit_program(1);
-    ti = getutime() - ti;
-    if (do_benchmark) {
-        int maxrss = getmaxrss() / 1024;
-        printf("bench: utime=%0.3fs maxrss=%ikB\n", ti / 1000000.0, maxrss);
-    }
-
-    exit_program(0);
-    return 0;
-}
diff --git a/avconv.h b/avconv.h
deleted file mode 100644 (file)
index 3c3f0ef..0000000
--- a/avconv.h
+++ /dev/null
@@ -1,513 +0,0 @@
-/*
- * This file is part of Libav.
- *
- * Libav is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Libav is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#ifndef AVCONV_H
-#define AVCONV_H
-
-#include "config.h"
-
-#include <stdint.h>
-#include <stdio.h>
-
-#if HAVE_PTHREADS
-#include <pthread.h>
-#endif
-
-#include "cmdutils.h"
-
-#include "libavformat/avformat.h"
-#include "libavformat/avio.h"
-
-#include "libavcodec/avcodec.h"
-
-#include "libavfilter/avfilter.h"
-
-#include "libavutil/avutil.h"
-#include "libavutil/dict.h"
-#include "libavutil/fifo.h"
-#include "libavutil/pixfmt.h"
-#include "libavutil/rational.h"
-
-#define VSYNC_AUTO       -1
-#define VSYNC_PASSTHROUGH 0
-#define VSYNC_CFR         1
-#define VSYNC_VFR         2
-
-enum HWAccelID {
-    HWACCEL_NONE = 0,
-    HWACCEL_AUTO,
-    HWACCEL_VDPAU,
-    HWACCEL_DXVA2,
-    HWACCEL_VDA,
-    HWACCEL_QSV,
-    HWACCEL_VAAPI,
-};
-
-typedef struct HWAccel {
-    const char *name;
-    int (*init)(AVCodecContext *s);
-    enum HWAccelID id;
-    enum AVPixelFormat pix_fmt;
-} HWAccel;
-
-/* select an input stream for an output stream */
-typedef struct StreamMap {
-    int disabled;           /* 1 is this mapping is disabled by a negative map */
-    int file_index;
-    int stream_index;
-    int sync_file_index;
-    int sync_stream_index;
-    char *linklabel;       /* name of an output link, for mapping lavfi outputs */
-} StreamMap;
-
-/* select an input file for an output file */
-typedef struct MetadataMap {
-    int  file;      // file index
-    char type;      // type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram
-    int  index;     // stream/chapter/program number
-} MetadataMap;
-
-typedef struct OptionsContext {
-    OptionGroup *g;
-
-    /* input/output options */
-    int64_t start_time;
-    const char *format;
-
-    SpecifierOpt *codec_names;
-    int        nb_codec_names;
-    SpecifierOpt *audio_channels;
-    int        nb_audio_channels;
-    SpecifierOpt *audio_sample_rate;
-    int        nb_audio_sample_rate;
-    SpecifierOpt *frame_rates;
-    int        nb_frame_rates;
-    SpecifierOpt *frame_sizes;
-    int        nb_frame_sizes;
-    SpecifierOpt *frame_pix_fmts;
-    int        nb_frame_pix_fmts;
-
-    /* input options */
-    int64_t input_ts_offset;
-    int loop;
-    int rate_emu;
-    int accurate_seek;
-
-    SpecifierOpt *ts_scale;
-    int        nb_ts_scale;
-    SpecifierOpt *dump_attachment;
-    int        nb_dump_attachment;
-    SpecifierOpt *hwaccels;
-    int        nb_hwaccels;
-    SpecifierOpt *hwaccel_devices;
-    int        nb_hwaccel_devices;
-    SpecifierOpt *hwaccel_output_formats;
-    int        nb_hwaccel_output_formats;
-    SpecifierOpt *autorotate;
-    int        nb_autorotate;
-
-    /* output options */
-    StreamMap *stream_maps;
-    int     nb_stream_maps;
-    /* first item specifies output metadata, second is input */
-    MetadataMap (*meta_data_maps)[2];
-    int nb_meta_data_maps;
-    int metadata_global_manual;
-    int metadata_streams_manual;
-    int metadata_chapters_manual;
-    const char **attachments;
-    int       nb_attachments;
-
-    int chapters_input_file;
-
-    int64_t recording_time;
-    uint64_t limit_filesize;
-    float mux_preload;
-    float mux_max_delay;
-    int shortest;
-
-    int video_disable;
-    int audio_disable;
-    int subtitle_disable;
-    int data_disable;
-
-    /* indexed by output file stream index */
-    int   *streamid_map;
-    int nb_streamid_map;
-
-    SpecifierOpt *metadata;
-    int        nb_metadata;
-    SpecifierOpt *max_frames;
-    int        nb_max_frames;
-    SpecifierOpt *bitstream_filters;
-    int        nb_bitstream_filters;
-    SpecifierOpt *codec_tags;
-    int        nb_codec_tags;
-    SpecifierOpt *sample_fmts;
-    int        nb_sample_fmts;
-    SpecifierOpt *qscale;
-    int        nb_qscale;
-    SpecifierOpt *bitrates;
-    int        nb_bitrates;
-    SpecifierOpt *forced_key_frames;
-    int        nb_forced_key_frames;
-    SpecifierOpt *force_fps;
-    int        nb_force_fps;
-    SpecifierOpt *frame_aspect_ratios;
-    int        nb_frame_aspect_ratios;
-    SpecifierOpt *rc_overrides;
-    int        nb_rc_overrides;
-    SpecifierOpt *intra_matrices;
-    int        nb_intra_matrices;
-    SpecifierOpt *inter_matrices;
-    int        nb_inter_matrices;
-    SpecifierOpt *top_field_first;
-    int        nb_top_field_first;
-    SpecifierOpt *metadata_map;
-    int        nb_metadata_map;
-    SpecifierOpt *presets;
-    int        nb_presets;
-    SpecifierOpt *copy_initial_nonkeyframes;
-    int        nb_copy_initial_nonkeyframes;
-    SpecifierOpt *filters;
-    int        nb_filters;
-    SpecifierOpt *filter_scripts;
-    int        nb_filter_scripts;
-    SpecifierOpt *pass;
-    int        nb_pass;
-    SpecifierOpt *passlogfiles;
-    int        nb_passlogfiles;
-    SpecifierOpt *max_muxing_queue_size;
-    int        nb_max_muxing_queue_size;
-} OptionsContext;
-
-typedef struct InputFilter {
-    AVFilterContext    *filter;
-    struct InputStream *ist;
-    struct FilterGraph *graph;
-    uint8_t            *name;
-
-    AVFifoBuffer *frame_queue;
-
-    // parameters configured for this input
-    int format;
-
-    int width, height;
-    AVRational sample_aspect_ratio;
-
-    int sample_rate;
-    uint64_t channel_layout;
-
-    AVBufferRef *hw_frames_ctx;
-
-    int eof;
-} InputFilter;
-
-typedef struct OutputFilter {
-    AVFilterContext     *filter;
-    struct OutputStream *ost;
-    struct FilterGraph  *graph;
-    uint8_t             *name;
-
-    /* temporary storage until stream maps are processed */
-    AVFilterInOut       *out_tmp;
-    enum AVMediaType     type;
-
-    /* desired output stream properties */
-    int width, height;
-    AVRational frame_rate;
-    int format;
-    int sample_rate;
-    uint64_t channel_layout;
-
-    // those are only set if no format is specified and the encoder gives us multiple options
-    int *formats;
-    uint64_t *channel_layouts;
-    int *sample_rates;
-} OutputFilter;
-
-typedef struct FilterGraph {
-    int            index;
-    const char    *graph_desc;
-
-    AVFilterGraph *graph;
-
-    InputFilter   **inputs;
-    int          nb_inputs;
-    OutputFilter **outputs;
-    int         nb_outputs;
-} FilterGraph;
-
-typedef struct InputStream {
-    int file_index;
-    AVStream *st;
-    int discard;             /* true if stream data should be discarded */
-    int decoding_needed;     /* true if the packets must be decoded in 'raw_fifo' */
-    AVCodecContext *dec_ctx;
-    AVCodec *dec;
-    AVFrame *decoded_frame;
-    AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
-
-    int64_t       start;     /* time when read started */
-    /* predicted dts of the next packet read for this stream or (when there are
-     * several frames in a packet) of the next frame in current packet */
-    int64_t       next_dts;
-    /* dts of the last packet read for this stream */
-    int64_t       last_dts;
-    int64_t min_pts; /* pts with the smallest value in a current stream */
-    int64_t max_pts; /* pts with the higher value in a current stream */
-
-    // when forcing constant input framerate through -r,
-    // this contains the pts that will be given to the next decoded frame
-    int64_t cfr_next_pts;
-
-    int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
-    PtsCorrectionContext pts_ctx;
-    double ts_scale;
-    AVDictionary *decoder_opts;
-    AVRational framerate;               /* framerate forced with -r */
-
-    int autorotate;
-
-    /* decoded data from this stream goes into all those filters
-     * currently video and audio only */
-    InputFilter **filters;
-    int        nb_filters;
-
-    /* hwaccel options */
-    enum HWAccelID hwaccel_id;
-    char  *hwaccel_device;
-    enum AVPixelFormat hwaccel_output_format;
-
-    /* hwaccel context */
-    enum HWAccelID active_hwaccel_id;
-    void  *hwaccel_ctx;
-    void (*hwaccel_uninit)(AVCodecContext *s);
-    int  (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
-    int  (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
-    enum AVPixelFormat hwaccel_pix_fmt;
-    enum AVPixelFormat hwaccel_retrieved_pix_fmt;
-    AVBufferRef *hw_frames_ctx;
-
-    /* stats */
-    // combined size of all the packets read
-    uint64_t data_size;
-    /* number of packets successfully read for this stream */
-    uint64_t nb_packets;
-    // number of frames/samples retrieved from the decoder
-    uint64_t frames_decoded;
-    uint64_t samples_decoded;
-} InputStream;
-
-typedef struct InputFile {
-    AVFormatContext *ctx;
-    int eof_reached;      /* true if eof reached */
-    int eagain;           /* true if last read attempt returned EAGAIN */
-    int ist_index;        /* index of first stream in ist_table */
-    int loop;             /* set number of times input stream should be looped */
-    int64_t duration;     /* actual duration of the longest stream in a file
-                             at the moment when looping happens */
-    AVRational time_base; /* time base of the duration */
-    int64_t ts_offset;
-    int64_t start_time;   /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
-    int64_t recording_time;
-    int nb_streams;       /* number of stream that avconv is aware of; may be different
-                             from ctx.nb_streams if new streams appear during av_read_frame() */
-    int rate_emu;
-    int accurate_seek;
-
-#if HAVE_PTHREADS
-    pthread_t thread;           /* thread reading from this file */
-    int finished;               /* the thread has exited */
-    int joined;                 /* the thread has been joined */
-    pthread_mutex_t fifo_lock;  /* lock for access to fifo */
-    pthread_cond_t  fifo_cond;  /* the main thread will signal on this cond after reading from fifo */
-    AVFifoBuffer *fifo;         /* demuxed packets are stored here; freed by the main thread */
-#endif
-} InputFile;
-
-typedef struct OutputStream {
-    int file_index;          /* file index */
-    int index;               /* stream index in the output file */
-    int source_index;        /* InputStream index */
-    AVStream *st;            /* stream in the output file */
-    int encoding_needed;     /* true if encoding needed for this stream */
-    int frame_number;
-    /* input pts and corresponding output pts
-       for A/V sync */
-    // double sync_ipts;        /* dts from the AVPacket of the demuxer in second units */
-    struct InputStream *sync_ist; /* input stream to sync against */
-    int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
-    /* pts of the first frame encoded for this stream, used for limiting
-     * recording time */
-    int64_t first_pts;
-    /* dts of the last packet sent to the muxer */
-    int64_t last_mux_dts;
-    // the timebase of the packets sent to the muxer
-    AVRational mux_timebase;
-
-    int                    nb_bitstream_filters;
-    AVBSFContext            **bsf_ctx;
-
-    AVCodecContext *enc_ctx;
-    AVCodec *enc;
-    int64_t max_frames;
-    AVFrame *filtered_frame;
-
-    void  *hwaccel_ctx;
-
-    /* video only */
-    AVRational frame_rate;
-    int force_fps;
-    int top_field_first;
-
-    float frame_aspect_ratio;
-
-    /* forced key frames */
-    int64_t *forced_kf_pts;
-    int forced_kf_count;
-    int forced_kf_index;
-    char *forced_keyframes;
-
-    // the bitrate to send to the muxer for streamcopy
-    int bitrate_override;
-
-    char *logfile_prefix;
-    FILE *logfile;
-
-    OutputFilter *filter;
-    char *avfilter;
-
-    int64_t sws_flags;
-    AVDictionary *encoder_opts;
-    AVDictionary *resample_opts;
-    int finished;        /* no more packets should be written for this stream */
-    int stream_copy;
-
-    // init_output_stream() has been called for this stream
-    // The encoder and the bistream filters have been initialized and the stream
-    // parameters are set in the AVStream.
-    int initialized;
-
-    const char *attachment_filename;
-    int copy_initial_nonkeyframes;
-
-    enum AVPixelFormat pix_fmts[2];
-
-    AVCodecParserContext *parser;
-    AVCodecContext       *parser_avctx;
-
-    /* stats */
-    // combined size of all the packets written
-    uint64_t data_size;
-    // number of packets send to the muxer
-    uint64_t packets_written;
-    // number of frames/samples sent to the encoder
-    uint64_t frames_encoded;
-    uint64_t samples_encoded;
-
-    /* packet quality factor */
-    int quality;
-
-    int max_muxing_queue_size;
-
-    /* the packets are buffered here until the muxer is ready to be initialized */
-    AVFifoBuffer *muxing_queue;
-} OutputStream;
-
-typedef struct OutputFile {
-    AVFormatContext *ctx;
-    AVDictionary *opts;
-    int ost_index;       /* index of the first stream in output_streams */
-    int64_t recording_time; /* desired length of the resulting file in microseconds */
-    int64_t start_time;     /* start time in microseconds */
-    uint64_t limit_filesize;
-
-    int shortest;
-
-    int header_written;
-} OutputFile;
-
-extern InputStream **input_streams;
-extern int        nb_input_streams;
-extern InputFile   **input_files;
-extern int        nb_input_files;
-
-extern OutputStream **output_streams;
-extern int         nb_output_streams;
-extern OutputFile   **output_files;
-extern int         nb_output_files;
-
-extern FilterGraph **filtergraphs;
-extern int        nb_filtergraphs;
-
-extern char *vstats_filename;
-
-extern float audio_drift_threshold;
-extern float dts_delta_threshold;
-
-extern int audio_volume;
-extern int audio_sync_method;
-extern int video_sync_method;
-extern int do_benchmark;
-extern int do_deinterlace;
-extern int do_hex_dump;
-extern int do_pkt_dump;
-extern int copy_ts;
-extern int copy_tb;
-extern int exit_on_error;
-extern int print_stats;
-extern int qp_hist;
-
-extern const AVIOInterruptCB int_cb;
-
-extern const OptionDef options[];
-
-extern const HWAccel hwaccels[];
-extern int hwaccel_lax_profile_check;
-extern AVBufferRef *hw_device_ctx;
-
-void reset_options(OptionsContext *o);
-void show_usage(void);
-
-void opt_output_file(void *optctx, const char *filename);
-
-void assert_avoptions(AVDictionary *m);
-
-int guess_input_channel_layout(InputStream *ist);
-
-int configure_filtergraph(FilterGraph *fg);
-int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
-int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
-int filtergraph_is_simple(FilterGraph *fg);
-int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
-int init_complex_filtergraph(FilterGraph *fg);
-
-int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
-
-int avconv_parse_options(int argc, char **argv);
-
-int vdpau_init(AVCodecContext *s);
-int dxva2_init(AVCodecContext *s);
-int vda_init(AVCodecContext *s);
-int qsv_init(AVCodecContext *s);
-int qsv_transcode_init(OutputStream *ost);
-int vaapi_decode_init(AVCodecContext *avctx);
-int vaapi_device_init(const char *device);
-
-#endif /* AVCONV_H */
diff --git a/avconv_dxva2.c b/avconv_dxva2.c
deleted file mode 100644 (file)
index 7578c3f..0000000
+++ /dev/null
@@ -1,440 +0,0 @@
-/*
- * This file is part of Libav.
- *
- * Libav is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Libav is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <windows.h>
-
-#ifdef _WIN32_WINNT
-#undef _WIN32_WINNT
-#endif
-#define _WIN32_WINNT 0x0600
-#define DXVA2API_USE_BITFIELDS
-#define COBJMACROS
-
-#include <stdint.h>
-
-#include <d3d9.h>
-#include <dxva2api.h>
-
-#include "avconv.h"
-
-#include "libavcodec/dxva2.h"
-
-#include "libavutil/avassert.h"
-#include "libavutil/buffer.h"
-#include "libavutil/frame.h"
-#include "libavutil/imgutils.h"
-#include "libavutil/pixfmt.h"
-
-#include "libavutil/hwcontext.h"
-#include "libavutil/hwcontext_dxva2.h"
-
-/* define all the GUIDs used directly here,
-   to avoid problems with inconsistent dxva2api.h versions in mingw-w64 and different MSVC version */
-#include <initguid.h>
-DEFINE_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
-
-DEFINE_GUID(DXVA2_ModeMPEG2_VLD,      0xee27417f, 0x5e28,0x4e65,0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9);
-DEFINE_GUID(DXVA2_ModeMPEG2and1_VLD,  0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60);
-DEFINE_GUID(DXVA2_ModeH264_E,         0x1b81be68, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
-DEFINE_GUID(DXVA2_ModeH264_F,         0x1b81be69, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
-DEFINE_GUID(DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6);
-DEFINE_GUID(DXVA2_ModeVC1_D,          0x1b81beA3, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
-DEFINE_GUID(DXVA2_ModeVC1_D2010,      0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
-DEFINE_GUID(DXVA2_ModeHEVC_VLD_Main,  0x5b11d51b, 0x2f4c,0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
-DEFINE_GUID(DXVA2_ModeHEVC_VLD_Main10,0x107af0e0, 0xef1a,0x4d19,0xab,0xa8,0x67,0xa1,0x63,0x07,0x3d,0x13);
-DEFINE_GUID(DXVA2_NoEncrypt,          0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
-DEFINE_GUID(GUID_NULL,                0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
-
-typedef struct dxva2_mode {
-  const GUID     *guid;
-  enum AVCodecID codec;
-} dxva2_mode;
-
-static const dxva2_mode dxva2_modes[] = {
-    /* MPEG-2 */
-    { &DXVA2_ModeMPEG2_VLD,      AV_CODEC_ID_MPEG2VIDEO },
-    { &DXVA2_ModeMPEG2and1_VLD,  AV_CODEC_ID_MPEG2VIDEO },
-
-    /* H.264 */
-    { &DXVA2_ModeH264_F,         AV_CODEC_ID_H264 },
-    { &DXVA2_ModeH264_E,         AV_CODEC_ID_H264 },
-    /* Intel specific H.264 mode */
-    { &DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264 },
-
-    /* VC-1 / WMV3 */
-    { &DXVA2_ModeVC1_D2010,      AV_CODEC_ID_VC1  },
-    { &DXVA2_ModeVC1_D2010,      AV_CODEC_ID_WMV3 },
-    { &DXVA2_ModeVC1_D,          AV_CODEC_ID_VC1  },
-    { &DXVA2_ModeVC1_D,          AV_CODEC_ID_WMV3 },
-
-    /* HEVC/H.265 */
-    { &DXVA2_ModeHEVC_VLD_Main,  AV_CODEC_ID_HEVC },
-    { &DXVA2_ModeHEVC_VLD_Main10, AV_CODEC_ID_HEVC },
-
-    { NULL,                      0 },
-};
-
-typedef struct DXVA2Context {
-    IDirectXVideoDecoder        *decoder;
-
-    GUID                        decoder_guid;
-    DXVA2_ConfigPictureDecode   decoder_config;
-    IDirectXVideoDecoderService *decoder_service;
-
-    AVFrame                     *tmp_frame;
-
-    AVBufferRef                 *hw_device_ctx;
-    AVBufferRef                 *hw_frames_ctx;
-} DXVA2Context;
-
-static void dxva2_uninit(AVCodecContext *s)
-{
-    InputStream  *ist = s->opaque;
-    DXVA2Context *ctx = ist->hwaccel_ctx;
-
-    ist->hwaccel_uninit        = NULL;
-    ist->hwaccel_get_buffer    = NULL;
-    ist->hwaccel_retrieve_data = NULL;
-
-    if (ctx->decoder_service)
-        IDirectXVideoDecoderService_Release(ctx->decoder_service);
-
-    av_buffer_unref(&ctx->hw_frames_ctx);
-    av_buffer_unref(&ctx->hw_device_ctx);
-
-    av_frame_free(&ctx->tmp_frame);
-
-    av_freep(&ist->hwaccel_ctx);
-    av_freep(&s->hwaccel_context);
-}
-
-static int dxva2_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
-{
-    InputStream  *ist = s->opaque;
-    DXVA2Context *ctx = ist->hwaccel_ctx;
-
-    return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
-}
-
-static int dxva2_retrieve_data(AVCodecContext *s, AVFrame *frame)
-{
-    InputStream        *ist = s->opaque;
-    DXVA2Context       *ctx = ist->hwaccel_ctx;
-    int                ret;
-
-    ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
-    if (ret < 0)
-        return ret;
-
-    ret = av_frame_copy_props(ctx->tmp_frame, frame);
-    if (ret < 0) {
-        av_frame_unref(ctx->tmp_frame);
-        return ret;
-    }
-
-    av_frame_unref(frame);
-    av_frame_move_ref(frame, ctx->tmp_frame);
-
-    return 0;
-}
-
-static int dxva2_alloc(AVCodecContext *s)
-{
-    InputStream  *ist = s->opaque;
-    int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
-    DXVA2Context *ctx;
-    HANDLE device_handle;
-    HRESULT hr;
-
-    AVHWDeviceContext    *device_ctx;
-    AVDXVA2DeviceContext *device_hwctx;
-    int ret;
-
-    ctx = av_mallocz(sizeof(*ctx));
-    if (!ctx)
-        return AVERROR(ENOMEM);
-
-    ist->hwaccel_ctx           = ctx;
-    ist->hwaccel_uninit        = dxva2_uninit;
-    ist->hwaccel_get_buffer    = dxva2_get_buffer;
-    ist->hwaccel_retrieve_data = dxva2_retrieve_data;
-
-    ret = av_hwdevice_ctx_create(&ctx->hw_device_ctx, AV_HWDEVICE_TYPE_DXVA2,
-                                 ist->hwaccel_device, NULL, 0);
-    if (ret < 0)
-        goto fail;
-    device_ctx   = (AVHWDeviceContext*)ctx->hw_device_ctx->data;
-    device_hwctx = device_ctx->hwctx;
-
-    hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr,
-                                                  &device_handle);
-    if (FAILED(hr)) {
-        av_log(NULL, loglevel, "Failed to open a device handle\n");
-        goto fail;
-    }
-
-    hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr, device_handle,
-                                                 &IID_IDirectXVideoDecoderService,
-                                                 (void **)&ctx->decoder_service);
-    IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, device_handle);
-    if (FAILED(hr)) {
-        av_log(NULL, loglevel, "Failed to create IDirectXVideoDecoderService\n");
-        goto fail;
-    }
-
-    ctx->tmp_frame = av_frame_alloc();
-    if (!ctx->tmp_frame)
-        goto fail;
-
-    s->hwaccel_context = av_mallocz(sizeof(struct dxva_context));
-    if (!s->hwaccel_context)
-        goto fail;
-
-    return 0;
-fail:
-    dxva2_uninit(s);
-    return AVERROR(EINVAL);
-}
-
-static int dxva2_get_decoder_configuration(AVCodecContext *s, const GUID *device_guid,
-                                           const DXVA2_VideoDesc *desc,
-                                           DXVA2_ConfigPictureDecode *config)
-{
-    InputStream  *ist = s->opaque;
-    int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
-    DXVA2Context *ctx = ist->hwaccel_ctx;
-    unsigned cfg_count = 0, best_score = 0;
-    DXVA2_ConfigPictureDecode *cfg_list = NULL;
-    DXVA2_ConfigPictureDecode best_cfg = {{0}};
-    HRESULT hr;
-    int i;
-
-    hr = IDirectXVideoDecoderService_GetDecoderConfigurations(ctx->decoder_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
-    if (FAILED(hr)) {
-        av_log(NULL, loglevel, "Unable to retrieve decoder configurations\n");
-        return AVERROR(EINVAL);
-    }
-
-    for (i = 0; i < cfg_count; i++) {
-        DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
-
-        unsigned score;
-        if (cfg->ConfigBitstreamRaw == 1)
-            score = 1;
-        else if (s->codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
-            score = 2;
-        else
-            continue;
-        if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
-            score += 16;
-        if (score > best_score) {
-            best_score = score;
-            best_cfg   = *cfg;
-        }
-    }
-    CoTaskMemFree(cfg_list);
-
-    if (!best_score) {
-        av_log(NULL, loglevel, "No valid decoder configuration available\n");
-        return AVERROR(EINVAL);
-    }
-
-    *config = best_cfg;
-    return 0;
-}
-
-static int dxva2_create_decoder(AVCodecContext *s)
-{
-    InputStream  *ist = s->opaque;
-    int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
-    DXVA2Context *ctx = ist->hwaccel_ctx;
-    struct dxva_context *dxva_ctx = s->hwaccel_context;
-    GUID *guid_list = NULL;
-    unsigned guid_count = 0, i, j;
-    GUID device_guid = GUID_NULL;
-    const D3DFORMAT surface_format = s->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
-                                     MKTAG('P', '0', '1', '0') : MKTAG('N', 'V', '1', '2');
-    D3DFORMAT target_format = 0;
-    DXVA2_VideoDesc desc = { 0 };
-    DXVA2_ConfigPictureDecode config;
-    HRESULT hr;
-    int surface_alignment, num_surfaces;
-    int ret;
-
-    AVDXVA2FramesContext *frames_hwctx;
-    AVHWFramesContext *frames_ctx;
-
-    hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(ctx->decoder_service, &guid_count, &guid_list);
-    if (FAILED(hr)) {
-        av_log(NULL, loglevel, "Failed to retrieve decoder device GUIDs\n");
-        goto fail;
-    }
-
-    for (i = 0; dxva2_modes[i].guid; i++) {
-        D3DFORMAT *target_list = NULL;
-        unsigned target_count = 0;
-        const dxva2_mode *mode = &dxva2_modes[i];
-        if (mode->codec != s->codec_id)
-            continue;
-
-        for (j = 0; j < guid_count; j++) {
-            if (IsEqualGUID(mode->guid, &guid_list[j]))
-                break;
-        }
-        if (j == guid_count)
-            continue;
-
-        hr = IDirectXVideoDecoderService_GetDecoderRenderTargets(ctx->decoder_service, mode->guid, &target_count, &target_list);
-        if (FAILED(hr)) {
-            continue;
-        }
-        for (j = 0; j < target_count; j++) {
-            const D3DFORMAT format = target_list[j];
-            if (format == surface_format) {
-                target_format = format;
-                break;
-            }
-        }
-        CoTaskMemFree(target_list);
-        if (target_format) {
-            device_guid = *mode->guid;
-            break;
-        }
-    }
-    CoTaskMemFree(guid_list);
-
-    if (IsEqualGUID(&device_guid, &GUID_NULL)) {
-        av_log(NULL, loglevel, "No decoder device for codec found\n");
-        goto fail;
-    }
-
-    desc.SampleWidth  = s->coded_width;
-    desc.SampleHeight = s->coded_height;
-    desc.Format       = target_format;
-
-    ret = dxva2_get_decoder_configuration(s, &device_guid, &desc, &config);
-    if (ret < 0) {
-        goto fail;
-    }
-
-    /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
-       but it causes issues for H.264 on certain AMD GPUs..... */
-    if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO)
-        surface_alignment = 32;
-    /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
-       all coding features have enough room to work with */
-    else if  (s->codec_id == AV_CODEC_ID_HEVC)
-        surface_alignment = 128;
-    else
-        surface_alignment = 16;
-
-    /* 4 base work surfaces */
-    num_surfaces = 4;
-
-    /* add surfaces based on number of possible refs */
-    if (s->codec_id == AV_CODEC_ID_H264 || s->codec_id == AV_CODEC_ID_HEVC)
-        num_surfaces += 16;
-    else
-        num_surfaces += 2;
-
-    /* add extra surfaces for frame threading */
-    if (s->active_thread_type & FF_THREAD_FRAME)
-        num_surfaces += s->thread_count;
-
-    ctx->hw_frames_ctx = av_hwframe_ctx_alloc(ctx->hw_device_ctx);
-    if (!ctx->hw_frames_ctx)
-        goto fail;
-    frames_ctx   = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
-    frames_hwctx = frames_ctx->hwctx;
-
-    frames_ctx->format            = AV_PIX_FMT_DXVA2_VLD;
-    frames_ctx->sw_format         = s->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
-                                    AV_PIX_FMT_P010 : AV_PIX_FMT_NV12;
-    frames_ctx->width             = FFALIGN(s->coded_width, surface_alignment);
-    frames_ctx->height            = FFALIGN(s->coded_height, surface_alignment);
-    frames_ctx->initial_pool_size = num_surfaces;
-
-    frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
-
-    ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
-    if (ret < 0) {
-        av_log(NULL, loglevel, "Failed to initialize the HW frames context\n");
-        goto fail;
-    }
-
-    hr = IDirectXVideoDecoderService_CreateVideoDecoder(ctx->decoder_service, &device_guid,
-                                                        &desc, &config, frames_hwctx->surfaces,
-                                                        frames_hwctx->nb_surfaces, &frames_hwctx->decoder_to_release);
-    if (FAILED(hr)) {
-        av_log(NULL, loglevel, "Failed to create DXVA2 video decoder\n");
-        goto fail;
-    }
-
-    ctx->decoder_guid   = device_guid;
-    ctx->decoder_config = config;
-
-    dxva_ctx->cfg           = &ctx->decoder_config;
-    dxva_ctx->decoder       = frames_hwctx->decoder_to_release;
-    dxva_ctx->surface       = frames_hwctx->surfaces;
-    dxva_ctx->surface_count = frames_hwctx->nb_surfaces;
-
-    if (IsEqualGUID(&ctx->decoder_guid, &DXVADDI_Intel_ModeH264_E))
-        dxva_ctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
-
-    return 0;
-fail:
-    av_buffer_unref(&ctx->hw_frames_ctx);
-    return AVERROR(EINVAL);
-}
-
-int dxva2_init(AVCodecContext *s)
-{
-    InputStream *ist = s->opaque;
-    int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
-    DXVA2Context *ctx;
-    int ret;
-
-    if (!ist->hwaccel_ctx) {
-        ret = dxva2_alloc(s);
-        if (ret < 0)
-            return ret;
-    }
-    ctx = ist->hwaccel_ctx;
-
-    if (s->codec_id == AV_CODEC_ID_H264 &&
-        (s->profile & ~FF_PROFILE_H264_CONSTRAINED) > FF_PROFILE_H264_HIGH) {
-        av_log(NULL, loglevel, "Unsupported H.264 profile for DXVA2 HWAccel: %d\n", s->profile);
-        return AVERROR(EINVAL);
-    }
-
-    if (s->codec_id == AV_CODEC_ID_HEVC &&
-        s->profile != FF_PROFILE_HEVC_MAIN && s->profile != FF_PROFILE_HEVC_MAIN_10) {
-        av_log(NULL, loglevel, "Unsupported HEVC profile for DXVA2 HWAccel: %d\n", s->profile);
-        return AVERROR(EINVAL);
-    }
-
-    av_buffer_unref(&ctx->hw_frames_ctx);
-
-    ret = dxva2_create_decoder(s);
-    if (ret < 0) {
-        av_log(NULL, loglevel, "Error creating the DXVA2 decoder\n");
-        return ret;
-    }
-
-    return 0;
-}
diff --git a/avconv_filter.c b/avconv_filter.c
deleted file mode 100644 (file)
index e53dcd2..0000000
+++ /dev/null
@@ -1,818 +0,0 @@
-/*
- * avconv filter configuration
- *
- * This file is part of Libav.
- *
- * Libav is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Libav is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <stdint.h>
-
-#include "avconv.h"
-
-#include "libavfilter/avfilter.h"
-#include "libavfilter/buffersrc.h"
-
-#include "libavresample/avresample.h"
-
-#include "libavutil/avassert.h"
-#include "libavutil/avstring.h"
-#include "libavutil/channel_layout.h"
-#include "libavutil/display.h"
-#include "libavutil/opt.h"
-#include "libavutil/pixdesc.h"
-#include "libavutil/pixfmt.h"
-#include "libavutil/samplefmt.h"
-
-/* Define a function for building a string containing a list of
- * allowed formats. */
-#define DEF_CHOOSE_FORMAT(suffix, type, var, supported_list, none, get_name)   \
-static char *choose_ ## suffix (OutputFilter *ofilter)                         \
-{                                                                              \
-    if (ofilter->var != none) {                                                \
-        get_name(ofilter->var);                                                \
-        return av_strdup(name);                                                \
-    } else if (ofilter->supported_list) {                                      \
-        const type *p;                                                         \
-        AVIOContext *s = NULL;                                                 \
-        uint8_t *ret;                                                          \
-        int len;                                                               \
-                                                                               \
-        if (avio_open_dyn_buf(&s) < 0)                                         \
-            exit(1);                                                           \
-                                                                               \
-        for (p = ofilter->supported_list; *p != none; p++) {                   \
-            get_name(*p);                                                      \
-            avio_printf(s, "%s|", name);                                       \
-        }                                                                      \
-        len = avio_close_dyn_buf(s, &ret);                                     \
-        ret[len - 1] = 0;                                                      \
-        return ret;                                                            \
-    } else                                                                     \
-        return NULL;                                                           \
-}
-
-DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, formats, AV_PIX_FMT_NONE,
-                  GET_PIX_FMT_NAME)
-
-DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, formats,
-                  AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME)
-
-DEF_CHOOSE_FORMAT(sample_rates, int, sample_rate, sample_rates, 0,
-                  GET_SAMPLE_RATE_NAME)
-
-DEF_CHOOSE_FORMAT(channel_layouts, uint64_t, channel_layout, channel_layouts, 0,
-                  GET_CH_LAYOUT_NAME)
-
-int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
-{
-    FilterGraph *fg = av_mallocz(sizeof(*fg));
-
-    if (!fg)
-        exit(1);
-    fg->index = nb_filtergraphs;
-
-    GROW_ARRAY(fg->outputs, fg->nb_outputs);
-    if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
-        exit(1);
-    fg->outputs[0]->ost   = ost;
-    fg->outputs[0]->graph = fg;
-    fg->outputs[0]->format = -1;
-
-    ost->filter = fg->outputs[0];
-
-    GROW_ARRAY(fg->inputs, fg->nb_inputs);
-    if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
-        exit(1);
-    fg->inputs[0]->ist   = ist;
-    fg->inputs[0]->graph = fg;
-    fg->inputs[0]->format = -1;
-
-    fg->inputs[0]->frame_queue = av_fifo_alloc(8 * sizeof(AVFrame*));
-    if (!fg->inputs[0])
-        exit_program(1);
-
-    GROW_ARRAY(ist->filters, ist->nb_filters);
-    ist->filters[ist->nb_filters - 1] = fg->inputs[0];
-
-    GROW_ARRAY(filtergraphs, nb_filtergraphs);
-    filtergraphs[nb_filtergraphs - 1] = fg;
-
-    return 0;
-}
-
-static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
-{
-    InputStream *ist = NULL;
-    enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
-    int i;
-
-    // TODO: support other filter types
-    if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
-        av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
-               "currently.\n");
-        exit(1);
-    }
-
-    if (in->name) {
-        AVFormatContext *s;
-        AVStream       *st = NULL;
-        char *p;
-        int file_idx = strtol(in->name, &p, 0);
-
-        if (file_idx < 0 || file_idx >= nb_input_files) {
-            av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtegraph description %s.\n",
-                   file_idx, fg->graph_desc);
-            exit(1);
-        }
-        s = input_files[file_idx]->ctx;
-
-        for (i = 0; i < s->nb_streams; i++) {
-            if (s->streams[i]->codecpar->codec_type != type)
-                continue;
-            if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
-                st = s->streams[i];
-                break;
-            }
-        }
-        if (!st) {
-            av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
-                   "matches no streams.\n", p, fg->graph_desc);
-            exit(1);
-        }
-        ist = input_streams[input_files[file_idx]->ist_index + st->index];
-    } else {
-        /* find the first unused stream of corresponding type */
-        for (i = 0; i < nb_input_streams; i++) {
-            ist = input_streams[i];
-            if (ist->dec_ctx->codec_type == type && ist->discard)
-                break;
-        }
-        if (i == nb_input_streams) {
-            av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
-                   "unlabeled input pad %d on filter %s\n", in->pad_idx,
-                   in->filter_ctx->name);
-            exit(1);
-        }
-    }
-    av_assert0(ist);
-
-    ist->discard         = 0;
-    ist->decoding_needed = 1;
-    ist->st->discard = AVDISCARD_NONE;
-
-    GROW_ARRAY(fg->inputs, fg->nb_inputs);
-    if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
-        exit(1);
-    fg->inputs[fg->nb_inputs - 1]->ist   = ist;
-    fg->inputs[fg->nb_inputs - 1]->graph = fg;
-    fg->inputs[fg->nb_inputs - 1]->format = -1;
-
-    fg->inputs[fg->nb_inputs - 1]->frame_queue = av_fifo_alloc(8 * sizeof(AVFrame*));
-    if (!fg->inputs[fg->nb_inputs - 1]->frame_queue)
-        exit_program(1);
-
-    GROW_ARRAY(ist->filters, ist->nb_filters);
-    ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
-}
-
-int init_complex_filtergraph(FilterGraph *fg)
-{
-    AVFilterInOut *inputs, *outputs, *cur;
-    AVFilterGraph *graph;
-    int ret = 0;
-
-    /* this graph is only used for determining the kinds of inputs
-     * and outputs we have, and is discarded on exit from this function */
-    graph = avfilter_graph_alloc();
-    if (!graph)
-        return AVERROR(ENOMEM);
-
-    ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
-    if (ret < 0)
-        goto fail;
-
-    for (cur = inputs; cur; cur = cur->next)
-        init_input_filter(fg, cur);
-
-    for (cur = outputs; cur;) {
-        GROW_ARRAY(fg->outputs, fg->nb_outputs);
-        fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]));
-        if (!fg->outputs[fg->nb_outputs - 1])
-            exit(1);
-
-        fg->outputs[fg->nb_outputs - 1]->graph   = fg;
-        fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
-        fg->outputs[fg->nb_outputs - 1]->type    = avfilter_pad_get_type(cur->filter_ctx->output_pads,
-                                                                         cur->pad_idx);
-        cur = cur->next;
-        fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
-    }
-
-fail:
-    avfilter_inout_free(&inputs);
-    avfilter_graph_free(&graph);
-    return ret;
-}
-
-static int insert_trim(int64_t start_time, int64_t duration,
-                       AVFilterContext **last_filter, int *pad_idx,
-                       const char *filter_name)
-{
-    AVFilterGraph *graph = (*last_filter)->graph;
-    AVFilterContext *ctx;
-    const AVFilter *trim;
-    enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
-    const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
-    int ret = 0;
-
-    if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
-        return 0;
-
-    trim = avfilter_get_by_name(name);
-    if (!trim) {
-        av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
-               "recording time.\n", name);
-        return AVERROR_FILTER_NOT_FOUND;
-    }
-
-    ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
-    if (!ctx)
-        return AVERROR(ENOMEM);
-
-    if (duration != INT64_MAX) {
-        ret = av_opt_set_double(ctx, "duration", (double)duration / 1e6,
-                                AV_OPT_SEARCH_CHILDREN);
-    }
-    if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
-        ret = av_opt_set_double(ctx, "start", (double)start_time / 1e6,
-                                AV_OPT_SEARCH_CHILDREN);
-    }
-    if (ret < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
-        return ret;
-    }
-
-    ret = avfilter_init_str(ctx, NULL);
-    if (ret < 0)
-        return ret;
-
-    ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
-    if (ret < 0)
-        return ret;
-
-    *last_filter = ctx;
-    *pad_idx     = 0;
-    return 0;
-}
-
-static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
-                         const char *filter_name, const char *args)
-{
-    AVFilterGraph *graph = (*last_filter)->graph;
-    AVFilterContext *ctx;
-    int ret;
-
-    ret = avfilter_graph_create_filter(&ctx,
-                                       avfilter_get_by_name(filter_name),
-                                       filter_name, args, NULL, graph);
-    if (ret < 0)
-        return ret;
-
-    ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
-    if (ret < 0)
-        return ret;
-
-    *last_filter = ctx;
-    *pad_idx     = 0;
-    return 0;
-}
-
-static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
-{
-    char *pix_fmts;
-    OutputStream *ost = ofilter->ost;
-    OutputFile    *of = output_files[ost->file_index];
-    AVFilterContext *last_filter = out->filter_ctx;
-    int pad_idx = out->pad_idx;
-    int ret;
-    char name[255];
-
-    snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
-    ret = avfilter_graph_create_filter(&ofilter->filter,
-                                       avfilter_get_by_name("buffersink"),
-                                       name, NULL, NULL, fg->graph);
-    if (ret < 0)
-        return ret;
-
-    if (ofilter->width || ofilter->height) {
-        char args[255];
-        AVFilterContext *filter;
-
-        snprintf(args, sizeof(args), "%d:%d:0x%X",
-                 ofilter->width, ofilter->height,
-                 (unsigned)ost->sws_flags);
-        snprintf(name, sizeof(name), "scaler for output stream %d:%d",
-                 ost->file_index, ost->index);
-        if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
-                                                name, args, NULL, fg->graph)) < 0)
-            return ret;
-        if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
-            return ret;
-
-        last_filter = filter;
-        pad_idx = 0;
-    }
-
-    if ((pix_fmts = choose_pix_fmts(ofilter))) {
-        AVFilterContext *filter;
-        snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
-                 ost->file_index, ost->index);
-        ret = avfilter_graph_create_filter(&filter,
-                                           avfilter_get_by_name("format"),
-                                           "format", pix_fmts, NULL, fg->graph);
-        av_freep(&pix_fmts);
-        if (ret < 0)
-            return ret;
-        if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
-            return ret;
-
-        last_filter = filter;
-        pad_idx     = 0;
-    }
-
-    if (ost->frame_rate.num) {
-        AVFilterContext *fps;
-        char args[255];
-
-        snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
-                 ost->frame_rate.den);
-        snprintf(name, sizeof(name), "fps for output stream %d:%d",
-                 ost->file_index, ost->index);
-        ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
-                                           name, args, NULL, fg->graph);
-        if (ret < 0)
-            return ret;
-
-        ret = avfilter_link(last_filter, pad_idx, fps, 0);
-        if (ret < 0)
-            return ret;
-        last_filter = fps;
-        pad_idx = 0;
-    }
-
-    snprintf(name, sizeof(name), "trim for output stream %d:%d",
-             ost->file_index, ost->index);
-    ret = insert_trim(of->start_time, of->recording_time,
-                      &last_filter, &pad_idx, name);
-    if (ret < 0)
-        return ret;
-
-
-    if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
-        return ret;
-
-    return 0;
-}
-
-static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
-{
-    OutputStream *ost = ofilter->ost;
-    OutputFile    *of = output_files[ost->file_index];
-    AVCodecContext *codec  = ost->enc_ctx;
-    AVFilterContext *last_filter = out->filter_ctx;
-    int pad_idx = out->pad_idx;
-    char *sample_fmts, *sample_rates, *channel_layouts;
-    char name[255];
-    int ret;
-
-
-    snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
-    ret = avfilter_graph_create_filter(&ofilter->filter,
-                                       avfilter_get_by_name("abuffersink"),
-                                       name, NULL, NULL, fg->graph);
-    if (ret < 0)
-        return ret;
-
-    if (codec->channels && !codec->channel_layout)
-        codec->channel_layout = av_get_default_channel_layout(codec->channels);
-
-    sample_fmts     = choose_sample_fmts(ofilter);
-    sample_rates    = choose_sample_rates(ofilter);
-    channel_layouts = choose_channel_layouts(ofilter);
-    if (sample_fmts || sample_rates || channel_layouts) {
-        AVFilterContext *format;
-        char args[256];
-        int len = 0;
-
-        if (sample_fmts)
-            len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
-                            sample_fmts);
-        if (sample_rates)
-            len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
-                            sample_rates);
-        if (channel_layouts)
-            len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
-                            channel_layouts);
-        args[len - 1] = 0;
-
-        av_freep(&sample_fmts);
-        av_freep(&sample_rates);
-        av_freep(&channel_layouts);
-
-        snprintf(name, sizeof(name), "audio format for output stream %d:%d",
-                 ost->file_index, ost->index);
-        ret = avfilter_graph_create_filter(&format,
-                                           avfilter_get_by_name("aformat"),
-                                           name, args, NULL, fg->graph);
-        if (ret < 0)
-            return ret;
-
-        ret = avfilter_link(last_filter, pad_idx, format, 0);
-        if (ret < 0)
-            return ret;
-
-        last_filter = format;
-        pad_idx = 0;
-    }
-
-    snprintf(name, sizeof(name), "trim for output stream %d:%d",
-             ost->file_index, ost->index);
-    ret = insert_trim(of->start_time, of->recording_time,
-                      &last_filter, &pad_idx, name);
-    if (ret < 0)
-        return ret;
-
-    if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
-        return ret;
-
-    return 0;
-}
-
-#define DESCRIBE_FILTER_LINK(f, inout, in)                         \
-{                                                                  \
-    AVFilterContext *ctx = inout->filter_ctx;                      \
-    AVFilterPad *pads = in ? ctx->input_pads  : ctx->output_pads;  \
-    int       nb_pads = in ? ctx->nb_inputs   : ctx->nb_outputs;   \
-    AVIOContext *pb;                                               \
-                                                                   \
-    if (avio_open_dyn_buf(&pb) < 0)                                \
-        exit(1);                                                   \
-                                                                   \
-    avio_printf(pb, "%s", ctx->filter->name);                      \
-    if (nb_pads > 1)                                               \
-        avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
-    avio_w8(pb, 0);                                                \
-    avio_close_dyn_buf(pb, &f->name);                              \
-}
-
-int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
-{
-    av_freep(&ofilter->name);
-    DESCRIBE_FILTER_LINK(ofilter, out, 0);
-
-    switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
-    case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
-    case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
-    default: av_assert0(0);
-    }
-}
-
-static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
-                                        AVFilterInOut *in)
-{
-    AVFilterContext *last_filter;
-    const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
-    InputStream *ist = ifilter->ist;
-    InputFile     *f = input_files[ist->file_index];
-    AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
-                                         ist->st->time_base;
-    AVBufferSrcParameters *par;
-    char name[255];
-    int ret, pad_idx = 0;
-
-    snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
-             ist->file_index, ist->st->index);
-
-    ifilter->filter = avfilter_graph_alloc_filter(fg->graph, buffer_filt, name);
-    if (!ifilter->filter)
-        return AVERROR(ENOMEM);
-
-    par = av_buffersrc_parameters_alloc();
-    if (!par)
-        return AVERROR(ENOMEM);
-
-    par->sample_aspect_ratio = ifilter->sample_aspect_ratio;
-    par->width               = ifilter->width;
-    par->height              = ifilter->height;
-    par->format              = ifilter->format;
-    par->time_base           = tb;
-    if (ist->framerate.num)
-        par->frame_rate      = ist->framerate;
-    par->hw_frames_ctx       = ifilter->hw_frames_ctx;
-
-    ret = av_buffersrc_parameters_set(ifilter->filter, par);
-    av_freep(&par);
-    if (ret < 0)
-        return ret;
-
-    ret = avfilter_init_str(ifilter->filter, NULL);
-    if (ret < 0)
-        return ret;
-
-    last_filter = ifilter->filter;
-
-    if (ist->autorotate) {
-        uint8_t* displaymatrix = av_stream_get_side_data(ist->st,
-                                                         AV_PKT_DATA_DISPLAYMATRIX, NULL);
-        if (displaymatrix) {
-            double rot = av_display_rotation_get((int32_t*) displaymatrix);
-            if (rot < -135 || rot > 135) {
-                ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
-                if (ret < 0)
-                    return ret;
-                ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
-            } else if (rot < -45) {
-                ret = insert_filter(&last_filter, &pad_idx, "transpose", "dir=clock");
-            } else if (rot > 45) {
-                ret = insert_filter(&last_filter, &pad_idx, "transpose", "dir=cclock");
-            }
-            if (ret < 0)
-                return ret;
-        }
-    }
-
-    snprintf(name, sizeof(name), "trim for input stream %d:%d",
-             ist->file_index, ist->st->index);
-    ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
-                      AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
-    if (ret < 0)
-        return ret;
-
-    if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
-        return ret;
-    return 0;
-}
-
-static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
-                                        AVFilterInOut *in)
-{
-    AVFilterContext *last_filter;
-    const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
-    InputStream *ist = ifilter->ist;
-    InputFile     *f = input_files[ist->file_index];
-    AVBufferSrcParameters *par;
-    char args[255], name[255];
-    int ret, pad_idx = 0;
-
-    snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
-             ist->file_index, ist->st->index);
-
-    ifilter->filter = avfilter_graph_alloc_filter(fg->graph, abuffer_filt, name);
-    if (!ifilter->filter)
-        return AVERROR(ENOMEM);
-
-    par = av_buffersrc_parameters_alloc();
-    if (!par)
-        return AVERROR(ENOMEM);
-
-    par->time_base      = (AVRational){ 1, ifilter->sample_rate };
-    par->sample_rate    = ifilter->sample_rate;
-    par->format         = ifilter->format;
-    par->channel_layout = ifilter->channel_layout;
-
-    ret = av_buffersrc_parameters_set(ifilter->filter, par);
-    av_freep(&par);
-    if (ret < 0)
-        return ret;
-
-    ret = avfilter_init_str(ifilter->filter, NULL);
-    if (ret < 0)
-        return ret;
-    last_filter = ifilter->filter;
-
-    if (audio_sync_method > 0) {
-        AVFilterContext *async;
-        int  len = 0;
-
-        av_log(NULL, AV_LOG_WARNING, "-async has been deprecated. Used the "
-               "asyncts audio filter instead.\n");
-
-        if (audio_sync_method > 1)
-            len += snprintf(args + len, sizeof(args) - len, "compensate=1:"
-                            "max_comp=%d:", audio_sync_method);
-        snprintf(args + len, sizeof(args) - len, "min_delta=%f",
-                 audio_drift_threshold);
-
-        snprintf(name, sizeof(name), "graph %d audio sync for input stream %d:%d",
-                 fg->index, ist->file_index, ist->st->index);
-        ret = avfilter_graph_create_filter(&async,
-                                           avfilter_get_by_name("asyncts"),
-                                           name, args, NULL, fg->graph);
-        if (ret < 0)
-            return ret;
-
-        ret = avfilter_link(last_filter, 0, async, 0);
-        if (ret < 0)
-            return ret;
-
-        last_filter = async;
-    }
-    if (audio_volume != 256) {
-        AVFilterContext *volume;
-
-        av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
-               "audio filter instead.\n");
-
-        snprintf(args, sizeof(args), "volume=%f", audio_volume / 256.0);
-
-        snprintf(name, sizeof(name), "graph %d volume for input stream %d:%d",
-                 fg->index, ist->file_index, ist->st->index);
-        ret = avfilter_graph_create_filter(&volume,
-                                           avfilter_get_by_name("volume"),
-                                           name, args, NULL, fg->graph);
-        if (ret < 0)
-            return ret;
-
-        ret = avfilter_link(last_filter, 0, volume, 0);
-        if (ret < 0)
-            return ret;
-
-        last_filter = volume;
-    }
-
-    snprintf(name, sizeof(name), "trim for input stream %d:%d",
-             ist->file_index, ist->st->index);
-    ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
-                      AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
-    if (ret < 0)
-        return ret;
-
-    if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
-        return ret;
-
-    return 0;
-}
-
-static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
-                                  AVFilterInOut *in)
-{
-    av_freep(&ifilter->name);
-    DESCRIBE_FILTER_LINK(ifilter, in, 1);
-
-    switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
-    case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
-    case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
-    default: av_assert0(0);
-    }
-}
-
-int configure_filtergraph(FilterGraph *fg)
-{
-    AVFilterInOut *inputs, *outputs, *cur;
-    int ret, i, simple = filtergraph_is_simple(fg);
-    const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
-                                      fg->graph_desc;
-
-    avfilter_graph_free(&fg->graph);
-    if (!(fg->graph = avfilter_graph_alloc()))
-        return AVERROR(ENOMEM);
-
-    if (simple) {
-        OutputStream *ost = fg->outputs[0]->ost;
-        char args[512];
-        AVDictionaryEntry *e = NULL;
-
-        snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
-        fg->graph->scale_sws_opts = av_strdup(args);
-
-        args[0] = '\0';
-        while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
-                                AV_DICT_IGNORE_SUFFIX))) {
-            av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
-        }
-        if (strlen(args))
-            args[strlen(args) - 1] = '\0';
-        fg->graph->resample_lavr_opts = av_strdup(args);
-    }
-
-    if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
-        goto fail;
-
-    if (hw_device_ctx) {
-        for (i = 0; i < fg->graph->nb_filters; i++) {
-            fg->graph->filters[i]->hw_device_ctx = av_buffer_ref(hw_device_ctx);
-        }
-    }
-
-    if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
-        av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
-               "exactly one input and output.\n", graph_desc);
-        ret = AVERROR(EINVAL);
-        goto fail;
-    }
-
-    for (cur = inputs, i = 0; cur; cur = cur->next, i++)
-        if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
-            goto fail;
-    avfilter_inout_free(&inputs);
-
-    for (cur = outputs, i = 0; cur; cur = cur->next, i++) {
-        OutputFilter *ofilter = fg->outputs[i];
-        if (ofilter->ost)
-            configure_output_filter(fg, ofilter, cur);
-    }
-
-    avfilter_inout_free(&outputs);
-
-    if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
-        goto fail;
-
-    /* limit the lists of allowed formats to the ones selected, to
-     * make sure they stay the same if the filtergraph is reconfigured later */
-    for (i = 0; i < fg->nb_outputs; i++) {
-        OutputFilter *ofilter = fg->outputs[i];
-        AVFilterLink *link = ofilter->filter->inputs[0];
-
-        ofilter->format = link->format;
-
-        ofilter->width  = link->w;
-        ofilter->height = link->h;
-
-        ofilter->sample_rate    = link->sample_rate;
-        ofilter->channel_layout = link->channel_layout;
-    }
-
-    for (i = 0; i < fg->nb_inputs; i++) {
-        while (av_fifo_size(fg->inputs[i]->frame_queue)) {
-            AVFrame *tmp;
-            av_fifo_generic_read(fg->inputs[i]->frame_queue, &tmp, sizeof(tmp), NULL);
-            ret = av_buffersrc_add_frame(fg->inputs[i]->filter, tmp);
-            av_frame_free(&tmp);
-            if (ret < 0)
-                goto fail;
-        }
-    }
-
-    /* send the EOFs for the finished inputs */
-    for (i = 0; i < fg->nb_inputs; i++) {
-        if (fg->inputs[i]->eof) {
-            ret = av_buffersrc_add_frame(fg->inputs[i]->filter, NULL);
-            if (ret < 0)
-                goto fail;
-        }
-    }
-
-    return 0;
-fail:
-    avfilter_graph_free(&fg->graph);
-    return ret;
-}
-
-int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
-{
-    av_buffer_unref(&ifilter->hw_frames_ctx);
-
-    ifilter->format = frame->format;
-
-    ifilter->width               = frame->width;
-    ifilter->height              = frame->height;
-    ifilter->sample_aspect_ratio = frame->sample_aspect_ratio;
-
-    ifilter->sample_rate         = frame->sample_rate;
-    ifilter->channel_layout      = frame->channel_layout;
-
-    if (frame->hw_frames_ctx) {
-        ifilter->hw_frames_ctx = av_buffer_ref(frame->hw_frames_ctx);
-        if (!ifilter->hw_frames_ctx)
-            return AVERROR(ENOMEM);
-    }
-
-    return 0;
-}
-
-int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
-{
-    int i;
-    for (i = 0; i < fg->nb_inputs; i++)
-        if (fg->inputs[i]->ist == ist)
-            return 1;
-    return 0;
-}
-
-int filtergraph_is_simple(FilterGraph *fg)
-{
-    return !fg->graph_desc;
-}
diff --git a/avconv_opt.c b/avconv_opt.c
deleted file mode 100644 (file)
index e078a0b..0000000
+++ /dev/null
@@ -1,2745 +0,0 @@
-/*
- * avconv option parsing
- *
- * This file is part of Libav.
- *
- * Libav is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Libav is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <stdint.h>
-
-#include "avconv.h"
-#include "cmdutils.h"
-
-#include "libavformat/avformat.h"
-
-#include "libavcodec/avcodec.h"
-
-#include "libavfilter/avfilter.h"
-
-#include "libavutil/avassert.h"
-#include "libavutil/avstring.h"
-#include "libavutil/avutil.h"
-#include "libavutil/channel_layout.h"
-#include "libavutil/intreadwrite.h"
-#include "libavutil/fifo.h"
-#include "libavutil/mathematics.h"
-#include "libavutil/opt.h"
-#include "libavutil/parseutils.h"
-#include "libavutil/pixdesc.h"
-#include "libavutil/pixfmt.h"
-
-#define DEFAULT_PASS_LOGFILENAME_PREFIX "av2pass"
-
-#define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
-{\
-    int i, ret;\
-    for (i = 0; i < o->nb_ ## name; i++) {\
-        char *spec = o->name[i].specifier;\
-        if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0)\
-            outvar = o->name[i].u.type;\
-        else if (ret < 0)\
-            exit_program(1);\
-    }\
-}
-
-const HWAccel hwaccels[] = {
-#if HAVE_VDPAU_X11
-    { "vdpau", vdpau_init, HWACCEL_VDPAU, AV_PIX_FMT_VDPAU },
-#endif
-#if HAVE_DXVA2_LIB
-    { "dxva2", dxva2_init, HWACCEL_DXVA2, AV_PIX_FMT_DXVA2_VLD },
-#endif
-#if CONFIG_VDA
-    { "vda",   vda_init,   HWACCEL_VDA,   AV_PIX_FMT_VDA },
-#endif
-#if CONFIG_LIBMFX
-    { "qsv",   qsv_init,   HWACCEL_QSV,   AV_PIX_FMT_QSV },
-#endif
-#if CONFIG_VAAPI
-    { "vaapi", vaapi_decode_init, HWACCEL_VAAPI, AV_PIX_FMT_VAAPI },
-#endif
-    { 0 },
-};
-int hwaccel_lax_profile_check = 0;
-AVBufferRef *hw_device_ctx;
-
-char *vstats_filename;
-
-float audio_drift_threshold = 0.1;
-float dts_delta_threshold   = 10;
-
-int audio_volume      = 256;
-int audio_sync_method = 0;
-int video_sync_method = VSYNC_AUTO;
-int do_benchmark      = 0;
-int do_hex_dump       = 0;
-int do_pkt_dump       = 0;
-int copy_ts           = 0;
-int copy_tb           = 1;
-int exit_on_error     = 0;
-int print_stats       = 1;
-int qp_hist           = 0;
-
-static int file_overwrite     = 0;
-static int file_skip          = 0;
-static int video_discard      = 0;
-static int intra_dc_precision = 8;
-static int using_stdin        = 0;
-static int input_sync;
-
-static void uninit_options(OptionsContext *o)
-{
-    const OptionDef *po = options;
-    int i;
-
-    /* all OPT_SPEC and OPT_STRING can be freed in generic way */
-    while (po->name) {
-        void *dst = (uint8_t*)o + po->u.off;
-
-        if (po->flags & OPT_SPEC) {
-            SpecifierOpt **so = dst;
-            int i, *count = (int*)(so + 1);
-            for (i = 0; i < *count; i++) {
-                av_freep(&(*so)[i].specifier);
-                if (po->flags & OPT_STRING)
-                    av_freep(&(*so)[i].u.str);
-            }
-            av_freep(so);
-            *count = 0;
-        } else if (po->flags & OPT_OFFSET && po->flags & OPT_STRING)
-            av_freep(dst);
-        po++;
-    }
-
-    for (i = 0; i < o->nb_stream_maps; i++)
-        av_freep(&o->stream_maps[i].linklabel);
-    av_freep(&o->stream_maps);
-    av_freep(&o->meta_data_maps);
-    av_freep(&o->streamid_map);
-}
-
-static void init_options(OptionsContext *o)
-{
-    memset(o, 0, sizeof(*o));
-
-    o->mux_max_delay  = 0.7;
-    o->start_time     = AV_NOPTS_VALUE;
-    o->recording_time = INT64_MAX;
-    o->limit_filesize = UINT64_MAX;
-    o->chapters_input_file = INT_MAX;
-    o->accurate_seek  = 1;
-}
-
-/* return a copy of the input with the stream specifiers removed from the keys */
-static AVDictionary *strip_specifiers(AVDictionary *dict)
-{
-    AVDictionaryEntry *e = NULL;
-    AVDictionary    *ret = NULL;
-
-    while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))) {
-        char *p = strchr(e->key, ':');
-
-        if (p)
-            *p = 0;
-        av_dict_set(&ret, e->key, e->value, 0);
-        if (p)
-            *p = ':';
-    }
-    return ret;
-}
-
-static double parse_frame_aspect_ratio(const char *arg)
-{
-    int x = 0, y = 0;
-    double ar = 0;
-    const char *p;
-    char *end;
-
-    p = strchr(arg, ':');
-    if (p) {
-        x = strtol(arg, &end, 10);
-        if (end == p)
-            y = strtol(end + 1, &end, 10);
-        if (x > 0 && y > 0)
-            ar = (double)x / (double)y;
-    } else
-        ar = strtod(arg, NULL);
-
-    if (!ar) {
-        av_log(NULL, AV_LOG_FATAL, "Incorrect aspect ratio specification.\n");
-        exit_program(1);
-    }
-    return ar;
-}
-
-static int show_hwaccels(void *optctx, const char *opt, const char *arg)
-{
-    int i;
-
-    printf("Supported hardware acceleration:\n");
-    for (i = 0; hwaccels[i].name; i++) {
-        printf("%s\n", hwaccels[i].name);
-    }
-    printf("\n");
-    return 0;
-}
-
-static int opt_audio_codec(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    return parse_option(o, "codec:a", arg, options);
-}
-
-static int opt_video_codec(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    return parse_option(o, "codec:v", arg, options);
-}
-
-static int opt_subtitle_codec(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    return parse_option(o, "codec:s", arg, options);
-}
-
-static int opt_data_codec(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    return parse_option(o, "codec:d", arg, options);
-}
-
-static int opt_map(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    StreamMap *m = NULL;
-    int i, negative = 0, file_idx;
-    int sync_file_idx = -1, sync_stream_idx;
-    char *p, *sync;
-    char *map;
-
-    if (*arg == '-') {
-        negative = 1;
-        arg++;
-    }
-    map = av_strdup(arg);
-    if (!map)
-        return AVERROR(ENOMEM);
-
-    /* parse sync stream first, just pick first matching stream */
-    if (sync = strchr(map, ',')) {
-        *sync = 0;
-        sync_file_idx = strtol(sync + 1, &sync, 0);
-        if (sync_file_idx >= nb_input_files || sync_file_idx < 0) {
-            av_log(NULL, AV_LOG_FATAL, "Invalid sync file index: %d.\n", sync_file_idx);
-            exit_program(1);
-        }
-        if (*sync)
-            sync++;
-        for (i = 0; i < input_files[sync_file_idx]->nb_streams; i++)
-            if (check_stream_specifier(input_files[sync_file_idx]->ctx,
-                                       input_files[sync_file_idx]->ctx->streams[i], sync) == 1) {
-                sync_stream_idx = i;
-                break;
-            }
-        if (i == input_files[sync_file_idx]->nb_streams) {
-            av_log(NULL, AV_LOG_FATAL, "Sync stream specification in map %s does not "
-                                       "match any streams.\n", arg);
-            exit_program(1);
-        }
-    }
-
-
-    if (map[0] == '[') {
-        /* this mapping refers to lavfi output */
-        const char *c = map + 1;
-        GROW_ARRAY(o->stream_maps, o->nb_stream_maps);
-        m = &o->stream_maps[o->nb_stream_maps - 1];
-        m->linklabel = av_get_token(&c, "]");
-        if (!m->linklabel) {
-            av_log(NULL, AV_LOG_ERROR, "Invalid output link label: %s.\n", map);
-            exit_program(1);
-        }
-    } else {
-        file_idx = strtol(map, &p, 0);
-        if (file_idx >= nb_input_files || file_idx < 0) {
-            av_log(NULL, AV_LOG_FATAL, "Invalid input file index: %d.\n", file_idx);
-            exit_program(1);
-        }
-        if (negative)
-            /* disable some already defined maps */
-            for (i = 0; i < o->nb_stream_maps; i++) {
-                m = &o->stream_maps[i];
-                if (file_idx == m->file_index &&
-                    check_stream_specifier(input_files[m->file_index]->ctx,
-                                           input_files[m->file_index]->ctx->streams[m->stream_index],
-                                           *p == ':' ? p + 1 : p) > 0)
-                    m->disabled = 1;
-            }
-        else
-            for (i = 0; i < input_files[file_idx]->nb_streams; i++) {
-                if (check_stream_specifier(input_files[file_idx]->ctx, input_files[file_idx]->ctx->streams[i],
-                            *p == ':' ? p + 1 : p) <= 0)
-                    continue;
-                GROW_ARRAY(o->stream_maps, o->nb_stream_maps);
-                m = &o->stream_maps[o->nb_stream_maps - 1];
-
-                m->file_index   = file_idx;
-                m->stream_index = i;
-
-                if (sync_file_idx >= 0) {
-                    m->sync_file_index   = sync_file_idx;
-                    m->sync_stream_index = sync_stream_idx;
-                } else {
-                    m->sync_file_index   = file_idx;
-                    m->sync_stream_index = i;
-                }
-            }
-    }
-
-    if (!m) {
-        av_log(NULL, AV_LOG_FATAL, "Stream map '%s' matches no streams.\n", arg);
-        exit_program(1);
-    }
-
-    av_freep(&map);
-    return 0;
-}
-
-static int opt_attach(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    GROW_ARRAY(o->attachments, o->nb_attachments);
-    o->attachments[o->nb_attachments - 1] = arg;
-    return 0;
-}
-
-#if CONFIG_VAAPI
-static int opt_vaapi_device(void *optctx, const char *opt, const char *arg)
-{
-    int err;
-    err = vaapi_device_init(arg);
-    if (err < 0)
-        exit_program(1);
-    return 0;
-}
-#endif
-
-/**
- * Parse a metadata specifier passed as 'arg' parameter.
- * @param arg  metadata string to parse
- * @param type metadata type is written here -- g(lobal)/s(tream)/c(hapter)/p(rogram)
- * @param index for type c/p, chapter/program index is written here
- * @param stream_spec for type s, the stream specifier is written here
- */
-static void parse_meta_type(char *arg, char *type, int *index, const char **stream_spec)
-{
-    if (*arg) {
-        *type = *arg;
-        switch (*arg) {
-        case 'g':
-            break;
-        case 's':
-            if (*(++arg) && *arg != ':') {
-                av_log(NULL, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", arg);
-                exit_program(1);
-            }
-            *stream_spec = *arg == ':' ? arg + 1 : "";
-            break;
-        case 'c':
-        case 'p':
-            if (*(++arg) == ':')
-                *index = strtol(++arg, NULL, 0);
-            break;
-        default:
-            av_log(NULL, AV_LOG_FATAL, "Invalid metadata type %c.\n", *arg);
-            exit_program(1);
-        }
-    } else
-        *type = 'g';
-}
-
-static int copy_metadata(char *outspec, char *inspec, AVFormatContext *oc, AVFormatContext *ic, OptionsContext *o)
-{
-    AVDictionary **meta_in = NULL;
-    AVDictionary **meta_out;
-    int i, ret = 0;
-    char type_in, type_out;
-    const char *istream_spec = NULL, *ostream_spec = NULL;
-    int idx_in = 0, idx_out = 0;
-
-    parse_meta_type(inspec,  &type_in,  &idx_in,  &istream_spec);
-    parse_meta_type(outspec, &type_out, &idx_out, &ostream_spec);
-
-    if (type_in == 'g' || type_out == 'g')
-        o->metadata_global_manual = 1;
-    if (type_in == 's' || type_out == 's')
-        o->metadata_streams_manual = 1;
-    if (type_in == 'c' || type_out == 'c')
-        o->metadata_chapters_manual = 1;
-
-    /* ic is NULL when just disabling automatic mappings */
-    if (!ic)
-        return 0;
-
-#define METADATA_CHECK_INDEX(index, nb_elems, desc)\
-    if ((index) < 0 || (index) >= (nb_elems)) {\
-        av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
-                (desc), (index));\
-        exit_program(1);\
-    }
-
-#define SET_DICT(type, meta, context, index)\
-        switch (type) {\
-        case 'g':\
-            meta = &context->metadata;\
-            break;\
-        case 'c':\
-            METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
-            meta = &context->chapters[index]->metadata;\
-            break;\
-        case 'p':\
-            METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
-            meta = &context->programs[index]->metadata;\
-            break;\
-        case 's':\
-            break; /* handled separately below */ \
-        default: av_assert0(0);\
-        }\
-
-    SET_DICT(type_in, meta_in, ic, idx_in);
-    SET_DICT(type_out, meta_out, oc, idx_out);
-
-    /* for input streams choose first matching stream */
-    if (type_in == 's') {
-        for (i = 0; i < ic->nb_streams; i++) {
-            if ((ret = check_stream_specifier(ic, ic->streams[i], istream_spec)) > 0) {
-                meta_in = &ic->streams[i]->metadata;
-                break;
-            } else if (ret < 0)
-                exit_program(1);
-        }
-        if (!meta_in) {
-            av_log(NULL, AV_LOG_FATAL, "Stream specifier %s does not match  any streams.\n", istream_spec);
-            exit_program(1);
-        }
-    }
-
-    if (type_out == 's') {
-        for (i = 0; i < oc->nb_streams; i++) {
-            if ((ret = check_stream_specifier(oc, oc->streams[i], ostream_spec)) > 0) {
-                meta_out = &oc->streams[i]->metadata;
-                av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
-            } else if (ret < 0)
-                exit_program(1);
-        }
-    } else
-        av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
-
-    return 0;
-}
-
-static AVCodec *find_codec_or_die(const char *name, enum AVMediaType type, int encoder)
-{
-    const AVCodecDescriptor *desc;
-    const char *codec_string = encoder ? "encoder" : "decoder";
-    AVCodec *codec;
-
-    codec = encoder ?
-        avcodec_find_encoder_by_name(name) :
-        avcodec_find_decoder_by_name(name);
-
-    if (!codec && (desc = avcodec_descriptor_get_by_name(name))) {
-        codec = encoder ? avcodec_find_encoder(desc->id) :
-                          avcodec_find_decoder(desc->id);
-        if (codec)
-            av_log(NULL, AV_LOG_VERBOSE, "Matched %s '%s' for codec '%s'.\n",
-                   codec_string, codec->name, desc->name);
-    }
-
-    if (!codec) {
-        av_log(NULL, AV_LOG_FATAL, "Unknown %s '%s'\n", codec_string, name);
-        exit_program(1);
-    }
-    if (codec->type != type) {
-        av_log(NULL, AV_LOG_FATAL, "Invalid %s type '%s'\n", codec_string, name);
-        exit_program(1);
-    }
-    return codec;
-}
-
-static AVCodec *choose_decoder(OptionsContext *o, AVFormatContext *s, AVStream *st)
-{
-    char *codec_name = NULL;
-
-    MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
-    if (codec_name) {
-        AVCodec *codec = find_codec_or_die(codec_name, st->codecpar->codec_type, 0);
-        st->codecpar->codec_id = codec->id;
-        return codec;
-    } else
-        return avcodec_find_decoder(st->codecpar->codec_id);
-}
-
-/* Add all the streams from the given input file to the global
- * list of input streams. */
-static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
-{
-    int i, ret;
-
-    for (i = 0; i < ic->nb_streams; i++) {
-        AVStream *st = ic->streams[i];
-        AVCodecParameters *par = st->codecpar;
-        InputStream *ist = av_mallocz(sizeof(*ist));
-        char *framerate = NULL, *hwaccel = NULL, *hwaccel_device = NULL;
-        char *hwaccel_output_format = NULL;
-        char *codec_tag = NULL;
-        char *next;
-
-        if (!ist)
-            exit_program(1);
-
-        GROW_ARRAY(input_streams, nb_input_streams);
-        input_streams[nb_input_streams - 1] = ist;
-
-        ist->st = st;
-        ist->file_index = nb_input_files;
-        ist->discard = 1;
-        st->discard  = AVDISCARD_ALL;
-        ist->nb_samples = 0;
-        ist->min_pts = INT64_MAX;
-        ist->max_pts = INT64_MIN;
-
-        ist->ts_scale = 1.0;
-        MATCH_PER_STREAM_OPT(ts_scale, dbl, ist->ts_scale, ic, st);
-
-        ist->autorotate = 1;
-        MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
-
-        MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
-        if (codec_tag) {
-            uint32_t tag = strtol(codec_tag, &next, 0);
-            if (*next)
-                tag = AV_RL32(codec_tag);
-            st->codecpar->codec_tag = tag;
-        }
-
-        ist->dec = choose_decoder(o, ic, st);
-        ist->decoder_opts = filter_codec_opts(o->g->codec_opts, par->codec_id, ic, st, ist->dec);
-
-        ist->dec_ctx = avcodec_alloc_context3(ist->dec);
-        if (!ist->dec_ctx) {
-            av_log(NULL, AV_LOG_ERROR, "Error allocating the decoder context.\n");
-            exit_program(1);
-        }
-
-        ret = avcodec_parameters_to_context(ist->dec_ctx, par);
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_ERROR, "Error initializing the decoder context.\n");
-            exit_program(1);
-        }
-
-        switch (par->codec_type) {
-        case AVMEDIA_TYPE_VIDEO:
-            MATCH_PER_STREAM_OPT(frame_rates, str, framerate, ic, st);
-            if (framerate && av_parse_video_rate(&ist->framerate,
-                                                 framerate) < 0) {
-                av_log(NULL, AV_LOG_ERROR, "Error parsing framerate %s.\n",
-                       framerate);
-                exit_program(1);
-            }
-
-            MATCH_PER_STREAM_OPT(hwaccels, str, hwaccel, ic, st);
-            if (hwaccel) {
-                if (!strcmp(hwaccel, "none"))
-                    ist->hwaccel_id = HWACCEL_NONE;
-                else if (!strcmp(hwaccel, "auto"))
-                    ist->hwaccel_id = HWACCEL_AUTO;
-                else {
-                    int i;
-                    for (i = 0; hwaccels[i].name; i++) {
-                        if (!strcmp(hwaccels[i].name, hwaccel)) {
-                            ist->hwaccel_id = hwaccels[i].id;
-                            break;
-                        }
-                    }
-
-                    if (!ist->hwaccel_id) {
-                        av_log(NULL, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
-                               hwaccel);
-                        av_log(NULL, AV_LOG_FATAL, "Supported hwaccels: ");
-                        for (i = 0; hwaccels[i].name; i++)
-                            av_log(NULL, AV_LOG_FATAL, "%s ", hwaccels[i].name);
-                        av_log(NULL, AV_LOG_FATAL, "\n");
-                        exit_program(1);
-                    }
-                }
-            }
-
-            MATCH_PER_STREAM_OPT(hwaccel_devices, str, hwaccel_device, ic, st);
-            if (hwaccel_device) {
-                ist->hwaccel_device = av_strdup(hwaccel_device);
-                if (!ist->hwaccel_device)
-                    exit_program(1);
-            }
-
-            MATCH_PER_STREAM_OPT(hwaccel_output_formats, str,
-                                 hwaccel_output_format, ic, st);
-            if (hwaccel_output_format) {
-                ist->hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
-                if (ist->hwaccel_output_format == AV_PIX_FMT_NONE) {
-                    av_log(NULL, AV_LOG_FATAL, "Unrecognised hwaccel output "
-                           "format: %s", hwaccel_output_format);
-                }
-            } else {
-                ist->hwaccel_output_format = AV_PIX_FMT_NONE;
-            }
-
-            ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
-
-            break;
-        case AVMEDIA_TYPE_AUDIO:
-            guess_input_channel_layout(ist);
-            break;
-        case AVMEDIA_TYPE_DATA:
-        case AVMEDIA_TYPE_SUBTITLE:
-        case AVMEDIA_TYPE_ATTACHMENT:
-        case AVMEDIA_TYPE_UNKNOWN:
-            break;
-        default:
-            abort();
-        }
-    }
-}
-
-static void assert_file_overwrite(const char *filename)
-{
-    if (file_overwrite && file_skip) {
-        fprintf(stderr, "Error, both -y and -n supplied. Exiting.\n");
-        exit_program(1);
-    }
-
-    if (!file_overwrite &&
-        (!strchr(filename, ':') || filename[1] == ':' ||
-         av_strstart(filename, "file:", NULL))) {
-        if (avio_check(filename, 0) == 0) {
-            if (!using_stdin && !file_skip) {
-                fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
-                fflush(stderr);
-                if (!read_yesno()) {
-                    fprintf(stderr, "Not overwriting - exiting\n");
-                    exit_program(1);
-                }
-            }
-            else {
-                fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
-                exit_program(1);
-            }
-        }
-    }
-}
-
-static void dump_attachment(AVStream *st, const char *filename)
-{
-    int ret;
-    AVIOContext *out = NULL;
-    AVDictionaryEntry *e;
-
-    if (!st->codecpar->extradata_size) {
-        av_log(NULL, AV_LOG_WARNING, "No extradata to dump in stream #%d:%d.\n",
-               nb_input_files - 1, st->index);
-        return;
-    }
-    if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
-        filename = e->value;
-    if (!*filename) {
-        av_log(NULL, AV_LOG_FATAL, "No filename specified and no 'filename' tag"
-               "in stream #%d:%d.\n", nb_input_files - 1, st->index);
-        exit_program(1);
-    }
-
-    assert_file_overwrite(filename);
-
-    if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
-        av_log(NULL, AV_LOG_FATAL, "Could not open file %s for writing.\n",
-               filename);
-        exit_program(1);
-    }
-
-    avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
-    avio_flush(out);
-    avio_close(out);
-}
-
-static int open_input_file(OptionsContext *o, const char *filename)
-{
-    InputFile *f;
-    AVFormatContext *ic;
-    AVInputFormat *file_iformat = NULL;
-    int err, i, ret;
-    int64_t timestamp;
-    uint8_t buf[128];
-    AVDictionary **opts;
-    AVDictionary *unused_opts = NULL;
-    AVDictionaryEntry *e = NULL;
-    int orig_nb_streams;                     // number of streams before avformat_find_stream_info
-
-    if (o->format) {
-        if (!(file_iformat = av_find_input_format(o->format))) {
-            av_log(NULL, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
-            exit_program(1);
-        }
-    }
-
-    if (!strcmp(filename, "-"))
-        filename = "pipe:";
-
-    using_stdin |= !strncmp(filename, "pipe:", 5) ||
-                    !strcmp(filename, "/dev/stdin");
-
-    /* get default parameters from command line */
-    ic = avformat_alloc_context();
-    if (!ic) {
-        print_error(filename, AVERROR(ENOMEM));
-        exit_program(1);
-    }
-    if (o->nb_audio_sample_rate) {
-        snprintf(buf, sizeof(buf), "%d", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i);
-        av_dict_set(&o->g->format_opts, "sample_rate", buf, 0);
-    }
-    if (o->nb_audio_channels) {
-        /* because we set audio_channels based on both the "ac" and
-         * "channel_layout" options, we need to check that the specified
-         * demuxer actually has the "channels" option before setting it */
-        if (file_iformat && file_iformat->priv_class &&
-            av_opt_find(&file_iformat->priv_class, "channels", NULL, 0,
-                        AV_OPT_SEARCH_FAKE_OBJ)) {
-            snprintf(buf, sizeof(buf), "%d",
-                     o->audio_channels[o->nb_audio_channels - 1].u.i);
-            av_dict_set(&o->g->format_opts, "channels", buf, 0);
-        }
-    }
-    if (o->nb_frame_rates) {
-        /* set the format-level framerate option;
-         * this is important for video grabbers, e.g. x11 */
-        if (file_iformat && file_iformat->priv_class &&
-            av_opt_find(&file_iformat->priv_class, "framerate", NULL, 0,
-                        AV_OPT_SEARCH_FAKE_OBJ)) {
-            av_dict_set(&o->g->format_opts, "framerate",
-                        o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
-        }
-    }
-    if (o->nb_frame_sizes) {
-        av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
-    }
-    if (o->nb_frame_pix_fmts)
-        av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
-
-    ic->flags |= AVFMT_FLAG_NONBLOCK;
-    ic->interrupt_callback = int_cb;
-
-    /* open the input file with generic Libav function */
-    err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
-    if (err < 0) {
-        print_error(filename, err);
-        exit_program(1);
-    }
-    assert_avoptions(o->g->format_opts);
-
-    /* apply forced codec ids */
-    for (i = 0; i < ic->nb_streams; i++)
-        choose_decoder(o, ic, ic->streams[i]);
-
-    /* Set AVCodecContext options for avformat_find_stream_info */
-    opts = setup_find_stream_info_opts(ic, o->g->codec_opts);
-    orig_nb_streams = ic->nb_streams;
-
-    /* If not enough info to get the stream parameters, we decode the
-       first frames to get it. (used in mpeg case for example) */
-    ret = avformat_find_stream_info(ic, opts);
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_FATAL, "%s: could not find codec parameters\n", filename);
-        avformat_close_input(&ic);
-        exit_program(1);
-    }
-
-    timestamp = (o->start_time == AV_NOPTS_VALUE) ? 0 : o->start_time;
-    /* add the stream start time */
-    if (ic->start_time != AV_NOPTS_VALUE)
-        timestamp += ic->start_time;
-
-    /* if seeking requested, we execute it */
-    if (o->start_time != AV_NOPTS_VALUE) {
-        ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
-                   filename, (double)timestamp / AV_TIME_BASE);
-        }
-    }
-
-    /* update the current parameters so that they match the one of the input stream */
-    add_input_streams(o, ic);
-
-    /* dump the file content */
-    av_dump_format(ic, nb_input_files, filename, 0);
-
-    GROW_ARRAY(input_files, nb_input_files);
-    f = av_mallocz(sizeof(*f));
-    if (!f)
-        exit_program(1);
-    input_files[nb_input_files - 1] = f;
-
-    f->ctx        = ic;
-    f->ist_index  = nb_input_streams - ic->nb_streams;
-    f->start_time = o->start_time;
-    f->recording_time = o->recording_time;
-    f->ts_offset  = o->input_ts_offset - (copy_ts ? 0 : timestamp);
-    f->nb_streams = ic->nb_streams;
-    f->rate_emu   = o->rate_emu;
-    f->accurate_seek = o->accurate_seek;
-    f->loop = o->loop;
-    f->duration = 0;
-    f->time_base = (AVRational){ 1, 1 };
-
-    /* check if all codec options have been used */
-    unused_opts = strip_specifiers(o->g->codec_opts);
-    for (i = f->ist_index; i < nb_input_streams; i++) {
-        e = NULL;
-        while ((e = av_dict_get(input_streams[i]->decoder_opts, "", e,
-                                AV_DICT_IGNORE_SUFFIX)))
-            av_dict_set(&unused_opts, e->key, NULL, 0);
-    }
-
-    e = NULL;
-    while ((e = av_dict_get(unused_opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
-        const AVClass *class = avcodec_get_class();
-        const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
-                                             AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
-        if (!option)
-            continue;
-        if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
-            av_log(NULL, AV_LOG_ERROR, "Codec AVOption %s (%s) specified for "
-                   "input file #%d (%s) is not a decoding option.\n", e->key,
-                   option->help ? option->help : "", nb_input_files - 1,
-                   filename);
-            exit_program(1);
-        }
-
-        av_log(NULL, AV_LOG_WARNING, "Codec AVOption %s (%s) specified for "
-               "input file #%d (%s) has not been used for any stream. The most "
-               "likely reason is either wrong type (e.g. a video option with "
-               "no video streams) or that it is a private option of some decoder "
-               "which was not actually used for any stream.\n", e->key,
-               option->help ? option->help : "", nb_input_files - 1, filename);
-    }
-    av_dict_free(&unused_opts);
-
-    for (i = 0; i < o->nb_dump_attachment; i++) {
-        int j;
-
-        for (j = 0; j < ic->nb_streams; j++) {
-            AVStream *st = ic->streams[j];
-
-            if (check_stream_specifier(ic, st, o->dump_attachment[i].specifier) == 1)
-                dump_attachment(st, o->dump_attachment[i].u.str);
-        }
-    }
-
-    for (i = 0; i < orig_nb_streams; i++)
-        av_dict_free(&opts[i]);
-    av_freep(&opts);
-
-    return 0;
-}
-
-static uint8_t *get_line(AVIOContext *s)
-{
-    AVIOContext *line;
-    uint8_t *buf;
-    char c;
-
-    if (avio_open_dyn_buf(&line) < 0) {
-        av_log(NULL, AV_LOG_FATAL, "Could not alloc buffer for reading preset.\n");
-        exit_program(1);
-    }
-
-    while ((c = avio_r8(s)) && c != '\n')
-        avio_w8(line, c);
-    avio_w8(line, 0);
-    avio_close_dyn_buf(line, &buf);
-
-    return buf;
-}
-
-static int get_preset_file_2(const char *preset_name, const char *codec_name, AVIOContext **s)
-{
-    int i, ret = -1;
-    char filename[1000];
-    const char *base[3] = { getenv("AVCONV_DATADIR"),
-                            getenv("HOME"),
-                            AVCONV_DATADIR,
-                            };
-
-    for (i = 0; i < FF_ARRAY_ELEMS(base) && ret < 0; i++) {
-        if (!base[i])
-            continue;
-        if (codec_name) {
-            snprintf(filename, sizeof(filename), "%s%s/%s-%s.avpreset", base[i],
-                     i != 1 ? "" : "/.avconv", codec_name, preset_name);
-            ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
-        }
-        if (ret < 0) {
-            snprintf(filename, sizeof(filename), "%s%s/%s.avpreset", base[i],
-                     i != 1 ? "" : "/.avconv", preset_name);
-            ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
-        }
-    }
-    return ret;
-}
-
-static int choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *ost)
-{
-    enum AVMediaType type = ost->st->codecpar->codec_type;
-    char *codec_name = NULL;
-
-    if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
-        MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st);
-        if (!codec_name) {
-            ost->st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->filename,
-                                                         NULL, ost->st->codecpar->codec_type);
-            ost->enc = avcodec_find_encoder(ost->st->codecpar->codec_id);
-            if (!ost->enc) {
-                av_log(NULL, AV_LOG_FATAL, "Automatic encoder selection failed for "
-                       "output stream #%d:%d. Default encoder for format %s is "
-                       "probably disabled. Please choose an encoder manually.\n",
-                       ost->file_index, ost->index, s->oformat->name);
-                return AVERROR_ENCODER_NOT_FOUND;
-            }
-        } else if (!strcmp(codec_name, "copy"))
-            ost->stream_copy = 1;
-        else {
-            ost->enc = find_codec_or_die(codec_name, ost->st->codecpar->codec_type, 1);
-            ost->st->codecpar->codec_id = ost->enc->id;
-        }
-
-        ost->encoding_needed = !ost->stream_copy;
-    } else {
-        /* no encoding supported for other media types */
-        ost->stream_copy     = 1;
-        ost->encoding_needed = 0;
-    }
-
-    return 0;
-}
-
-static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type)
-{
-    OutputStream *ost;
-    AVStream *st = avformat_new_stream(oc, NULL);
-    int idx      = oc->nb_streams - 1, ret = 0;
-    const char *bsfs = NULL;
-    char *next, *codec_tag = NULL;
-    double qscale = -1;
-    int bitrate = 0;
-
-    if (!st) {
-        av_log(NULL, AV_LOG_FATAL, "Could not alloc stream.\n");
-        exit_program(1);
-    }
-
-    if (oc->nb_streams - 1 < o->nb_streamid_map)
-        st->id = o->streamid_map[oc->nb_streams - 1];
-
-    GROW_ARRAY(output_streams, nb_output_streams);
-    if (!(ost = av_mallocz(sizeof(*ost))))
-        exit_program(1);
-    output_streams[nb_output_streams - 1] = ost;
-
-    ost->file_index = nb_output_files - 1;
-    ost->index      = idx;
-    ost->st         = st;
-    st->codecpar->codec_type = type;
-
-    ret = choose_encoder(o, oc, ost);
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_FATAL, "Error selecting an encoder for stream "
-               "%d:%d\n", ost->file_index, ost->index);
-        exit_program(1);
-    }
-
-    ost->enc_ctx = avcodec_alloc_context3(ost->enc);
-    if (!ost->enc_ctx) {
-        av_log(NULL, AV_LOG_ERROR, "Error allocating the encoding context.\n");
-        exit_program(1);
-    }
-    ost->enc_ctx->codec_type = type;
-
-    if (ost->enc) {
-        AVIOContext *s = NULL;
-        char *buf = NULL, *arg = NULL, *preset = NULL;
-
-        ost->encoder_opts  = filter_codec_opts(o->g->codec_opts, ost->enc->id, oc, st, ost->enc);
-
-        MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);
-        if (preset && (!(ret = get_preset_file_2(preset, ost->enc->name, &s)))) {
-            do  {
-                buf = get_line(s);
-                if (!buf[0] || buf[0] == '#') {
-                    av_free(buf);
-                    continue;
-                }
-                if (!(arg = strchr(buf, '='))) {
-                    av_log(NULL, AV_LOG_FATAL, "Invalid line found in the preset file.\n");
-                    exit_program(1);
-                }
-                *arg++ = 0;
-                av_dict_set(&ost->encoder_opts, buf, arg, AV_DICT_DONT_OVERWRITE);
-                av_free(buf);
-            } while (!s->eof_reached);
-            avio_close(s);
-        }
-        if (ret) {
-            av_log(NULL, AV_LOG_FATAL,
-                   "Preset %s specified for stream %d:%d, but could not be opened.\n",
-                   preset, ost->file_index, ost->index);
-            exit_program(1);
-        }
-    } else {
-        ost->encoder_opts = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st, NULL);
-    }
-
-    ost->max_frames = INT64_MAX;
-    MATCH_PER_STREAM_OPT(max_frames, i64, ost->max_frames, oc, st);
-
-    MATCH_PER_STREAM_OPT(bitstream_filters, str, bsfs, oc, st);
-    while (bsfs && *bsfs) {
-        const AVBitStreamFilter *filter;
-        const char *bsf, *bsf_options_str, *bsf_name;
-        AVDictionary *bsf_options = NULL;
-
-        bsf = bsf_options_str = av_get_token(&bsfs, ",");
-        if (!bsf)
-            exit_program(1);
-        bsf_name = av_get_token(&bsf_options_str, "=");
-        if (!bsf_name)
-            exit_program(1);
-
-        filter = av_bsf_get_by_name(bsf_name);
-        if (!filter) {
-            av_log(NULL, AV_LOG_FATAL, "Unknown bitstream filter %s\n", bsf_name);
-            exit_program(1);
-        }
-        if (*bsf_options_str++) {
-            ret = av_dict_parse_string(&bsf_options, bsf_options_str, "=", ":", 0);
-            if (ret < 0) {
-                av_log(NULL, AV_LOG_ERROR, "Error parsing options for bitstream filter %s\n", bsf_name);
-                exit_program(1);
-            }
-        }
-        av_freep(&bsf);
-
-        ost->bsf_ctx = av_realloc_array(ost->bsf_ctx,
-                                        ost->nb_bitstream_filters + 1,
-                                        sizeof(*ost->bsf_ctx));
-        if (!ost->bsf_ctx)
-            exit_program(1);
-
-        ret = av_bsf_alloc(filter, &ost->bsf_ctx[ost->nb_bitstream_filters]);
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_ERROR, "Error allocating a bistream filter context\n");
-            exit_program(1);
-        }
-        ost->nb_bitstream_filters++;
-
-        if (bsf_options) {
-            ret = av_opt_set_dict(ost->bsf_ctx[ost->nb_bitstream_filters-1]->priv_data, &bsf_options);
-            if (ret < 0) {
-                av_log(NULL, AV_LOG_ERROR, "Error setting options for bitstream filter %s\n", bsf_name);
-                exit_program(1);
-            }
-            assert_avoptions(bsf_options);
-            av_dict_free(&bsf_options);
-        }
-        av_freep(&bsf_name);
-
-        if (*bsfs)
-            bsfs++;
-    }
-
-    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);
-        ost->enc_ctx->codec_tag = tag;
-    }
-
-    MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st);
-    if (qscale >= 0) {
-        ost->enc_ctx->flags |= AV_CODEC_FLAG_QSCALE;
-        ost->enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
-    }
-
-    MATCH_PER_STREAM_OPT(bitrates, i, bitrate, oc, st);
-    if (bitrate > 0) {
-        if (ost->stream_copy)
-            ost->bitrate_override = bitrate;
-        else
-            ost->enc_ctx->bit_rate = bitrate;
-    }
-
-    ost->max_muxing_queue_size = 128;
-    MATCH_PER_STREAM_OPT(max_muxing_queue_size, i, ost->max_muxing_queue_size, oc, st);
-    ost->max_muxing_queue_size *= sizeof(AVPacket);
-
-    if (oc->oformat->flags & AVFMT_GLOBALHEADER)
-        ost->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
-
-    av_opt_get_int(o->g->sws_opts, "sws_flags", 0, &ost->sws_flags);
-
-    av_dict_copy(&ost->resample_opts, o->g->resample_opts, 0);
-
-    ost->pix_fmts[0] = ost->pix_fmts[1] = AV_PIX_FMT_NONE;
-    ost->last_mux_dts = AV_NOPTS_VALUE;
-
-    ost->muxing_queue = av_fifo_alloc(8 * sizeof(AVPacket));
-    if (!ost->muxing_queue)
-        exit_program(1);
-
-    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++;
-    }
-}
-
-/* read file contents into a string */
-static uint8_t *read_file(const char *filename)
-{
-    AVIOContext *pb      = NULL;
-    AVIOContext *dyn_buf = NULL;
-    int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
-    uint8_t buf[1024], *str;
-
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
-        return NULL;
-    }
-
-    ret = avio_open_dyn_buf(&dyn_buf);
-    if (ret < 0) {
-        avio_closep(&pb);
-        return NULL;
-    }
-    while ((ret = avio_read(pb, buf, sizeof(buf))) > 0)
-        avio_write(dyn_buf, buf, ret);
-    avio_w8(dyn_buf, 0);
-    avio_closep(&pb);
-
-    ret = avio_close_dyn_buf(dyn_buf, &str);
-    if (ret < 0)
-        return NULL;
-    return str;
-}
-
-static char *get_ost_filters(OptionsContext *o, AVFormatContext *oc,
-                             OutputStream *ost)
-{
-    AVStream *st = ost->st;
-    char *filter = NULL, *filter_script = NULL;
-
-    MATCH_PER_STREAM_OPT(filter_scripts, str, filter_script, oc, st);
-    MATCH_PER_STREAM_OPT(filters, str, filter, oc, st);
-
-    if (filter_script && filter) {
-        av_log(NULL, AV_LOG_ERROR, "Both -filter and -filter_script set for "
-               "output stream #%d:%d.\n", nb_output_files, st->index);
-        exit_program(1);
-    }
-
-    if (filter_script)
-        return read_file(filter_script);
-    else if (filter)
-        return av_strdup(filter);
-
-    return av_strdup(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ?
-                     "null" : "anull");
-}
-
-static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc)
-{
-    AVStream *st;
-    OutputStream *ost;
-    AVCodecContext *video_enc;
-    char *frame_aspect_ratio = NULL;
-
-    ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO);
-    st  = ost->st;
-    video_enc = ost->enc_ctx;
-
-    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);
-
-    if (!ost->stream_copy) {
-        const char *p = NULL;
-        char *frame_rate = NULL, *frame_size = NULL;
-        char *frame_pix_fmt = NULL;
-        char *intra_matrix = NULL, *inter_matrix = NULL;
-        int do_pass = 0;
-        int i;
-
-        MATCH_PER_STREAM_OPT(frame_rates, str, frame_rate, oc, st);
-        if (frame_rate && av_parse_video_rate(&ost->frame_rate, frame_rate) < 0) {
-            av_log(NULL, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);
-            exit_program(1);
-        }
-
-        MATCH_PER_STREAM_OPT(frame_sizes, str, frame_size, oc, st);
-        if (frame_size && av_parse_video_size(&video_enc->width, &video_enc->height, frame_size) < 0) {
-            av_log(NULL, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
-            exit_program(1);
-        }
-
-        MATCH_PER_STREAM_OPT(frame_pix_fmts, str, frame_pix_fmt, oc, st);
-        if (frame_pix_fmt && (video_enc->pix_fmt = av_get_pix_fmt(frame_pix_fmt)) == AV_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;
-
-        MATCH_PER_STREAM_OPT(intra_matrices, str, intra_matrix, oc, st);
-        if (intra_matrix) {
-            if (!(video_enc->intra_matrix = av_mallocz(sizeof(*video_enc->intra_matrix) * 64))) {
-                av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for intra matrix.\n");
-                exit_program(1);
-            }
-            parse_matrix_coeffs(video_enc->intra_matrix, intra_matrix);
-        }
-        MATCH_PER_STREAM_OPT(inter_matrices, str, inter_matrix, oc, st);
-        if (inter_matrix) {
-            if (!(video_enc->inter_matrix = av_mallocz(sizeof(*video_enc->inter_matrix) * 64))) {
-                av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for inter matrix.\n");
-                exit_program(1);
-            }
-            parse_matrix_coeffs(video_enc->inter_matrix, inter_matrix);
-        }
-
-        MATCH_PER_STREAM_OPT(rc_overrides, str, p, oc, st);
-        for (i = 0; p; i++) {
-            int start, end, q;
-            int e = sscanf(p, "%d,%d,%d", &start, &end, &q);
-            if (e != 3) {
-                av_log(NULL, AV_LOG_FATAL, "error parsing rc_override\n");
-                exit_program(1);
-            }
-            video_enc->rc_override =
-                av_realloc(video_enc->rc_override,
-                           sizeof(RcOverride) * (i + 1));
-            if (!video_enc->rc_override) {
-                av_log(NULL, AV_LOG_FATAL, "Could not (re)allocate memory for rc_override.\n");
-                exit_program(1);
-            }
-            video_enc->rc_override[i].start_frame = start;
-            video_enc->rc_override[i].end_frame   = end;
-            if (q > 0) {
-                video_enc->rc_override[i].qscale         = q;
-                video_enc->rc_override[i].quality_factor = 1.0;
-            }
-            else {
-                video_enc->rc_override[i].qscale         = 0;
-                video_enc->rc_override[i].quality_factor = -q/100.0;
-            }
-            p = strchr(p, '/');
-            if (p) p++;
-        }
-        video_enc->rc_override_count = i;
-        video_enc->intra_dc_precision = intra_dc_precision - 8;
-
-        /* two pass mode */
-        MATCH_PER_STREAM_OPT(pass, i, do_pass, oc, st);
-        if (do_pass) {
-            if (do_pass == 1) {
-                video_enc->flags |= AV_CODEC_FLAG_PASS1;
-            } else {
-                video_enc->flags |= AV_CODEC_FLAG_PASS2;
-            }
-        }
-
-        MATCH_PER_STREAM_OPT(passlogfiles, str, ost->logfile_prefix, oc, st);
-        if (ost->logfile_prefix &&
-            !(ost->logfile_prefix = av_strdup(ost->logfile_prefix)))
-            exit_program(1);
-
-        if (do_pass) {
-            char logfilename[1024];
-            FILE *f;
-
-            snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
-                     ost->logfile_prefix ? ost->logfile_prefix :
-                                           DEFAULT_PASS_LOGFILENAME_PREFIX,
-                     i);
-            if (!strcmp(ost->enc->name, "libx264")) {
-                av_dict_set(&ost->encoder_opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
-            } else {
-                if (video_enc->flags & AV_CODEC_FLAG_PASS1) {
-                    f = fopen(logfilename, "wb");
-                    if (!f) {
-                        av_log(NULL, AV_LOG_FATAL, "Cannot write log file '%s' for pass-1 encoding: %s\n",
-                               logfilename, strerror(errno));
-                        exit_program(1);
-                    }
-                    ost->logfile = f;
-                } else {
-                    char  *logbuffer = read_file(logfilename);
-
-                    if (!logbuffer) {
-                        av_log(NULL, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
-                               logfilename);
-                        exit_program(1);
-                    }
-                    video_enc->stats_in = logbuffer;
-                }
-            }
-        }
-
-        MATCH_PER_STREAM_OPT(forced_key_frames, str, ost->forced_keyframes, oc, st);
-        if (ost->forced_keyframes)
-            ost->forced_keyframes = av_strdup(ost->forced_keyframes);
-
-        MATCH_PER_STREAM_OPT(force_fps, i, ost->force_fps, oc, st);
-
-        ost->top_field_first = -1;
-        MATCH_PER_STREAM_OPT(top_field_first, i, ost->top_field_first, oc, st);
-
-
-        ost->avfilter = get_ost_filters(o, oc, ost);
-        if (!ost->avfilter)
-            exit_program(1);
-    } else {
-        MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, ost->copy_initial_nonkeyframes, oc ,st);
-    }
-
-    return ost;
-}
-
-static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc)
-{
-    AVStream *st;
-    OutputStream *ost;
-    AVCodecContext *audio_enc;
-
-    ost = new_output_stream(o, oc, AVMEDIA_TYPE_AUDIO);
-    st  = ost->st;
-
-    audio_enc = ost->enc_ctx;
-    audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
-
-    if (!ost->stream_copy) {
-        char *sample_fmt = NULL;
-
-        MATCH_PER_STREAM_OPT(audio_channels, i, audio_enc->channels, oc, st);
-
-        MATCH_PER_STREAM_OPT(sample_fmts, str, sample_fmt, oc, st);
-        if (sample_fmt &&
-            (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
-            av_log(NULL, AV_LOG_FATAL, "Invalid sample format '%s'\n", sample_fmt);
-            exit_program(1);
-        }
-
-        MATCH_PER_STREAM_OPT(audio_sample_rate, i, audio_enc->sample_rate, oc, st);
-
-        ost->avfilter = get_ost_filters(o, oc, ost);
-        if (!ost->avfilter)
-            exit_program(1);
-    }
-
-    return ost;
-}
-
-static OutputStream *new_data_stream(OptionsContext *o, AVFormatContext *oc)
-{
-    OutputStream *ost;
-
-    ost = new_output_stream(o, oc, AVMEDIA_TYPE_DATA);
-    if (!ost->stream_copy) {
-        av_log(NULL, AV_LOG_FATAL, "Data stream encoding not supported yet (only streamcopy)\n");
-        exit_program(1);
-    }
-
-    return ost;
-}
-
-static OutputStream *new_attachment_stream(OptionsContext *o, AVFormatContext *oc)
-{
-    OutputStream *ost = new_output_stream(o, oc, AVMEDIA_TYPE_ATTACHMENT);
-    ost->stream_copy = 1;
-    ost->finished    = 1;
-    return ost;
-}
-
-static OutputStream *new_subtitle_stream(OptionsContext *o, AVFormatContext *oc)
-{
-    OutputStream *ost;
-    AVCodecContext *subtitle_enc;
-
-    ost = new_output_stream(o, oc, AVMEDIA_TYPE_SUBTITLE);
-    subtitle_enc = ost->enc_ctx;
-
-    subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
-
-    return ost;
-}
-
-/* arg format is "output-stream-index:streamid-value". */
-static int opt_streamid(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    int idx;
-    char *p;
-    char idx_str[16];
-
-    av_strlcpy(idx_str, arg, sizeof(idx_str));
-    p = strchr(idx_str, ':');
-    if (!p) {
-        av_log(NULL, AV_LOG_FATAL,
-               "Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
-               arg, opt);
-        exit_program(1);
-    }
-    *p++ = '\0';
-    idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, INT_MAX);
-    o->streamid_map = grow_array(o->streamid_map, sizeof(*o->streamid_map), &o->nb_streamid_map, idx+1);
-    o->streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);
-    return 0;
-}
-
-static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata)
-{
-    AVFormatContext *is = ifile->ctx;
-    AVFormatContext *os = ofile->ctx;
-    AVChapter **tmp;
-    int i;
-
-    tmp = av_realloc(os->chapters, sizeof(*os->chapters) * (is->nb_chapters + os->nb_chapters));
-    if (!tmp)
-        return AVERROR(ENOMEM);
-    os->chapters = tmp;
-
-    for (i = 0; i < is->nb_chapters; i++) {
-        AVChapter *in_ch = is->chapters[i], *out_ch;
-        int64_t start_time = (ofile->start_time == AV_NOPTS_VALUE) ? 0 : ofile->start_time;
-        int64_t ts_off   = av_rescale_q(start_time - ifile->ts_offset,
-                                       AV_TIME_BASE_Q, in_ch->time_base);
-        int64_t rt       = (ofile->recording_time == INT64_MAX) ? INT64_MAX :
-                           av_rescale_q(ofile->recording_time, AV_TIME_BASE_Q, in_ch->time_base);
-
-
-        if (in_ch->end < ts_off)
-            continue;
-        if (rt != INT64_MAX && in_ch->start > rt + ts_off)
-            break;
-
-        out_ch = av_mallocz(sizeof(AVChapter));
-        if (!out_ch)
-            return AVERROR(ENOMEM);
-
-        out_ch->id        = in_ch->id;
-        out_ch->time_base = in_ch->time_base;
-        out_ch->start     = FFMAX(0,  in_ch->start - ts_off);
-        out_ch->end       = FFMIN(rt, in_ch->end   - ts_off);
-
-        if (copy_metadata)
-            av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
-
-        os->chapters[os->nb_chapters++] = out_ch;
-    }
-    return 0;
-}
-
-static void init_output_filter(OutputFilter *ofilter, OptionsContext *o,
-                               AVFormatContext *oc)
-{
-    OutputStream *ost;
-
-    switch (ofilter->type) {
-    case AVMEDIA_TYPE_VIDEO: ost = new_video_stream(o, oc); break;
-    case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream(o, oc); break;
-    default:
-        av_log(NULL, AV_LOG_FATAL, "Only video and audio filters are supported "
-               "currently.\n");
-        exit_program(1);
-    }
-
-    ost->source_index = -1;
-    ost->filter       = ofilter;
-
-    ofilter->ost      = ost;
-    ofilter->format   = -1;
-
-    if (ost->stream_copy) {
-        av_log(NULL, AV_LOG_ERROR, "Streamcopy requested for output stream %d:%d, "
-               "which is fed from a complex filtergraph. Filtering and streamcopy "
-               "cannot be used together.\n", ost->file_index, ost->index);
-        exit_program(1);
-    }
-
-    avfilter_inout_free(&ofilter->out_tmp);
-}
-
-static int init_complex_filters(void)
-{
-    int i, ret = 0;
-
-    for (i = 0; i < nb_filtergraphs; i++) {
-        ret = init_complex_filtergraph(filtergraphs[i]);
-        if (ret < 0)
-            return ret;
-    }
-    return 0;
-}
-
-static int open_output_file(OptionsContext *o, const char *filename)
-{
-    AVFormatContext *oc;
-    int i, j, err;
-    AVOutputFormat *file_oformat;
-    OutputFile *of;
-    OutputStream *ost;
-    InputStream  *ist;
-    AVDictionary *unused_opts = NULL;
-    AVDictionaryEntry *e = NULL;
-
-    GROW_ARRAY(output_files, nb_output_files);
-    of = av_mallocz(sizeof(*of));
-    if (!of)
-        exit_program(1);
-    output_files[nb_output_files - 1] = of;
-
-    of->ost_index      = nb_output_streams;
-    of->recording_time = o->recording_time;
-    of->start_time     = o->start_time;
-    of->limit_filesize = o->limit_filesize;
-    of->shortest       = o->shortest;
-    av_dict_copy(&of->opts, o->g->format_opts, 0);
-
-    if (!strcmp(filename, "-"))
-        filename = "pipe:";
-
-    oc = avformat_alloc_context();
-    if (!oc) {
-        print_error(filename, AVERROR(ENOMEM));
-        exit_program(1);
-    }
-    of->ctx = oc;
-    if (o->recording_time != INT64_MAX)
-        oc->duration = o->recording_time;
-
-    if (o->format) {
-        file_oformat = av_guess_format(o->format, NULL, NULL);
-        if (!file_oformat) {
-            av_log(NULL, AV_LOG_FATAL, "Requested output format '%s' is not a suitable output format\n", o->format);
-            exit_program(1);
-        }
-    } else {
-        file_oformat = av_guess_format(NULL, filename, NULL);
-        if (!file_oformat) {
-            av_log(NULL, AV_LOG_FATAL, "Unable to find a suitable output format for '%s'\n",
-                   filename);
-            exit_program(1);
-        }
-    }
-
-    oc->oformat = file_oformat;
-    oc->interrupt_callback = int_cb;
-    av_strlcpy(oc->filename, filename, sizeof(oc->filename));
-
-    /* create streams for all unlabeled output pads */
-    for (i = 0; i < nb_filtergraphs; i++) {
-        FilterGraph *fg = filtergraphs[i];
-        for (j = 0; j < fg->nb_outputs; j++) {
-            OutputFilter *ofilter = fg->outputs[j];
-
-            if (!ofilter->out_tmp || ofilter->out_tmp->name)
-                continue;
-
-            switch (ofilter->type) {
-            case AVMEDIA_TYPE_VIDEO:    o->video_disable    = 1; break;
-            case AVMEDIA_TYPE_AUDIO:    o->audio_disable    = 1; break;
-            case AVMEDIA_TYPE_SUBTITLE: o->subtitle_disable = 1; break;
-            }
-            init_output_filter(ofilter, o, oc);
-        }
-    }
-
-    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;\
-            input_streams[index]->st->discard = AVDISCARD_NONE;\
-        }
-
-        /* video: highest resolution */
-        if (!o->video_disable && oc->oformat->video_codec != AV_CODEC_ID_NONE) {
-            int area = 0, idx = -1;
-            for (i = 0; i < nb_input_streams; i++) {
-                ist = input_streams[i];
-                if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
-                    ist->st->codecpar->width * ist->st->codecpar->height > area) {
-                    area = ist->st->codecpar->width * ist->st->codecpar->height;
-                    idx = i;
-                }
-            }
-            NEW_STREAM(video, idx);
-        }
-
-        /* audio: most channels */
-        if (!o->audio_disable && oc->oformat->audio_codec != AV_CODEC_ID_NONE) {
-            int channels = 0, idx = -1;
-            for (i = 0; i < nb_input_streams; i++) {
-                ist = input_streams[i];
-                if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
-                    ist->st->codecpar->channels > channels) {
-                    channels = ist->st->codecpar->channels;
-                    idx = i;
-                }
-            }
-            NEW_STREAM(audio, idx);
-        }
-
-        /* subtitles: pick first */
-        if (!o->subtitle_disable && oc->oformat->subtitle_codec != AV_CODEC_ID_NONE) {
-            for (i = 0; i < nb_input_streams; i++)
-                if (input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
-                    NEW_STREAM(subtitle, i);
-                    break;
-                }
-        }
-        /* do something with data? */
-    } else {
-        for (i = 0; i < o->nb_stream_maps; i++) {
-            StreamMap *map = &o->stream_maps[i];
-
-            if (map->disabled)
-                continue;
-
-            if (map->linklabel) {
-                FilterGraph *fg;
-                OutputFilter *ofilter = NULL;
-                int j, k;
-
-                for (j = 0; j < nb_filtergraphs; j++) {
-                    fg = filtergraphs[j];
-                    for (k = 0; k < fg->nb_outputs; k++) {
-                        AVFilterInOut *out = fg->outputs[k]->out_tmp;
-                        if (out && !strcmp(out->name, map->linklabel)) {
-                            ofilter = fg->outputs[k];
-                            goto loop_end;
-                        }
-                    }
-                }
-loop_end:
-                if (!ofilter) {
-                    av_log(NULL, AV_LOG_FATAL, "Output with label '%s' does not exist "
-                           "in any defined filter graph.\n", map->linklabel);
-                    exit_program(1);
-                }
-                init_output_filter(ofilter, o, oc);
-            } else {
-                ist = input_streams[input_files[map->file_index]->ist_index + map->stream_index];
-                switch (ist->st->codecpar->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;
-                ist->st->discard = AVDISCARD_NONE;
-            }
-        }
-    }
-
-    /* handle attached files */
-    for (i = 0; i < o->nb_attachments; i++) {
-        AVIOContext *pb;
-        uint8_t *attachment;
-        const char *p;
-        int64_t len;
-
-        if ((err = avio_open2(&pb, o->attachments[i], AVIO_FLAG_READ, &int_cb, NULL)) < 0) {
-            av_log(NULL, AV_LOG_FATAL, "Could not open attachment file %s.\n",
-                   o->attachments[i]);
-            exit_program(1);
-        }
-        if ((len = avio_size(pb)) <= 0) {
-            av_log(NULL, AV_LOG_FATAL, "Could not get size of the attachment %s.\n",
-                   o->attachments[i]);
-            exit_program(1);
-        }
-        if (!(attachment = av_malloc(len))) {
-            av_log(NULL, AV_LOG_FATAL, "Attachment %s too large to fit into memory.\n",
-                   o->attachments[i]);
-            exit_program(1);
-        }
-        avio_read(pb, attachment, len);
-
-        ost = new_attachment_stream(o, oc);
-        ost->stream_copy               = 0;
-        ost->source_index              = -1;
-        ost->attachment_filename       = o->attachments[i];
-        ost->st->codecpar->extradata      = attachment;
-        ost->st->codecpar->extradata_size = len;
-
-        p = strrchr(o->attachments[i], '/');
-        av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE);
-        avio_close(pb);
-    }
-
-    if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
-        av_dump_format(oc, nb_output_files - 1, oc->filename, 1);
-        av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", nb_output_files - 1);
-        exit_program(1);
-    }
-
-    /* check if all codec options have been used */
-    unused_opts = strip_specifiers(o->g->codec_opts);
-    for (i = of->ost_index; i < nb_output_streams; i++) {
-        e = NULL;
-        while ((e = av_dict_get(output_streams[i]->encoder_opts, "", e,
-                                AV_DICT_IGNORE_SUFFIX)))
-            av_dict_set(&unused_opts, e->key, NULL, 0);
-    }
-
-    e = NULL;
-    while ((e = av_dict_get(unused_opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
-        const AVClass *class = avcodec_get_class();
-        const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
-                                             AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
-        if (!option)
-            continue;
-        if (!(option->flags & AV_OPT_FLAG_ENCODING_PARAM)) {
-            av_log(NULL, AV_LOG_ERROR, "Codec AVOption %s (%s) specified for "
-                   "output file #%d (%s) is not an encoding option.\n", e->key,
-                   option->help ? option->help : "", nb_output_files - 1,
-                   filename);
-            exit_program(1);
-        }
-
-        av_log(NULL, AV_LOG_WARNING, "Codec AVOption %s (%s) specified for "
-               "output file #%d (%s) has not been used for any stream. The most "
-               "likely reason is either wrong type (e.g. a video option with "
-               "no video streams) or that it is a private option of some encoder "
-               "which was not actually used for any stream.\n", e->key,
-               option->help ? option->help : "", nb_output_files - 1, filename);
-    }
-    av_dict_free(&unused_opts);
-
-    /* set the decoding_needed flags and create simple filtergraphs */
-    for (i = of->ost_index; i < nb_output_streams; i++) {
-        OutputStream *ost = output_streams[i];
-
-        if (ost->encoding_needed && ost->source_index >= 0) {
-            InputStream *ist = input_streams[ost->source_index];
-            ist->decoding_needed = 1;
-
-            if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
-                ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
-                err = init_simple_filtergraph(ist, ost);
-                if (err < 0) {
-                    av_log(NULL, AV_LOG_ERROR,
-                           "Error initializing a simple filtergraph between streams "
-                           "%d:%d->%d:%d\n", ist->file_index, ost->source_index,
-                           nb_output_files - 1, ost->st->index);
-                    exit_program(1);
-                }
-            }
-        }
-
-        /*
-         * We want CFR output if and only if one of those is true:
-         * 1) user specified output framerate with -r
-         * 2) user specified -vsync cfr
-         * 3) output format is CFR and the user didn't force vsync to
-         *    something else than CFR
-         *
-         * in such a case, set ost->frame_rate
-         */
-        if (ost->encoding_needed && ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
-            int format_cfr = !(oc->oformat->flags & (AVFMT_NOTIMESTAMPS | AVFMT_VARIABLE_FPS));
-            int need_cfr = !!ost->frame_rate.num;
-
-            if (video_sync_method == VSYNC_CFR ||
-                (video_sync_method == VSYNC_AUTO && format_cfr))
-                need_cfr = 1;
-
-            if (need_cfr && !ost->frame_rate.num) {
-                InputStream *ist = ost->source_index >= 0 ? input_streams[ost->source_index] : NULL;
-
-                if (ist && ist->framerate.num)
-                    ost->frame_rate = ist->framerate;
-                else if (ist && ist->st->avg_frame_rate.num)
-                    ost->frame_rate = ist->st->avg_frame_rate;
-                else {
-                    av_log(NULL, AV_LOG_WARNING, "Constant framerate requested "
-                           "for the output stream #%d:%d, but no information "
-                           "about the input framerate is available. Falling "
-                           "back to a default value of 25fps. Use the -r option "
-                           "if you want a different framerate.\n",
-                           ost->file_index, ost->index);
-                    ost->frame_rate = (AVRational){ 25, 1 };
-                }
-            }
-
-            if (need_cfr && ost->enc->supported_framerates && !ost->force_fps) {
-                int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
-                ost->frame_rate = ost->enc->supported_framerates[idx];
-            }
-        }
-
-        /* set the filter output constraints */
-        if (ost->filter) {
-            OutputFilter *f = ost->filter;
-            int count;
-            switch (ost->enc_ctx->codec_type) {
-            case AVMEDIA_TYPE_VIDEO:
-                f->frame_rate = ost->frame_rate;
-                f->width      = ost->enc_ctx->width;
-                f->height     = ost->enc_ctx->height;
-                if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
-                    f->format = ost->enc_ctx->pix_fmt;
-                } else if (ost->enc->pix_fmts) {
-                    count = 0;
-                    while (ost->enc->pix_fmts[count] != AV_PIX_FMT_NONE)
-                        count++;
-                    f->formats = av_mallocz_array(count + 1, sizeof(*f->formats));
-                    if (!f->formats)
-                        exit_program(1);
-                    memcpy(f->formats, ost->enc->pix_fmts, (count + 1) * sizeof(*f->formats));
-                }
-                break;
-            case AVMEDIA_TYPE_AUDIO:
-                if (ost->enc_ctx->sample_fmt != AV_SAMPLE_FMT_NONE) {
-                    f->format = ost->enc_ctx->sample_fmt;
-                } else if (ost->enc->sample_fmts) {
-                    count = 0;
-                    while (ost->enc->sample_fmts[count] != AV_SAMPLE_FMT_NONE)
-                        count++;
-                    f->formats = av_mallocz_array(count + 1, sizeof(*f->formats));
-                    if (!f->formats)
-                        exit_program(1);
-                    memcpy(f->formats, ost->enc->sample_fmts, (count + 1) * sizeof(*f->formats));
-                }
-                if (ost->enc_ctx->sample_rate) {
-                    f->sample_rate = ost->enc_ctx->sample_rate;
-                } else if (ost->enc->supported_samplerates) {
-                    count = 0;
-                    while (ost->enc->supported_samplerates[count])
-                        count++;
-                    f->sample_rates = av_mallocz_array(count + 1, sizeof(*f->sample_rates));
-                    if (!f->sample_rates)
-                        exit_program(1);
-                    memcpy(f->sample_rates, ost->enc->supported_samplerates,
-                           (count + 1) * sizeof(*f->sample_rates));
-                }
-                if (ost->enc_ctx->channels) {
-                    f->channel_layout = av_get_default_channel_layout(ost->enc_ctx->channels);
-                } else if (ost->enc->channel_layouts) {
-                    count = 0;
-                    while (ost->enc->channel_layouts[count])
-                        count++;
-                    f->channel_layouts = av_mallocz_array(count + 1, sizeof(*f->channel_layouts));
-                    if (!f->channel_layouts)
-                        exit_program(1);
-                    memcpy(f->channel_layouts, ost->enc->channel_layouts,
-                           (count + 1) * sizeof(*f->channel_layouts));
-                }
-                break;
-            }
-        }
-
-    }
-
-    /* check filename in case of an image number is expected */
-    if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
-        if (!av_filename_number_test(oc->filename)) {
-            print_error(oc->filename, AVERROR(EINVAL));
-            exit_program(1);
-        }
-    }
-
-    if (!(oc->oformat->flags & AVFMT_NOFILE)) {
-        /* test if it already exists to avoid losing precious files */
-        assert_file_overwrite(filename);
-
-        /* open the file */
-        if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
-                              &oc->interrupt_callback,
-                              &of->opts)) < 0) {
-            print_error(filename, err);
-            exit_program(1);
-        }
-    }
-
-    if (o->mux_preload) {
-        uint8_t buf[64];
-        snprintf(buf, sizeof(buf), "%d", (int)(o->mux_preload*AV_TIME_BASE));
-        av_dict_set(&of->opts, "preload", buf, 0);
-    }
-    oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
-    oc->flags |= AVFMT_FLAG_NONBLOCK;
-
-    /* copy metadata */
-    for (i = 0; i < o->nb_metadata_map; i++) {
-        char *p;
-        int in_file_index = strtol(o->metadata_map[i].u.str, &p, 0);
-
-        if (in_file_index >= nb_input_files) {
-            av_log(NULL, AV_LOG_FATAL, "Invalid input file index %d while processing metadata maps\n", in_file_index);
-            exit_program(1);
-        }
-        copy_metadata(o->metadata_map[i].specifier, *p ? p + 1 : p, oc,
-                      in_file_index >= 0 ?
-                      input_files[in_file_index]->ctx : NULL, o);
-    }
-
-    /* copy chapters */
-    if (o->chapters_input_file >= nb_input_files) {
-        if (o->chapters_input_file == INT_MAX) {
-            /* copy chapters from the first input file that has them*/
-            o->chapters_input_file = -1;
-            for (i = 0; i < nb_input_files; i++)
-                if (input_files[i]->ctx->nb_chapters) {
-                    o->chapters_input_file = i;
-                    break;
-                }
-        } else {
-            av_log(NULL, AV_LOG_FATAL, "Invalid input file index %d in chapter mapping.\n",
-                   o->chapters_input_file);
-            exit_program(1);
-        }
-    }
-    if (o->chapters_input_file >= 0)
-        copy_chapters(input_files[o->chapters_input_file], of,
-                      !o->metadata_chapters_manual);
-
-    /* copy global metadata by default */
-    if (!o->metadata_global_manual && nb_input_files)
-        av_dict_copy(&oc->metadata, input_files[0]->ctx->metadata,
-                     AV_DICT_DONT_OVERWRITE);
-    if (!o->metadata_streams_manual)
-        for (i = of->ost_index; i < nb_output_streams; i++) {
-            InputStream *ist;
-            if (output_streams[i]->source_index < 0)         /* this is true e.g. for attached files */
-                continue;
-            ist = input_streams[output_streams[i]->source_index];
-            av_dict_copy(&output_streams[i]->st->metadata, ist->st->metadata, AV_DICT_DONT_OVERWRITE);
-        }
-
-    /* process manually set metadata */
-    for (i = 0; i < o->nb_metadata; i++) {
-        AVDictionary **m;
-        char type, *val;
-        const char *stream_spec;
-        int index = 0, j, ret;
-
-        val = strchr(o->metadata[i].u.str, '=');
-        if (!val) {
-            av_log(NULL, AV_LOG_FATAL, "No '=' character in metadata string %s.\n",
-                   o->metadata[i].u.str);
-            exit_program(1);
-        }
-        *val++ = 0;
-
-        parse_meta_type(o->metadata[i].specifier, &type, &index, &stream_spec);
-        if (type == 's') {
-            for (j = 0; j < oc->nb_streams; j++) {
-                if ((ret = check_stream_specifier(oc, oc->streams[j], stream_spec)) > 0) {
-                    av_dict_set(&oc->streams[j]->metadata, o->metadata[i].u.str, *val ? val : NULL, 0);
-                } else if (ret < 0)
-                    exit_program(1);
-            }
-        }
-        else {
-            switch (type) {
-            case 'g':
-                m = &oc->metadata;
-                break;
-            case 'c':
-                if (index < 0 || index >= oc->nb_chapters) {
-                    av_log(NULL, AV_LOG_FATAL, "Invalid chapter index %d in metadata specifier.\n", index);
-                    exit_program(1);
-                }
-                m = &oc->chapters[index]->metadata;
-                break;
-            default:
-                av_log(NULL, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", o->metadata[i].specifier);
-                exit_program(1);
-            }
-            av_dict_set(m, o->metadata[i].u.str, *val ? val : NULL, 0);
-        }
-    }
-
-    return 0;
-}
-
-static int opt_target(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
-    static const char *const frame_rates[] = { "25", "30000/1001", "24000/1001" };
-
-    if (!strncmp(arg, "pal-", 4)) {
-        norm = PAL;
-        arg += 4;
-    } else if (!strncmp(arg, "ntsc-", 5)) {
-        norm = NTSC;
-        arg += 5;
-    } else if (!strncmp(arg, "film-", 5)) {
-        norm = FILM;
-        arg += 5;
-    } else {
-        /* Try to determine PAL/NTSC by peeking in the input files */
-        if (nb_input_files) {
-            int i, j, fr;
-            for (j = 0; j < nb_input_files; j++) {
-                for (i = 0; i < input_files[j]->nb_streams; i++) {
-                    AVStream *st = input_files[j]->ctx->streams[i];
-                    if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
-                        continue;
-                    fr = st->time_base.den * 1000 / st->time_base.num;
-                    if (fr == 25000) {
-                        norm = PAL;
-                        break;
-                    } else if ((fr == 29970) || (fr == 23976)) {
-                        norm = NTSC;
-                        break;
-                    }
-                }
-                if (norm != UNKNOWN)
-                    break;
-            }
-        }
-        if (norm != UNKNOWN)
-            av_log(NULL, AV_LOG_INFO, "Assuming %s for target.\n", norm == PAL ? "PAL" : "NTSC");
-    }
-
-    if (norm == UNKNOWN) {
-        av_log(NULL, AV_LOG_FATAL, "Could not determine norm (PAL/NTSC/NTSC-Film) for target.\n");
-        av_log(NULL, AV_LOG_FATAL, "Please prefix target with \"pal-\", \"ntsc-\" or \"film-\",\n");
-        av_log(NULL, AV_LOG_FATAL, "or set a framerate with \"-r xxx\".\n");
-        exit_program(1);
-    }
-
-    if (!strcmp(arg, "vcd")) {
-        opt_video_codec(o, "c:v", "mpeg1video");
-        opt_audio_codec(o, "c:a", "mp2");
-        parse_option(o, "f", "vcd", options);
-
-        parse_option(o, "s", norm == PAL ? "352x288" : "352x240", options);
-        parse_option(o, "r", frame_rates[norm], options);
-        opt_default(NULL, "g", norm == PAL ? "15" : "18");
-
-        opt_default(NULL, "b", "1150000");
-        opt_default(NULL, "maxrate", "1150000");
-        opt_default(NULL, "minrate", "1150000");
-        opt_default(NULL, "bufsize", "327680"); // 40*1024*8;
-
-        opt_default(NULL, "b:a", "224000");
-        parse_option(o, "ar", "44100", options);
-        parse_option(o, "ac", "2", options);
-
-        opt_default(NULL, "packetsize", "2324");
-        opt_default(NULL, "muxrate", "3528"); // 2352 * 75 / 50;
-
-        /* We have to offset the PTS, so that it is consistent with the SCR.
-           SCR starts at 36000, but the first two packs contain only padding
-           and the first pack from the other stream, respectively, may also have
-           been written before.
-           So the real data starts at SCR 36000+3*1200. */
-        o->mux_preload = (36000 + 3 * 1200) / 90000.0; // 0.44
-    } else if (!strcmp(arg, "svcd")) {
-
-        opt_video_codec(o, "c:v", "mpeg2video");
-        opt_audio_codec(o, "c:a", "mp2");
-        parse_option(o, "f", "svcd", options);
-
-        parse_option(o, "s", norm == PAL ? "480x576" : "480x480", options);
-        parse_option(o, "r", frame_rates[norm], options);
-        opt_default(NULL, "g", norm == PAL ? "15" : "18");
-
-        opt_default(NULL, "b", "2040000");
-        opt_default(NULL, "maxrate", "2516000");
-        opt_default(NULL, "minrate", "0"); // 1145000;
-        opt_default(NULL, "bufsize", "1835008"); // 224*1024*8;
-        opt_default(NULL, "scan_offset", "1");
-
-
-        opt_default(NULL, "b:a", "224000");
-        parse_option(o, "ar", "44100", options);
-
-        opt_default(NULL, "packetsize", "2324");
-
-    } else if (!strcmp(arg, "dvd")) {
-
-        opt_video_codec(o, "c:v", "mpeg2video");
-        opt_audio_codec(o, "c:a", "ac3");
-        parse_option(o, "f", "dvd", options);
-
-        parse_option(o, "s", norm == PAL ? "720x576" : "720x480", options);
-        parse_option(o, "r", frame_rates[norm], options);
-        opt_default(NULL, "g", norm == PAL ? "15" : "18");
-
-        opt_default(NULL, "b", "6000000");
-        opt_default(NULL, "maxrate", "9000000");
-        opt_default(NULL, "minrate", "0"); // 1500000;
-        opt_default(NULL, "bufsize", "1835008"); // 224*1024*8;
-
-        opt_default(NULL, "packetsize", "2048");  // from www.mpucoder.com: DVD sectors contain 2048 bytes of data, this is also the size of one pack.
-        opt_default(NULL, "muxrate", "25200"); // from mplex project: data_rate = 1260000. mux_rate = data_rate / 50
-
-        opt_default(NULL, "b:a", "448000");
-        parse_option(o, "ar", "48000", options);
-
-    } else if (!strncmp(arg, "dv", 2)) {
-
-        parse_option(o, "f", "dv", options);
-
-        parse_option(o, "s", norm == PAL ? "720x576" : "720x480", options);
-        parse_option(o, "pix_fmt", !strncmp(arg, "dv50", 4) ? "yuv422p" :
-                          norm == PAL ? "yuv420p" : "yuv411p", options);
-        parse_option(o, "r", frame_rates[norm], options);
-
-        parse_option(o, "ar", "48000", options);
-        parse_option(o, "ac", "2", options);
-
-    } else {
-        av_log(NULL, AV_LOG_ERROR, "Unknown target: %s\n", arg);
-        return AVERROR(EINVAL);
-    }
-
-    av_dict_copy(&o->g->codec_opts,  codec_opts, 0);
-    av_dict_copy(&o->g->format_opts, format_opts, 0);
-
-    return 0;
-}
-
-static int opt_vstats_file(void *optctx, const char *opt, const char *arg)
-{
-    av_free (vstats_filename);
-    vstats_filename = av_strdup (arg);
-    return 0;
-}
-
-static int opt_vstats(void *optctx, const char *opt, const char *arg)
-{
-    char filename[40];
-    time_t today2 = time(NULL);
-    struct tm *today = localtime(&today2);
-
-    if (!today) { // maybe tomorrow
-        av_log(NULL, AV_LOG_FATAL, "Unable to get current time.\n");
-        exit_program(1);
-    }
-
-    snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
-             today->tm_sec);
-    return opt_vstats_file(NULL, opt, filename);
-}
-
-static int opt_video_frames(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    return parse_option(o, "frames:v", arg, options);
-}
-
-static int opt_audio_frames(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    return parse_option(o, "frames:a", arg, options);
-}
-
-static int opt_data_frames(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    return parse_option(o, "frames:d", arg, options);
-}
-
-static int opt_video_tag(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    return parse_option(o, "tag:v", arg, options);
-}
-
-static int opt_audio_tag(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    return parse_option(o, "tag:a", arg, options);
-}
-
-static int opt_subtitle_tag(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    return parse_option(o, "tag:s", arg, options);
-}
-
-static int opt_video_filters(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    return parse_option(o, "filter:v", arg, options);
-}
-
-static int opt_audio_filters(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    return parse_option(o, "filter:a", arg, options);
-}
-
-static int opt_vsync(void *optctx, const char *opt, const char *arg)
-{
-    if      (!av_strcasecmp(arg, "cfr"))         video_sync_method = VSYNC_CFR;
-    else if (!av_strcasecmp(arg, "vfr"))         video_sync_method = VSYNC_VFR;
-    else if (!av_strcasecmp(arg, "passthrough")) video_sync_method = VSYNC_PASSTHROUGH;
-
-    if (video_sync_method == VSYNC_AUTO)
-        video_sync_method = parse_number_or_die("vsync", arg, OPT_INT, VSYNC_AUTO, VSYNC_VFR);
-    return 0;
-}
-
-static int opt_channel_layout(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    char layout_str[32];
-    char *stream_str;
-    char *ac_str;
-    int ret, channels, ac_str_size;
-    uint64_t layout;
-
-    layout = av_get_channel_layout(arg);
-    if (!layout) {
-        av_log(NULL, AV_LOG_ERROR, "Unknown channel layout: %s\n", arg);
-        return AVERROR(EINVAL);
-    }
-    snprintf(layout_str, sizeof(layout_str), "%"PRIu64, layout);
-    ret = opt_default(NULL, opt, layout_str);
-    if (ret < 0)
-        return ret;
-
-    /* set 'ac' option based on channel layout */
-    channels = av_get_channel_layout_nb_channels(layout);
-    snprintf(layout_str, sizeof(layout_str), "%d", channels);
-    stream_str = strchr(opt, ':');
-    ac_str_size = 3 + (stream_str ? strlen(stream_str) : 0);
-    ac_str = av_mallocz(ac_str_size);
-    if (!ac_str)
-        return AVERROR(ENOMEM);
-    av_strlcpy(ac_str, "ac", 3);
-    if (stream_str)
-        av_strlcat(ac_str, stream_str, ac_str_size);
-    ret = parse_option(o, ac_str, layout_str, options);
-    av_free(ac_str);
-
-    return ret;
-}
-
-static int opt_audio_qscale(void *optctx, const char *opt, const char *arg)
-{
-    OptionsContext *o = optctx;
-    return parse_option(o, "q:a", arg, options);
-}
-
-static int opt_filter_complex(void *optctx, const char *opt, const char *arg)
-{
-    GROW_ARRAY(filtergraphs, nb_filtergraphs);
-    if (!(filtergraphs[nb_filtergraphs - 1] = av_mallocz(sizeof(*filtergraphs[0]))))
-        return AVERROR(ENOMEM);
-    filtergraphs[nb_filtergraphs - 1]->index      = nb_filtergraphs - 1;
-    filtergraphs[nb_filtergraphs - 1]->graph_desc = av_strdup(arg);
-    if (!filtergraphs[nb_filtergraphs - 1]->graph_desc)
-        return AVERROR(ENOMEM);
-    return 0;
-}
-
-static int opt_filter_complex_script(void *optctx, const char *opt, const char *arg)
-{
-    uint8_t *graph_desc = read_file(arg);
-    if (!graph_desc)
-        return AVERROR(EINVAL);
-
-    GROW_ARRAY(filtergraphs, nb_filtergraphs);
-    if (!(filtergraphs[nb_filtergraphs - 1] = av_mallocz(sizeof(*filtergraphs[0]))))
-        return AVERROR(ENOMEM);
-    filtergraphs[nb_filtergraphs - 1]->index      = nb_filtergraphs - 1;
-    filtergraphs[nb_filtergraphs - 1]->graph_desc = graph_desc;
-    return 0;
-}
-
-void show_help_default(const char *opt, const char *arg)
-{
-    /* per-file options have at least one of those set */
-    const int per_file = OPT_SPEC | OPT_OFFSET | OPT_PERFILE;
-    int show_advanced = 0, show_avoptions = 0;
-
-    if (opt && *opt) {
-        if (!strcmp(opt, "long"))
-            show_advanced = 1;
-        else if (!strcmp(opt, "full"))
-            show_advanced = show_avoptions = 1;
-        else
-            av_log(NULL, AV_LOG_ERROR, "Unknown help option '%s'.\n", opt);
-    }
-
-    show_usage();
-
-    printf("Getting help:\n"
-           "    -h      -- print basic options\n"
-           "    -h long -- print more options\n"
-           "    -h full -- print all options (including all format and codec specific options, very long)\n"
-           "    -h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter\n"
-           "    See man %s for detailed description of the options.\n"
-           "\n", program_name);
-
-    show_help_options(options, "Print help / information / capabilities:",
-                      OPT_EXIT, 0, 0);
-
-    show_help_options(options, "Global options (affect whole program "
-                      "instead of just one file:",
-                      0, per_file | OPT_EXIT | OPT_EXPERT, 0);
-    if (show_advanced)
-        show_help_options(options, "Advanced global options:", OPT_EXPERT,
-                          per_file | OPT_EXIT, 0);
-
-    show_help_options(options, "Per-file main options:", 0,
-                      OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_SUBTITLE |
-                      OPT_EXIT, per_file);
-    if (show_advanced)
-        show_help_options(options, "Advanced per-file options:",
-                          OPT_EXPERT, OPT_AUDIO | OPT_VIDEO | OPT_SUBTITLE, per_file);
-
-    show_help_options(options, "Video options:",
-                      OPT_VIDEO, OPT_EXPERT | OPT_AUDIO, 0);
-    if (show_advanced)
-        show_help_options(options, "Advanced Video options:",
-                          OPT_EXPERT | OPT_VIDEO, OPT_AUDIO, 0);
-
-    show_help_options(options, "Audio options:",
-                      OPT_AUDIO, OPT_EXPERT | OPT_VIDEO, 0);
-    if (show_advanced)
-        show_help_options(options, "Advanced Audio options:",
-                          OPT_EXPERT | OPT_AUDIO, OPT_VIDEO, 0);
-    show_help_options(options, "Subtitle options:",
-                      OPT_SUBTITLE, 0, 0);
-    printf("\n");
-
-    if (show_avoptions) {
-        int flags = AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM;
-        show_help_children(avcodec_get_class(), flags);
-        show_help_children(avformat_get_class(), flags);
-        show_help_children(sws_get_class(), flags);
-        show_help_children(avfilter_get_class(), AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM);
-    }
-}
-
-void show_usage(void)
-{
-    printf("Hyper fast Audio and Video encoder\n");
-    printf("usage: %s [options] [[infile options] -i infile]... {[outfile options] outfile}...\n", program_name);
-    printf("\n");
-}
-
-enum OptGroup {
-    GROUP_OUTFILE,
-    GROUP_INFILE,
-};
-
-static const OptionGroupDef groups[] = {
-    [GROUP_OUTFILE] = { "output file",  NULL, OPT_OUTPUT },
-    [GROUP_INFILE]  = { "input file",   "i",  OPT_INPUT },
-};
-
-static int open_files(OptionGroupList *l, const char *inout,
-                      int (*open_file)(OptionsContext*, const char*))
-{
-    int i, ret;
-
-    for (i = 0; i < l->nb_groups; i++) {
-        OptionGroup *g = &l->groups[i];
-        OptionsContext o;
-
-        init_options(&o);
-        o.g = g;
-
-        ret = parse_optgroup(&o, g);
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_ERROR, "Error parsing options for %s file "
-                   "%s.\n", inout, g->arg);
-            return ret;
-        }
-
-        av_log(NULL, AV_LOG_DEBUG, "Opening an %s file: %s.\n", inout, g->arg);
-        ret = open_file(&o, g->arg);
-        uninit_options(&o);
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_ERROR, "Error opening %s file %s.\n",
-                   inout, g->arg);
-            return ret;
-        }
-        av_log(NULL, AV_LOG_DEBUG, "Successfully opened the file.\n");
-    }
-
-    return 0;
-}
-
-int avconv_parse_options(int argc, char **argv)
-{
-    OptionParseContext octx;
-    uint8_t error[128];
-    int ret;
-
-    memset(&octx, 0, sizeof(octx));
-
-    /* split the commandline into an internal representation */
-    ret = split_commandline(&octx, argc, argv, options, groups,
-                            FF_ARRAY_ELEMS(groups));
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_FATAL, "Error splitting the argument list: ");
-        goto fail;
-    }
-
-    /* apply global options */
-    ret = parse_optgroup(NULL, &octx.global_opts);
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_FATAL, "Error parsing global options: ");
-        goto fail;
-    }
-
-    /* open input files */
-    ret = open_files(&octx.groups[GROUP_INFILE], "input", open_input_file);
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_FATAL, "Error opening input files: ");
-        goto fail;
-    }
-
-    /* create the complex filtergraphs */
-    ret = init_complex_filters();
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_FATAL, "Error initializing complex filters.\n");
-        goto fail;
-    }
-
-    /* open output files */
-    ret = open_files(&octx.groups[GROUP_OUTFILE], "output", open_output_file);
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_FATAL, "Error opening output files: ");
-        goto fail;
-    }
-
-fail:
-    uninit_parse_context(&octx);
-    if (ret < 0) {
-        av_strerror(ret, error, sizeof(error));
-        av_log(NULL, AV_LOG_FATAL, "%s\n", error);
-    }
-    return ret;
-}
-
-#define OFFSET(x) offsetof(OptionsContext, x)
-const OptionDef options[] = {
-    /* main options */
-    CMDUTILS_COMMON_OPTIONS
-    { "f",              HAS_ARG | OPT_STRING | OPT_OFFSET |
-                        OPT_INPUT | OPT_OUTPUT,                      { .off       = OFFSET(format) },
-        "force format", "fmt" },
-    { "y",              OPT_BOOL,                                    {              &file_overwrite },
-        "overwrite output files" },
-    { "n",              OPT_BOOL,                                    {              &file_skip },
-        "never overwrite output files" },
-    { "c",              HAS_ARG | OPT_STRING | OPT_SPEC |
-                        OPT_INPUT | OPT_OUTPUT,                      { .off       = OFFSET(codec_names) },
-        "codec name", "codec" },
-    { "codec",          HAS_ARG | OPT_STRING | OPT_SPEC |
-                        OPT_INPUT | OPT_OUTPUT,                      { .off       = OFFSET(codec_names) },
-        "codec name", "codec" },
-    { "pre",            HAS_ARG | OPT_STRING | OPT_SPEC |
-                        OPT_OUTPUT,                                  { .off       = OFFSET(presets) },
-        "preset name", "preset" },
-    { "map",            HAS_ARG | OPT_EXPERT | OPT_PERFILE |
-                        OPT_OUTPUT,                                  { .func_arg = opt_map },
-        "set input stream mapping",
-        "[-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]]" },
-    { "map_metadata",   HAS_ARG | OPT_STRING | OPT_SPEC |
-                        OPT_OUTPUT,                                  { .off       = OFFSET(metadata_map) },
-        "set metadata information of outfile from infile",
-        "outfile[,metadata]:infile[,metadata]" },
-    { "map_chapters",   HAS_ARG | OPT_INT | OPT_EXPERT | OPT_OFFSET |
-                        OPT_OUTPUT,                                  { .off = OFFSET(chapters_input_file) },
-        "set chapters mapping", "input_file_index" },
-    { "t",              HAS_ARG | OPT_TIME | OPT_OFFSET |
-                        OPT_INPUT | OPT_OUTPUT,                      { .off = OFFSET(recording_time) },
-        "record or transcode \"duration\" seconds of audio/video",
-        "duration" },
-    { "fs",             HAS_ARG | OPT_INT64 | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(limit_filesize) },
-        "set the limit file size in bytes", "limit_size" },
-    { "ss",             HAS_ARG | OPT_TIME | OPT_OFFSET |
-                        OPT_INPUT | OPT_OUTPUT,                      { .off = OFFSET(start_time) },
-        "set the start time offset", "time_off" },
-    { "accurate_seek",  OPT_BOOL | OPT_OFFSET | OPT_EXPERT |
-                        OPT_INPUT,                                   { .off = OFFSET(accurate_seek) },
-        "enable/disable accurate seeking with -ss" },
-    { "itsoffset",      HAS_ARG | OPT_TIME | OPT_OFFSET |
-                        OPT_EXPERT | OPT_INPUT,                      { .off = OFFSET(input_ts_offset) },
-        "set the input ts offset", "time_off" },
-    { "itsscale",       HAS_ARG | OPT_DOUBLE | OPT_SPEC |
-                        OPT_EXPERT | OPT_INPUT,                      { .off = OFFSET(ts_scale) },
-        "set the input ts scale", "scale" },
-    { "metadata",       HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(metadata) },
-        "add metadata", "string=string" },
-    { "dframes",        HAS_ARG | OPT_PERFILE | OPT_EXPERT |
-                        OPT_OUTPUT,                                  { .func_arg = opt_data_frames },
-        "set the number of data frames to record", "number" },
-    { "benchmark",      OPT_BOOL | OPT_EXPERT,                       { &do_benchmark },
-        "add timings for benchmarking" },
-    { "timelimit",      HAS_ARG | OPT_EXPERT,                        { .func_arg = opt_timelimit },
-        "set max runtime in seconds", "limit" },
-    { "dump",           OPT_BOOL | OPT_EXPERT,                       { &do_pkt_dump },
-        "dump each input packet" },
-    { "hex",            OPT_BOOL | OPT_EXPERT,                       { &do_hex_dump },
-        "when dumping packets, also dump the payload" },
-    { "re",             OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
-                        OPT_INPUT,                                   { .off = OFFSET(rate_emu) },
-        "read input at native frame rate", "" },
-    { "target",         HAS_ARG | OPT_PERFILE | OPT_OUTPUT,          { .func_arg = opt_target },
-        "specify target file type (\"vcd\", \"svcd\", \"dvd\","
-        " \"dv\", \"dv50\", \"pal-vcd\", \"ntsc-svcd\", ...)", "type" },
-    { "vsync",          HAS_ARG | OPT_EXPERT,                        { .func_arg = opt_vsync },
-        "video sync method", "" },
-    { "async",          HAS_ARG | OPT_INT | OPT_EXPERT,              { &audio_sync_method },
-        "audio sync method", "" },
-    { "adrift_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT,          { &audio_drift_threshold },
-        "audio drift threshold", "threshold" },
-    { "copyts",         OPT_BOOL | OPT_EXPERT,                       { &copy_ts },
-        "copy timestamps" },
-    { "copytb",         OPT_BOOL | OPT_EXPERT,                       { &copy_tb },
-        "copy input stream time base when stream copying" },
-    { "shortest",       OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
-                        OPT_OUTPUT,                                  { .off = OFFSET(shortest) },
-        "finish encoding within shortest input" },
-    { "dts_delta_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT,       { &dts_delta_threshold },
-        "timestamp discontinuity delta threshold", "threshold" },
-    { "xerror",         OPT_BOOL | OPT_EXPERT,                       { &exit_on_error },
-        "exit on error", "error" },
-    { "copyinkf",       OPT_BOOL | OPT_EXPERT | OPT_SPEC |
-                        OPT_OUTPUT,                                  { .off = OFFSET(copy_initial_nonkeyframes) },
-        "copy initial non-keyframes" },
-    { "frames",         OPT_INT64 | HAS_ARG | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(max_frames) },
-        "set the number of frames to record", "number" },
-    { "tag",            OPT_STRING | HAS_ARG | OPT_SPEC |
-                        OPT_EXPERT | OPT_OUTPUT | OPT_INPUT,         { .off = OFFSET(codec_tags) },
-        "force codec tag/fourcc", "fourcc/tag" },
-    { "q",              HAS_ARG | OPT_EXPERT | OPT_DOUBLE |
-                        OPT_SPEC | OPT_OUTPUT,                       { .off = OFFSET(qscale) },
-        "use fixed quality scale (VBR)", "q" },
-    { "qscale",         HAS_ARG | OPT_EXPERT | OPT_DOUBLE |
-                        OPT_SPEC | OPT_OUTPUT,                       { .off = OFFSET(qscale) },
-        "use fixed quality scale (VBR)", "q" },
-    { "b",              HAS_ARG | OPT_INT | OPT_SPEC | OPT_OUTPUT,    { .off = OFFSET(bitrates) },
-        "set stream bitrate in bits/second", "bitrate" },
-    { "filter",         HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filters) },
-        "set stream filterchain", "filter_list" },
-    { "filter_script",  HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filter_scripts) },
-        "read stream filtergraph description from a file", "filename" },
-    { "filter_complex", HAS_ARG | OPT_EXPERT,                        { .func_arg = opt_filter_complex },
-        "create a complex filtergraph", "graph_description" },
-    { "filter_complex_script", HAS_ARG | OPT_EXPERT,                 { .func_arg = opt_filter_complex_script },
-        "read complex filtergraph description from a file", "filename" },
-    { "stats",          OPT_BOOL,                                    { &print_stats },
-        "print progress report during encoding", },
-    { "attach",         HAS_ARG | OPT_PERFILE | OPT_EXPERT |
-                        OPT_OUTPUT,                                  { .func_arg = opt_attach },
-        "add an attachment to the output file", "filename" },
-    { "dump_attachment", HAS_ARG | OPT_STRING | OPT_SPEC |
-                         OPT_EXPERT | OPT_INPUT,                     { .off = OFFSET(dump_attachment) },
-        "extract an attachment into a file", "filename" },
-    { "loop", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_INPUT |
-                        OPT_OFFSET,                                  { .off = OFFSET(loop) }, "set number of times input stream shall be looped", "loop count" },
-
-    /* video options */
-    { "vframes",      OPT_VIDEO | HAS_ARG  | OPT_PERFILE | OPT_OUTPUT,           { .func_arg = opt_video_frames },
-        "set the number of video frames to record", "number" },
-    { "r",            OPT_VIDEO | HAS_ARG  | OPT_STRING | OPT_SPEC |
-                      OPT_INPUT | OPT_OUTPUT,                                    { .off = OFFSET(frame_rates) },
-        "set frame rate (Hz value, fraction or abbreviation)", "rate" },
-    { "s",            OPT_VIDEO | HAS_ARG  | OPT_STRING | OPT_SPEC |
-                      OPT_INPUT | OPT_OUTPUT,                                    { .off = OFFSET(frame_sizes) },
-        "set frame size (WxH or abbreviation)", "size" },
-    { "aspect",       OPT_VIDEO | HAS_ARG  | OPT_STRING | OPT_SPEC |
-                      OPT_OUTPUT,                                                { .off = OFFSET(frame_aspect_ratios) },
-        "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
-    { "pix_fmt",      OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_STRING | OPT_SPEC |
-                      OPT_INPUT | OPT_OUTPUT,                                    { .off = OFFSET(frame_pix_fmts) },
-        "set pixel format", "format" },
-    { "vn",           OPT_VIDEO | OPT_BOOL  | OPT_OFFSET | OPT_OUTPUT,           { .off = OFFSET(video_disable) },
-        "disable video" },
-    { "vdt",          OPT_VIDEO | OPT_INT | HAS_ARG | OPT_EXPERT ,               { &video_discard },
-        "discard threshold", "n" },
-    { "rc_override",  OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_STRING | OPT_SPEC |
-                      OPT_OUTPUT,                                                { .off = OFFSET(rc_overrides) },
-        "rate control override for specific intervals", "override" },
-    { "vcodec",       OPT_VIDEO | HAS_ARG  | OPT_PERFILE | OPT_INPUT |
-                      OPT_OUTPUT,                                                { .func_arg = opt_video_codec },
-        "force video codec ('copy' to copy stream)", "codec" },
-    { "pass",         OPT_VIDEO | HAS_ARG | OPT_SPEC | OPT_INT | OPT_OUTPUT,     { .off = OFFSET(pass) },
-        "select the pass number (1 or 2)", "n" },
-    { "passlogfile",  OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_EXPERT | OPT_SPEC |
-                      OPT_OUTPUT,                                                { .off = OFFSET(passlogfiles) },
-        "select two pass log file name prefix", "prefix" },
-    { "vstats",       OPT_VIDEO | OPT_EXPERT ,                                   { .func_arg = &opt_vstats },
-        "dump video coding statistics to file" },
-    { "vstats_file",  OPT_VIDEO | HAS_ARG | OPT_EXPERT ,                         { .func_arg = opt_vstats_file },
-        "dump video coding statistics to file", "file" },
-    { "vf",           OPT_VIDEO | HAS_ARG  | OPT_PERFILE | OPT_OUTPUT,           { .func_arg = opt_video_filters },
-        "video filters", "filter list" },
-    { "intra_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_STRING | OPT_SPEC |
-                      OPT_OUTPUT,                                                { .off = OFFSET(intra_matrices) },
-        "specify intra matrix coeffs", "matrix" },
-    { "inter_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_STRING | OPT_SPEC |
-                      OPT_OUTPUT,                                                { .off = OFFSET(inter_matrices) },
-        "specify inter matrix coeffs", "matrix" },
-    { "top",          OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_INT| OPT_SPEC |
-                      OPT_OUTPUT,                                                { .off = OFFSET(top_field_first) },
-        "top=1/bottom=0/auto=-1 field first", "" },
-    { "dc",           OPT_VIDEO | OPT_INT | HAS_ARG | OPT_EXPERT ,               { &intra_dc_precision },
-        "intra_dc_precision", "precision" },
-    { "vtag",         OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_PERFILE |
-                      OPT_OUTPUT,                                                { .func_arg = opt_video_tag },
-        "force video tag/fourcc", "fourcc/tag" },
-    { "qphist",       OPT_VIDEO | OPT_BOOL | OPT_EXPERT ,                        { &qp_hist },
-        "show QP histogram" },
-    { "force_fps",    OPT_VIDEO | OPT_BOOL | OPT_EXPERT  | OPT_SPEC |
-                      OPT_OUTPUT,                                                { .off = OFFSET(force_fps) },
-        "force the selected framerate, disable the best supported framerate selection" },
-    { "streamid",     OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_PERFILE |
-                      OPT_OUTPUT,                                                { .func_arg = opt_streamid },
-        "set the value of an outfile streamid", "streamIndex:value" },
-    { "force_key_frames", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
-                          OPT_SPEC | OPT_OUTPUT,                                 { .off = OFFSET(forced_key_frames) },
-        "force key frames at specified timestamps", "timestamps" },
-    { "hwaccel",          OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
-                          OPT_SPEC | OPT_INPUT,                                  { .off = OFFSET(hwaccels) },
-        "use HW accelerated decoding", "hwaccel name" },
-    { "hwaccel_device",   OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
-                          OPT_SPEC | OPT_INPUT,                                  { .off = OFFSET(hwaccel_devices) },
-        "select a device for HW acceleration", "devicename" },
-    { "hwaccel_output_format", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
-                          OPT_SPEC | OPT_INPUT,                                  { .off = OFFSET(hwaccel_output_formats) },
-        "select output format used with HW accelerated decoding", "format" },
-
-    { "hwaccels",         OPT_EXIT,                                              { .func_arg = show_hwaccels },
-        "show available HW acceleration methods" },
-    { "autorotate",       HAS_ARG | OPT_BOOL | OPT_SPEC |
-                          OPT_EXPERT | OPT_INPUT,                                { .off = OFFSET(autorotate) },
-        "automatically insert correct rotate filters" },
-    { "hwaccel_lax_profile_check", OPT_BOOL | OPT_EXPERT,                        { &hwaccel_lax_profile_check},
-        "attempt to decode anyway if HW accelerated decoder's supported profiles do not exactly match the stream" },
-
-    /* audio options */
-    { "aframes",        OPT_AUDIO | HAS_ARG  | OPT_PERFILE | OPT_OUTPUT,           { .func_arg = opt_audio_frames },
-        "set the number of audio frames to record", "number" },
-    { "aq",             OPT_AUDIO | HAS_ARG  | OPT_PERFILE | OPT_OUTPUT,           { .func_arg = opt_audio_qscale },
-        "set audio quality (codec-specific)", "quality", },
-    { "ar",             OPT_AUDIO | HAS_ARG  | OPT_INT | OPT_SPEC |
-                        OPT_INPUT | OPT_OUTPUT,                                    { .off = OFFSET(audio_sample_rate) },
-        "set audio sampling rate (in Hz)", "rate" },
-    { "ac",             OPT_AUDIO | HAS_ARG  | OPT_INT | OPT_SPEC |
-                        OPT_INPUT | OPT_OUTPUT,                                    { .off = OFFSET(audio_channels) },
-        "set number of audio channels", "channels" },
-    { "an",             OPT_AUDIO | OPT_BOOL | OPT_OFFSET | OPT_OUTPUT,            { .off = OFFSET(audio_disable) },
-        "disable audio" },
-    { "acodec",         OPT_AUDIO | HAS_ARG  | OPT_PERFILE |
-                        OPT_INPUT | OPT_OUTPUT,                                    { .func_arg = opt_audio_codec },
-        "force audio codec ('copy' to copy stream)", "codec" },
-    { "atag",           OPT_AUDIO | HAS_ARG  | OPT_EXPERT | OPT_PERFILE |
-                        OPT_OUTPUT,                                                { .func_arg = opt_audio_tag },
-        "force audio tag/fourcc", "fourcc/tag" },
-    { "vol",            OPT_AUDIO | HAS_ARG  | OPT_INT,                            { &audio_volume },
-        "change audio volume (256=normal)" , "volume" },
-    { "sample_fmt",     OPT_AUDIO | HAS_ARG  | OPT_EXPERT | OPT_SPEC |
-                        OPT_STRING | OPT_INPUT | OPT_OUTPUT,                       { .off = OFFSET(sample_fmts) },
-        "set sample format", "format" },
-    { "channel_layout", OPT_AUDIO | HAS_ARG  | OPT_EXPERT | OPT_PERFILE |
-                        OPT_INPUT | OPT_OUTPUT,                                    { .func_arg = opt_channel_layout },
-        "set channel layout", "layout" },
-    { "af",             OPT_AUDIO | HAS_ARG  | OPT_PERFILE | OPT_OUTPUT,           { .func_arg = opt_audio_filters },
-        "audio filters", "filter list" },
-
-    /* subtitle options */
-    { "sn",     OPT_SUBTITLE | OPT_BOOL | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(subtitle_disable) },
-        "disable subtitle" },
-    { "scodec", OPT_SUBTITLE | HAS_ARG  | OPT_PERFILE | OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_subtitle_codec },
-        "force subtitle codec ('copy' to copy stream)", "codec" },
-    { "stag",   OPT_SUBTITLE | HAS_ARG  | OPT_EXPERT  | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_subtitle_tag }
-        , "force subtitle tag/fourcc", "fourcc/tag" },
-
-    /* grab options */
-    { "isync", OPT_BOOL | OPT_EXPERT, { &input_sync }, "this option is deprecated and does nothing", "" },
-
-    /* muxer options */
-    { "muxdelay",   OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(mux_max_delay) },
-        "set the maximum demux-decode delay", "seconds" },
-    { "muxpreload", OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(mux_preload) },
-        "set the initial demux-decode delay", "seconds" },
-
-    { "bsf", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(bitstream_filters) },
-        "A comma-separated list of bitstream filters", "bitstream_filters" },
-
-    { "max_muxing_queue_size", HAS_ARG | OPT_INT | OPT_SPEC | OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(max_muxing_queue_size) },
-        "maximum number of packets that can be buffered while waiting for all streams to initialize", "packets" },
-
-    /* data codec support */
-    { "dcodec", HAS_ARG | OPT_DATA | OPT_PERFILE | OPT_EXPERT | OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_data_codec },
-        "force data codec ('copy' to copy stream)", "codec" },
-
-#if CONFIG_VAAPI
-    { "vaapi_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_vaapi_device },
-        "set VAAPI hardware device (DRM path or X11 display name)", "device" },
-#endif
-
-    { NULL, },
-};
diff --git a/avconv_qsv.c b/avconv_qsv.c
deleted file mode 100644 (file)
index 723c6e0..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * This file is part of Libav.
- *
- * Libav is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Libav is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <mfx/mfxvideo.h>
-#include <stdlib.h>
-
-#include "libavutil/dict.h"
-#include "libavutil/hwcontext.h"
-#include "libavutil/hwcontext_qsv.h"
-#include "libavutil/mem.h"
-#include "libavutil/opt.h"
-#include "libavcodec/qsv.h"
-
-#include "avconv.h"
-
-static int qsv_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
-{
-    InputStream *ist = s->opaque;
-
-    return av_hwframe_get_buffer(ist->hw_frames_ctx, frame, 0);
-}
-
-static void qsv_uninit(AVCodecContext *s)
-{
-    InputStream *ist = s->opaque;
-    av_buffer_unref(&ist->hw_frames_ctx);
-}
-
-static int qsv_device_init(InputStream *ist)
-{
-    int err;
-
-    err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_QSV,
-                                 ist->hwaccel_device, NULL, 0);
-    if (err < 0) {
-        av_log(NULL, AV_LOG_ERROR, "Error creating a QSV device\n");
-        return err;
-    }
-
-    return 0;
-}
-
-int qsv_init(AVCodecContext *s)
-{
-    InputStream *ist = s->opaque;
-    AVHWFramesContext *frames_ctx;
-    AVQSVFramesContext *frames_hwctx;
-    int ret;
-
-    if (!hw_device_ctx) {
-        ret = qsv_device_init(ist);
-        if (ret < 0)
-            return ret;
-    }
-
-    av_buffer_unref(&ist->hw_frames_ctx);
-    ist->hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx);
-    if (!ist->hw_frames_ctx)
-        return AVERROR(ENOMEM);
-
-    frames_ctx   = (AVHWFramesContext*)ist->hw_frames_ctx->data;
-    frames_hwctx = frames_ctx->hwctx;
-
-    frames_ctx->width             = FFALIGN(s->coded_width,  32);
-    frames_ctx->height            = FFALIGN(s->coded_height, 32);
-    frames_ctx->format            = AV_PIX_FMT_QSV;
-    frames_ctx->sw_format         = s->sw_pix_fmt;
-    frames_ctx->initial_pool_size = 32;
-    frames_hwctx->frame_type      = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
-
-    ret = av_hwframe_ctx_init(ist->hw_frames_ctx);
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_ERROR, "Error initializing a QSV frame pool\n");
-        return ret;
-    }
-
-    ist->hwaccel_get_buffer = qsv_get_buffer;
-    ist->hwaccel_uninit     = qsv_uninit;
-
-    return 0;
-}
diff --git a/avconv_vaapi.c b/avconv_vaapi.c
deleted file mode 100644 (file)
index 584b8b4..0000000
+++ /dev/null
@@ -1,231 +0,0 @@
-/*
- * This file is part of Libav.
- *
- * Libav is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Libav is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "config.h"
-
-#include "libavutil/avassert.h"
-#include "libavutil/frame.h"
-#include "libavutil/hwcontext.h"
-#include "libavutil/log.h"
-
-#include "avconv.h"
-
-
-static AVClass vaapi_class = {
-    .class_name = "vaapi",
-    .item_name  = av_default_item_name,
-    .version    = LIBAVUTIL_VERSION_INT,
-};
-
-#define DEFAULT_SURFACES 20
-
-typedef struct VAAPIDecoderContext {
-    const AVClass *class;
-
-    AVBufferRef       *device_ref;
-    AVHWDeviceContext *device;
-    AVBufferRef       *frames_ref;
-    AVHWFramesContext *frames;
-
-    // The output need not have the same format, width and height as the
-    // decoded frames - the copy for non-direct-mapped access is actually
-    // a whole vpp instance which can do arbitrary scaling and format
-    // conversion.
-    enum AVPixelFormat output_format;
-} VAAPIDecoderContext;
-
-
-static int vaapi_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
-{
-    InputStream *ist = avctx->opaque;
-    VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
-    int err;
-
-    err = av_hwframe_get_buffer(ctx->frames_ref, frame, 0);
-    if (err < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to allocate decoder surface.\n");
-    } else {
-        av_log(ctx, AV_LOG_DEBUG, "Decoder given surface %#x.\n",
-               (unsigned int)(uintptr_t)frame->data[3]);
-    }
-    return err;
-}
-
-static int vaapi_retrieve_data(AVCodecContext *avctx, AVFrame *input)
-{
-    InputStream *ist = avctx->opaque;
-    VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
-    AVFrame *output = 0;
-    int err;
-
-    av_assert0(input->format == AV_PIX_FMT_VAAPI);
-
-    if (ctx->output_format == AV_PIX_FMT_VAAPI) {
-        // Nothing to do.
-        return 0;
-    }
-
-    av_log(ctx, AV_LOG_DEBUG, "Retrieve data from surface %#x.\n",
-           (unsigned int)(uintptr_t)input->data[3]);
-
-    output = av_frame_alloc();
-    if (!output)
-        return AVERROR(ENOMEM);
-
-    output->format = ctx->output_format;
-
-    err = av_hwframe_transfer_data(output, input, 0);
-    if (err < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to transfer data to "
-               "output frame: %d.\n", err);
-        goto fail;
-    }
-
-    err = av_frame_copy_props(output, input);
-    if (err < 0) {
-        av_frame_unref(output);
-        goto fail;
-    }
-
-    av_frame_unref(input);
-    av_frame_move_ref(input, output);
-    av_frame_free(&output);
-
-    return 0;
-
-fail:
-    if (output)
-        av_frame_free(&output);
-    return err;
-}
-
-static void vaapi_decode_uninit(AVCodecContext *avctx)
-{
-    InputStream *ist = avctx->opaque;
-    VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
-
-    if (ctx) {
-        av_buffer_unref(&ctx->frames_ref);
-        av_buffer_unref(&ctx->device_ref);
-        av_free(ctx);
-    }
-
-    av_buffer_unref(&ist->hw_frames_ctx);
-
-    ist->hwaccel_ctx           = NULL;
-    ist->hwaccel_uninit        = NULL;
-    ist->hwaccel_get_buffer    = NULL;
-    ist->hwaccel_retrieve_data = NULL;
-}
-
-int vaapi_decode_init(AVCodecContext *avctx)
-{
-    InputStream *ist = avctx->opaque;
-    VAAPIDecoderContext *ctx;
-    int err;
-    int loglevel = (ist->hwaccel_id != HWACCEL_VAAPI ? AV_LOG_VERBOSE
-                                                     : AV_LOG_ERROR);
-
-    if (ist->hwaccel_ctx)
-        vaapi_decode_uninit(avctx);
-
-    // We have -hwaccel without -vaapi_device, so just initialise here with
-    // the device passed as -hwaccel_device (if -vaapi_device was passed, it
-    // will always have been called before now).
-    if (!hw_device_ctx) {
-        err = vaapi_device_init(ist->hwaccel_device);
-        if (err < 0)
-            return err;
-    }
-
-    ctx = av_mallocz(sizeof(*ctx));
-    if (!ctx)
-        return AVERROR(ENOMEM);
-    ctx->class = &vaapi_class;
-
-    ctx->device_ref = av_buffer_ref(hw_device_ctx);
-    ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
-
-    ctx->output_format = ist->hwaccel_output_format;
-    avctx->pix_fmt = ctx->output_format;
-
-    ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
-    if (!ctx->frames_ref) {
-        av_log(ctx, loglevel, "Failed to create VAAPI frame context.\n");
-        err = AVERROR(ENOMEM);
-        goto fail;
-    }
-
-    ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;
-
-    ctx->frames->format = AV_PIX_FMT_VAAPI;
-    ctx->frames->width  = avctx->coded_width;
-    ctx->frames->height = avctx->coded_height;
-
-    // It would be nice if we could query the available formats here,
-    // but unfortunately we don't have a VAConfigID to do it with.
-    // For now, just assume an NV12 format (or P010 if 10-bit).
-    ctx->frames->sw_format = (avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
-                              AV_PIX_FMT_P010 : AV_PIX_FMT_NV12);
-
-    // For frame-threaded decoding, at least one additional surface
-    // is needed for each thread.
-    ctx->frames->initial_pool_size = DEFAULT_SURFACES;
-    if (avctx->active_thread_type & FF_THREAD_FRAME)
-        ctx->frames->initial_pool_size += avctx->thread_count;
-
-    err = av_hwframe_ctx_init(ctx->frames_ref);
-    if (err < 0) {
-        av_log(ctx, loglevel, "Failed to initialise VAAPI frame "
-               "context: %d\n", err);
-        goto fail;
-    }
-
-    ist->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
-    if (!ist->hw_frames_ctx) {
-        err = AVERROR(ENOMEM);
-        goto fail;
-    }
-
-    ist->hwaccel_ctx           = ctx;
-    ist->hwaccel_uninit        = &vaapi_decode_uninit;
-    ist->hwaccel_get_buffer    = &vaapi_get_buffer;
-    ist->hwaccel_retrieve_data = &vaapi_retrieve_data;
-
-    return 0;
-
-fail:
-    vaapi_decode_uninit(avctx);
-    return err;
-}
-
-static AVClass *vaapi_log = &vaapi_class;
-
-av_cold int vaapi_device_init(const char *device)
-{
-    int err;
-
-    err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
-                                 device, NULL, 0);
-    if (err < 0) {
-        av_log(&vaapi_log, AV_LOG_ERROR, "Failed to create a VAAPI device\n");
-        return err;
-    }
-
-    return 0;
-}
diff --git a/avconv_vda.c b/avconv_vda.c
deleted file mode 100644 (file)
index d86076e..0000000
+++ /dev/null
@@ -1,136 +0,0 @@
-/*
- * This file is part of Libav.
- *
- * Libav is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Libav is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "libavcodec/avcodec.h"
-#include "libavcodec/vda.h"
-#include "libavutil/imgutils.h"
-
-#include "avconv.h"
-
-typedef struct VDAContext {
-    AVFrame *tmp_frame;
-} VDAContext;
-
-static int vda_retrieve_data(AVCodecContext *s, AVFrame *frame)
-{
-    InputStream *ist = s->opaque;
-    VDAContext  *vda = ist->hwaccel_ctx;
-    CVPixelBufferRef pixbuf = (CVPixelBufferRef)frame->data[3];
-    OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
-    CVReturn err;
-    uint8_t *data[4] = { 0 };
-    int linesize[4] = { 0 };
-    int planes, ret, i;
-
-    av_frame_unref(vda->tmp_frame);
-
-    switch (pixel_format) {
-    case kCVPixelFormatType_420YpCbCr8Planar: vda->tmp_frame->format = AV_PIX_FMT_YUV420P; break;
-    case kCVPixelFormatType_422YpCbCr8:       vda->tmp_frame->format = AV_PIX_FMT_UYVY422; break;
-    default:
-        av_log(NULL, AV_LOG_ERROR,
-               "Unsupported pixel format: %u\n", pixel_format);
-        return AVERROR(ENOSYS);
-    }
-
-    vda->tmp_frame->width  = frame->width;
-    vda->tmp_frame->height = frame->height;
-    ret = av_frame_get_buffer(vda->tmp_frame, 32);
-    if (ret < 0)
-        return ret;
-
-    err = CVPixelBufferLockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
-    if (err != kCVReturnSuccess) {
-        av_log(NULL, AV_LOG_ERROR, "Error locking the pixel buffer.\n");
-        return AVERROR_UNKNOWN;
-    }
-
-    if (CVPixelBufferIsPlanar(pixbuf)) {
-
-        planes = CVPixelBufferGetPlaneCount(pixbuf);
-        for (i = 0; i < planes; i++) {
-            data[i]     = CVPixelBufferGetBaseAddressOfPlane(pixbuf, i);
-            linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i);
-        }
-    } else {
-        data[0] = CVPixelBufferGetBaseAddress(pixbuf);
-        linesize[0] = CVPixelBufferGetBytesPerRow(pixbuf);
-    }
-
-    av_image_copy(vda->tmp_frame->data, vda->tmp_frame->linesize,
-                  data, linesize, vda->tmp_frame->format,
-                  frame->width, frame->height);
-
-    CVPixelBufferUnlockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
-
-    ret = av_frame_copy_props(vda->tmp_frame, frame);
-    if (ret < 0)
-        return ret;
-
-    av_frame_unref(frame);
-    av_frame_move_ref(frame, vda->tmp_frame);
-
-    return 0;
-}
-
-static void vda_uninit(AVCodecContext *s)
-{
-    InputStream *ist = s->opaque;
-    VDAContext  *vda = ist->hwaccel_ctx;
-
-    ist->hwaccel_uninit        = NULL;
-    ist->hwaccel_retrieve_data = NULL;
-
-    av_frame_free(&vda->tmp_frame);
-
-    av_vda_default_free(s);
-    av_freep(&ist->hwaccel_ctx);
-}
-
-int vda_init(AVCodecContext *s)
-{
-    InputStream *ist = s->opaque;
-    int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
-    VDAContext *vda;
-    int ret;
-
-    vda = av_mallocz(sizeof(*vda));
-    if (!vda)
-        return AVERROR(ENOMEM);
-
-    ist->hwaccel_ctx           = vda;
-    ist->hwaccel_uninit        = vda_uninit;
-    ist->hwaccel_retrieve_data = vda_retrieve_data;
-
-    vda->tmp_frame = av_frame_alloc();
-    if (!vda->tmp_frame) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
-
-    ret = av_vda_default_init(s);
-    if (ret < 0) {
-        av_log(NULL, loglevel, "Error creating VDA decoder.\n");
-        goto fail;
-    }
-
-    return 0;
-fail:
-    vda_uninit(s);
-    return ret;
-}
diff --git a/avconv_vdpau.c b/avconv_vdpau.c
deleted file mode 100644 (file)
index 5fedcee..0000000
+++ /dev/null
@@ -1,159 +0,0 @@
-/*
- * This file is part of Libav.
- *
- * Libav is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Libav is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <stdint.h>
-
-#include "avconv.h"
-
-#include "libavcodec/vdpau.h"
-
-#include "libavutil/buffer.h"
-#include "libavutil/frame.h"
-#include "libavutil/hwcontext.h"
-#include "libavutil/hwcontext_vdpau.h"
-#include "libavutil/pixfmt.h"
-
-typedef struct VDPAUContext {
-    AVBufferRef *hw_frames_ctx;
-    AVFrame *tmp_frame;
-} VDPAUContext;
-
-static void vdpau_uninit(AVCodecContext *s)
-{
-    InputStream  *ist = s->opaque;
-    VDPAUContext *ctx = ist->hwaccel_ctx;
-
-    ist->hwaccel_uninit        = NULL;
-    ist->hwaccel_get_buffer    = NULL;
-    ist->hwaccel_retrieve_data = NULL;
-
-    av_buffer_unref(&ctx->hw_frames_ctx);
-    av_frame_free(&ctx->tmp_frame);
-
-    av_freep(&ist->hwaccel_ctx);
-    av_freep(&s->hwaccel_context);
-}
-
-static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
-{
-    InputStream         *ist = s->opaque;
-    VDPAUContext        *ctx = ist->hwaccel_ctx;
-
-    return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
-}
-
-static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame)
-{
-    InputStream        *ist = s->opaque;
-    VDPAUContext       *ctx = ist->hwaccel_ctx;
-    int ret;
-
-    ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
-    if (ret < 0)
-        return ret;
-
-    ret = av_frame_copy_props(ctx->tmp_frame, frame);
-    if (ret < 0) {
-        av_frame_unref(ctx->tmp_frame);
-        return ret;
-    }
-
-    av_frame_unref(frame);
-    av_frame_move_ref(frame, ctx->tmp_frame);
-
-    return 0;
-}
-
-static int vdpau_alloc(AVCodecContext *s)
-{
-    InputStream  *ist = s->opaque;
-    int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
-    VDPAUContext *ctx;
-    int ret;
-
-    AVBufferRef          *device_ref = NULL;
-    AVHWDeviceContext    *device_ctx;
-    AVVDPAUDeviceContext *device_hwctx;
-    AVHWFramesContext    *frames_ctx;
-
-    ctx = av_mallocz(sizeof(*ctx));
-    if (!ctx)
-        return AVERROR(ENOMEM);
-
-    ist->hwaccel_ctx           = ctx;
-    ist->hwaccel_uninit        = vdpau_uninit;
-    ist->hwaccel_get_buffer    = vdpau_get_buffer;
-    ist->hwaccel_retrieve_data = vdpau_retrieve_data;
-
-    ctx->tmp_frame = av_frame_alloc();
-    if (!ctx->tmp_frame)
-        goto fail;
-
-    ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_VDPAU,
-                                 ist->hwaccel_device, NULL, 0);
-    if (ret < 0)
-        goto fail;
-    device_ctx   = (AVHWDeviceContext*)device_ref->data;
-    device_hwctx = device_ctx->hwctx;
-
-    ctx->hw_frames_ctx = av_hwframe_ctx_alloc(device_ref);
-    if (!ctx->hw_frames_ctx)
-        goto fail;
-    av_buffer_unref(&device_ref);
-
-    frames_ctx            = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
-    frames_ctx->format    = AV_PIX_FMT_VDPAU;
-    frames_ctx->sw_format = s->sw_pix_fmt;
-    frames_ctx->width     = s->coded_width;
-    frames_ctx->height    = s->coded_height;
-
-    ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
-    if (ret < 0)
-        goto fail;
-
-    if (av_vdpau_bind_context(s, device_hwctx->device, device_hwctx->get_proc_address, 0))
-        goto fail;
-
-    av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU to decode input stream #%d:%d.\n",
-           ist->file_index, ist->st->index);
-
-    return 0;
-
-fail:
-    av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
-           ist->file_index, ist->st->index);
-    av_buffer_unref(&device_ref);
-    vdpau_uninit(s);
-    return AVERROR(EINVAL);
-}
-
-int vdpau_init(AVCodecContext *s)
-{
-    InputStream *ist = s->opaque;
-
-    if (!ist->hwaccel_ctx) {
-        int ret = vdpau_alloc(s);
-        if (ret < 0)
-            return ret;
-    }
-
-    ist->hwaccel_get_buffer    = vdpau_get_buffer;
-    ist->hwaccel_retrieve_data = vdpau_retrieve_data;
-
-    return 0;
-}
diff --git a/avplay.c b/avplay.c
deleted file mode 100644 (file)
index 18879e1..0000000
--- a/avplay.c
+++ /dev/null
@@ -1,3061 +0,0 @@
-/*
- * avplay : Simple Media Player based on the Libav libraries
- * Copyright (c) 2003 Fabrice Bellard
- *
- * This file is part of Libav.
- *
- * Libav is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Libav is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "config.h"
-#include <inttypes.h>
-#include <math.h>
-#include <limits.h>
-#include <stdint.h>
-
-#include "libavutil/avstring.h"
-#include "libavutil/colorspace.h"
-#include "libavutil/display.h"
-#include "libavutil/mathematics.h"
-#include "libavutil/pixdesc.h"
-#include "libavutil/imgutils.h"
-#include "libavutil/dict.h"
-#include "libavutil/parseutils.h"
-#include "libavutil/samplefmt.h"
-#include "libavutil/time.h"
-#include "libavformat/avformat.h"
-#include "libavdevice/avdevice.h"
-#include "libavresample/avresample.h"
-#include "libavutil/opt.h"
-#include "libavcodec/avfft.h"
-
-#include "libavfilter/avfilter.h"
-#include "libavfilter/buffersink.h"
-#include "libavfilter/buffersrc.h"
-
-#include "cmdutils.h"
-
-#include <SDL.h>
-#include <SDL_thread.h>
-
-#ifdef __MINGW32__
-#undef main /* We don't want SDL to override our main() */
-#endif
-
-#include <assert.h>
-
-const char program_name[] = "avplay";
-const int program_birth_year = 2003;
-
-#define MAX_QUEUE_SIZE (15 * 1024 * 1024)
-#define MIN_AUDIOQ_SIZE (20 * 16 * 1024)
-#define MIN_FRAMES 5
-
-/* SDL audio buffer size, in samples. Should be small to have precise
-   A/V sync as SDL does not have hardware buffer fullness info. */
-#define SDL_AUDIO_BUFFER_SIZE 1024
-
-/* no AV sync correction is done if below the AV sync threshold */
-#define AV_SYNC_THRESHOLD 0.01
-/* no AV correction is done if too big error */
-#define AV_NOSYNC_THRESHOLD 10.0
-
-#define FRAME_SKIP_FACTOR 0.05
-
-/* maximum audio speed change to get correct sync */
-#define SAMPLE_CORRECTION_PERCENT_MAX 10
-
-/* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
-#define AUDIO_DIFF_AVG_NB   20
-
-/* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
-#define SAMPLE_ARRAY_SIZE (2 * 65536)
-
-static int64_t sws_flags = SWS_BICUBIC;
-
-typedef struct PacketQueue {
-    AVPacketList *first_pkt, *last_pkt;
-    int nb_packets;
-    int size;
-    int abort_request;
-    SDL_mutex *mutex;
-    SDL_cond *cond;
-} PacketQueue;
-
-#define VIDEO_PICTURE_QUEUE_SIZE 2
-#define SUBPICTURE_QUEUE_SIZE 4
-
-typedef struct VideoPicture {
-    double pts;             // presentation timestamp for this picture
-    double target_clock;    // av_gettime_relative() time at which this should be displayed ideally
-    int64_t pos;            // byte position in file
-    SDL_Overlay *bmp;
-    int width, height; /* source height & width */
-    int allocated;
-    int reallocate;
-    enum AVPixelFormat pix_fmt;
-
-    AVRational sar;
-} VideoPicture;
-
-typedef struct SubPicture {
-    double pts; /* presentation time stamp for this picture */
-    AVSubtitle sub;
-} SubPicture;
-
-enum {
-    AV_SYNC_AUDIO_MASTER, /* default choice */
-    AV_SYNC_VIDEO_MASTER,
-    AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
-};
-
-typedef struct PlayerState {
-    SDL_Thread *parse_tid;
-    SDL_Thread *video_tid;
-    SDL_Thread *refresh_tid;
-    AVInputFormat *iformat;
-    int no_background;
-    int abort_request;
-    int paused;
-    int last_paused;
-    int seek_req;
-    int seek_flags;
-    int64_t seek_pos;
-    int64_t seek_rel;
-    int read_pause_return;
-    AVFormatContext *ic;
-
-    int audio_stream;
-
-    int av_sync_type;
-    double external_clock; /* external clock base */
-    int64_t external_clock_time;
-
-    double audio_clock;
-    double audio_diff_cum; /* used for AV difference average computation */
-    double audio_diff_avg_coef;
-    double audio_diff_threshold;
-    int audio_diff_avg_count;
-    AVStream *audio_st;
-    AVCodecContext *audio_dec;
-    PacketQueue audioq;
-    int audio_hw_buf_size;
-    uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
-    uint8_t *audio_buf;
-    uint8_t *audio_buf1;
-    unsigned int audio_buf_size; /* in bytes */
-    int audio_buf_index; /* in bytes */
-    AVPacket audio_pkt_temp;
-    AVPacket audio_pkt;
-    enum AVSampleFormat sdl_sample_fmt;
-    uint64_t sdl_channel_layout;
-    int sdl_channels;
-    int sdl_sample_rate;
-    enum AVSampleFormat resample_sample_fmt;
-    uint64_t resample_channel_layout;
-    int resample_sample_rate;
-    AVAudioResampleContext *avr;
-    AVFrame *frame;
-
-    int show_audio; /* if true, display audio samples */
-    int16_t sample_array[SAMPLE_ARRAY_SIZE];
-    int sample_array_index;
-    int last_i_start;
-    RDFTContext *rdft;
-    int rdft_bits;
-    FFTSample *rdft_data;
-    int xpos;
-
-    SDL_Thread *subtitle_tid;
-    int subtitle_stream;
-    int subtitle_stream_changed;
-    AVStream *subtitle_st;
-    AVCodecContext *subtitle_dec;
-    PacketQueue subtitleq;
-    SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
-    int subpq_size, subpq_rindex, subpq_windex;
-    SDL_mutex *subpq_mutex;
-    SDL_cond *subpq_cond;
-
-    double frame_timer;
-    double frame_last_pts;
-    double frame_last_delay;
-    double video_clock;             // pts of last decoded frame / predicted pts of next decoded frame
-    int video_stream;
-    AVStream *video_st;
-    AVCodecContext *video_dec;
-    PacketQueue videoq;
-    double video_current_pts;       // current displayed pts (different from video_clock if frame fifos are used)
-    double video_current_pts_drift; // video_current_pts - time (av_gettime_relative) at which we updated video_current_pts - used to have running video pts
-    int64_t video_current_pos;      // current displayed file pos
-    VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
-    int pictq_size, pictq_rindex, pictq_windex;
-    SDL_mutex *pictq_mutex;
-    SDL_cond *pictq_cond;
-
-    //    QETimer *video_timer;
-    char filename[1024];
-    int width, height, xleft, ytop;
-
-    PtsCorrectionContext pts_ctx;
-
-    AVFilterContext *in_video_filter;   // the first filter in the video chain
-    AVFilterContext *out_video_filter;  // the last filter in the video chain
-
-    float skip_frames;
-    float skip_frames_index;
-    int refresh;
-
-    SpecifierOpt *codec_names;
-    int        nb_codec_names;
-} PlayerState;
-
-/* options specified by the user */
-static AVInputFormat *file_iformat;
-static const char *input_filename;
-static const char *window_title;
-static int fs_screen_width;
-static int fs_screen_height;
-static int screen_width  = 0;
-static int screen_height = 0;
-static int audio_disable;
-static int video_disable;
-static int wanted_stream[AVMEDIA_TYPE_NB] = {
-    [AVMEDIA_TYPE_AUDIO]    = -1,
-    [AVMEDIA_TYPE_VIDEO]    = -1,
-    [AVMEDIA_TYPE_SUBTITLE] = -1,
-};
-static int seek_by_bytes = -1;
-static int display_disable;
-static int show_status = 1;
-static int av_sync_type = AV_SYNC_AUDIO_MASTER;
-static int64_t start_time = AV_NOPTS_VALUE;
-static int64_t duration = AV_NOPTS_VALUE;
-static int step = 0;
-static int workaround_bugs = 1;
-static int fast = 0;
-static int genpts = 0;
-static int idct = FF_IDCT_AUTO;
-static enum AVDiscard skip_frame       = AVDISCARD_DEFAULT;
-static enum AVDiscard skip_idct        = AVDISCARD_DEFAULT;
-static enum AVDiscard skip_loop_filter = AVDISCARD_DEFAULT;
-static int error_concealment = 3;
-static int decoder_reorder_pts = -1;
-static int noautoexit;
-static int exit_on_keydown;
-static int exit_on_mousedown;
-static int loop = 1;
-static int framedrop = 1;
-static int infinite_buffer = 0;
-
-static int rdftspeed = 20;
-static char *vfilters = NULL;
-static int autorotate = 1;
-
-/* current context */
-static int is_full_screen;
-static PlayerState player_state;
-static PlayerState *player = &player_state;
-static int64_t audio_callback_time;
-
-static AVPacket flush_pkt;
-
-#define FF_ALLOC_EVENT   (SDL_USEREVENT)
-#define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
-#define FF_QUIT_EVENT    (SDL_USEREVENT + 2)
-
-static SDL_Surface *screen;
-
-static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
-
-/* packet queue handling */
-static void packet_queue_init(PacketQueue *q)
-{
-    memset(q, 0, sizeof(PacketQueue));
-    q->mutex = SDL_CreateMutex();
-    q->cond = SDL_CreateCond();
-    packet_queue_put(q, &flush_pkt);
-}
-
-static void packet_queue_flush(PacketQueue *q)
-{
-    AVPacketList *pkt, *pkt1;
-
-    SDL_LockMutex(q->mutex);
-    for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
-        pkt1 = pkt->next;
-        av_packet_unref(&pkt->pkt);
-        av_freep(&pkt);
-    }
-    q->last_pkt = NULL;
-    q->first_pkt = NULL;
-    q->nb_packets = 0;
-    q->size = 0;
-    SDL_UnlockMutex(q->mutex);
-}
-
-static void packet_queue_end(PacketQueue *q)
-{
-    packet_queue_flush(q);
-    SDL_DestroyMutex(q->mutex);
-    SDL_DestroyCond(q->cond);
-}
-
-static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
-{
-    AVPacketList *pkt1;
-
-    pkt1 = av_malloc(sizeof(AVPacketList));
-    if (!pkt1)
-        return -1;
-    pkt1->pkt = *pkt;
-    pkt1->next = NULL;
-
-
-    SDL_LockMutex(q->mutex);
-
-    if (!q->last_pkt)
-
-        q->first_pkt = pkt1;
-    else
-        q->last_pkt->next = pkt1;
-    q->last_pkt = pkt1;
-    q->nb_packets++;
-    q->size += pkt1->pkt.size + sizeof(*pkt1);
-    /* XXX: should duplicate packet data in DV case */
-    SDL_CondSignal(q->cond);
-
-    SDL_UnlockMutex(q->mutex);
-    return 0;
-}
-
-static void packet_queue_abort(PacketQueue *q)
-{
-    SDL_LockMutex(q->mutex);
-
-    q->abort_request = 1;
-
-    SDL_CondSignal(q->cond);
-
-    SDL_UnlockMutex(q->mutex);
-}
-
-/* return < 0 if aborted, 0 if no packet and > 0 if packet.  */
-static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
-{
-    AVPacketList *pkt1;
-    int ret;
-
-    SDL_LockMutex(q->mutex);
-
-    for (;;) {
-        if (q->abort_request) {
-            ret = -1;
-            break;
-        }
-
-        pkt1 = q->first_pkt;
-        if (pkt1) {
-            q->first_pkt = pkt1->next;
-            if (!q->first_pkt)
-                q->last_pkt = NULL;
-            q->nb_packets--;
-            q->size -= pkt1->pkt.size + sizeof(*pkt1);
-            *pkt = pkt1->pkt;
-            av_free(pkt1);
-            ret = 1;
-            break;
-        } else if (!block) {
-            ret = 0;
-            break;
-        } else {
-            SDL_CondWait(q->cond, q->mutex);
-        }
-    }
-    SDL_UnlockMutex(q->mutex);
-    return ret;
-}
-
-static inline void fill_rectangle(SDL_Surface *screen,
-                                  int x, int y, int w, int h, int color)
-{
-    SDL_Rect rect;
-    rect.x = x;
-    rect.y = y;
-    rect.w = w;
-    rect.h = h;
-    SDL_FillRect(screen, &rect, color);
-}
-
-#define ALPHA_BLEND(a, oldp, newp, s)\
-((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
-
-#define RGBA_IN(r, g, b, a, s)\
-{\
-    unsigned int v = ((const uint32_t *)(s))[0];\
-    a = (v >> 24) & 0xff;\
-    r = (v >> 16) & 0xff;\
-    g = (v >> 8) & 0xff;\
-    b = v & 0xff;\
-}
-
-#define YUVA_IN(y, u, v, a, s, pal)\
-{\
-    unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
-    a = (val >> 24) & 0xff;\
-    y = (val >> 16) & 0xff;\
-    u = (val >> 8) & 0xff;\
-    v = val & 0xff;\
-}
-
-#define YUVA_OUT(d, y, u, v, a)\
-{\
-    ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
-}
-
-
-#define BPP 1
-
-static void blend_subrect(uint8_t *dst[4], uint16_t dst_linesize[4],
-                          const AVSubtitleRect *rect, int imgw, int imgh)
-{
-    int wrap, wrap3, width2, skip2;
-    int y, u, v, a, u1, v1, a1, w, h;
-    uint8_t *lum, *cb, *cr;
-    const uint8_t *p;
-    const uint32_t *pal;
-    int dstx, dsty, dstw, dsth;
-
-    dstw = av_clip(rect->w, 0, imgw);
-    dsth = av_clip(rect->h, 0, imgh);
-    dstx = av_clip(rect->x, 0, imgw - dstw);
-    dsty = av_clip(rect->y, 0, imgh - dsth);
-    /* sdl has U and V inverted */
-    lum = dst[0] +  dsty       * dst_linesize[0];
-    cb  = dst[2] + (dsty >> 1) * dst_linesize[2];
-    cr  = dst[1] + (dsty >> 1) * dst_linesize[1];
-
-    width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
-    skip2 = dstx >> 1;
-    wrap = dst_linesize[0];
-    wrap3 = rect->linesize[0];
-    p = rect->data[0];
-    pal = (const uint32_t *)rect->data[1];  /* Now in YCrCb! */
-
-    if (dsty & 1) {
-        lum += dstx;
-        cb += skip2;
-        cr += skip2;
-
-        if (dstx & 1) {
-            YUVA_IN(y, u, v, a, p, pal);
-            lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
-            cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
-            cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
-            cb++;
-            cr++;
-            lum++;
-            p += BPP;
-        }
-        for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
-            YUVA_IN(y, u, v, a, p, pal);
-            u1 = u;
-            v1 = v;
-            a1 = a;
-            lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
-
-            YUVA_IN(y, u, v, a, p + BPP, pal);
-            u1 += u;
-            v1 += v;
-            a1 += a;
-            lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
-            cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
-            cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
-            cb++;
-            cr++;
-            p += 2 * BPP;
-            lum += 2;
-        }
-        if (w) {
-            YUVA_IN(y, u, v, a, p, pal);
-            lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
-            cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
-            cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
-            p++;
-            lum++;
-        }
-        p += wrap3 - dstw * BPP;
-        lum += wrap - dstw - dstx;
-        cb += dst_linesize[2] - width2 - skip2;
-        cr += dst_linesize[1] - width2 - skip2;
-    }
-    for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
-        lum += dstx;
-        cb += skip2;
-        cr += skip2;
-
-        if (dstx & 1) {
-            YUVA_IN(y, u, v, a, p, pal);
-            u1 = u;
-            v1 = v;
-            a1 = a;
-            lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
-            p += wrap3;
-            lum += wrap;
-            YUVA_IN(y, u, v, a, p, pal);
-            u1 += u;
-            v1 += v;
-            a1 += a;
-            lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
-            cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
-            cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
-            cb++;
-            cr++;
-            p += -wrap3 + BPP;
-            lum += -wrap + 1;
-        }
-        for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
-            YUVA_IN(y, u, v, a, p, pal);
-            u1 = u;
-            v1 = v;
-            a1 = a;
-            lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
-
-            YUVA_IN(y, u, v, a, p + BPP, pal);
-            u1 += u;
-            v1 += v;
-            a1 += a;
-            lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
-            p += wrap3;
-            lum += wrap;
-
-            YUVA_IN(y, u, v, a, p, pal);
-            u1 += u;
-            v1 += v;
-            a1 += a;
-            lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
-
-            YUVA_IN(y, u, v, a, p + BPP, pal);
-            u1 += u;
-            v1 += v;
-            a1 += a;
-            lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
-
-            cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
-            cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
-
-            cb++;
-            cr++;
-            p += -wrap3 + 2 * BPP;
-            lum += -wrap + 2;
-        }
-        if (w) {
-            YUVA_IN(y, u, v, a, p, pal);
-            u1 = u;
-            v1 = v;
-            a1 = a;
-            lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
-            p += wrap3;
-            lum += wrap;
-            YUVA_IN(y, u, v, a, p, pal);
-            u1 += u;
-            v1 += v;
-            a1 += a;
-            lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
-            cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
-            cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
-            cb++;
-            cr++;
-            p += -wrap3 + BPP;
-            lum += -wrap + 1;
-        }
-        p += wrap3 + (wrap3 - dstw * BPP);
-        lum += wrap + (wrap - dstw - dstx);
-        cb += dst_linesize[2] - width2 - skip2;
-        cr += dst_linesize[1] - width2 - skip2;
-    }
-    /* handle odd height */
-    if (h) {
-        lum += dstx;
-        cb += skip2;
-        cr += skip2;
-
-        if (dstx & 1) {
-            YUVA_IN(y, u, v, a, p, pal);
-            lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
-            cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
-            cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
-            cb++;
-            cr++;
-            lum++;
-            p += BPP;
-        }
-        for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
-            YUVA_IN(y, u, v, a, p, pal);
-            u1 = u;
-            v1 = v;
-            a1 = a;
-            lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
-
-            YUVA_IN(y, u, v, a, p + BPP, pal);
-            u1 += u;
-            v1 += v;
-            a1 += a;
-            lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
-            cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
-            cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
-            cb++;
-            cr++;
-            p += 2 * BPP;
-            lum += 2;
-        }
-        if (w) {
-            YUVA_IN(y, u, v, a, p, pal);
-            lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
-            cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
-            cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
-        }
-    }
-}
-
-static void free_subpicture(SubPicture *sp)
-{
-    avsubtitle_free(&sp->sub);
-}
-
-static void video_image_display(PlayerState *is)
-{
-    VideoPicture *vp;
-    SubPicture *sp;
-    float aspect_ratio;
-    int width, height, x, y;
-    SDL_Rect rect;
-    int i;
-
-    vp = &is->pictq[is->pictq_rindex];
-    if (vp->bmp) {
-         if (!vp->sar.num)
-             aspect_ratio = 0;
-         else
-             aspect_ratio = av_q2d(vp->sar);
-        if (aspect_ratio <= 0.0)
-            aspect_ratio = 1.0;
-        aspect_ratio *= (float)vp->width / (float)vp->height;
-
-        if (is->subtitle_st)
-        {
-            if (is->subpq_size > 0)
-            {
-                sp = &is->subpq[is->subpq_rindex];
-
-                if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000))
-                {
-                    SDL_LockYUVOverlay (vp->bmp);
-
-                    for (i = 0; i < sp->sub.num_rects; i++)
-                        blend_subrect(vp->bmp->pixels, vp->bmp->pitches,
-                                      sp->sub.rects[i], vp->bmp->w, vp->bmp->h);
-
-                    SDL_UnlockYUVOverlay (vp->bmp);
-                }
-            }
-        }
-
-
-        /* XXX: we suppose the screen has a 1.0 pixel ratio */
-        height = is->height;
-        width = ((int)rint(height * aspect_ratio)) & ~1;
-        if (width > is->width) {
-            width = is->width;
-            height = ((int)rint(width / aspect_ratio)) & ~1;
-        }
-        x = (is->width - width) / 2;
-        y = (is->height - height) / 2;
-        is->no_background = 0;
-        rect.x = is->xleft + x;
-        rect.y = is->ytop  + y;
-        rect.w = width;
-        rect.h = height;
-        SDL_DisplayYUVOverlay(vp->bmp, &rect);
-    }
-}
-
-/* get the current audio output buffer size, in samples. With SDL, we
-   cannot have a precise information */
-static int audio_write_get_buf_size(PlayerState *is)
-{
-    return is->audio_buf_size - is->audio_buf_index;
-}
-
-static inline int compute_mod(int a, int b)
-{
-    a = a % b;
-    if (a >= 0)
-        return a;
-    else
-        return a + b;
-}
-
-static void video_audio_display(PlayerState *s)
-{
-    int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
-    int ch, channels, h, h2, bgcolor, fgcolor;
-    int16_t time_diff;
-    int rdft_bits, nb_freq;
-
-    for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
-        ;
-    nb_freq = 1 << (rdft_bits - 1);
-
-    /* compute display index : center on currently output samples */
-    channels = s->sdl_channels;
-    nb_display_channels = channels;
-    if (!s->paused) {
-        int data_used = s->show_audio == 1 ? s->width : (2 * nb_freq);
-        n = 2 * channels;
-        delay = audio_write_get_buf_size(s);
-        delay /= n;
-
-        /* to be more precise, we take into account the time spent since
-           the last buffer computation */
-        if (audio_callback_time) {
-            time_diff = av_gettime_relative() - audio_callback_time;
-            delay -= (time_diff * s->sdl_sample_rate) / 1000000;
-        }
-
-        delay += 2 * data_used;
-        if (delay < data_used)
-            delay = data_used;
-
-        i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
-        if (s->show_audio == 1) {
-            h = INT_MIN;
-            for (i = 0; i < 1000; i += channels) {
-                int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
-                int a = s->sample_array[idx];
-                int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
-                int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
-                int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
-                int score = a - d;
-                if (h < score && (b ^ c) < 0) {
-                    h = score;
-                    i_start = idx;
-                }
-            }
-        }
-
-        s->last_i_start = i_start;
-    } else {
-        i_start = s->last_i_start;
-    }
-
-    bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
-    if (s->show_audio == 1) {
-        fill_rectangle(screen,
-                       s->xleft, s->ytop, s->width, s->height,
-                       bgcolor);
-
-        fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
-
-        /* total height for one channel */
-        h = s->height / nb_display_channels;
-        /* graph height / 2 */
-        h2 = (h * 9) / 20;
-        for (ch = 0; ch < nb_display_channels; ch++) {
-            i = i_start + ch;
-            y1 = s->ytop + ch * h + (h / 2); /* position of center line */
-            for (x = 0; x < s->width; x++) {
-                y = (s->sample_array[i] * h2) >> 15;
-                if (y < 0) {
-                    y = -y;
-                    ys = y1 - y;
-                } else {
-                    ys = y1;
-                }
-                fill_rectangle(screen,
-                               s->xleft + x, ys, 1, y,
-                               fgcolor);
-                i += channels;
-                if (i >= SAMPLE_ARRAY_SIZE)
-                    i -= SAMPLE_ARRAY_SIZE;
-            }
-        }
-
-        fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
-
-        for (ch = 1; ch < nb_display_channels; ch++) {
-            y = s->ytop + ch * h;
-            fill_rectangle(screen,
-                           s->xleft, y, s->width, 1,
-                           fgcolor);
-        }
-        SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
-    } else {
-        nb_display_channels= FFMIN(nb_display_channels, 2);
-        if (rdft_bits != s->rdft_bits) {
-            av_rdft_end(s->rdft);
-            av_free(s->rdft_data);
-            s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
-            s->rdft_bits = rdft_bits;
-            s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data));
-        }
-        {
-            FFTSample *data[2];
-            for (ch = 0; ch < nb_display_channels; ch++) {
-                data[ch] = s->rdft_data + 2 * nb_freq * ch;
-                i = i_start + ch;
-                for (x = 0; x < 2 * nb_freq; x++) {
-                    double w = (x-nb_freq) * (1.0 / nb_freq);
-                    data[ch][x] = s->sample_array[i] * (1.0 - w * w);
-                    i += channels;
-                    if (i >= SAMPLE_ARRAY_SIZE)
-                        i -= SAMPLE_ARRAY_SIZE;
-                }
-                av_rdft_calc(s->rdft, data[ch]);
-            }
-            /* Least efficient way to do this, we should of course
-             * directly access it but it is more than fast enough. */
-            for (y = 0; y < s->height; y++) {
-                double w = 1 / sqrt(nb_freq);
-                int a = sqrt(w * sqrt(data[0][2 * y + 0] * data[0][2 * y + 0] + data[0][2 * y + 1] * data[0][2 * y + 1]));
-                int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
-                       + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
-                a = FFMIN(a, 255);
-                b = FFMIN(b, 255);
-                fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
-
-                fill_rectangle(screen,
-                            s->xpos, s->height-y, 1, 1,
-                            fgcolor);
-            }
-        }
-        SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
-        s->xpos++;
-        if (s->xpos >= s->width)
-            s->xpos= s->xleft;
-    }
-}
-
-static int video_open(PlayerState *is)
-{
-    int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
-    int w,h;
-
-    if (is_full_screen) flags |= SDL_FULLSCREEN;
-    else                flags |= SDL_RESIZABLE;
-
-    if (is_full_screen && fs_screen_width) {
-        w = fs_screen_width;
-        h = fs_screen_height;
-    } else if (!is_full_screen && screen_width) {
-        w = screen_width;
-        h = screen_height;
-    } else if (is->out_video_filter && is->out_video_filter->inputs[0]) {
-        w = is->out_video_filter->inputs[0]->w;
-        h = is->out_video_filter->inputs[0]->h;
-    } else {
-        w = 640;
-        h = 480;
-    }
-    if (screen && is->width == screen->w && screen->w == w
-       && is->height== screen->h && screen->h == h)
-        return 0;
-
-#if defined(__APPLE__) && !SDL_VERSION_ATLEAST(1, 2, 14)
-    /* setting bits_per_pixel = 0 or 32 causes blank video on OS X and older SDL */
-    screen = SDL_SetVideoMode(w, h, 24, flags);
-#else
-    screen = SDL_SetVideoMode(w, h, 0, flags);
-#endif
-    if (!screen) {
-        fprintf(stderr, "SDL: could not set video mode - exiting\n");
-        return -1;
-    }
-    if (!window_title)
-        window_title = input_filename;
-    SDL_WM_SetCaption(window_title, window_title);
-
-    is->width  = screen->w;
-    is->height = screen->h;
-
-    return 0;
-}
-
-/* display the current picture, if any */
-static void video_display(PlayerState *is)
-{
-    if (!screen)
-        video_open(player);
-    if (is->audio_st && is->show_audio)
-        video_audio_display(is);
-    else if (is->video_st)
-        video_image_display(is);
-}
-
-static int refresh_thread(void *opaque)
-{
-    PlayerState *is= opaque;
-    while (!is->abort_request) {
-        SDL_Event event;
-        event.type = FF_REFRESH_EVENT;
-        event.user.data1 = opaque;
-        if (!is->refresh) {
-            is->refresh = 1;
-            SDL_PushEvent(&event);
-        }
-        av_usleep(is->audio_st && is->show_audio ? rdftspeed * 1000 : 5000); // FIXME ideally we should wait the correct time but SDLs event passing is so slow it would be silly
-    }
-    return 0;
-}
-
-/* get the current audio clock value */
-static double get_audio_clock(PlayerState *is)
-{
-    double pts;
-    int hw_buf_size, bytes_per_sec;
-    pts = is->audio_clock;
-    hw_buf_size = audio_write_get_buf_size(is);
-    bytes_per_sec = 0;
-    if (is->audio_st) {
-        bytes_per_sec = is->sdl_sample_rate * is->sdl_channels *
-                        av_get_bytes_per_sample(is->sdl_sample_fmt);
-    }
-    if (bytes_per_sec)
-        pts -= (double)hw_buf_size / bytes_per_sec;