summaryrefslogtreecommitdiff
path: root/avtools
diff options
context:
space:
mode:
authorDiego Biurrun <diego@biurrun.de>2017-01-04 15:09:29 +0100
committerDiego Biurrun <diego@biurrun.de>2017-02-21 16:10:51 +0100
commitc95169f0ec68bdeeabc5fde8aa4076f406242524 (patch)
tree4d2898d7b831351b6567ea7732b7473d71389b62 /avtools
parentab566cc96bc0c31b34d944214bc06cec8ae8b640 (diff)
build: Move cli tool sources to a separate subdirectory
This unclutters the top-level directory and groups related files together.
Diffstat (limited to 'avtools')
-rw-r--r--avtools/Makefile52
-rw-r--r--avtools/avconv.c2913
-rw-r--r--avtools/avconv.h513
-rw-r--r--avtools/avconv_dxva2.c440
-rw-r--r--avtools/avconv_filter.c818
-rw-r--r--avtools/avconv_opt.c2745
-rw-r--r--avtools/avconv_qsv.c96
-rw-r--r--avtools/avconv_vaapi.c231
-rw-r--r--avtools/avconv_vda.c136
-rw-r--r--avtools/avconv_vdpau.c159
-rw-r--r--avtools/avplay.c3061
-rw-r--r--avtools/avprobe.c1187
-rw-r--r--avtools/cmdutils.c1702
-rw-r--r--avtools/cmdutils.h566
14 files changed, 14619 insertions, 0 deletions
diff --git a/avtools/Makefile b/avtools/Makefile
new file mode 100644
index 0000000000..d6d609f544
--- /dev/null
+++ b/avtools/Makefile
@@ -0,0 +1,52 @@
+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))
+
+OBJS-avconv += avtools/avconv_opt.o avtools/avconv_filter.o
+OBJS-avconv-$(CONFIG_LIBMFX) += avtools/avconv_qsv.o
+OBJS-avconv-$(CONFIG_VAAPI) += avtools/avconv_vaapi.o
+OBJS-avconv-$(CONFIG_VDA) += avtools/avconv_vda.o
+OBJS-avconv-$(HAVE_DXVA2_LIB) += avtools/avconv_dxva2.o
+OBJS-avconv-$(HAVE_VDPAU_X11) += avtools/avconv_vdpau.o
+
+define DOAVTOOL
+OBJS-$(1) += avtools/cmdutils.o avtools/$(1).o $(OBJS-$(1)-yes)
+$(1)$(EXESUF): $$(OBJS-$(1))
+$$(OBJS-$(1)): | avtools
+$$(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 DOAVTOOL,$(P))))
+
+all: $(AVPROGS)
+
+avtools/cmdutils.o: avversion.h | avtools
+OBJDIRS += avtools
+
+ifdef AVPROGS
+install: install-progs install-data
+endif
+
+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)"
+
+uninstall: uninstall-progs
+
+uninstall-progs:
+ $(RM) $(addprefix "$(BINDIR)/", $(ALLAVPROGS))
+
+clean::
+ $(RM) $(ALLAVPROGS) $(CLEANSUFFIXES:%=avtools/%)
diff --git a/avtools/avconv.c b/avtools/avconv.c
new file mode 100644
index 0000000000..5c36761c1d
--- /dev/null
+++ b/avtools/avconv.c
@@ -0,0 +1,2913 @@
+/*
+ * 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/avtools/avconv.h b/avtools/avconv.h
new file mode 100644
index 0000000000..3c3f0ef659
--- /dev/null
+++ b/avtools/avconv.h
@@ -0,0 +1,513 @@
+/*
+ * 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/avtools/avconv_dxva2.c b/avtools/avconv_dxva2.c
new file mode 100644
index 0000000000..7578c3f632
--- /dev/null
+++ b/avtools/avconv_dxva2.c
@@ -0,0 +1,440 @@
+/*
+ * 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/avtools/avconv_filter.c b/avtools/avconv_filter.c
new file mode 100644
index 0000000000..e53dcd271c
--- /dev/null
+++ b/avtools/avconv_filter.c
@@ -0,0 +1,818 @@
+/*
+ * 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/avtools/avconv_opt.c b/avtools/avconv_opt.c
new file mode 100644
index 0000000000..e078a0b89d
--- /dev/null
+++ b/avtools/avconv_opt.c
@@ -0,0 +1,2745 @@
+/*
+ * 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/avtools/avconv_qsv.c b/avtools/avconv_qsv.c
new file mode 100644
index 0000000000..723c6e0224
--- /dev/null
+++ b/avtools/avconv_qsv.c
@@ -0,0 +1,96 @@
+/*
+ * 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/avtools/avconv_vaapi.c b/avtools/avconv_vaapi.c
new file mode 100644
index 0000000000..584b8b4df0
--- /dev/null
+++ b/avtools/avconv_vaapi.c
@@ -0,0 +1,231 @@
+/*
+ * 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/avtools/avconv_vda.c b/avtools/avconv_vda.c
new file mode 100644
index 0000000000..d86076e79e
--- /dev/null
+++ b/avtools/avconv_vda.c
@@ -0,0 +1,136 @@
+/*
+ * 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/avtools/avconv_vdpau.c b/avtools/avconv_vdpau.c
new file mode 100644
index 0000000000..5fedceef95
--- /dev/null
+++ b/avtools/avconv_vdpau.c
@@ -0,0 +1,159 @@
+/*
+ * 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/avtools/avplay.c b/avtools/avplay.c
new file mode 100644
index 0000000000..18879e16bc
--- /dev/null
+++ b/avtools/avplay.c
@@ -0,0 +1,3061 @@
+/*
+ * 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;
+ return pts;
+}
+
+/* get the current video clock value */
+static double get_video_clock(PlayerState *is)
+{
+ if (is->paused) {
+ return is->video_current_pts;
+ } else {
+ return is->video_current_pts_drift + av_gettime_relative() / 1000000.0;
+ }
+}
+
+/* get the current external clock value */
+static double get_external_clock(PlayerState *is)
+{
+ int64_t ti;
+ ti = av_gettime_relative();
+ return is->external_clock + ((ti - is->external_clock_time) * 1e-6);
+}
+
+/* get the current master clock value */
+static double get_master_clock(PlayerState *is)
+{
+ double val;
+
+ if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
+ if (is->video_st)
+ val = get_video_clock(is);
+ else
+ val = get_audio_clock(is);
+ } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
+ if (is->audio_st)
+ val = get_audio_clock(is);
+ else
+ val = get_video_clock(is);
+ } else {
+ val = get_external_clock(is);
+ }
+ return val;
+}
+
+/* seek in the stream */
+static void stream_seek(PlayerState *is, int64_t pos, int64_t rel, int seek_by_bytes)
+{
+ if (!is->seek_req) {
+ is->seek_pos = pos;
+ is->seek_rel = rel;
+ is->seek_flags &= ~AVSEEK_FLAG_BYTE;
+ if (seek_by_bytes)
+ is->seek_flags |= AVSEEK_FLAG_BYTE;
+ is->seek_req = 1;
+ }
+}
+
+/* pause or resume the video */
+static void stream_pause(PlayerState *is)
+{
+ if (is->paused) {
+ is->frame_timer += av_gettime_relative() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
+ if (is->read_pause_return != AVERROR(ENOSYS)) {
+ is->video_current_pts = is->video_current_pts_drift + av_gettime_relative() / 1000000.0;
+ }
+ is->video_current_pts_drift = is->video_current_pts - av_gettime_relative() / 1000000.0;
+ }
+ is->paused = !is->paused;
+}
+
+static double compute_target_time(double frame_current_pts, PlayerState *is)
+{
+ double delay, sync_threshold, diff = 0;
+
+ /* compute nominal delay */
+ delay = frame_current_pts - is->frame_last_pts;
+ if (delay <= 0 || delay >= 10.0) {
+ /* if incorrect delay, use previous one */
+ delay = is->frame_last_delay;
+ } else {
+ is->frame_last_delay = delay;
+ }
+ is->frame_last_pts = frame_current_pts;
+
+ /* update delay to follow master synchronisation source */
+ if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) ||
+ is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
+ /* if video is slave, we try to correct big delays by
+ duplicating or deleting a frame */
+ diff = get_video_clock(is) - get_master_clock(is);
+
+ /* skip or repeat frame. We take into account the
+ delay to compute the threshold. I still don't know
+ if it is the best guess */
+ sync_threshold = FFMAX(AV_SYNC_THRESHOLD, delay);
+ if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
+ if (diff <= -sync_threshold)
+ delay = 0;
+ else if (diff >= sync_threshold)
+ delay = 2 * delay;
+ }
+ }
+ is->frame_timer += delay;
+
+ av_log(NULL, AV_LOG_TRACE, "video: delay=%0.3f pts=%0.3f A-V=%f\n",
+ delay, frame_current_pts, -diff);
+
+ return is->frame_timer;
+}
+
+/* called to display each frame */
+static void video_refresh_timer(void *opaque)
+{
+ PlayerState *is = opaque;
+ VideoPicture *vp;
+
+ SubPicture *sp, *sp2;
+
+ if (is->video_st) {
+retry:
+ if (is->pictq_size == 0) {
+ // nothing to do, no picture to display in the que
+ } else {
+ double time = av_gettime_relative() / 1000000.0;
+ double next_target;
+ /* dequeue the picture */
+ vp = &is->pictq[is->pictq_rindex];
+
+ if (time < vp->target_clock)
+ return;
+ /* update current video pts */
+ is->video_current_pts = vp->pts;
+ is->video_current_pts_drift = is->video_current_pts - time;
+ is->video_current_pos = vp->pos;
+ if (is->pictq_size > 1) {
+ VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
+ assert(nextvp->target_clock >= vp->target_clock);
+ next_target= nextvp->target_clock;
+ } else {
+ next_target = vp->target_clock + is->video_clock - vp->pts; // FIXME pass durations cleanly
+ }
+ if (framedrop && time > next_target) {
+ is->skip_frames *= 1.0 + FRAME_SKIP_FACTOR;
+ if (is->pictq_size > 1 || time > next_target + 0.5) {
+ /* update queue size and signal for next picture */
+ if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
+ is->pictq_rindex = 0;
+
+ SDL_LockMutex(is->pictq_mutex);
+ is->pictq_size--;
+ SDL_CondSignal(is->pictq_cond);
+ SDL_UnlockMutex(is->pictq_mutex);
+ goto retry;
+ }
+ }
+
+ if (is->subtitle_st) {
+ if (is->subtitle_stream_changed) {
+ SDL_LockMutex(is->subpq_mutex);
+
+ while (is->subpq_size) {
+ free_subpicture(&is->subpq[is->subpq_rindex]);
+
+ /* update queue size and signal for next picture */
+ if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
+ is->subpq_rindex = 0;
+
+ is->subpq_size--;
+ }
+ is->subtitle_stream_changed = 0;
+
+ SDL_CondSignal(is->subpq_cond);
+ SDL_UnlockMutex(is->subpq_mutex);
+ } else {
+ if (is->subpq_size > 0) {
+ sp = &is->subpq[is->subpq_rindex];
+
+ if (is->subpq_size > 1)
+ sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
+ else
+ sp2 = NULL;
+
+ if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
+ || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
+ {
+ free_subpicture(sp);
+
+ /* update queue size and signal for next picture */
+ if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
+ is->subpq_rindex = 0;
+
+ SDL_LockMutex(is->subpq_mutex);
+ is->subpq_size--;
+ SDL_CondSignal(is->subpq_cond);
+ SDL_UnlockMutex(is->subpq_mutex);
+ }
+ }
+ }
+ }
+
+ /* display picture */
+ if (!display_disable)
+ video_display(is);
+
+ /* update queue size and signal for next picture */
+ if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
+ is->pictq_rindex = 0;
+
+ SDL_LockMutex(is->pictq_mutex);
+ is->pictq_size--;
+ SDL_CondSignal(is->pictq_cond);
+ SDL_UnlockMutex(is->pictq_mutex);
+ }
+ } else if (is->audio_st) {
+ /* draw the next audio frame */
+
+ /* if only audio stream, then display the audio bars (better
+ than nothing, just to test the implementation */
+
+ /* display picture */
+ if (!display_disable)
+ video_display(is);
+ }
+ if (show_status) {
+ static int64_t last_time;
+ int64_t cur_time;
+ int aqsize, vqsize, sqsize;
+ double av_diff;
+
+ cur_time = av_gettime_relative();
+ if (!last_time || (cur_time - last_time) >= 30000) {
+ aqsize = 0;
+ vqsize = 0;
+ sqsize = 0;
+ if (is->audio_st)
+ aqsize = is->audioq.size;
+ if (is->video_st)
+ vqsize = is->videoq.size;
+ if (is->subtitle_st)
+ sqsize = is->subtitleq.size;
+ av_diff = 0;
+ if (is->audio_st && is->video_st)
+ av_diff = get_audio_clock(is) - get_video_clock(is);
+ printf("%7.2f A-V:%7.3f s:%3.1f aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
+ get_master_clock(is), av_diff, FFMAX(is->skip_frames - 1, 0), aqsize / 1024,
+ vqsize / 1024, sqsize, is->pts_ctx.num_faulty_dts, is->pts_ctx.num_faulty_pts);
+ fflush(stdout);
+ last_time = cur_time;
+ }
+ }
+}
+
+static void player_close(PlayerState *is)
+{
+ VideoPicture *vp;
+ int i;
+ /* XXX: use a special url_shutdown call to abort parse cleanly */
+ is->abort_request = 1;
+ SDL_WaitThread(is->parse_tid, NULL);
+ SDL_WaitThread(is->refresh_tid, NULL);
+
+ /* free all pictures */
+ for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
+ vp = &is->pictq[i];
+ if (vp->bmp) {
+ SDL_FreeYUVOverlay(vp->bmp);
+ vp->bmp = NULL;
+ }
+ }
+ SDL_DestroyMutex(is->pictq_mutex);
+ SDL_DestroyCond(is->pictq_cond);
+ SDL_DestroyMutex(is->subpq_mutex);
+ SDL_DestroyCond(is->subpq_cond);
+}
+
+static void do_exit(void)
+{
+ if (player) {
+ player_close(player);
+ player = NULL;
+ }
+ uninit_opts();
+ avformat_network_deinit();
+ if (show_status)
+ printf("\n");
+ SDL_Quit();
+ av_log(NULL, AV_LOG_QUIET, "");
+ exit(0);
+}
+
+/* allocate a picture (needs to do that in main thread to avoid
+ potential locking problems */
+static void alloc_picture(void *opaque)
+{
+ PlayerState *is = opaque;
+ VideoPicture *vp;
+
+ vp = &is->pictq[is->pictq_windex];
+
+ if (vp->bmp)
+ SDL_FreeYUVOverlay(vp->bmp);
+
+ vp->width = is->out_video_filter->inputs[0]->w;
+ vp->height = is->out_video_filter->inputs[0]->h;
+ vp->pix_fmt = is->out_video_filter->inputs[0]->format;
+
+ vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
+ SDL_YV12_OVERLAY,
+ screen);
+ if (!vp->bmp || vp->bmp->pitches[0] < vp->width) {
+ /* SDL allocates a buffer smaller than requested if the video
+ * overlay hardware is unable to support the requested size. */
+ fprintf(stderr, "Error: the video system does not support an image\n"
+ "size of %dx%d pixels. Try using -vf \"scale=w:h\"\n"
+ "to reduce the image size.\n", vp->width, vp->height );
+ do_exit();
+ }
+
+ SDL_LockMutex(is->pictq_mutex);
+ vp->allocated = 1;
+ SDL_CondSignal(is->pictq_cond);
+ SDL_UnlockMutex(is->pictq_mutex);
+}
+
+/* The 'pts' parameter is the dts of the packet / pts of the frame and
+ * guessed if not known. */
+static int queue_picture(PlayerState *is, AVFrame *src_frame, double pts, int64_t pos)
+{
+ VideoPicture *vp;
+
+ /* wait until we have space to put a new picture */
+ SDL_LockMutex(is->pictq_mutex);
+
+ if (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE && !is->refresh)
+ is->skip_frames = FFMAX(1.0 - FRAME_SKIP_FACTOR, is->skip_frames * (1.0 - FRAME_SKIP_FACTOR));
+
+ while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
+ !is->videoq.abort_request) {
+ SDL_CondWait(is->pictq_cond, is->pictq_mutex);
+ }
+ SDL_UnlockMutex(is->pictq_mutex);
+
+ if (is->videoq.abort_request)
+ return -1;
+
+ vp = &is->pictq[is->pictq_windex];
+
+ vp->sar = src_frame->sample_aspect_ratio;
+
+ /* alloc or resize hardware picture buffer */
+ if (!vp->bmp || vp->reallocate ||
+ vp->width != is->out_video_filter->inputs[0]->w ||
+ vp->height != is->out_video_filter->inputs[0]->h) {
+ SDL_Event event;
+
+ vp->allocated = 0;
+ vp->reallocate = 0;
+
+ /* the allocation must be done in the main thread to avoid
+ locking problems */
+ event.type = FF_ALLOC_EVENT;
+ event.user.data1 = is;
+ SDL_PushEvent(&event);
+
+ /* wait until the picture is allocated */
+ SDL_LockMutex(is->pictq_mutex);
+ while (!vp->allocated && !is->videoq.abort_request) {
+ SDL_CondWait(is->pictq_cond, is->pictq_mutex);
+ }
+ SDL_UnlockMutex(is->pictq_mutex);
+
+ if (is->videoq.abort_request)
+ return -1;
+ }
+
+ /* if the frame is not skipped, then display it */
+ if (vp->bmp) {
+ uint8_t *data[4];
+ int linesize[4];
+
+ /* get a pointer on the bitmap */
+ SDL_LockYUVOverlay (vp->bmp);
+
+ data[0] = vp->bmp->pixels[0];
+ data[1] = vp->bmp->pixels[2];
+ data[2] = vp->bmp->pixels[1];
+
+ linesize[0] = vp->bmp->pitches[0];
+ linesize[1] = vp->bmp->pitches[2];
+ linesize[2] = vp->bmp->pitches[1];
+
+ // FIXME use direct rendering
+ av_image_copy(data, linesize, src_frame->data, src_frame->linesize,
+ vp->pix_fmt, vp->width, vp->height);
+
+ /* update the bitmap content */
+ SDL_UnlockYUVOverlay(vp->bmp);
+
+ vp->pts = pts;
+ vp->pos = pos;
+
+ /* now we can update the picture count */
+ if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
+ is->pictq_windex = 0;
+ SDL_LockMutex(is->pictq_mutex);
+ vp->target_clock = compute_target_time(vp->pts, is);
+
+ is->pictq_size++;
+ SDL_UnlockMutex(is->pictq_mutex);
+ }
+ return 0;
+}
+
+/* Compute the exact PTS for the picture if it is omitted in the stream.
+ * The 'pts1' parameter is the dts of the packet / pts of the frame. */
+static int output_picture2(PlayerState *is, AVFrame *src_frame, double pts1, int64_t pos)
+{
+ double frame_delay, pts;
+ int ret;
+
+ pts = pts1;
+
+ if (pts != 0) {
+ /* update video clock with pts, if present */
+ is->video_clock = pts;
+ } else {
+ pts = is->video_clock;
+ }
+ /* update video clock for next frame */
+ frame_delay = av_q2d(is->video_dec->time_base);
+ /* For MPEG-2, the frame can be repeated, so we update the
+ clock accordingly */
+ frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
+ is->video_clock += frame_delay;
+
+ ret = queue_picture(is, src_frame, pts, pos);
+ av_frame_unref(src_frame);
+ return ret;
+}
+
+static int get_video_frame(PlayerState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt)
+{
+ int got_picture, i;
+
+ if (packet_queue_get(&is->videoq, pkt, 1) < 0)
+ return -1;
+
+ if (pkt->data == flush_pkt.data) {
+ avcodec_flush_buffers(is->video_dec);
+
+ SDL_LockMutex(is->pictq_mutex);
+ // Make sure there are no long delay timers (ideally we should just flush the que but thats harder)
+ for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
+ is->pictq[i].target_clock= 0;
+ }
+ while (is->pictq_size && !is->videoq.abort_request) {
+ SDL_CondWait(is->pictq_cond, is->pictq_mutex);
+ }
+ is->video_current_pos = -1;
+ SDL_UnlockMutex(is->pictq_mutex);
+
+ init_pts_correction(&is->pts_ctx);
+ is->frame_last_pts = AV_NOPTS_VALUE;
+ is->frame_last_delay = 0;
+ is->frame_timer = (double)av_gettime_relative() / 1000000.0;
+ is->skip_frames = 1;
+ is->skip_frames_index = 0;
+ return 0;
+ }
+
+ avcodec_decode_video2(is->video_dec, frame, &got_picture, pkt);
+
+ if (got_picture) {
+ if (decoder_reorder_pts == -1) {
+ *pts = guess_correct_pts(&is->pts_ctx, frame->pts, frame->pkt_dts);
+ } else if (decoder_reorder_pts) {
+ *pts = frame->pts;
+ } else {
+ *pts = frame->pkt_dts;
+ }
+
+ if (*pts == AV_NOPTS_VALUE) {
+ *pts = 0;
+ }
+ if (is->video_st->sample_aspect_ratio.num) {
+ frame->sample_aspect_ratio = is->video_st->sample_aspect_ratio;
+ }
+
+ is->skip_frames_index += 1;
+ if (is->skip_frames_index >= is->skip_frames) {
+ is->skip_frames_index -= FFMAX(is->skip_frames, 1.0);
+ return 1;
+ }
+ av_frame_unref(frame);
+ }
+ return 0;
+}
+
+static int configure_video_filters(AVFilterGraph *graph, PlayerState *is, const char *vfilters)
+{
+ char sws_flags_str[128];
+ char buffersrc_args[256];
+ int ret;
+ AVFilterContext *filt_src = NULL, *filt_out = NULL, *last_filter;
+ AVCodecContext *codec = is->video_dec;
+
+ snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
+ graph->scale_sws_opts = av_strdup(sws_flags_str);
+
+ snprintf(buffersrc_args, sizeof(buffersrc_args), "%d:%d:%d:%d:%d:%d:%d",
+ codec->width, codec->height, codec->pix_fmt,
+ is->video_st->time_base.num, is->video_st->time_base.den,
+ codec->sample_aspect_ratio.num, codec->sample_aspect_ratio.den);
+
+
+ if ((ret = avfilter_graph_create_filter(&filt_src,
+ avfilter_get_by_name("buffer"),
+ "src", buffersrc_args, NULL,
+ graph)) < 0)
+ return ret;
+ if ((ret = avfilter_graph_create_filter(&filt_out,
+ avfilter_get_by_name("buffersink"),
+ "out", NULL, NULL, graph)) < 0)
+ return ret;
+
+ last_filter = filt_out;
+
+/* Note: this macro adds a filter before the lastly added filter, so the
+ * processing order of the filters is in reverse */
+#define INSERT_FILT(name, arg) do { \
+ AVFilterContext *filt_ctx; \
+ \
+ ret = avfilter_graph_create_filter(&filt_ctx, \
+ avfilter_get_by_name(name), \
+ "avplay_" name, arg, NULL, graph); \
+ if (ret < 0) \
+ return ret; \
+ \
+ ret = avfilter_link(filt_ctx, 0, last_filter, 0); \
+ if (ret < 0) \
+ return ret; \
+ \
+ last_filter = filt_ctx; \
+} while (0)
+
+ INSERT_FILT("format", "yuv420p");
+
+ if (autorotate) {
+ uint8_t* displaymatrix = av_stream_get_side_data(is->video_st,
+ AV_PKT_DATA_DISPLAYMATRIX, NULL);
+ if (displaymatrix) {
+ double rot = av_display_rotation_get((int32_t*) displaymatrix);
+ if (rot < -135 || rot > 135) {
+ INSERT_FILT("vflip", NULL);
+ INSERT_FILT("hflip", NULL);
+ } else if (rot < -45) {
+ INSERT_FILT("transpose", "dir=clock");
+ } else if (rot > 45) {
+ INSERT_FILT("transpose", "dir=cclock");
+ }
+ }
+ }
+
+ if (vfilters) {
+ AVFilterInOut *outputs = avfilter_inout_alloc();
+ AVFilterInOut *inputs = avfilter_inout_alloc();
+
+ outputs->name = av_strdup("in");
+ outputs->filter_ctx = filt_src;
+ outputs->pad_idx = 0;
+ outputs->next = NULL;
+
+ inputs->name = av_strdup("out");
+ inputs->filter_ctx = last_filter;
+ inputs->pad_idx = 0;
+ inputs->next = NULL;
+
+ if ((ret = avfilter_graph_parse(graph, vfilters, inputs, outputs, NULL)) < 0)
+ return ret;
+ } else {
+ if ((ret = avfilter_link(filt_src, 0, last_filter, 0)) < 0)
+ return ret;
+ }
+
+ if ((ret = avfilter_graph_config(graph, NULL)) < 0)
+ return ret;
+
+ is->in_video_filter = filt_src;
+ is->out_video_filter = filt_out;
+
+ return ret;
+}
+
+static int video_thread(void *arg)
+{
+ AVPacket pkt = { 0 };
+ PlayerState *is = arg;
+ AVFrame *frame = av_frame_alloc();
+ int64_t pts_int;
+ double pts;
+ int ret;
+
+ AVFilterGraph *graph = avfilter_graph_alloc();
+ AVFilterContext *filt_out = NULL, *filt_in = NULL;
+ int last_w = is->video_dec->width;
+ int last_h = is->video_dec->height;
+ if (!graph) {
+ av_frame_free(&frame);
+ return AVERROR(ENOMEM);
+ }
+
+ if ((ret = configure_video_filters(graph, is, vfilters)) < 0)
+ goto the_end;
+ filt_in = is->in_video_filter;
+ filt_out = is->out_video_filter;
+
+ if (!frame) {
+ avfilter_graph_free(&graph);
+ return AVERROR(ENOMEM);
+ }
+
+ for (;;) {
+ AVRational tb;
+ while (is->paused && !is->videoq.abort_request)
+ SDL_Delay(10);
+
+ av_packet_unref(&pkt);
+
+ ret = get_video_frame(is, frame, &pts_int, &pkt);
+ if (ret < 0)
+ goto the_end;
+
+ if (!ret)
+ continue;
+
+ if ( last_w != is->video_dec->width
+ || last_h != is->video_dec->height) {
+ av_log(NULL, AV_LOG_TRACE, "Changing size %dx%d -> %dx%d\n", last_w, last_h,
+ is->video_dec->width, is->video_dec->height);
+ avfilter_graph_free(&graph);
+ graph = avfilter_graph_alloc();
+ if ((ret = configure_video_filters(graph, is, vfilters)) < 0)
+ goto the_end;
+ filt_in = is->in_video_filter;
+ filt_out = is->out_video_filter;
+ last_w = is->video_dec->width;
+ last_h = is->video_dec->height;
+ }
+
+ frame->pts = pts_int;
+ ret = av_buffersrc_add_frame(filt_in, frame);
+ if (ret < 0)
+ goto the_end;
+
+ while (ret >= 0) {
+ ret = av_buffersink_get_frame(filt_out, frame);
+ if (ret < 0) {
+ ret = 0;
+ break;
+ }
+
+ pts_int = frame->pts;
+ tb = filt_out->inputs[0]->time_base;
+ if (av_cmp_q(tb, is->video_st->time_base)) {
+ av_unused int64_t pts1 = pts_int;
+ pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
+ av_log(NULL, AV_LOG_TRACE, "video_thread(): "
+ "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
+ tb.num, tb.den, pts1,
+ is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
+ }
+ pts = pts_int * av_q2d(is->video_st->time_base);
+ ret = output_picture2(is, frame, pts, 0);
+ }
+
+ if (ret < 0)
+ goto the_end;
+
+
+ if (step)
+ if (player)
+ stream_pause(player);
+ }
+ the_end:
+ av_freep(&vfilters);
+ avfilter_graph_free(&graph);
+ av_packet_unref(&pkt);
+ av_frame_free(&frame);
+ return 0;
+}
+
+static int subtitle_thread(void *arg)
+{
+ PlayerState *is = arg;
+ SubPicture *sp;
+ AVPacket pkt1, *pkt = &pkt1;
+ int got_subtitle;
+ double pts;
+ int i, j;
+ int r, g, b, y, u, v, a;
+
+ for (;;) {
+ while (is->paused && !is->subtitleq.abort_request) {
+ SDL_Delay(10);
+ }
+ if (packet_queue_get(&is->subtitleq, pkt, 1) < 0)
+ break;
+
+ if (pkt->data == flush_pkt.data) {
+ avcodec_flush_buffers(is->subtitle_dec);
+ continue;
+ }
+ SDL_LockMutex(is->subpq_mutex);
+ while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
+ !is->subtitleq.abort_request) {
+ SDL_CondWait(is->subpq_cond, is->subpq_mutex);
+ }
+ SDL_UnlockMutex(is->subpq_mutex);
+
+ if (is->subtitleq.abort_request)
+ return 0;
+
+ sp = &is->subpq[is->subpq_windex];
+
+ /* NOTE: ipts is the PTS of the _first_ picture beginning in
+ this packet, if any */
+ pts = 0;
+ if (pkt->pts != AV_NOPTS_VALUE)
+ pts = av_q2d(is->subtitle_dec->time_base) * pkt->pts;
+
+ avcodec_decode_subtitle2(is->subtitle_dec, &sp->sub,
+ &got_subtitle, pkt);
+
+ if (got_subtitle && sp->sub.format == 0) {
+ sp->pts = pts;
+
+ for (i = 0; i < sp->sub.num_rects; i++)
+ {
+ for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
+ {
+ RGBA_IN(r, g, b, a, (uint32_t *)sp->sub.rects[i]->data[1] + j);
+ y = RGB_TO_Y_CCIR(r, g, b);
+ u = RGB_TO_U_CCIR(r, g, b, 0);
+ v = RGB_TO_V_CCIR(r, g, b, 0);
+ YUVA_OUT((uint32_t *)sp->sub.rects[i]->data[1] + j, y, u, v, a);
+ }
+ }
+
+ /* now we can update the picture count */
+ if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
+ is->subpq_windex = 0;
+ SDL_LockMutex(is->subpq_mutex);
+ is->subpq_size++;
+ SDL_UnlockMutex(is->subpq_mutex);
+ }
+ av_packet_unref(pkt);
+ }
+ return 0;
+}
+
+/* copy samples for viewing in editor window */
+static void update_sample_display(PlayerState *is, short *samples, int samples_size)
+{
+ int size, len;
+
+ size = samples_size / sizeof(short);
+ while (size > 0) {
+ len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
+ if (len > size)
+ len = size;
+ memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
+ samples += len;
+ is->sample_array_index += len;
+ if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
+ is->sample_array_index = 0;
+ size -= len;
+ }
+}
+
+/* return the new audio buffer size (samples can be added or deleted
+ to get better sync if video or external master clock) */
+static int synchronize_audio(PlayerState *is, short *samples,
+ int samples_size1, double pts)
+{
+ int n, samples_size;
+ double ref_clock;
+
+ n = is->sdl_channels * av_get_bytes_per_sample(is->sdl_sample_fmt);
+ samples_size = samples_size1;
+
+ /* if not master, then we try to remove or add samples to correct the clock */
+ if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||
+ is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
+ double diff, avg_diff;
+ int wanted_size, min_size, max_size, nb_samples;
+
+ ref_clock = get_master_clock(is);
+ diff = get_audio_clock(is) - ref_clock;
+
+ if (diff < AV_NOSYNC_THRESHOLD) {
+ is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
+ if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
+ /* not enough measures to have a correct estimate */
+ is->audio_diff_avg_count++;
+ } else {
+ /* estimate the A-V difference */
+ avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
+
+ if (fabs(avg_diff) >= is->audio_diff_threshold) {
+ wanted_size = samples_size + ((int)(diff * is->sdl_sample_rate) * n);
+ nb_samples = samples_size / n;
+
+ min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
+ max_size = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
+ if (wanted_size < min_size)
+ wanted_size = min_size;
+ else if (wanted_size > max_size)
+ wanted_size = max_size;
+
+ /* add or remove samples to correction the synchro */
+ if (wanted_size < samples_size) {
+ /* remove samples */
+ samples_size = wanted_size;
+ } else if (wanted_size > samples_size) {
+ uint8_t *samples_end, *q;
+ int nb;
+
+ /* add samples */
+ nb = (samples_size - wanted_size);
+ samples_end = (uint8_t *)samples + samples_size - n;
+ q = samples_end + n;
+ while (nb > 0) {
+ memcpy(q, samples_end, n);
+ q += n;
+ nb -= n;
+ }
+ samples_size = wanted_size;
+ }
+ }
+ av_log(NULL, AV_LOG_TRACE, "diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
+ diff, avg_diff, samples_size - samples_size1,
+ is->audio_clock, is->video_clock, is->audio_diff_threshold);
+ }
+ } else {
+ /* too big difference : may be initial PTS errors, so
+ reset A-V filter */
+ is->audio_diff_avg_count = 0;
+ is->audio_diff_cum = 0;
+ }
+ }
+
+ return samples_size;
+}
+
+/* decode one audio frame and returns its uncompressed size */
+static int audio_decode_frame(PlayerState *is, double *pts_ptr)
+{
+ AVPacket *pkt_temp = &is->audio_pkt_temp;
+ AVPacket *pkt = &is->audio_pkt;
+ AVCodecContext *dec = is->audio_dec;
+ int n, len1, data_size, got_frame;
+ double pts;
+ int new_packet = 0;
+ int flush_complete = 0;
+
+ for (;;) {
+ /* NOTE: the audio packet can contain several frames */
+ while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) {
+ int resample_changed, audio_resample;
+
+ if (!is->frame) {
+ if (!(is->frame = av_frame_alloc()))
+ return AVERROR(ENOMEM);
+ }
+
+ if (flush_complete)
+ break;
+ new_packet = 0;
+ len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
+ if (len1 < 0) {
+ /* if error, we skip the frame */
+ pkt_temp->size = 0;
+ break;
+ }
+
+ pkt_temp->data += len1;
+ pkt_temp->size -= len1;
+
+ if (!got_frame) {
+ /* stop sending empty packets if the decoder is finished */
+ if (!pkt_temp->data && (dec->codec->capabilities & AV_CODEC_CAP_DELAY))
+ flush_complete = 1;
+ continue;
+ }
+ data_size = av_samples_get_buffer_size(NULL, dec->channels,
+ is->frame->nb_samples,
+ is->frame->format, 1);
+
+ audio_resample = is->frame->format != is->sdl_sample_fmt ||
+ is->frame->channel_layout != is->sdl_channel_layout ||
+ is->frame->sample_rate != is->sdl_sample_rate;
+
+ resample_changed = is->frame->format != is->resample_sample_fmt ||
+ is->frame->channel_layout != is->resample_channel_layout ||
+ is->frame->sample_rate != is->resample_sample_rate;
+
+ if ((!is->avr && audio_resample) || resample_changed) {
+ int ret;
+ if (is->avr)
+ avresample_close(is->avr);
+ else if (audio_resample) {
+ is->avr = avresample_alloc_context();
+ if (!is->avr) {
+ fprintf(stderr, "error allocating AVAudioResampleContext\n");
+ break;
+ }
+ }
+ if (audio_resample) {
+ av_opt_set_int(is->avr, "in_channel_layout", is->frame->channel_layout, 0);
+ av_opt_set_int(is->avr, "in_sample_fmt", is->frame->format, 0);
+ av_opt_set_int(is->avr, "in_sample_rate", is->frame->sample_rate, 0);
+ av_opt_set_int(is->avr, "out_channel_layout", is->sdl_channel_layout, 0);
+ av_opt_set_int(is->avr, "out_sample_fmt", is->sdl_sample_fmt, 0);
+ av_opt_set_int(is->avr, "out_sample_rate", is->sdl_sample_rate, 0);
+
+ if ((ret = avresample_open(is->avr)) < 0) {
+ fprintf(stderr, "error initializing libavresample\n");
+ break;
+ }
+ }
+ is->resample_sample_fmt = is->frame->format;
+ is->resample_channel_layout = is->frame->channel_layout;
+ is->resample_sample_rate = is->frame->sample_rate;
+ }
+
+ if (audio_resample) {
+ void *tmp_out;
+ int out_samples, out_size, out_linesize;
+ int osize = av_get_bytes_per_sample(is->sdl_sample_fmt);
+ int nb_samples = is->frame->nb_samples;
+
+ out_size = av_samples_get_buffer_size(&out_linesize,
+ is->sdl_channels,
+ nb_samples,
+ is->sdl_sample_fmt, 0);
+ tmp_out = av_realloc(is->audio_buf1, out_size);
+ if (!tmp_out)
+ return AVERROR(ENOMEM);
+ is->audio_buf1 = tmp_out;
+
+ out_samples = avresample_convert(is->avr,
+ &is->audio_buf1,
+ out_linesize, nb_samples,
+ is->frame->data,
+ is->frame->linesize[0],
+ is->frame->nb_samples);
+ if (out_samples < 0) {
+ fprintf(stderr, "avresample_convert() failed\n");
+ break;
+ }
+ is->audio_buf = is->audio_buf1;
+ data_size = out_samples * osize * is->sdl_channels;
+ } else {
+ is->audio_buf = is->frame->data[0];
+ }
+
+ /* if no pts, then compute it */
+ pts = is->audio_clock;
+ *pts_ptr = pts;
+ n = is->sdl_channels * av_get_bytes_per_sample(is->sdl_sample_fmt);
+ is->audio_clock += (double)data_size /
+ (double)(n * is->sdl_sample_rate);
+#ifdef DEBUG
+ {
+ static double last_clock;
+ printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
+ is->audio_clock - last_clock,
+ is->audio_clock, pts);
+ last_clock = is->audio_clock;
+ }
+#endif
+ return data_size;
+ }
+
+ /* free the current packet */
+ if (pkt->data)
+ av_packet_unref(pkt);
+ memset(pkt_temp, 0, sizeof(*pkt_temp));
+
+ if (is->paused || is->audioq.abort_request) {
+ return -1;
+ }
+
+ /* read next packet */
+ if ((new_packet = packet_queue_get(&is->audioq, pkt, 1)) < 0)
+ return -1;
+
+ if (pkt->data == flush_pkt.data) {
+ avcodec_flush_buffers(dec);
+ flush_complete = 0;
+ }
+
+ *pkt_temp = *pkt;
+
+ /* if update the audio clock with the pts */
+ if (pkt->pts != AV_NOPTS_VALUE) {
+ is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
+ }
+ }
+}
+
+/* prepare a new audio buffer */
+static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
+{
+ PlayerState *is = opaque;
+ int audio_size, len1;
+ double pts;
+
+ audio_callback_time = av_gettime_relative();
+
+ while (len > 0) {
+ if (is->audio_buf_index >= is->audio_buf_size) {
+ audio_size = audio_decode_frame(is, &pts);
+ if (audio_size < 0) {
+ /* if error, just output silence */
+ is->audio_buf = is->silence_buf;
+ is->audio_buf_size = sizeof(is->silence_buf);
+ } else {
+ if (is->show_audio)
+ update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
+ audio_size = synchronize_audio(is, (int16_t *)is->audio_buf, audio_size,
+ pts);
+ is->audio_buf_size = audio_size;
+ }
+ is->audio_buf_index = 0;
+ }
+ len1 = is->audio_buf_size - is->audio_buf_index;
+ if (len1 > len)
+ len1 = len;
+ memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
+ len -= len1;
+ stream += len1;
+ is->audio_buf_index += len1;
+ }
+}
+
+static AVCodec *find_codec_or_die(const char *name, enum AVMediaType type)
+{
+ const AVCodecDescriptor *desc;
+ AVCodec *codec = avcodec_find_decoder_by_name(name);
+
+ if (!codec && (desc = avcodec_descriptor_get_by_name(name))) {
+ codec = avcodec_find_decoder(desc->id);
+ if (codec)
+ av_log(NULL, AV_LOG_VERBOSE, "Matched decoder '%s' for codec '%s'.\n",
+ codec->name, desc->name);
+ }
+
+ if (!codec) {
+ av_log(NULL, AV_LOG_FATAL, "Unknown decoder '%s'\n", name);
+ exit_program(1);
+ }
+
+ if (codec->type != type) {
+ av_log(NULL, AV_LOG_FATAL, "Invalid decoder type '%s'\n", name);
+ exit_program(1);
+ }
+
+ return codec;
+}
+
+static AVCodec *choose_decoder(PlayerState *is, AVFormatContext *ic, AVStream *st)
+{
+ char *codec_name = NULL;
+ int i, ret;
+
+ for (i = 0; i < is->nb_codec_names; i++) {
+ char *spec = is->codec_names[i].specifier;
+ if ((ret = check_stream_specifier(ic, st, spec)) > 0)
+ codec_name = is->codec_names[i].u.str;
+ else if (ret < 0)
+ exit_program(1);
+ }
+
+ if (codec_name) {
+ AVCodec *codec = find_codec_or_die(codec_name, st->codecpar->codec_type);
+ st->codecpar->codec_id = codec->id;
+ return codec;
+ } else
+ return avcodec_find_decoder(st->codecpar->codec_id);
+}
+
+/* open a given stream. Return 0 if OK */
+static int stream_component_open(PlayerState *is, int stream_index)
+{
+ AVFormatContext *ic = is->ic;
+ AVCodecContext *avctx;
+ AVCodec *codec;
+ SDL_AudioSpec wanted_spec, spec;
+ AVDictionary *opts;
+ AVDictionaryEntry *t = NULL;
+ int ret = 0;
+
+ if (stream_index < 0 || stream_index >= ic->nb_streams)
+ return -1;
+
+ avctx = avcodec_alloc_context3(NULL);
+ if (!avctx)
+ return AVERROR(ENOMEM);
+
+ ret = avcodec_parameters_to_context(avctx, ic->streams[stream_index]->codecpar);
+ if (ret < 0) {
+ avcodec_free_context(&avctx);
+ return ret;
+ }
+
+ opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], NULL);
+
+ codec = choose_decoder(is, ic, ic->streams[stream_index]);
+ avctx->workaround_bugs = workaround_bugs;
+ avctx->idct_algo = idct;
+ avctx->skip_frame = skip_frame;
+ avctx->skip_idct = skip_idct;
+ avctx->skip_loop_filter = skip_loop_filter;
+ avctx->error_concealment = error_concealment;
+
+ if (fast)
+ avctx->flags2 |= AV_CODEC_FLAG2_FAST;
+
+ if (!av_dict_get(opts, "threads", NULL, 0))
+ av_dict_set(&opts, "threads", "auto", 0);
+ if (avctx->codec_type == AVMEDIA_TYPE_VIDEO)
+ av_dict_set(&opts, "refcounted_frames", "1", 0);
+ if (!codec ||
+ (ret = avcodec_open2(avctx, codec, &opts)) < 0) {
+ goto fail;
+ }
+ if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
+ av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
+ ret = AVERROR_OPTION_NOT_FOUND;
+ goto fail;
+ }
+
+ /* prepare audio output */
+ if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
+ is->sdl_sample_rate = avctx->sample_rate;
+
+ if (!avctx->channel_layout)
+ avctx->channel_layout = av_get_default_channel_layout(avctx->channels);
+ if (!avctx->channel_layout) {
+ fprintf(stderr, "unable to guess channel layout\n");
+ ret = AVERROR_INVALIDDATA;
+ goto fail;
+ }
+ if (avctx->channels == 1)
+ is->sdl_channel_layout = AV_CH_LAYOUT_MONO;
+ else
+ is->sdl_channel_layout = AV_CH_LAYOUT_STEREO;
+ is->sdl_channels = av_get_channel_layout_nb_channels(is->sdl_channel_layout);
+
+ wanted_spec.format = AUDIO_S16SYS;
+ wanted_spec.freq = is->sdl_sample_rate;
+ wanted_spec.channels = is->sdl_channels;
+ wanted_spec.silence = 0;
+ wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
+ wanted_spec.callback = sdl_audio_callback;
+ wanted_spec.userdata = is;
+ if (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
+ fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
+ ret = AVERROR_UNKNOWN;
+ goto fail;
+ }
+ is->audio_hw_buf_size = spec.size;
+ is->sdl_sample_fmt = AV_SAMPLE_FMT_S16;
+ is->resample_sample_fmt = is->sdl_sample_fmt;
+ is->resample_channel_layout = avctx->channel_layout;
+ is->resample_sample_rate = avctx->sample_rate;
+ }
+
+ ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
+ switch (avctx->codec_type) {
+ case AVMEDIA_TYPE_AUDIO:
+ is->audio_stream = stream_index;
+ is->audio_st = ic->streams[stream_index];
+ is->audio_dec = avctx;
+ is->audio_buf_size = 0;
+ is->audio_buf_index = 0;
+
+ /* init averaging filter */
+ is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
+ is->audio_diff_avg_count = 0;
+ /* since we do not have a precise enough audio FIFO fullness,
+ we correct audio sync only if larger than this threshold */
+ is->audio_diff_threshold = 2.0 * SDL_AUDIO_BUFFER_SIZE / avctx->sample_rate;
+
+ memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
+ packet_queue_init(&is->audioq);
+ SDL_PauseAudio(0);
+ break;
+ case AVMEDIA_TYPE_VIDEO:
+ is->video_stream = stream_index;
+ is->video_st = ic->streams[stream_index];
+ is->video_dec = avctx;
+
+ packet_queue_init(&is->videoq);
+ is->video_tid = SDL_CreateThread(video_thread, is);
+ break;
+ case AVMEDIA_TYPE_SUBTITLE:
+ is->subtitle_stream = stream_index;
+ is->subtitle_st = ic->streams[stream_index];
+ is->subtitle_dec = avctx;
+ packet_queue_init(&is->subtitleq);
+
+ is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
+ break;
+ default:
+ break;
+ }
+
+fail:
+ av_dict_free(&opts);
+
+ return ret;
+}
+
+static void stream_component_close(PlayerState *is, int stream_index)
+{
+ AVFormatContext *ic = is->ic;
+ AVCodecParameters *par;
+
+ if (stream_index < 0 || stream_index >= ic->nb_streams)
+ return;
+ par = ic->streams[stream_index]->codecpar;
+
+ switch (par->codec_type) {
+ case AVMEDIA_TYPE_AUDIO:
+ packet_queue_abort(&is->audioq);
+
+ SDL_CloseAudio();
+
+ packet_queue_end(&is->audioq);
+ av_packet_unref(&is->audio_pkt);
+ if (is->avr)
+ avresample_free(&is->avr);
+ av_freep(&is->audio_buf1);
+ is->audio_buf = NULL;
+ av_frame_free(&is->frame);
+
+ if (is->rdft) {
+ av_rdft_end(is->rdft);
+ av_freep(&is->rdft_data);
+ is->rdft = NULL;
+ is->rdft_bits = 0;
+ }
+ break;
+ case AVMEDIA_TYPE_VIDEO:
+ packet_queue_abort(&is->videoq);
+
+ /* note: we also signal this mutex to make sure we deblock the
+ video thread in all cases */
+ SDL_LockMutex(is->pictq_mutex);
+ SDL_CondSignal(is->pictq_cond);
+ SDL_UnlockMutex(is->pictq_mutex);
+
+ SDL_WaitThread(is->video_tid, NULL);
+
+ packet_queue_end(&is->videoq);
+ break;
+ case AVMEDIA_TYPE_SUBTITLE:
+ packet_queue_abort(&is->subtitleq);
+
+ /* note: we also signal this mutex to make sure we deblock the
+ video thread in all cases */
+ SDL_LockMutex(is->subpq_mutex);
+ is->subtitle_stream_changed = 1;
+
+ SDL_CondSignal(is->subpq_cond);
+ SDL_UnlockMutex(is->subpq_mutex);
+
+ SDL_WaitThread(is->subtitle_tid, NULL);
+
+ packet_queue_end(&is->subtitleq);
+ break;
+ default:
+ break;
+ }
+
+ ic->streams[stream_index]->discard = AVDISCARD_ALL;
+ switch (par->codec_type) {
+ case AVMEDIA_TYPE_AUDIO:
+ avcodec_free_context(&is->audio_dec);
+ is->audio_st = NULL;
+ is->audio_stream = -1;
+ break;
+ case AVMEDIA_TYPE_VIDEO:
+ avcodec_free_context(&is->video_dec);
+ is->video_st = NULL;
+ is->video_stream = -1;
+ break;
+ case AVMEDIA_TYPE_SUBTITLE:
+ avcodec_free_context(&is->subtitle_dec);
+ is->subtitle_st = NULL;
+ is->subtitle_stream = -1;
+ break;
+ default:
+ break;
+ }
+}
+
+/* since we have only one decoding thread, we can use a global
+ variable instead of a thread local variable */
+static PlayerState *global_video_state;
+
+static int decode_interrupt_cb(void *ctx)
+{
+ return global_video_state && global_video_state->abort_request;
+}
+
+static void stream_close(PlayerState *is)
+{
+ /* disable interrupting */
+ global_video_state = NULL;
+
+ /* close each stream */
+ if (is->audio_stream >= 0)
+ stream_component_close(is, is->audio_stream);
+ if (is->video_stream >= 0)
+ stream_component_close(is, is->video_stream);
+ if (is->subtitle_stream >= 0)
+ stream_component_close(is, is->subtitle_stream);
+ if (is->ic) {
+ avformat_close_input(&is->ic);
+ }
+}
+
+static int stream_setup(PlayerState *is)
+{
+ AVFormatContext *ic = NULL;
+ int err, i, ret;
+ int st_index[AVMEDIA_TYPE_NB];
+ AVDictionaryEntry *t;
+ AVDictionary **opts;
+ int orig_nb_streams;
+
+ memset(st_index, -1, sizeof(st_index));
+ is->video_stream = -1;
+ is->audio_stream = -1;
+ is->subtitle_stream = -1;
+
+ global_video_state = is;
+
+ ic = avformat_alloc_context();
+ if (!ic) {
+ av_log(NULL, AV_LOG_FATAL, "Could not allocate context.\n");
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+ ic->interrupt_callback.callback = decode_interrupt_cb;
+ err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
+ if (err < 0) {
+ print_error(is->filename, err);
+ ret = -1;
+ goto fail;
+ }
+
+ if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
+ av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
+ ret = AVERROR_OPTION_NOT_FOUND;
+ goto fail;
+ }
+ is->ic = ic;
+
+ if (genpts)
+ ic->flags |= AVFMT_FLAG_GENPTS;
+
+ opts = setup_find_stream_info_opts(ic, codec_opts);
+ orig_nb_streams = ic->nb_streams;
+
+ for (i = 0; i < ic->nb_streams; i++)
+ choose_decoder(is, ic, ic->streams[i]);
+
+ err = avformat_find_stream_info(ic, opts);
+
+ for (i = 0; i < orig_nb_streams; i++)
+ av_dict_free(&opts[i]);
+ av_freep(&opts);
+
+ if (err < 0) {
+ fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
+ ret = -1;
+ goto fail;
+ }
+
+ if (ic->pb)
+ ic->pb->eof_reached = 0; // FIXME hack, avplay maybe should not use url_feof() to test for the end
+
+ if (seek_by_bytes < 0)
+ seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT);
+
+ /* if seeking requested, we execute it */
+ if (start_time != AV_NOPTS_VALUE) {
+ int64_t timestamp;
+
+ timestamp = start_time;
+ /* add the stream start time */
+ if (ic->start_time != AV_NOPTS_VALUE)
+ timestamp += ic->start_time;
+ ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
+ if (ret < 0) {
+ fprintf(stderr, "%s: could not seek to position %0.3f\n",
+ is->filename, (double)timestamp / AV_TIME_BASE);
+ }
+ }
+
+ for (i = 0; i < ic->nb_streams; i++)
+ ic->streams[i]->discard = AVDISCARD_ALL;
+ if (!video_disable)
+ st_index[AVMEDIA_TYPE_VIDEO] =
+ av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
+ wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
+ if (!audio_disable)
+ st_index[AVMEDIA_TYPE_AUDIO] =
+ av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
+ wanted_stream[AVMEDIA_TYPE_AUDIO],
+ st_index[AVMEDIA_TYPE_VIDEO],
+ NULL, 0);
+ if (!video_disable)
+ st_index[AVMEDIA_TYPE_SUBTITLE] =
+ av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
+ wanted_stream[AVMEDIA_TYPE_SUBTITLE],
+ (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
+ st_index[AVMEDIA_TYPE_AUDIO] :
+ st_index[AVMEDIA_TYPE_VIDEO]),
+ NULL, 0);
+ if (show_status) {
+ av_dump_format(ic, 0, is->filename, 0);
+ }
+
+ /* open the streams */
+ if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
+ stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
+ }
+
+ ret = -1;
+ if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
+ ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
+ }
+ if (ret < 0) {
+ if (!display_disable)
+ is->show_audio = 2;
+ }
+
+ if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
+ stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
+ }
+
+ if (is->video_stream < 0 && is->audio_stream < 0) {
+ fprintf(stderr, "%s: could not open codecs\n", is->filename);
+ ret = -1;
+ goto fail;
+ }
+
+ return 0;
+
+fail:
+ return ret;
+}
+
+/* this thread gets the stream from the disk or the network */
+static int decode_thread(void *arg)
+{
+ PlayerState *is = arg;
+ AVPacket pkt1, *pkt = &pkt1;
+ AVFormatContext *ic = is->ic;
+ int pkt_in_play_range = 0;
+ int ret, eof = 0;
+
+ for (;;) {
+ if (is->abort_request)
+ break;
+ if (is->paused != is->last_paused) {
+ is->last_paused = is->paused;
+ if (is->paused)
+ is->read_pause_return = av_read_pause(ic);
+ else
+ av_read_play(ic);
+ }
+#if CONFIG_RTSP_DEMUXER
+ if (is->paused && !strcmp(ic->iformat->name, "rtsp")) {
+ /* wait 10 ms to avoid trying to get another packet */
+ /* XXX: horrible */
+ SDL_Delay(10);
+ continue;
+ }
+#endif
+ if (is->seek_req) {
+ int64_t seek_target = is->seek_pos;
+ int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
+ int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
+// FIXME the +-2 is due to rounding being not done in the correct direction in generation
+// of the seek_pos/seek_rel variables
+
+ ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
+ if (ret < 0) {
+ fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
+ } else {
+ if (is->audio_stream >= 0) {
+ packet_queue_flush(&is->audioq);
+ packet_queue_put(&is->audioq, &flush_pkt);
+ }
+ if (is->subtitle_stream >= 0) {
+ packet_queue_flush(&is->subtitleq);
+ packet_queue_put(&is->subtitleq, &flush_pkt);
+ }
+ if (is->video_stream >= 0) {
+ packet_queue_flush(&is->videoq);
+ packet_queue_put(&is->videoq, &flush_pkt);
+ }
+ }
+ is->seek_req = 0;
+ eof = 0;
+ }
+
+ /* if the queue are full, no need to read more */
+ if (!infinite_buffer &&
+ (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
+ || ( (is->audioq .size > MIN_AUDIOQ_SIZE || is->audio_stream < 0)
+ && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0)
+ && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0)))) {
+ /* wait 10 ms */
+ SDL_Delay(10);
+ continue;
+ }
+ if (eof) {
+ if (is->video_stream >= 0) {
+ av_init_packet(pkt);
+ pkt->data = NULL;
+ pkt->size = 0;
+ pkt->stream_index = is->video_stream;
+ packet_queue_put(&is->videoq, pkt);
+ }
+ if (is->audio_stream >= 0 &&
+ (is->audio_dec->codec->capabilities & AV_CODEC_CAP_DELAY)) {
+ av_init_packet(pkt);
+ pkt->data = NULL;
+ pkt->size = 0;
+ pkt->stream_index = is->audio_stream;
+ packet_queue_put(&is->audioq, pkt);
+ }
+ SDL_Delay(10);
+ if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
+ if (loop != 1 && (!loop || --loop)) {
+ stream_seek(player, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
+ } else if (!noautoexit) {
+ ret = AVERROR_EOF;
+ goto fail;
+ }
+ }
+ continue;
+ }
+ ret = av_read_frame(ic, pkt);
+ if (ret < 0) {
+ if (ret == AVERROR_EOF || (ic->pb && ic->pb->eof_reached))
+ eof = 1;
+ if (ic->pb && ic->pb->error)
+ break;
+ SDL_Delay(100); /* wait for user event */
+ continue;
+ }
+ /* check if packet is in play range specified by user, then queue, otherwise discard */
+ pkt_in_play_range = duration == AV_NOPTS_VALUE ||
+ (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
+ av_q2d(ic->streams[pkt->stream_index]->time_base) -
+ (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
+ <= ((double)duration / 1000000);
+ if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
+ packet_queue_put(&is->audioq, pkt);
+ } else if (pkt->stream_index == is->video_stream && pkt_in_play_range) {
+ packet_queue_put(&is->videoq, pkt);
+ } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
+ packet_queue_put(&is->subtitleq, pkt);
+ } else {
+ av_packet_unref(pkt);
+ }
+ }
+ /* wait until the end */
+ while (!is->abort_request) {
+ SDL_Delay(100);
+ }
+
+ ret = 0;
+
+fail:
+ stream_close(is);
+
+ if (ret != 0) {
+ SDL_Event event;
+
+ event.type = FF_QUIT_EVENT;
+ event.user.data1 = is;
+ SDL_PushEvent(&event);
+ }
+ return 0;
+}
+
+static int stream_open(PlayerState *is,
+ const char *filename, AVInputFormat *iformat)
+{
+ int ret;
+
+ av_strlcpy(is->filename, filename, sizeof(is->filename));
+ is->iformat = iformat;
+ is->ytop = 0;
+ is->xleft = 0;
+
+ if ((ret = stream_setup(is)) < 0) {
+ return ret;
+ }
+
+ /* start video display */
+ is->pictq_mutex = SDL_CreateMutex();
+ is->pictq_cond = SDL_CreateCond();
+
+ is->subpq_mutex = SDL_CreateMutex();
+ is->subpq_cond = SDL_CreateCond();
+
+ is->av_sync_type = av_sync_type;
+ is->refresh_tid = SDL_CreateThread(refresh_thread, is);
+ if (!is->refresh_tid)
+ return -1;
+ is->parse_tid = SDL_CreateThread(decode_thread, is);
+ if (!is->parse_tid)
+ return -1;
+ return 0;
+}
+
+static void stream_cycle_channel(PlayerState *is, int codec_type)
+{
+ AVFormatContext *ic = is->ic;
+ int start_index, stream_index;
+ AVStream *st;
+
+ if (codec_type == AVMEDIA_TYPE_VIDEO)
+ start_index = is->video_stream;
+ else if (codec_type == AVMEDIA_TYPE_AUDIO)
+ start_index = is->audio_stream;
+ else
+ start_index = is->subtitle_stream;
+ if (start_index < (codec_type == AVMEDIA_TYPE_SUBTITLE ? -1 : 0))
+ return;
+ stream_index = start_index;
+ for (;;) {
+ if (++stream_index >= is->ic->nb_streams)
+ {
+ if (codec_type == AVMEDIA_TYPE_SUBTITLE)
+ {
+ stream_index = -1;
+ goto the_end;
+ } else
+ stream_index = 0;
+ }
+ if (stream_index == start_index)
+ return;
+ st = ic->streams[stream_index];
+ if (st->codecpar->codec_type == codec_type) {
+ /* check that parameters are OK */
+ switch (codec_type) {
+ case AVMEDIA_TYPE_AUDIO:
+ if (st->codecpar->sample_rate != 0 &&
+ st->codecpar->channels != 0)
+ goto the_end;
+ break;
+ case AVMEDIA_TYPE_VIDEO:
+ case AVMEDIA_TYPE_SUBTITLE:
+ goto the_end;
+ default:
+ break;
+ }
+ }
+ }
+ the_end:
+ stream_component_close(is, start_index);
+ stream_component_open(is, stream_index);
+}
+
+
+static void toggle_full_screen(void)
+{
+#if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
+ /* OS X needs to empty the picture_queue */
+ int i;
+ for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
+ player->pictq[i].reallocate = 1;
+#endif
+ is_full_screen = !is_full_screen;
+ video_open(player);
+}
+
+static void toggle_pause(void)
+{
+ if (player)
+ stream_pause(player);
+ step = 0;
+}
+
+static void step_to_next_frame(void)
+{
+ if (player) {
+ /* if the stream is paused unpause it, then step */
+ if (player->paused)
+ stream_pause(player);
+ }
+ step = 1;
+}
+
+static void toggle_audio_display(void)
+{
+ if (player) {
+ int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
+ player->show_audio = (player->show_audio + 1) % 3;
+ fill_rectangle(screen,
+ player->xleft, player->ytop, player->width, player->height,
+ bgcolor);
+ SDL_UpdateRect(screen, player->xleft, player->ytop, player->width, player->height);
+ }
+}
+
+static void seek_chapter(PlayerState *is, int incr)
+{
+ int64_t pos = get_master_clock(is) * AV_TIME_BASE;
+ int i;
+
+ if (!is->ic->nb_chapters)
+ return;
+
+ /* find the current chapter */
+ for (i = 0; i < is->ic->nb_chapters; i++) {
+ AVChapter *ch = is->ic->chapters[i];
+ if (av_compare_ts(pos, AV_TIME_BASE_Q, ch->start, ch->time_base) < 0) {
+ i--;
+ break;
+ }
+ }
+
+ i += incr;
+ i = FFMAX(i, 0);
+ if (i >= is->ic->nb_chapters)
+ return;
+
+ av_log(NULL, AV_LOG_VERBOSE, "Seeking to chapter %d.\n", i);
+ stream_seek(is, av_rescale_q(is->ic->chapters[i]->start, is->ic->chapters[i]->time_base,
+ AV_TIME_BASE_Q), 0, 0);
+}
+
+/* handle an event sent by the GUI */
+static void event_loop(void)
+{
+ SDL_Event event;
+ double incr, pos, frac;
+
+ for (;;) {
+ double x;
+ SDL_WaitEvent(&event);
+ switch (event.type) {
+ case SDL_KEYDOWN:
+ if (exit_on_keydown) {
+ do_exit();
+ break;
+ }
+ switch (event.key.keysym.sym) {
+ case SDLK_ESCAPE:
+ case SDLK_q:
+ do_exit();
+ break;
+ case SDLK_f:
+ toggle_full_screen();
+ break;
+ case SDLK_p:
+ case SDLK_SPACE:
+ toggle_pause();
+ break;
+ case SDLK_s: // S: Step to next frame
+ step_to_next_frame();
+ break;
+ case SDLK_a:
+ if (player)
+ stream_cycle_channel(player, AVMEDIA_TYPE_AUDIO);
+ break;
+ case SDLK_v:
+ if (player)
+ stream_cycle_channel(player, AVMEDIA_TYPE_VIDEO);
+ break;
+ case SDLK_t:
+ if (player)
+ stream_cycle_channel(player, AVMEDIA_TYPE_SUBTITLE);
+ break;
+ case SDLK_w:
+ toggle_audio_display();
+ break;
+ case SDLK_PAGEUP:
+ seek_chapter(player, 1);
+ break;
+ case SDLK_PAGEDOWN:
+ seek_chapter(player, -1);
+ break;
+ case SDLK_LEFT:
+ incr = -10.0;
+ goto do_seek;
+ case SDLK_RIGHT:
+ incr = 10.0;
+ goto do_seek;
+ case SDLK_UP:
+ incr = 60.0;
+ goto do_seek;
+ case SDLK_DOWN:
+ incr = -60.0;
+ do_seek:
+ if (player) {
+ if (seek_by_bytes) {
+ if (player->video_stream >= 0 && player->video_current_pos >= 0) {
+ pos = player->video_current_pos;
+ } else if (player->audio_stream >= 0 && player->audio_pkt.pos >= 0) {
+ pos = player->audio_pkt.pos;
+ } else
+ pos = avio_tell(player->ic->pb);
+ if (player->ic->bit_rate)
+ incr *= player->ic->bit_rate / 8.0;
+ else
+ incr *= 180000.0;
+ pos += incr;
+ stream_seek(player, pos, incr, 1);
+ } else {
+ pos = get_master_clock(player);
+ pos += incr;
+ stream_seek(player, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
+ }
+ }
+ break;
+ default:
+ break;
+ }
+ break;
+ case SDL_MOUSEBUTTONDOWN:
+ if (exit_on_mousedown) {
+ do_exit();
+ break;
+ }
+ case SDL_MOUSEMOTION:
+ if (event.type == SDL_MOUSEBUTTONDOWN) {
+ x = event.button.x;
+ } else {
+ if (event.motion.state != SDL_PRESSED)
+ break;
+ x = event.motion.x;
+ }
+ if (player) {
+ if (seek_by_bytes || player->ic->duration <= 0) {
+ uint64_t size = avio_size(player->ic->pb);
+ stream_seek(player, size*x/player->width, 0, 1);
+ } else {
+ int64_t ts;
+ int ns, hh, mm, ss;
+ int tns, thh, tmm, tss;
+ tns = player->ic->duration / 1000000LL;
+ thh = tns / 3600;
+ tmm = (tns % 3600) / 60;
+ tss = (tns % 60);
+ frac = x / player->width;
+ ns = frac * tns;
+ hh = ns / 3600;
+ mm = (ns % 3600) / 60;
+ ss = (ns % 60);
+ fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
+ hh, mm, ss, thh, tmm, tss);
+ ts = frac * player->ic->duration;
+ if (player->ic->start_time != AV_NOPTS_VALUE)
+ ts += player->ic->start_time;
+ stream_seek(player, ts, 0, 0);
+ }
+ }
+ break;
+ case SDL_VIDEORESIZE:
+ if (player) {
+ screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
+ SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
+ screen_width = player->width = event.resize.w;
+ screen_height = player->height = event.resize.h;
+ }
+ break;
+ case SDL_QUIT:
+ case FF_QUIT_EVENT:
+ do_exit();
+ break;
+ case FF_ALLOC_EVENT:
+ video_open(event.user.data1);
+ alloc_picture(event.user.data1);
+ break;
+ case FF_REFRESH_EVENT:
+ video_refresh_timer(event.user.data1);
+ player->refresh = 0;
+ break;
+ default:
+ break;
+ }
+ }
+}
+
+static int opt_frame_size(void *optctx, const char *opt, const char *arg)
+{
+ av_log(NULL, AV_LOG_ERROR,
+ "Option '%s' has been removed, use private format options instead\n", opt);
+ return AVERROR(EINVAL);
+}
+
+static int opt_width(void *optctx, const char *opt, const char *arg)
+{
+ screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
+ return 0;
+}
+
+static int opt_height(void *optctx, const char *opt, const char *arg)
+{
+ screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
+ return 0;
+}
+
+static int opt_format(void *optctx, const char *opt, const char *arg)
+{
+ file_iformat = av_find_input_format(arg);
+ if (!file_iformat) {
+ fprintf(stderr, "Unknown input format: %s\n", arg);
+ return AVERROR(EINVAL);
+ }
+ return 0;
+}
+
+static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
+{
+ av_log(NULL, AV_LOG_ERROR,
+ "Option '%s' has been removed, use private format options instead\n", opt);
+ return AVERROR(EINVAL);
+}
+
+static int opt_sync(void *optctx, const char *opt, const char *arg)
+{
+ if (!strcmp(arg, "audio"))
+ av_sync_type = AV_SYNC_AUDIO_MASTER;
+ else if (!strcmp(arg, "video"))
+ av_sync_type = AV_SYNC_VIDEO_MASTER;
+ else if (!strcmp(arg, "ext"))
+ av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
+ else {
+ fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);
+ exit(1);
+ }
+ return 0;
+}
+
+static int opt_seek(void *optctx, const char *opt, const char *arg)
+{
+ start_time = parse_time_or_die(opt, arg, 1);
+ return 0;
+}
+
+static int opt_duration(void *optctx, const char *opt, const char *arg)
+{
+ duration = parse_time_or_die(opt, arg, 1);
+ return 0;
+}
+
+#define OFF(x) offsetof(PlayerState, x)
+static const OptionDef options[] = {
+ CMDUTILS_COMMON_OPTIONS
+ { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
+ { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
+ { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
+ { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
+ { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
+ { "vn", OPT_BOOL, { &video_disable }, "disable video" },
+ { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
+ { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
+ { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
+ { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
+ { "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
+ { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
+ { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
+ { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
+ { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
+ { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
+ { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
+ { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
+ { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
+ { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
+ { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_loop_filter }, "", "" },
+ { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_frame }, "", "" },
+ { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_idct }, "", "" },
+ { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { &idct }, "set idct algo", "algo" },
+ { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options", "bit_mask" },
+ { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
+ { "noautoexit", OPT_BOOL | OPT_EXPERT, { &noautoexit }, "Do not exit at the end of playback", "" },
+ { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
+ { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
+ { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
+ { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
+ { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
+ { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
+ { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "video filters", "filter list" },
+ { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
+ { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catchall option", "" },
+ { "i", 0, { NULL }, "avconv compatibility dummy option", ""},
+ { "autorotate", OPT_BOOL, { &autorotate }, "automatically rotate video", "" },
+ { "c", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_INPUT, { .off = OFF(codec_names) }, "codec name", "codec" },
+ { "codec", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_INPUT, { .off = OFF(codec_names) }, "codec name", "codec" },
+
+ { NULL, },
+};
+
+static void show_usage(void)
+{
+ printf("Simple media player\n");
+ printf("usage: %s [options] input_file\n", program_name);
+ printf("\n");
+}
+
+void show_help_default(const char *opt, const char *arg)
+{
+ av_log_set_callback(log_callback_help);
+ show_usage();
+ show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
+ show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
+ printf("\n");
+ show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
+ show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
+ printf("\nWhile playing:\n"
+ "q, ESC quit\n"
+ "f toggle full screen\n"
+ "p, SPC pause\n"
+ "a cycle audio channel\n"
+ "v cycle video channel\n"
+ "t cycle subtitle channel\n"
+ "w show audio waves\n"
+ "s activate frame-step mode\n"
+ "left/right seek backward/forward 10 seconds\n"
+ "down/up seek backward/forward 1 minute\n"
+ "mouse click seek to percentage in file corresponding to fraction of width\n"
+ );
+}
+
+static void opt_input_file(void *optctx, const char *filename)
+{
+ if (input_filename) {
+ fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
+ filename, input_filename);
+ exit(1);
+ }
+ if (!strcmp(filename, "-"))
+ filename = "pipe:";
+ input_filename = filename;
+}
+
+/* Called from the main */
+int main(int argc, char **argv)
+{
+ int flags;
+
+ av_log_set_flags(AV_LOG_SKIP_REPEATED);
+ parse_loglevel(argc, argv, options);
+
+ /* register all codecs, demux and protocols */
+ avcodec_register_all();
+#if CONFIG_AVDEVICE
+ avdevice_register_all();
+#endif
+ avfilter_register_all();
+ av_register_all();
+ avformat_network_init();
+
+ init_opts();
+
+ show_banner();
+
+ parse_options(player, argc, argv, options, opt_input_file);
+
+ if (!input_filename) {
+ show_usage();
+ fprintf(stderr, "An input file must be specified\n");
+ fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
+ exit(1);
+ }
+
+ if (display_disable) {
+ video_disable = 1;
+ }
+ flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
+#if !defined(__MINGW32__) && !defined(__APPLE__)
+ flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
+#endif
+ if (SDL_Init (flags)) {
+ fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
+ exit(1);
+ }
+
+ if (!display_disable) {
+ const SDL_VideoInfo *vi = SDL_GetVideoInfo();
+ fs_screen_width = vi->current_w;
+ fs_screen_height = vi->current_h;
+ }
+
+ SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
+ SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
+ SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
+
+ av_init_packet(&flush_pkt);
+ flush_pkt.data = (uint8_t *)&flush_pkt;
+
+ if (stream_open(player, input_filename, file_iformat) < 0) {
+ fprintf(stderr, "Could not setup the player\n");
+ stream_close(player);
+ exit(1);
+ }
+
+ event_loop();
+
+ /* never returns */
+
+ return 0;
+}
diff --git a/avtools/avprobe.c b/avtools/avprobe.c
new file mode 100644
index 0000000000..613e090be6
--- /dev/null
+++ b/avtools/avprobe.c
@@ -0,0 +1,1187 @@
+/*
+ * avprobe : Simple Media Prober based on the Libav libraries
+ * Copyright (c) 2007-2010 Stefano Sabatini
+ *
+ * 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 "libavformat/avformat.h"
+#include "libavcodec/avcodec.h"
+#include "libavutil/avstring.h"
+#include "libavutil/display.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/spherical.h"
+#include "libavutil/stereo3d.h"
+#include "libavutil/dict.h"
+#include "libavutil/libm.h"
+#include "libavdevice/avdevice.h"
+#include "cmdutils.h"
+
+typedef struct InputStream {
+ AVStream *st;
+
+ AVCodecContext *dec_ctx;
+} InputStream;
+
+typedef struct InputFile {
+ AVFormatContext *fmt_ctx;
+
+ InputStream *streams;
+ int nb_streams;
+} InputFile;
+
+const char program_name[] = "avprobe";
+const int program_birth_year = 2007;
+
+static int do_show_format = 0;
+static AVDictionary *fmt_entries_to_show = NULL;
+static int nb_fmt_entries_to_show;
+static int do_show_packets = 0;
+static int do_show_streams = 0;
+static AVDictionary *stream_entries_to_show = NULL;
+static int nb_stream_entries_to_show;
+
+/* key used to print when probe_{int,str}(NULL, ..) is invoked */
+static const char *header_key;
+
+static int show_value_unit = 0;
+static int use_value_prefix = 0;
+static int use_byte_value_binary_prefix = 0;
+static int use_value_sexagesimal_format = 0;
+
+/* globals */
+static const OptionDef *options;
+
+/* avprobe context */
+static const char *input_filename;
+static AVInputFormat *iformat = NULL;
+
+static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
+static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
+
+static const char unit_second_str[] = "s" ;
+static const char unit_hertz_str[] = "Hz" ;
+static const char unit_byte_str[] = "byte" ;
+static const char unit_bit_per_second_str[] = "bit/s";
+
+static void avprobe_cleanup(int ret)
+{
+ av_dict_free(&fmt_entries_to_show);
+ av_dict_free(&stream_entries_to_show);
+}
+
+/*
+ * The output is structured in array and objects that might contain items
+ * Array could require the objects within to not be named.
+ * Object could require the items within to be named.
+ *
+ * For flat representation the name of each section is saved on prefix so it
+ * can be rendered in order to represent nested structures (e.g. array of
+ * objects for the packets list).
+ *
+ * Within an array each element can need an unique identifier or an index.
+ *
+ * Nesting level is accounted separately.
+ */
+
+typedef enum {
+ ARRAY,
+ OBJECT
+} PrintElementType;
+
+typedef struct PrintElement {
+ const char *name;
+ PrintElementType type;
+ int64_t index;
+ int64_t nb_elems;
+} PrintElement;
+
+typedef struct PrintContext {
+ PrintElement *prefix;
+ int level;
+ void (*print_header)(void);
+ void (*print_footer)(void);
+
+ void (*print_array_header) (const char *name, int plain_values);
+ void (*print_array_footer) (const char *name, int plain_values);
+ void (*print_object_header)(const char *name);
+ void (*print_object_footer)(const char *name);
+
+ void (*print_integer) (const char *key, int64_t value);
+ void (*print_string) (const char *key, const char *value);
+} PrintContext;
+
+static AVIOContext *probe_out = NULL;
+static PrintContext octx;
+#define AVP_INDENT() avio_printf(probe_out, "%*c", octx.level * 2, ' ')
+
+/*
+ * Default format, INI
+ *
+ * - all key and values are utf8
+ * - '.' is the subgroup separator
+ * - newlines and the following characters are escaped
+ * - '\' is the escape character
+ * - '#' is the comment
+ * - '=' is the key/value separators
+ * - ':' is not used but usually parsed as key/value separator
+ */
+
+static void ini_print_header(void)
+{
+ avio_printf(probe_out, "# avprobe output\n\n");
+}
+static void ini_print_footer(void)
+{
+ avio_w8(probe_out, '\n');
+}
+
+static void ini_escape_print(const char *s)
+{
+ int i = 0;
+ char c = 0;
+
+ while (c = s[i++]) {
+ switch (c) {
+ case '\r': avio_printf(probe_out, "%s", "\\r"); break;
+ case '\n': avio_printf(probe_out, "%s", "\\n"); break;
+ case '\f': avio_printf(probe_out, "%s", "\\f"); break;
+ case '\b': avio_printf(probe_out, "%s", "\\b"); break;
+ case '\t': avio_printf(probe_out, "%s", "\\t"); break;
+ case '\\':
+ case '#' :
+ case '=' :
+ case ':' : avio_w8(probe_out, '\\');
+ default:
+ if ((unsigned char)c < 32)
+ avio_printf(probe_out, "\\x00%02x", c & 0xff);
+ else
+ avio_w8(probe_out, c);
+ break;
+ }
+ }
+}
+
+static void ini_print_array_header(const char *name, int plain_values)
+{
+ if (!plain_values) {
+ /* Add a new line if we create a new full group */
+ if (octx.prefix[octx.level -1].nb_elems)
+ avio_printf(probe_out, "\n");
+ } else {
+ ini_escape_print(name);
+ avio_w8(probe_out, '=');
+ }
+}
+
+static void ini_print_array_footer(const char *name, int plain_values)
+{
+ if (plain_values)
+ avio_printf(probe_out, "\n");
+}
+
+static void ini_print_object_header(const char *name)
+{
+ int i;
+ PrintElement *el = octx.prefix + octx.level -1;
+
+ if (el->nb_elems)
+ avio_printf(probe_out, "\n");
+
+ avio_printf(probe_out, "[");
+
+ for (i = 1; i < octx.level; i++) {
+ el = octx.prefix + i;
+ avio_printf(probe_out, "%s.", el->name);
+ if (el->index >= 0)
+ avio_printf(probe_out, "%"PRId64".", el->index);
+ }
+
+ avio_printf(probe_out, "%s", name);
+ if (el->type == ARRAY)
+ avio_printf(probe_out, ".%"PRId64"", el->nb_elems);
+ avio_printf(probe_out, "]\n");
+}
+
+static void ini_print_integer(const char *key, int64_t value)
+{
+ if (key) {
+ ini_escape_print(key);
+ avio_printf(probe_out, "=%"PRId64"\n", value);
+ } else {
+ if (octx.prefix[octx.level -1].nb_elems)
+ avio_printf(probe_out, ",");
+ avio_printf(probe_out, "%"PRId64, value);
+ }
+}
+
+
+static void ini_print_string(const char *key, const char *value)
+{
+ ini_escape_print(key);
+ avio_printf(probe_out, "=");
+ ini_escape_print(value);
+ avio_w8(probe_out, '\n');
+}
+
+/*
+ * Alternate format, JSON
+ */
+
+static void json_print_header(void)
+{
+ avio_printf(probe_out, "{");
+}
+static void json_print_footer(void)
+{
+ avio_printf(probe_out, "}\n");
+}
+
+static void json_print_array_header(const char *name, int plain_values)
+{
+ if (octx.prefix[octx.level -1].nb_elems)
+ avio_printf(probe_out, ",\n");
+ AVP_INDENT();
+ avio_printf(probe_out, "\"%s\" : ", name);
+ avio_printf(probe_out, "[\n");
+}
+
+static void json_print_array_footer(const char *name, int plain_values)
+{
+ avio_printf(probe_out, "\n");
+ AVP_INDENT();
+ avio_printf(probe_out, "]");
+}
+
+static void json_print_object_header(const char *name)
+{
+ if (octx.prefix[octx.level -1].nb_elems)
+ avio_printf(probe_out, ",\n");
+ AVP_INDENT();
+ if (octx.prefix[octx.level -1].type == OBJECT)
+ avio_printf(probe_out, "\"%s\" : ", name);
+ avio_printf(probe_out, "{\n");
+}
+
+static void json_print_object_footer(const char *name)
+{
+ avio_printf(probe_out, "\n");
+ AVP_INDENT();
+ avio_printf(probe_out, "}");
+}
+
+static void json_print_integer(const char *key, int64_t value)
+{
+ if (key) {
+ if (octx.prefix[octx.level -1].nb_elems)
+ avio_printf(probe_out, ",\n");
+ AVP_INDENT();
+ avio_printf(probe_out, "\"%s\" : ", key);
+ } else {
+ if (octx.prefix[octx.level -1].nb_elems)
+ avio_printf(probe_out, ", ");
+ else
+ AVP_INDENT();
+ }
+ avio_printf(probe_out, "%"PRId64, value);
+}
+
+static void json_escape_print(const char *s)
+{
+ int i = 0;
+ char c = 0;
+
+ while (c = s[i++]) {
+ switch (c) {
+ case '\r': avio_printf(probe_out, "%s", "\\r"); break;
+ case '\n': avio_printf(probe_out, "%s", "\\n"); break;
+ case '\f': avio_printf(probe_out, "%s", "\\f"); break;
+ case '\b': avio_printf(probe_out, "%s", "\\b"); break;
+ case '\t': avio_printf(probe_out, "%s", "\\t"); break;
+ case '\\':
+ case '"' : avio_w8(probe_out, '\\');
+ default:
+ if ((unsigned char)c < 32)
+ avio_printf(probe_out, "\\u00%02x", c & 0xff);
+ else
+ avio_w8(probe_out, c);
+ break;
+ }
+ }
+}
+
+static void json_print_string(const char *key, const char *value)
+{
+ if (octx.prefix[octx.level -1].nb_elems)
+ avio_printf(probe_out, ",\n");
+ AVP_INDENT();
+ avio_w8(probe_out, '\"');
+ json_escape_print(key);
+ avio_printf(probe_out, "\" : \"");
+ json_escape_print(value);
+ avio_w8(probe_out, '\"');
+}
+
+/*
+ * old-style pseudo-INI
+ */
+static void old_print_object_header(const char *name)
+{
+ char *str, *p;
+
+ if (!strcmp(name, "tags"))
+ return;
+
+ str = p = av_strdup(name);
+ if (!str)
+ return;
+ while (*p) {
+ *p = av_toupper(*p);
+ p++;
+ }
+
+ avio_printf(probe_out, "[%s]\n", str);
+ av_freep(&str);
+}
+
+static void old_print_object_footer(const char *name)
+{
+ char *str, *p;
+
+ if (!strcmp(name, "tags"))
+ return;
+
+ str = p = av_strdup(name);
+ if (!str)
+ return;
+ while (*p) {
+ *p = av_toupper(*p);
+ p++;
+ }
+
+ avio_printf(probe_out, "[/%s]\n", str);
+ av_freep(&str);
+}
+
+static void old_print_string(const char *key, const char *value)
+{
+ if (!strcmp(octx.prefix[octx.level - 1].name, "tags"))
+ avio_printf(probe_out, "TAG:");
+ ini_print_string(key, value);
+}
+
+/*
+ * Simple Formatter for single entries.
+ */
+
+static void show_format_entry_integer(const char *key, int64_t value)
+{
+ if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
+ if (nb_fmt_entries_to_show > 1)
+ avio_printf(probe_out, "%s=", key);
+ avio_printf(probe_out, "%"PRId64"\n", value);
+ }
+}
+
+static void show_format_entry_string(const char *key, const char *value)
+{
+ if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
+ if (nb_fmt_entries_to_show > 1)
+ avio_printf(probe_out, "%s=", key);
+ avio_printf(probe_out, "%s\n", value);
+ }
+}
+
+static void show_stream_entry_header(const char *key, int value)
+{
+ header_key = key;
+}
+
+static void show_stream_entry_footer(const char *key, int value)
+{
+ header_key = NULL;
+}
+
+static void show_stream_entry_integer(const char *key, int64_t value)
+{
+ if (!key)
+ key = header_key;
+
+ if (key && av_dict_get(stream_entries_to_show, key, NULL, 0)) {
+ if (nb_stream_entries_to_show > 1)
+ avio_printf(probe_out, "%s=", key);
+ avio_printf(probe_out, "%"PRId64"\n", value);
+ }
+}
+
+static void show_stream_entry_string(const char *key, const char *value)
+{
+ if (key && av_dict_get(stream_entries_to_show, key, NULL, 0)) {
+ if (nb_stream_entries_to_show > 1)
+ avio_printf(probe_out, "%s=", key);
+ avio_printf(probe_out, "%s\n", value);
+ }
+}
+
+static void probe_group_enter(const char *name, int type)
+{
+ int64_t count = -1;
+
+ octx.prefix =
+ av_realloc(octx.prefix, sizeof(PrintElement) * (octx.level + 1));
+
+ if (!octx.prefix || !name) {
+ fprintf(stderr, "Out of memory\n");
+ exit_program(1);
+ }
+
+ if (octx.level) {
+ PrintElement *parent = octx.prefix + octx.level -1;
+ if (parent->type == ARRAY)
+ count = parent->nb_elems;
+ parent->nb_elems++;
+ }
+
+ octx.prefix[octx.level++] = (PrintElement){name, type, count, 0};
+}
+
+static void probe_group_leave(void)
+{
+ --octx.level;
+}
+
+static void probe_header(void)
+{
+ if (octx.print_header)
+ octx.print_header();
+ probe_group_enter("root", OBJECT);
+}
+
+static void probe_footer(void)
+{
+ if (octx.print_footer)
+ octx.print_footer();
+ probe_group_leave();
+}
+
+
+static void probe_array_header(const char *name, int plain_values)
+{
+ if (octx.print_array_header)
+ octx.print_array_header(name, plain_values);
+
+ probe_group_enter(name, ARRAY);
+}
+
+static void probe_array_footer(const char *name, int plain_values)
+{
+ probe_group_leave();
+ if (octx.print_array_footer)
+ octx.print_array_footer(name, plain_values);
+}
+
+static void probe_object_header(const char *name)
+{
+ if (octx.print_object_header)
+ octx.print_object_header(name);
+
+ probe_group_enter(name, OBJECT);
+}
+
+static void probe_object_footer(const char *name)
+{
+ probe_group_leave();
+ if (octx.print_object_footer)
+ octx.print_object_footer(name);
+}
+
+static void probe_int(const char *key, int64_t value)
+{
+ octx.print_integer(key, value);
+ octx.prefix[octx.level -1].nb_elems++;
+}
+
+static void probe_str(const char *key, const char *value)
+{
+ octx.print_string(key, value);
+ octx.prefix[octx.level -1].nb_elems++;
+}
+
+static void probe_dict(AVDictionary *dict, const char *name)
+{
+ AVDictionaryEntry *entry = NULL;
+ if (!dict)
+ return;
+ probe_object_header(name);
+ while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
+ probe_str(entry->key, entry->value);
+ }
+ probe_object_footer(name);
+}
+
+static char *value_string(char *buf, int buf_size, double val, const char *unit)
+{
+ if (unit == unit_second_str && use_value_sexagesimal_format) {
+ double secs;
+ int hours, mins;
+ secs = val;
+ mins = (int)secs / 60;
+ secs = secs - mins * 60;
+ hours = mins / 60;
+ mins %= 60;
+ snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
+ } else if (use_value_prefix) {
+ const char *prefix_string;
+ int index;
+
+ if (unit == unit_byte_str && use_byte_value_binary_prefix) {
+ index = (int) log2(val) / 10;
+ index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
+ val /= pow(2, index * 10);
+ prefix_string = binary_unit_prefixes[index];
+ } else {
+ index = (int) (log10(val)) / 3;
+ index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
+ val /= pow(10, index * 3);
+ prefix_string = decimal_unit_prefixes[index];
+ }
+ snprintf(buf, buf_size, "%.*f%s%s",
+ index ? 3 : 0, val,
+ prefix_string,
+ show_value_unit ? unit : "");
+ } else {
+ snprintf(buf, buf_size, "%f%s", val, show_value_unit ? unit : "");
+ }
+
+ return buf;
+}
+
+static char *time_value_string(char *buf, int buf_size, int64_t val,
+ const AVRational *time_base)
+{
+ if (val == AV_NOPTS_VALUE) {
+ snprintf(buf, buf_size, "N/A");
+ } else {
+ value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
+ }
+
+ return buf;
+}
+
+static char *ts_value_string(char *buf, int buf_size, int64_t ts)
+{
+ if (ts == AV_NOPTS_VALUE) {
+ snprintf(buf, buf_size, "N/A");
+ } else {
+ snprintf(buf, buf_size, "%"PRId64, ts);
+ }
+
+ return buf;
+}
+
+static char *rational_string(char *buf, int buf_size, const char *sep,
+ const AVRational *rat)
+{
+ snprintf(buf, buf_size, "%d%s%d", rat->num, sep, rat->den);
+ return buf;
+}
+
+static char *tag_string(char *buf, int buf_size, int tag)
+{
+ snprintf(buf, buf_size, "0x%04x", tag);
+ return buf;
+}
+
+static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
+{
+ char val_str[128];
+ AVStream *st = fmt_ctx->streams[pkt->stream_index];
+
+ probe_object_header("packet");
+ probe_str("codec_type", media_type_string(st->codecpar->codec_type));
+ probe_int("stream_index", pkt->stream_index);
+ probe_str("pts", ts_value_string(val_str, sizeof(val_str), pkt->pts));
+ probe_str("pts_time", time_value_string(val_str, sizeof(val_str),
+ pkt->pts, &st->time_base));
+ probe_str("dts", ts_value_string(val_str, sizeof(val_str), pkt->dts));
+ probe_str("dts_time", time_value_string(val_str, sizeof(val_str),
+ pkt->dts, &st->time_base));
+ probe_str("duration", ts_value_string(val_str, sizeof(val_str),
+ pkt->duration));
+ probe_str("duration_time", time_value_string(val_str, sizeof(val_str),
+ pkt->duration,
+ &st->time_base));
+ probe_str("size", value_string(val_str, sizeof(val_str),
+ pkt->size, unit_byte_str));
+ probe_int("pos", pkt->pos);
+ probe_str("flags", pkt->flags & AV_PKT_FLAG_KEY ? "K" : "_");
+ probe_object_footer("packet");
+}
+
+static void show_packets(InputFile *ifile)
+{
+ AVFormatContext *fmt_ctx = ifile->fmt_ctx;
+ AVPacket pkt;
+
+ av_init_packet(&pkt);
+ probe_array_header("packets", 0);
+ while (!av_read_frame(fmt_ctx, &pkt)) {
+ show_packet(fmt_ctx, &pkt);
+ av_packet_unref(&pkt);
+ }
+ probe_array_footer("packets", 0);
+}
+
+static void show_stream(InputFile *ifile, InputStream *ist)
+{
+ AVFormatContext *fmt_ctx = ifile->fmt_ctx;
+ AVStream *stream = ist->st;
+ AVCodecParameters *par;
+ AVCodecContext *dec_ctx;
+ const AVCodecDescriptor *codec_desc;
+ const char *profile;
+ char val_str[128];
+ AVRational display_aspect_ratio, *sar = NULL;
+ const AVPixFmtDescriptor *desc;
+
+ probe_object_header("stream");
+
+ probe_int("index", stream->index);
+
+ par = stream->codecpar;
+ dec_ctx = ist->dec_ctx;
+ codec_desc = avcodec_descriptor_get(par->codec_id);
+ if (codec_desc) {
+ probe_str("codec_name", codec_desc->name);
+ probe_str("codec_long_name", codec_desc->long_name);
+ } else {
+ probe_str("codec_name", "unknown");
+ }
+
+ probe_str("codec_type", media_type_string(par->codec_type));
+
+ /* print AVI/FourCC tag */
+ av_get_codec_tag_string(val_str, sizeof(val_str), par->codec_tag);
+ probe_str("codec_tag_string", val_str);
+ probe_str("codec_tag", tag_string(val_str, sizeof(val_str),
+ par->codec_tag));
+
+ /* print profile, if there is one */
+ profile = avcodec_profile_name(par->codec_id, par->profile);
+ if (profile)
+ probe_str("profile", profile);
+
+ switch (par->codec_type) {
+ case AVMEDIA_TYPE_VIDEO:
+ probe_int("width", par->width);
+ probe_int("height", par->height);
+ if (dec_ctx) {
+ probe_int("coded_width", dec_ctx->coded_width);
+ probe_int("coded_height", dec_ctx->coded_height);
+ probe_int("has_b_frames", dec_ctx->has_b_frames);
+ }
+ if (dec_ctx && dec_ctx->sample_aspect_ratio.num)
+ sar = &dec_ctx->sample_aspect_ratio;
+ else if (par->sample_aspect_ratio.num)
+ sar = &par->sample_aspect_ratio;
+ else if (stream->sample_aspect_ratio.num)
+ sar = &stream->sample_aspect_ratio;
+
+ if (sar) {
+ probe_str("sample_aspect_ratio",
+ rational_string(val_str, sizeof(val_str), ":", sar));
+ av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
+ par->width * sar->num, par->height * sar->den,
+ 1024*1024);
+ probe_str("display_aspect_ratio",
+ rational_string(val_str, sizeof(val_str), ":",
+ &display_aspect_ratio));
+ }
+ desc = av_pix_fmt_desc_get(par->format);
+ probe_str("pix_fmt", desc ? desc->name : "unknown");
+ probe_int("level", par->level);
+
+ probe_str("color_range", av_color_range_name (par->color_range));
+ probe_str("color_space", av_color_space_name (par->color_space));
+ probe_str("color_trc", av_color_transfer_name (par->color_trc));
+ probe_str("color_pri", av_color_primaries_name(par->color_primaries));
+ probe_str("chroma_loc", av_chroma_location_name (par->chroma_location));
+ break;
+
+ case AVMEDIA_TYPE_AUDIO:
+ probe_str("sample_rate",
+ value_string(val_str, sizeof(val_str),
+ par->sample_rate,
+ unit_hertz_str));
+ probe_int("channels", par->channels);
+ probe_int("bits_per_sample",
+ av_get_bits_per_sample(par->codec_id));
+ break;
+ }
+
+ if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
+ probe_int("id", stream->id);
+ probe_str("avg_frame_rate",
+ rational_string(val_str, sizeof(val_str), "/",
+ &stream->avg_frame_rate));
+
+ if (par->bit_rate)
+ probe_str("bit_rate",
+ value_string(val_str, sizeof(val_str),
+ par->bit_rate, unit_bit_per_second_str));
+ probe_str("time_base",
+ rational_string(val_str, sizeof(val_str), "/",
+ &stream->time_base));
+ probe_str("start_time",
+ time_value_string(val_str, sizeof(val_str),
+ stream->start_time, &stream->time_base));
+ probe_str("duration",
+ time_value_string(val_str, sizeof(val_str),
+ stream->duration, &stream->time_base));
+ if (stream->nb_frames)
+ probe_int("nb_frames", stream->nb_frames);
+
+ probe_dict(stream->metadata, "tags");
+
+ if (stream->nb_side_data) {
+ int i, j;
+
+ probe_object_header("sidedata");
+ for (i = 0; i < stream->nb_side_data; i++) {
+ const AVPacketSideData* sd = &stream->side_data[i];
+ AVStereo3D *stereo;
+ AVSphericalMapping *spherical;
+
+ switch (sd->type) {
+ case AV_PKT_DATA_DISPLAYMATRIX:
+ probe_object_header("displaymatrix");
+ probe_array_header("matrix", 1);
+ for (j = 0; j < 9; j++)
+ probe_int(NULL, ((int32_t *)sd->data)[j]);
+ probe_array_footer("matrix", 1);
+ probe_int("rotation",
+ av_display_rotation_get((int32_t *)sd->data));
+ probe_object_footer("displaymatrix");
+ break;
+ case AV_PKT_DATA_STEREO3D:
+ stereo = (AVStereo3D *)sd->data;
+ probe_object_header("stereo3d");
+ probe_str("type", av_stereo3d_type_name(stereo->type));
+ probe_int("inverted",
+ !!(stereo->flags & AV_STEREO3D_FLAG_INVERT));
+ probe_object_footer("stereo3d");
+ break;
+ case AV_PKT_DATA_SPHERICAL:
+ spherical = (AVSphericalMapping *)sd->data;
+ probe_object_header("spherical");
+
+ if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR)
+ probe_str("projection", "equirectangular");
+ else if (spherical->projection == AV_SPHERICAL_CUBEMAP)
+ probe_str("projection", "cubemap");
+ else
+ probe_str("projection", "unknown");
+
+ probe_object_header("orientation");
+ probe_int("yaw", (double) spherical->yaw / (1 << 16));
+ probe_int("pitch", (double) spherical->pitch / (1 << 16));
+ probe_int("roll", (double) spherical->roll / (1 << 16));
+ probe_object_footer("orientation");
+
+ probe_object_footer("spherical");
+ break;
+ }
+ }
+ probe_object_footer("sidedata");
+ }
+
+ probe_object_footer("stream");
+}
+
+static void show_format(InputFile *ifile)
+{
+ AVFormatContext *fmt_ctx = ifile->fmt_ctx;
+ char val_str[128];
+ int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
+
+ probe_object_header("format");
+ probe_str("filename", fmt_ctx->filename);
+ probe_int("nb_streams", fmt_ctx->nb_streams);
+ probe_str("format_name", fmt_ctx->iformat->name);
+ probe_str("format_long_name", fmt_ctx->iformat->long_name);
+ probe_str("start_time",
+ time_value_string(val_str, sizeof(val_str),
+ fmt_ctx->start_time, &AV_TIME_BASE_Q));
+ probe_str("duration",
+ time_value_string(val_str, sizeof(val_str),
+ fmt_ctx->duration, &AV_TIME_BASE_Q));
+ probe_str("size",
+ size >= 0 ? value_string(val_str, sizeof(val_str),
+ size, unit_byte_str)
+ : "unknown");
+ probe_str("bit_rate",
+ value_string(val_str, sizeof(val_str),
+ fmt_ctx->bit_rate, unit_bit_per_second_str));
+
+ probe_dict(fmt_ctx->metadata, "tags");
+
+ probe_object_footer("format");
+}
+
+static int open_input_file(InputFile *ifile, const char *filename)
+{
+ int err, i;
+ AVFormatContext *fmt_ctx = NULL;
+ AVDictionaryEntry *t;
+
+ if ((err = avformat_open_input(&fmt_ctx, filename,
+ iformat, &format_opts)) < 0) {
+ print_error(filename, err);
+ return err;
+ }
+ if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
+ av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
+ return AVERROR_OPTION_NOT_FOUND;
+ }
+
+
+ /* fill the streams in the format context */
+ if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
+ print_error(filename, err);
+ return err;
+ }
+
+ av_dump_format(fmt_ctx, 0, filename, 0);
+
+ ifile->streams = av_mallocz_array(fmt_ctx->nb_streams,
+ sizeof(*ifile->streams));
+ if (!ifile->streams)
+ exit(1);
+ ifile->nb_streams = fmt_ctx->nb_streams;
+
+ /* bind a decoder to each input stream */
+ for (i = 0; i < fmt_ctx->nb_streams; i++) {
+ InputStream *ist = &ifile->streams[i];
+ AVStream *stream = fmt_ctx->streams[i];
+ AVCodec *codec;
+
+ ist->st = stream;
+
+ if (stream->codecpar->codec_id == AV_CODEC_ID_PROBE) {
+ fprintf(stderr, "Failed to probe codec for input stream %d\n",
+ stream->index);
+ continue;
+ }
+
+ codec = avcodec_find_decoder(stream->codecpar->codec_id);
+ if (!codec) {
+ fprintf(stderr,
+ "Unsupported codec with id %d for input stream %d\n",
+ stream->codecpar->codec_id, stream->index);
+ continue;
+ }
+
+ ist->dec_ctx = avcodec_alloc_context3(codec);
+ if (!ist->dec_ctx)
+ exit(1);
+
+ err = avcodec_parameters_to_context(ist->dec_ctx, stream->codecpar);
+ if (err < 0)
+ exit(1);
+
+ err = avcodec_open2(ist->dec_ctx, NULL, NULL);
+ if (err < 0) {
+ fprintf(stderr, "Error while opening codec for input stream %d\n",
+ stream->index);
+ exit(1);
+
+ }
+ }
+
+ ifile->fmt_ctx = fmt_ctx;
+ return 0;
+}
+
+static void close_input_file(InputFile *ifile)
+{
+ int i;
+
+ /* close decoder for each stream */
+ for (i = 0; i < ifile->nb_streams; i++) {
+ InputStream *ist = &ifile->streams[i];
+
+ avcodec_free_context(&ist->dec_ctx);
+ }
+
+ av_freep(&ifile->streams);
+ ifile->nb_streams = 0;
+
+ avformat_close_input(&ifile->fmt_ctx);
+}
+
+static int probe_file(const char *filename)
+{
+ InputFile ifile;
+ int ret, i;
+
+ ret = open_input_file(&ifile, filename);
+ if (ret < 0)
+ return ret;
+
+ if (do_show_format)
+ show_format(&ifile);
+
+ if (do_show_streams) {
+ probe_array_header("streams", 0);
+ for (i = 0; i < ifile.nb_streams; i++)
+ show_stream(&ifile, &ifile.streams[i]);
+ probe_array_footer("streams", 0);
+ }
+
+ if (do_show_packets)
+ show_packets(&ifile);
+
+ close_input_file(&ifile);
+ return 0;
+}
+
+static void show_usage(void)
+{
+ printf("Simple multimedia streams analyzer\n");
+ printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
+ printf("\n");
+}
+
+static int opt_format(void *optctx, const char *opt, const char *arg)
+{
+ iformat = av_find_input_format(arg);
+ if (!iformat) {
+ fprintf(stderr, "Unknown input format: %s\n", arg);
+ return AVERROR(EINVAL);
+ }
+ return 0;
+}
+
+static int opt_output_format(void *optctx, const char *opt, const char *arg)
+{
+
+ if (!strcmp(arg, "json")) {
+ octx.print_header = json_print_header;
+ octx.print_footer = json_print_footer;
+ octx.print_array_header = json_print_array_header;
+ octx.print_array_footer = json_print_array_footer;
+ octx.print_object_header = json_print_object_header;
+ octx.print_object_footer = json_print_object_footer;
+
+ octx.print_integer = json_print_integer;
+ octx.print_string = json_print_string;
+ } else if (!strcmp(arg, "ini")) {
+ octx.print_header = ini_print_header;
+ octx.print_footer = ini_print_footer;
+ octx.print_array_header = ini_print_array_header;
+ octx.print_array_footer = ini_print_array_footer;
+ octx.print_object_header = ini_print_object_header;
+
+ octx.print_integer = ini_print_integer;
+ octx.print_string = ini_print_string;
+ } else if (!strcmp(arg, "old")) {
+ octx.print_header = NULL;
+ octx.print_object_header = old_print_object_header;
+ octx.print_object_footer = old_print_object_footer;
+
+ octx.print_string = old_print_string;
+ } else {
+ av_log(NULL, AV_LOG_ERROR, "Unsupported formatter %s\n", arg);
+ return AVERROR(EINVAL);
+ }
+ return 0;
+}
+
+static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
+{
+ do_show_format = 1;
+ nb_fmt_entries_to_show++;
+ octx.print_header = NULL;
+ octx.print_footer = NULL;
+ octx.print_array_header = NULL;
+ octx.print_array_footer = NULL;
+ octx.print_object_header = NULL;
+ octx.print_object_footer = NULL;
+
+ octx.print_integer = show_format_entry_integer;
+ octx.print_string = show_format_entry_string;
+ av_dict_set(&fmt_entries_to_show, arg, "", 0);
+ return 0;
+}
+
+static int opt_show_stream_entry(void *optctx, const char *opt, const char *arg)
+{
+ const char *p = arg;
+
+ do_show_streams = 1;
+ nb_stream_entries_to_show++;
+ octx.print_header = NULL;
+ octx.print_footer = NULL;
+ octx.print_array_header = show_stream_entry_header;
+ octx.print_array_footer = show_stream_entry_footer;
+ octx.print_object_header = NULL;
+ octx.print_object_footer = NULL;
+
+ octx.print_integer = show_stream_entry_integer;
+ octx.print_string = show_stream_entry_string;
+
+ while (*p) {
+ char *val = av_get_token(&p, ",");
+ if (!val)
+ return AVERROR(ENOMEM);
+
+ av_dict_set(&stream_entries_to_show, val, "", 0);
+
+ av_free(val);
+ if (*p)
+ p++;
+ }
+
+ return 0;
+}
+
+static void opt_input_file(void *optctx, const char *arg)
+{
+ if (input_filename) {
+ fprintf(stderr,
+ "Argument '%s' provided as input filename, but '%s' was already specified.\n",
+ arg, input_filename);
+ exit_program(1);
+ }
+ if (!strcmp(arg, "-"))
+ arg = "pipe:";
+ input_filename = arg;
+}
+
+void show_help_default(const char *opt, const char *arg)
+{
+ av_log_set_callback(log_callback_help);
+ show_usage();
+ show_help_options(options, "Main options:", 0, 0, 0);
+ printf("\n");
+ show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
+}
+
+static int opt_pretty(void *optctx, const char *opt, const char *arg)
+{
+ show_value_unit = 1;
+ use_value_prefix = 1;
+ use_byte_value_binary_prefix = 1;
+ use_value_sexagesimal_format = 1;
+ return 0;
+}
+
+static const OptionDef real_options[] = {
+ CMDUTILS_COMMON_OPTIONS
+ { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
+ { "of", HAS_ARG, {.func_arg = opt_output_format}, "output the document either as ini or json", "output_format" },
+ { "unit", OPT_BOOL, {&show_value_unit},
+ "show unit of the displayed values" },
+ { "prefix", OPT_BOOL, {&use_value_prefix},
+ "use SI prefixes for the displayed values" },
+ { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
+ "use binary prefixes for byte units" },
+ { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
+ "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
+ { "pretty", 0, {.func_arg = opt_pretty},
+ "prettify the format of displayed values, make it more human readable" },
+ { "show_format", OPT_BOOL, {&do_show_format} , "show format/container info" },
+ { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
+ "show a particular entry from the format/container info", "entry" },
+ { "show_packets", OPT_BOOL, {&do_show_packets}, "show packets info" },
+ { "show_streams", OPT_BOOL, {&do_show_streams}, "show streams info" },
+ { "show_stream_entry", HAS_ARG, {.func_arg = opt_show_stream_entry},
+ "show a particular entry from all streams (comma separated)", "entry" },
+ { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default},
+ "generic catch all option", "" },
+ { NULL, },
+};
+
+static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
+{
+ printf("%.*s", buf_size, buf);
+ return 0;
+}
+
+#define AVP_BUFFSIZE 4096
+
+int main(int argc, char **argv)
+{
+ int ret;
+ uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
+
+ if (!buffer)
+ exit(1);
+
+ register_exit(avprobe_cleanup);
+
+ options = real_options;
+ parse_loglevel(argc, argv, options);
+ av_register_all();
+ avformat_network_init();
+ init_opts();
+#if CONFIG_AVDEVICE
+ avdevice_register_all();
+#endif
+
+ show_banner();
+
+ octx.print_header = ini_print_header;
+ octx.print_footer = ini_print_footer;
+
+ octx.print_array_header = ini_print_array_header;
+ octx.print_array_footer = ini_print_array_footer;
+ octx.print_object_header = ini_print_object_header;
+
+ octx.print_integer = ini_print_integer;
+ octx.print_string = ini_print_string;
+
+ parse_options(NULL, argc, argv, options, opt_input_file);
+
+ if (!input_filename) {
+ show_usage();
+ fprintf(stderr, "You have to specify one input file.\n");
+ fprintf(stderr,
+ "Use -h to get full help or, even better, run 'man %s'.\n",
+ program_name);
+ exit_program(1);
+ }
+
+ probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
+ probe_buf_write, NULL);
+ if (!probe_out)
+ exit_program(1);
+
+ probe_header();
+ ret = probe_file(input_filename);
+ probe_footer();
+ avio_flush(probe_out);
+ avio_context_free(&probe_out);
+ av_freep(&buffer);
+ uninit_opts();
+ avformat_network_deinit();
+
+ return ret;
+}
diff --git a/avtools/cmdutils.c b/avtools/cmdutils.c
new file mode 100644
index 0000000000..b0445eb85b
--- /dev/null
+++ b/avtools/cmdutils.c
@@ -0,0 +1,1702 @@
+/*
+ * Various utilities for command line tools
+ * Copyright (c) 2000-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 <string.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <math.h>
+
+/* Include only the enabled headers since some compilers (namely, Sun
+ Studio) will not omit unused inline functions and create undefined
+ references to libraries that are not being built. */
+
+#include "config.h"
+#include "libavformat/avformat.h"
+#include "libavfilter/avfilter.h"
+#include "libavdevice/avdevice.h"
+#include "libavresample/avresample.h"
+#include "libswscale/swscale.h"
+#include "libavutil/avassert.h"
+#include "libavutil/avstring.h"
+#include "libavutil/mathematics.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/parseutils.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/eval.h"
+#include "libavutil/dict.h"
+#include "libavutil/opt.h"
+#include "libavutil/cpu.h"
+#include "avversion.h"
+#include "cmdutils.h"
+#if CONFIG_NETWORK
+#include "libavformat/network.h"
+#endif
+#if HAVE_SYS_RESOURCE_H
+#include <sys/time.h>
+#include <sys/resource.h>
+#endif
+
+struct SwsContext *sws_opts;
+AVDictionary *format_opts, *codec_opts, *resample_opts;
+
+static const int this_year = 2017;
+
+void init_opts(void)
+{
+#if CONFIG_SWSCALE
+ sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
+ NULL, NULL, NULL);
+#endif
+}
+
+void uninit_opts(void)
+{
+#if CONFIG_SWSCALE
+ sws_freeContext(sws_opts);
+ sws_opts = NULL;
+#endif
+ av_dict_free(&format_opts);
+ av_dict_free(&codec_opts);
+ av_dict_free(&resample_opts);
+}
+
+void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
+{
+ vfprintf(stdout, fmt, vl);
+}
+
+static void (*program_exit)(int ret);
+
+void register_exit(void (*cb)(int ret))
+{
+ program_exit = cb;
+}
+
+void exit_program(int ret)
+{
+ if (program_exit)
+ program_exit(ret);
+
+ exit(ret);
+}
+
+double parse_number_or_die(const char *context, const char *numstr, int type,
+ double min, double max)
+{
+ char *tail;
+ const char *error;
+ double d = av_strtod(numstr, &tail);
+ if (*tail)
+ error = "Expected number for %s but found: %s\n";
+ else if (d < min || d > max)
+ error = "The value for %s was %s which is not within %f - %f\n";
+ else if (type == OPT_INT64 && (int64_t)d != d)
+ error = "Expected int64 for %s but found %s\n";
+ else if (type == OPT_INT && (int)d != d)
+ error = "Expected int for %s but found %s\n";
+ else
+ return d;
+ av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
+ exit_program(1);
+ return 0;
+}
+
+int64_t parse_time_or_die(const char *context, const char *timestr,
+ int is_duration)
+{
+ int64_t us;
+ if (av_parse_time(&us, timestr, is_duration) < 0) {
+ av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
+ is_duration ? "duration" : "date", context, timestr);
+ exit_program(1);
+ }
+ return us;
+}
+
+void show_help_options(const OptionDef *options, const char *msg, int req_flags,
+ int rej_flags, int alt_flags)
+{
+ const OptionDef *po;
+ int first;
+
+ first = 1;
+ for (po = options; po->name != NULL; po++) {
+ char buf[64];
+
+ if (((po->flags & req_flags) != req_flags) ||
+ (alt_flags && !(po->flags & alt_flags)) ||
+ (po->flags & rej_flags))
+ continue;
+
+ if (first) {
+ printf("%s\n", msg);
+ first = 0;
+ }
+ av_strlcpy(buf, po->name, sizeof(buf));
+ if (po->argname) {
+ av_strlcat(buf, " ", sizeof(buf));
+ av_strlcat(buf, po->argname, sizeof(buf));
+ }
+ printf("-%-17s %s\n", buf, po->help);
+ }
+ printf("\n");
+}
+
+void show_help_children(const AVClass *class, int flags)
+{
+ const AVClass *child = NULL;
+ av_opt_show2(&class, NULL, flags, 0);
+ printf("\n");
+
+ while (child = av_opt_child_class_next(class, child))
+ show_help_children(child, flags);
+}
+
+static const OptionDef *find_option(const OptionDef *po, const char *name)
+{
+ const char *p = strchr(name, ':');
+ int len = p ? p - name : strlen(name);
+
+ while (po->name) {
+ if (!strncmp(name, po->name, len) && strlen(po->name) == len)
+ break;
+ po++;
+ }
+ return po;
+}
+
+/* _WIN32 means using the windows libc - cygwin doesn't define that
+ * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
+ * it doesn't provide the actual command line via GetCommandLineW(). */
+#if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
+#include <windows.h>
+#include <shellapi.h>
+/* Will be leaked on exit */
+static char** win32_argv_utf8 = NULL;
+static int win32_argc = 0;
+
+/**
+ * Prepare command line arguments for executable.
+ * For Windows - perform wide-char to UTF-8 conversion.
+ * Input arguments should be main() function arguments.
+ * @param argc_ptr Arguments number (including executable)
+ * @param argv_ptr Arguments list.
+ */
+static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
+{
+ char *argstr_flat;
+ wchar_t **argv_w;
+ int i, buffsize = 0, offset = 0;
+
+ if (win32_argv_utf8) {
+ *argc_ptr = win32_argc;
+ *argv_ptr = win32_argv_utf8;
+ return;
+ }
+
+ win32_argc = 0;
+ argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
+ if (win32_argc <= 0 || !argv_w)
+ return;
+
+ /* determine the UTF-8 buffer size (including NULL-termination symbols) */
+ for (i = 0; i < win32_argc; i++)
+ buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
+ NULL, 0, NULL, NULL);
+
+ win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
+ argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
+ if (!win32_argv_utf8) {
+ LocalFree(argv_w);
+ return;
+ }
+
+ for (i = 0; i < win32_argc; i++) {
+ win32_argv_utf8[i] = &argstr_flat[offset];
+ offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
+ &argstr_flat[offset],
+ buffsize - offset, NULL, NULL);
+ }
+ win32_argv_utf8[i] = NULL;
+ LocalFree(argv_w);
+
+ *argc_ptr = win32_argc;
+ *argv_ptr = win32_argv_utf8;
+}
+#else
+static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
+{
+ /* nothing to do */
+}
+#endif /* HAVE_COMMANDLINETOARGVW */
+
+static int write_option(void *optctx, const OptionDef *po, const char *opt,
+ const char *arg)
+{
+ /* new-style options contain an offset into optctx, old-style address of
+ * a global var*/
+ void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
+ (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
+ int *dstcount;
+
+ if (po->flags & OPT_SPEC) {
+ SpecifierOpt **so = dst;
+ char *p = strchr(opt, ':');
+ char *str;
+
+ dstcount = (int *)(so + 1);
+ *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
+ str = av_strdup(p ? p + 1 : "");
+ if (!str)
+ return AVERROR(ENOMEM);
+ (*so)[*dstcount - 1].specifier = str;
+ dst = &(*so)[*dstcount - 1].u;
+ }
+
+ if (po->flags & OPT_STRING) {
+ char *str;
+ str = av_strdup(arg);
+ av_freep(dst);
+ if (!str)
+ return AVERROR(ENOMEM);
+ *(char **)dst = str;
+ } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
+ *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
+ } else if (po->flags & OPT_INT64) {
+ *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
+ } else if (po->flags & OPT_TIME) {
+ *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
+ } else if (po->flags & OPT_FLOAT) {
+ *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
+ } else if (po->flags & OPT_DOUBLE) {
+ *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
+ } else if (po->u.func_arg) {
+ int ret = po->u.func_arg(optctx, opt, arg);
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_ERROR,
+ "Failed to set value '%s' for option '%s'\n", arg, opt);
+ return ret;
+ }
+ }
+ if (po->flags & OPT_EXIT)
+ exit_program(0);
+
+ return 0;
+}
+
+int parse_option(void *optctx, const char *opt, const char *arg,
+ const OptionDef *options)
+{
+ const OptionDef *po;
+ int ret;
+
+ po = find_option(options, opt);
+ if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
+ /* handle 'no' bool option */
+ po = find_option(options, opt + 2);
+ if ((po->name && (po->flags & OPT_BOOL)))
+ arg = "0";
+ } else if (po->flags & OPT_BOOL)
+ arg = "1";
+
+ if (!po->name)
+ po = find_option(options, "default");
+ if (!po->name) {
+ av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
+ return AVERROR(EINVAL);
+ }
+ if (po->flags & HAS_ARG && !arg) {
+ av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
+ return AVERROR(EINVAL);
+ }
+
+ ret = write_option(optctx, po, opt, arg);
+ if (ret < 0)
+ return ret;
+
+ return !!(po->flags & HAS_ARG);
+}
+
+void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
+ void (*parse_arg_function)(void *, const char*))
+{
+ const char *opt;
+ int optindex, handleoptions = 1, ret;
+
+ /* perform system-dependent conversions for arguments list */
+ prepare_app_arguments(&argc, &argv);
+
+ /* parse options */
+ optindex = 1;
+ while (optindex < argc) {
+ opt = argv[optindex++];
+
+ if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
+ if (opt[1] == '-' && opt[2] == '\0') {
+ handleoptions = 0;
+ continue;
+ }
+ opt++;
+
+ if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
+ exit_program(1);
+ optindex += ret;
+ } else {
+ if (parse_arg_function)
+ parse_arg_function(optctx, opt);
+ }
+ }
+}
+
+int parse_optgroup(void *optctx, OptionGroup *g)
+{
+ int i, ret;
+
+ av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
+ g->group_def->name, g->arg);
+
+ for (i = 0; i < g->nb_opts; i++) {
+ Option *o = &g->opts[i];
+
+ if (g->group_def->flags &&
+ !(g->group_def->flags & o->opt->flags)) {
+ av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
+ "%s %s -- you are trying to apply an input option to an "
+ "output file or vice versa. Move this option before the "
+ "file it belongs to.\n", o->key, o->opt->help,
+ g->group_def->name, g->arg);
+ return AVERROR(EINVAL);
+ }
+
+ av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
+ o->key, o->opt->help, o->val);
+
+ ret = write_option(optctx, o->opt, o->key, o->val);
+ if (ret < 0)
+ return ret;
+ }
+
+ av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
+
+ return 0;
+}
+
+int locate_option(int argc, char **argv, const OptionDef *options,
+ const char *optname)
+{
+ const OptionDef *po;
+ int i;
+
+ for (i = 1; i < argc; i++) {
+ const char *cur_opt = argv[i];
+
+ if (*cur_opt++ != '-')
+ continue;
+
+ po = find_option(options, cur_opt);
+ if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
+ po = find_option(options, cur_opt + 2);
+
+ if ((!po->name && !strcmp(cur_opt, optname)) ||
+ (po->name && !strcmp(optname, po->name)))
+ return i;
+
+ if (!po->name || po->flags & HAS_ARG)
+ i++;
+ }
+ return 0;
+}
+
+void parse_loglevel(int argc, char **argv, const OptionDef *options)
+{
+ int idx = locate_option(argc, argv, options, "loglevel");
+ if (!idx)
+ idx = locate_option(argc, argv, options, "v");
+ if (idx && argv[idx + 1])
+ opt_loglevel(NULL, "loglevel", argv[idx + 1]);
+}
+
+#define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
+int opt_default(void *optctx, const char *opt, const char *arg)
+{
+ const AVOption *o;
+ char opt_stripped[128];
+ const char *p;
+ const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
+#if CONFIG_AVRESAMPLE
+ const AVClass *rc = avresample_get_class();
+#endif
+#if CONFIG_SWSCALE
+ const AVClass *sc = sws_get_class();
+#endif
+
+ if (!(p = strchr(opt, ':')))
+ p = opt + strlen(opt);
+ av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
+
+ if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
+ AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
+ ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
+ (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
+ av_dict_set(&codec_opts, opt, arg, FLAGS);
+ else if ((o = av_opt_find(&fc, opt, NULL, 0,
+ AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
+ av_dict_set(&format_opts, opt, arg, FLAGS);
+#if CONFIG_AVRESAMPLE
+ else if ((o = av_opt_find(&rc, opt, NULL, 0,
+ AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
+ av_dict_set(&resample_opts, opt, arg, FLAGS);
+#endif
+#if CONFIG_SWSCALE
+ else if ((o = av_opt_find(&sc, opt, NULL, 0,
+ AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
+ // XXX we only support sws_flags, not arbitrary sws options
+ int ret = av_opt_set(sws_opts, opt, arg, 0);
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
+ return ret;
+ }
+ }
+#endif
+
+ if (o)
+ return 0;
+ return AVERROR_OPTION_NOT_FOUND;
+}
+
+/*
+ * Check whether given option is a group separator.
+ *
+ * @return index of the group definition that matched or -1 if none
+ */
+static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
+ const char *opt)
+{
+ int i;
+
+ for (i = 0; i < nb_groups; i++) {
+ const OptionGroupDef *p = &groups[i];
+ if (p->sep && !strcmp(p->sep, opt))
+ return i;
+ }
+
+ return -1;
+}
+
+/*
+ * Finish parsing an option group.
+ *
+ * @param group_idx which group definition should this group belong to
+ * @param arg argument of the group delimiting option
+ */
+static void finish_group(OptionParseContext *octx, int group_idx,
+ const char *arg)
+{
+ OptionGroupList *l = &octx->groups[group_idx];
+ OptionGroup *g;
+
+ GROW_ARRAY(l->groups, l->nb_groups);
+ g = &l->groups[l->nb_groups - 1];
+
+ *g = octx->cur_group;
+ g->arg = arg;
+ g->group_def = l->group_def;
+#if CONFIG_SWSCALE
+ g->sws_opts = sws_opts;
+#endif
+ g->codec_opts = codec_opts;
+ g->format_opts = format_opts;
+ g->resample_opts = resample_opts;
+
+ codec_opts = NULL;
+ format_opts = NULL;
+ resample_opts = NULL;
+#if CONFIG_SWSCALE
+ sws_opts = NULL;
+#endif
+ init_opts();
+
+ memset(&octx->cur_group, 0, sizeof(octx->cur_group));
+}
+
+/*
+ * Add an option instance to currently parsed group.
+ */
+static void add_opt(OptionParseContext *octx, const OptionDef *opt,
+ const char *key, const char *val)
+{
+ int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
+ OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
+
+ GROW_ARRAY(g->opts, g->nb_opts);
+ g->opts[g->nb_opts - 1].opt = opt;
+ g->opts[g->nb_opts - 1].key = key;
+ g->opts[g->nb_opts - 1].val = val;
+}
+
+static void init_parse_context(OptionParseContext *octx,
+ const OptionGroupDef *groups, int nb_groups)
+{
+ static const OptionGroupDef global_group = { "global" };
+ int i;
+
+ memset(octx, 0, sizeof(*octx));
+
+ octx->nb_groups = nb_groups;
+ octx->groups = av_mallocz(sizeof(*octx->groups) * octx->nb_groups);
+ if (!octx->groups)
+ exit_program(1);
+
+ for (i = 0; i < octx->nb_groups; i++)
+ octx->groups[i].group_def = &groups[i];
+
+ octx->global_opts.group_def = &global_group;
+ octx->global_opts.arg = "";
+
+ init_opts();
+}
+
+void uninit_parse_context(OptionParseContext *octx)
+{
+ int i, j;
+
+ for (i = 0; i < octx->nb_groups; i++) {
+ OptionGroupList *l = &octx->groups[i];
+
+ for (j = 0; j < l->nb_groups; j++) {
+ av_freep(&l->groups[j].opts);
+ av_dict_free(&l->groups[j].codec_opts);
+ av_dict_free(&l->groups[j].format_opts);
+ av_dict_free(&l->groups[j].resample_opts);
+#if CONFIG_SWSCALE
+ sws_freeContext(l->groups[j].sws_opts);
+#endif
+ }
+ av_freep(&l->groups);
+ }
+ av_freep(&octx->groups);
+
+ av_freep(&octx->cur_group.opts);
+ av_freep(&octx->global_opts.opts);
+
+ uninit_opts();
+}
+
+int split_commandline(OptionParseContext *octx, int argc, char *argv[],
+ const OptionDef *options,
+ const OptionGroupDef *groups, int nb_groups)
+{
+ int optindex = 1;
+
+ /* perform system-dependent conversions for arguments list */
+ prepare_app_arguments(&argc, &argv);
+
+ init_parse_context(octx, groups, nb_groups);
+ av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
+
+ while (optindex < argc) {
+ const char *opt = argv[optindex++], *arg;
+ const OptionDef *po;
+ int ret;
+
+ av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
+
+ /* unnamed group separators, e.g. output filename */
+ if (opt[0] != '-' || !opt[1]) {
+ finish_group(octx, 0, opt);
+ av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
+ continue;
+ }
+ opt++;
+
+#define GET_ARG(arg) \
+do { \
+ arg = argv[optindex++]; \
+ if (!arg) { \
+ av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
+ return AVERROR(EINVAL); \
+ } \
+} while (0)
+
+ /* named group separators, e.g. -i */
+ if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
+ GET_ARG(arg);
+ finish_group(octx, ret, arg);
+ av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
+ groups[ret].name, arg);
+ continue;
+ }
+
+ /* normal options */
+ po = find_option(options, opt);
+ if (po->name) {
+ if (po->flags & OPT_EXIT) {
+ /* optional argument, e.g. -h */
+ arg = argv[optindex++];
+ } else if (po->flags & HAS_ARG) {
+ GET_ARG(arg);
+ } else {
+ arg = "1";
+ }
+
+ add_opt(octx, po, opt, arg);
+ av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
+ "argument '%s'.\n", po->name, po->help, arg);
+ continue;
+ }
+
+ /* AVOptions */
+ if (argv[optindex]) {
+ ret = opt_default(NULL, opt, argv[optindex]);
+ if (ret >= 0) {
+ av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
+ "argument '%s'.\n", opt, argv[optindex]);
+ optindex++;
+ continue;
+ } else if (ret != AVERROR_OPTION_NOT_FOUND) {
+ av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
+ "with argument '%s'.\n", opt, argv[optindex]);
+ return ret;
+ }
+ }
+
+ /* boolean -nofoo options */
+ if (opt[0] == 'n' && opt[1] == 'o' &&
+ (po = find_option(options, opt + 2)) &&
+ po->name && po->flags & OPT_BOOL) {
+ add_opt(octx, po, opt, "0");
+ av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
+ "argument 0.\n", po->name, po->help);
+ continue;
+ }
+
+ av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
+ return AVERROR_OPTION_NOT_FOUND;
+ }
+
+ if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
+ av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
+ "commandline.\n");
+
+ av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
+
+ return 0;
+}
+
+int opt_cpuflags(void *optctx, const char *opt, const char *arg)
+{
+ int flags = av_parse_cpu_flags(arg);
+
+ if (flags < 0)
+ return flags;
+
+ av_set_cpu_flags_mask(flags);
+ return 0;
+}
+
+int opt_loglevel(void *optctx, const char *opt, const char *arg)
+{
+ const struct { const char *name; int level; } log_levels[] = {
+ { "quiet" , AV_LOG_QUIET },
+ { "panic" , AV_LOG_PANIC },
+ { "fatal" , AV_LOG_FATAL },
+ { "error" , AV_LOG_ERROR },
+ { "warning", AV_LOG_WARNING },
+ { "info" , AV_LOG_INFO },
+ { "verbose", AV_LOG_VERBOSE },
+ { "debug" , AV_LOG_DEBUG },
+ { "trace" , AV_LOG_TRACE },
+ };
+ char *tail;
+ int level;
+ int i;
+
+ for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
+ if (!strcmp(log_levels[i].name, arg)) {
+ av_log_set_level(log_levels[i].level);
+ return 0;
+ }
+ }
+
+ level = strtol(arg, &tail, 10);
+ if (*tail) {
+ av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
+ "Possible levels are numbers or:\n", arg);
+ for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
+ av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
+ exit_program(1);
+ }
+ av_log_set_level(level);
+ return 0;
+}
+
+int opt_timelimit(void *optctx, const char *opt, const char *arg)
+{
+#if HAVE_SETRLIMIT
+ int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
+ struct rlimit rl = { lim, lim + 1 };
+ if (setrlimit(RLIMIT_CPU, &rl))
+ perror("setrlimit");
+#else
+ av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
+#endif
+ return 0;
+}
+
+void print_error(const char *filename, int err)
+{
+ char errbuf[128];
+ const char *errbuf_ptr = errbuf;
+
+ if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
+ errbuf_ptr = strerror(AVUNERROR(err));
+ av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
+}
+
+static int warned_cfg = 0;
+
+#define INDENT 1
+#define SHOW_VERSION 2
+#define SHOW_CONFIG 4
+
+#define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
+ if (CONFIG_##LIBNAME) { \
+ const char *indent = flags & INDENT? " " : ""; \
+ if (flags & SHOW_VERSION) { \
+ unsigned int version = libname##_version(); \
+ av_log(NULL, level, \
+ "%slib%-10s %2d.%3d.%2d / %2d.%3d.%2d\n", \
+ indent, #libname, \
+ LIB##LIBNAME##_VERSION_MAJOR, \
+ LIB##LIBNAME##_VERSION_MINOR, \
+ LIB##LIBNAME##_VERSION_MICRO, \
+ version >> 16, version >> 8 & 0xff, version & 0xff); \
+ } \
+ if (flags & SHOW_CONFIG) { \
+ const char *cfg = libname##_configuration(); \
+ if (strcmp(LIBAV_CONFIGURATION, cfg)) { \
+ if (!warned_cfg) { \
+ av_log(NULL, level, \
+ "%sWARNING: library configuration mismatch\n", \
+ indent); \
+ warned_cfg = 1; \
+ } \
+ av_log(NULL, level, "%s%-11s configuration: %s\n", \
+ indent, #libname, cfg); \
+ } \
+ } \
+ } \
+
+static void print_all_libs_info(int flags, int level)
+{
+ PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
+ PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
+ PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
+ PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
+ PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
+ PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
+ PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
+}
+
+void show_banner(void)
+{
+ av_log(NULL, AV_LOG_INFO,
+ "%s version " LIBAV_VERSION ", Copyright (c) %d-%d the Libav developers\n",
+ program_name, program_birth_year, this_year);
+ av_log(NULL, AV_LOG_INFO, " built on %s %s with %s\n",
+ __DATE__, __TIME__, CC_IDENT);
+ av_log(NULL, AV_LOG_VERBOSE, " configuration: " LIBAV_CONFIGURATION "\n");
+ print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_VERBOSE);
+ print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_VERBOSE);
+}
+
+int show_version(void *optctx, const char *opt, const char *arg)
+{
+ av_log_set_callback(log_callback_help);
+ printf("%s " LIBAV_VERSION "\n", program_name);
+ print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
+
+ return 0;
+}
+
+int show_license(void *optctx, const char *opt, const char *arg)
+{
+ printf(
+#if CONFIG_NONFREE
+ "This version of %s has nonfree parts compiled in.\n"
+ "Therefore it is not legally redistributable.\n",
+ program_name
+#elif CONFIG_GPLV3
+ "%s is free software; you can redistribute it and/or modify\n"
+ "it under the terms of the GNU General Public License as published by\n"
+ "the Free Software Foundation; either version 3 of the License, or\n"
+ "(at your option) any later version.\n"
+ "\n"
+ "%s is distributed in the hope that it will be useful,\n"
+ "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
+ "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
+ "GNU General Public License for more details.\n"
+ "\n"
+ "You should have received a copy of the GNU General Public License\n"
+ "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
+ program_name, program_name, program_name
+#elif CONFIG_GPL
+ "%s is free software; you can redistribute it and/or modify\n"
+ "it under the terms of the GNU General Public License as published by\n"
+ "the Free Software Foundation; either version 2 of the License, or\n"
+ "(at your option) any later version.\n"
+ "\n"
+ "%s is distributed in the hope that it will be useful,\n"
+ "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
+ "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
+ "GNU General Public License for more details.\n"
+ "\n"
+ "You should have received a copy of the GNU General Public License\n"
+ "along with %s; if not, write to the Free Software\n"
+ "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
+ program_name, program_name, program_name
+#elif CONFIG_LGPLV3
+ "%s is free software; you can redistribute it and/or modify\n"
+ "it under the terms of the GNU Lesser General Public License as published by\n"
+ "the Free Software Foundation; either version 3 of the License, or\n"
+ "(at your option) any later version.\n"
+ "\n"
+ "%s is distributed in the hope that it will be useful,\n"
+ "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
+ "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
+ "GNU Lesser General Public License for more details.\n"
+ "\n"
+ "You should have received a copy of the GNU Lesser General Public License\n"
+ "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
+ program_name, program_name, program_name
+#else
+ "%s is free software; you can redistribute it and/or\n"
+ "modify it under the terms of the GNU Lesser General Public\n"
+ "License as published by the Free Software Foundation; either\n"
+ "version 2.1 of the License, or (at your option) any later version.\n"
+ "\n"
+ "%s is distributed in the hope that it will be useful,\n"
+ "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
+ "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
+ "Lesser General Public License for more details.\n"
+ "\n"
+ "You should have received a copy of the GNU Lesser General Public\n"
+ "License along with %s; if not, write to the Free Software\n"
+ "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
+ program_name, program_name, program_name
+#endif
+ );
+
+ return 0;
+}
+
+int show_formats(void *optctx, const char *opt, const char *arg)
+{
+ AVInputFormat *ifmt = NULL;
+ AVOutputFormat *ofmt = NULL;
+ const char *last_name;
+
+ printf("File formats:\n"
+ " D. = Demuxing supported\n"
+ " .E = Muxing supported\n"
+ " --\n");
+ last_name = "000";
+ for (;;) {
+ int decode = 0;
+ int encode = 0;
+ const char *name = NULL;
+ const char *long_name = NULL;
+
+ while ((ofmt = av_oformat_next(ofmt))) {
+ if ((!name || strcmp(ofmt->name, name) < 0) &&
+ strcmp(ofmt->name, last_name) > 0) {
+ name = ofmt->name;
+ long_name = ofmt->long_name;
+ encode = 1;
+ }
+ }
+ while ((ifmt = av_iformat_next(ifmt))) {
+ if ((!name || strcmp(ifmt->name, name) < 0) &&
+ strcmp(ifmt->name, last_name) > 0) {
+ name = ifmt->name;
+ long_name = ifmt->long_name;
+ encode = 0;
+ }
+ if (name && strcmp(ifmt->name, name) == 0)
+ decode = 1;
+ }
+ if (!name)
+ break;
+ last_name = name;
+
+ printf(" %s%s %-15s %s\n",
+ decode ? "D" : " ",
+ encode ? "E" : " ",
+ name,
+ long_name ? long_name:" ");
+ }
+ return 0;
+}
+
+#define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
+ if (codec->field) { \
+ const type *p = c->field; \
+ \
+ printf(" Supported " list_name ":"); \
+ while (*p != term) { \
+ get_name(*p); \
+ printf(" %s", name); \
+ p++; \
+ } \
+ printf("\n"); \
+ } \
+
+static void print_codec(const AVCodec *c)
+{
+ int encoder = av_codec_is_encoder(c);
+
+ printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
+ c->long_name ? c->long_name : "");
+
+ printf(" General capabilities: ");
+ if (c->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)
+ printf("horizband ");
+ if (c->capabilities & AV_CODEC_CAP_DR1)
+ printf("dr1 ");
+ if (c->capabilities & AV_CODEC_CAP_TRUNCATED)
+ printf("trunc ");
+ if (c->capabilities & AV_CODEC_CAP_DELAY)
+ printf("delay ");
+ if (c->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)
+ printf("small ");
+ if (c->capabilities & AV_CODEC_CAP_SUBFRAMES)
+ printf("subframes ");
+ if (c->capabilities & AV_CODEC_CAP_EXPERIMENTAL)
+ printf("exp ");
+ if (c->capabilities & AV_CODEC_CAP_CHANNEL_CONF)
+ printf("chconf ");
+ if (c->capabilities & AV_CODEC_CAP_PARAM_CHANGE)
+ printf("paramchange ");
+ if (c->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
+ printf("variable ");
+ if (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
+ AV_CODEC_CAP_SLICE_THREADS |
+ AV_CODEC_CAP_AUTO_THREADS))
+ printf("threads ");
+ if (!c->capabilities)
+ printf("none");
+ printf("\n");
+
+ if (c->type == AVMEDIA_TYPE_VIDEO) {
+ printf(" Threading capabilities: ");
+ switch (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
+ AV_CODEC_CAP_SLICE_THREADS |
+ AV_CODEC_CAP_AUTO_THREADS)) {
+ case AV_CODEC_CAP_FRAME_THREADS |
+ AV_CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
+ case AV_CODEC_CAP_FRAME_THREADS: printf("frame"); break;
+ case AV_CODEC_CAP_SLICE_THREADS: printf("slice"); break;
+ case AV_CODEC_CAP_AUTO_THREADS : printf("auto"); break;
+ default: printf("none"); break;
+ }
+ printf("\n");
+ }
+
+ if (c->supported_framerates) {
+ const AVRational *fps = c->supported_framerates;
+
+ printf(" Supported framerates:");
+ while (fps->num) {
+ printf(" %d/%d", fps->num, fps->den);
+ fps++;
+ }
+ printf("\n");
+ }
+ PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
+ AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
+ PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
+ GET_SAMPLE_RATE_NAME);
+ PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
+ AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
+ PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
+ 0, GET_CH_LAYOUT_DESC);
+
+ if (c->priv_class) {
+ show_help_children(c->priv_class,
+ AV_OPT_FLAG_ENCODING_PARAM |
+ AV_OPT_FLAG_DECODING_PARAM);
+ }
+}
+
+static char get_media_type_char(enum AVMediaType type)
+{
+ switch (type) {
+ case AVMEDIA_TYPE_VIDEO: return 'V';
+ case AVMEDIA_TYPE_AUDIO: return 'A';
+ case AVMEDIA_TYPE_SUBTITLE: return 'S';
+ default: return '?';
+ }
+}
+
+static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
+ int encoder)
+{
+ while ((prev = av_codec_next(prev))) {
+ if (prev->id == id &&
+ (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
+ return prev;
+ }
+ return NULL;
+}
+
+static void print_codecs_for_id(enum AVCodecID id, int encoder)
+{
+ const AVCodec *codec = NULL;
+
+ printf(" (%s: ", encoder ? "encoders" : "decoders");
+
+ while ((codec = next_codec_for_id(id, codec, encoder)))
+ printf("%s ", codec->name);
+
+ printf(")");
+}
+
+int show_codecs(void *optctx, const char *opt, const char *arg)
+{
+ const AVCodecDescriptor *desc = NULL;
+
+ printf("Codecs:\n"
+ " D..... = Decoding supported\n"
+ " .E.... = Encoding supported\n"
+ " ..V... = Video codec\n"
+ " ..A... = Audio codec\n"
+ " ..S... = Subtitle codec\n"
+ " ...I.. = Intra frame-only codec\n"
+ " ....L. = Lossy compression\n"
+ " .....S = Lossless compression\n"
+ " -------\n");
+ while ((desc = avcodec_descriptor_next(desc))) {
+ const AVCodec *codec = NULL;
+
+ printf(avcodec_find_decoder(desc->id) ? "D" : ".");
+ printf(avcodec_find_encoder(desc->id) ? "E" : ".");
+
+ printf("%c", get_media_type_char(desc->type));
+ printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
+ printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
+ printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
+
+ printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
+
+ /* print decoders/encoders when there's more than one or their
+ * names are different from codec name */
+ while ((codec = next_codec_for_id(desc->id, codec, 0))) {
+ if (strcmp(codec->name, desc->name)) {
+ print_codecs_for_id(desc->id, 0);
+ break;
+ }
+ }
+ codec = NULL;
+ while ((codec = next_codec_for_id(desc->id, codec, 1))) {
+ if (strcmp(codec->name, desc->name)) {
+ print_codecs_for_id(desc->id, 1);
+ break;
+ }
+ }
+
+ printf("\n");
+ }
+ return 0;
+}
+
+static void print_codecs(int encoder)
+{
+ const AVCodecDescriptor *desc = NULL;
+
+ printf("%s:\n"
+ " V... = Video\n"
+ " A... = Audio\n"
+ " S... = Subtitle\n"
+ " .F.. = Frame-level multithreading\n"
+ " ..S. = Slice-level multithreading\n"
+ " ...X = Codec is experimental\n"
+ " ---\n",
+ encoder ? "Encoders" : "Decoders");
+ while ((desc = avcodec_descriptor_next(desc))) {
+ const AVCodec *codec = NULL;
+
+ while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
+ printf("%c", get_media_type_char(desc->type));
+ printf((codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) ? "F" : ".");
+ printf((codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) ? "S" : ".");
+ printf((codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
+
+ printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
+ if (strcmp(codec->name, desc->name))
+ printf(" (codec %s)", desc->name);
+
+ printf("\n");
+ }
+ }
+}
+
+int show_decoders(void *optctx, const char *opt, const char *arg)
+{
+ print_codecs(0);
+ return 0;
+}
+
+int show_encoders(void *optctx, const char *opt, const char *arg)
+{
+ print_codecs(1);
+ return 0;
+}
+
+int show_bsfs(void *optctx, const char *opt, const char *arg)
+{
+ const AVBitStreamFilter *bsf = NULL;
+ void *opaque = NULL;
+
+ printf("Bitstream filters:\n");
+ while ((bsf = av_bsf_next(&opaque)))
+ printf("%s\n", bsf->name);
+ printf("\n");
+ return 0;
+}
+
+int show_protocols(void *optctx, const char *opt, const char *arg)
+{
+ void *opaque = NULL;
+ const char *name;
+
+ printf("Supported file protocols:\n"
+ "Input:\n");
+ while ((name = avio_enum_protocols(&opaque, 0)))
+ printf("%s\n", name);
+ printf("Output:\n");
+ while ((name = avio_enum_protocols(&opaque, 1)))
+ printf("%s\n", name);
+ return 0;
+}
+
+int show_filters(void *optctx, const char *opt, const char *arg)
+{
+#if CONFIG_AVFILTER
+ const AVFilter *filter = NULL;
+
+ printf("Filters:\n");
+ while ((filter = avfilter_next(filter)))
+ printf("%-16s %s\n", filter->name, filter->description);
+#else
+ printf("No filters available: libavfilter disabled\n");
+#endif
+ return 0;
+}
+
+int show_pix_fmts(void *optctx, const char *opt, const char *arg)
+{
+ const AVPixFmtDescriptor *pix_desc = NULL;
+
+ printf("Pixel formats:\n"
+ "I.... = Supported Input format for conversion\n"
+ ".O... = Supported Output format for conversion\n"
+ "..H.. = Hardware accelerated format\n"
+ "...P. = Paletted format\n"
+ "....B = Bitstream format\n"
+ "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
+ "-----\n");
+
+#if !CONFIG_SWSCALE
+# define sws_isSupportedInput(x) 0
+# define sws_isSupportedOutput(x) 0
+#endif
+
+ while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
+ enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
+ printf("%c%c%c%c%c %-16s %d %2d\n",
+ sws_isSupportedInput (pix_fmt) ? 'I' : '.',
+ sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
+ pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
+ pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
+ pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
+ pix_desc->name,
+ pix_desc->nb_components,
+ av_get_bits_per_pixel(pix_desc));
+ }
+ return 0;
+}
+
+int show_sample_fmts(void *optctx, const char *opt, const char *arg)
+{
+ int i;
+ char fmt_str[128];
+ for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
+ printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
+ return 0;
+}
+
+static void show_help_codec(const char *name, int encoder)
+{
+ const AVCodecDescriptor *desc;
+ const AVCodec *codec;
+
+ if (!name) {
+ av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
+ return;
+ }
+
+ codec = encoder ? avcodec_find_encoder_by_name(name) :
+ avcodec_find_decoder_by_name(name);
+
+ if (codec)
+ print_codec(codec);
+ else if ((desc = avcodec_descriptor_get_by_name(name))) {
+ int printed = 0;
+
+ while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
+ printed = 1;
+ print_codec(codec);
+ }
+
+ if (!printed) {
+ av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to Libav, "
+ "but no %s for it are available. Libav might need to be "
+ "recompiled with additional external libraries.\n",
+ name, encoder ? "encoders" : "decoders");
+ }
+ } else {
+ av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by Libav.\n",
+ name);
+ }
+}
+
+static void show_help_demuxer(const char *name)
+{
+ const AVInputFormat *fmt = av_find_input_format(name);
+
+ if (!fmt) {
+ av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
+ return;
+ }
+
+ printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
+
+ if (fmt->extensions)
+ printf(" Common extensions: %s.\n", fmt->extensions);
+
+ if (fmt->priv_class)
+ show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
+}
+
+static void show_help_muxer(const char *name)
+{
+ const AVCodecDescriptor *desc;
+ const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
+
+ if (!fmt) {
+ av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
+ return;
+ }
+
+ printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
+
+ if (fmt->extensions)
+ printf(" Common extensions: %s.\n", fmt->extensions);
+ if (fmt->mime_type)
+ printf(" Mime type: %s.\n", fmt->mime_type);
+ if (fmt->video_codec != AV_CODEC_ID_NONE &&
+ (desc = avcodec_descriptor_get(fmt->video_codec))) {
+ printf(" Default video codec: %s.\n", desc->name);
+ }
+ if (fmt->audio_codec != AV_CODEC_ID_NONE &&
+ (desc = avcodec_descriptor_get(fmt->audio_codec))) {
+ printf(" Default audio codec: %s.\n", desc->name);
+ }
+ if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
+ (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
+ printf(" Default subtitle codec: %s.\n", desc->name);
+ }
+
+ if (fmt->priv_class)
+ show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
+}
+
+#if CONFIG_AVFILTER
+static void show_help_filter(const char *name)
+{
+ const AVFilter *f = avfilter_get_by_name(name);
+ int i, count;
+
+ if (!name) {
+ av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
+ return;
+ } else if (!f) {
+ av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
+ return;
+ }
+
+ printf("Filter %s [%s]:\n", f->name, f->description);
+
+ if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
+ printf(" slice threading supported\n");
+
+ printf(" Inputs:\n");
+ count = avfilter_pad_count(f->inputs);
+ for (i = 0; i < count; i++) {
+ printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
+ media_type_string(avfilter_pad_get_type(f->inputs, i)));
+ }
+ if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
+ printf(" dynamic (depending on the options)\n");
+
+ printf(" Outputs:\n");
+ count = avfilter_pad_count(f->outputs);
+ for (i = 0; i < count; i++) {
+ printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
+ media_type_string(avfilter_pad_get_type(f->outputs, i)));
+ }
+ if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
+ printf(" dynamic (depending on the options)\n");
+
+ if (f->priv_class)
+ show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM |
+ AV_OPT_FLAG_AUDIO_PARAM);
+}
+#endif
+
+int show_help(void *optctx, const char *opt, const char *arg)
+{
+ char *topic, *par;
+ av_log_set_callback(log_callback_help);
+
+ topic = av_strdup(arg ? arg : "");
+ if (!topic)
+ return AVERROR(ENOMEM);
+ par = strchr(topic, '=');
+ if (par)
+ *par++ = 0;
+
+ if (!*topic) {
+ show_help_default(topic, par);
+ } else if (!strcmp(topic, "decoder")) {
+ show_help_codec(par, 0);
+ } else if (!strcmp(topic, "encoder")) {
+ show_help_codec(par, 1);
+ } else if (!strcmp(topic, "demuxer")) {
+ show_help_demuxer(par);
+ } else if (!strcmp(topic, "muxer")) {
+ show_help_muxer(par);
+#if CONFIG_AVFILTER
+ } else if (!strcmp(topic, "filter")) {
+ show_help_filter(par);
+#endif
+ } else {
+ show_help_default(topic, par);
+ }
+
+ av_freep(&topic);
+ return 0;
+}
+
+int read_yesno(void)
+{
+ int c = getchar();
+ int yesno = (av_toupper(c) == 'Y');
+
+ while (c != '\n' && c != EOF)
+ c = getchar();
+
+ return yesno;
+}
+
+void init_pts_correction(PtsCorrectionContext *ctx)
+{
+ ctx->num_faulty_pts = ctx->num_faulty_dts = 0;
+ ctx->last_pts = ctx->last_dts = INT64_MIN;
+}
+
+int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts,
+ int64_t dts)
+{
+ int64_t pts = AV_NOPTS_VALUE;
+
+ if (dts != AV_NOPTS_VALUE) {
+ ctx->num_faulty_dts += dts <= ctx->last_dts;
+ ctx->last_dts = dts;
+ }
+ if (reordered_pts != AV_NOPTS_VALUE) {
+ ctx->num_faulty_pts += reordered_pts <= ctx->last_pts;
+ ctx->last_pts = reordered_pts;
+ }
+ if ((ctx->num_faulty_pts<=ctx->num_faulty_dts || dts == AV_NOPTS_VALUE)
+ && reordered_pts != AV_NOPTS_VALUE)
+ pts = reordered_pts;
+ else
+ pts = dts;
+
+ return pts;
+}
+
+FILE *get_preset_file(char *filename, size_t filename_size,
+ const char *preset_name, int is_path,
+ const char *codec_name)
+{
+ FILE *f = NULL;
+ int i;
+ const char *base[3] = { getenv("AVCONV_DATADIR"),
+ getenv("HOME"),
+ AVCONV_DATADIR, };
+
+ if (is_path) {
+ av_strlcpy(filename, preset_name, filename_size);
+ f = fopen(filename, "r");
+ } else {
+ for (i = 0; i < 3 && !f; i++) {
+ if (!base[i])
+ continue;
+ snprintf(filename, filename_size, "%s%s/%s.avpreset", base[i],
+ i != 1 ? "" : "/.avconv", preset_name);
+ f = fopen(filename, "r");
+ if (!f && codec_name) {
+ snprintf(filename, filename_size,
+ "%s%s/%s-%s.avpreset",
+ base[i], i != 1 ? "" : "/.avconv", codec_name,
+ preset_name);
+ f = fopen(filename, "r");
+ }
+ }
+ }
+
+ return f;
+}
+
+int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
+{
+ if (*spec <= '9' && *spec >= '0') /* opt:index */
+ return strtol(spec, NULL, 0) == st->index;
+ else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
+ *spec == 't') { /* opt:[vasdt] */
+ enum AVMediaType type;
+
+ switch (*spec++) {
+ case 'v': type = AVMEDIA_TYPE_VIDEO; break;
+ case 'a': type = AVMEDIA_TYPE_AUDIO; break;
+ case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
+ case 'd': type = AVMEDIA_TYPE_DATA; break;
+ case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
+ default: av_assert0(0);
+ }
+ if (type != st->codecpar->codec_type)
+ return 0;
+ if (*spec++ == ':') { /* possibly followed by :index */
+ int i, index = strtol(spec, NULL, 0);
+ for (i = 0; i < s->nb_streams; i++)
+ if (s->streams[i]->codecpar->codec_type == type && index-- == 0)
+ return i == st->index;
+ return 0;
+ }
+ return 1;
+ } else if (*spec == 'p' && *(spec + 1) == ':') {
+ int prog_id, i, j;
+ char *endptr;
+ spec += 2;
+ prog_id = strtol(spec, &endptr, 0);
+ for (i = 0; i < s->nb_programs; i++) {
+ if (s->programs[i]->id != prog_id)
+ continue;
+
+ if (*endptr++ == ':') {
+ int stream_idx = strtol(endptr, NULL, 0);
+ return stream_idx >= 0 &&
+ stream_idx < s->programs[i]->nb_stream_indexes &&
+ st->index == s->programs[i]->stream_index[stream_idx];
+ }
+
+ for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
+ if (st->index == s->programs[i]->stream_index[j])
+ return 1;
+ }
+ return 0;
+ } else if (*spec == 'i' && *(spec + 1) == ':') {
+ int stream_id;
+ char *endptr;
+ spec += 2;
+ stream_id = strtol(spec, &endptr, 0);
+ return stream_id == st->id;
+ } else if (*spec == 'm' && *(spec + 1) == ':') {
+ AVDictionaryEntry *tag;
+ char *key, *val;
+ int ret;
+
+ spec += 2;
+ val = strchr(spec, ':');
+
+ key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
+ if (!key)
+ return AVERROR(ENOMEM);
+
+ tag = av_dict_get(st->metadata, key, NULL, 0);
+ if (tag) {
+ if (!val || !strcmp(tag->value, val + 1))
+ ret = 1;
+ else
+ ret = 0;
+ } else
+ ret = 0;
+
+ av_freep(&key);
+ return ret;
+ } else if (*spec == 'u') {
+ AVCodecParameters *par = st->codecpar;
+ int val;
+ switch (par->codec_type) {
+ case AVMEDIA_TYPE_AUDIO:
+ val = par->sample_rate && par->channels;
+ if (par->format == AV_SAMPLE_FMT_NONE)
+ return 0;
+ break;
+ case AVMEDIA_TYPE_VIDEO:
+ val = par->width && par->height;
+ if (par->format == AV_PIX_FMT_NONE)
+ return 0;
+ break;
+ case AVMEDIA_TYPE_UNKNOWN:
+ val = 0;
+ break;
+ default:
+ val = 1;
+ break;
+ }
+ return par->codec_id != AV_CODEC_ID_NONE && val != 0;
+ } else if (!*spec) /* empty specifier, matches everything */
+ return 1;
+
+ av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
+ return AVERROR(EINVAL);
+}
+
+AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
+ AVFormatContext *s, AVStream *st, AVCodec *codec)
+{
+ AVDictionary *ret = NULL;
+ AVDictionaryEntry *t = NULL;
+ int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
+ : AV_OPT_FLAG_DECODING_PARAM;
+ char prefix = 0;
+ const AVClass *cc = avcodec_get_class();
+
+ if (!codec)
+ codec = s->oformat ? avcodec_find_encoder(codec_id)
+ : avcodec_find_decoder(codec_id);
+
+ switch (st->codecpar->codec_type) {
+ case AVMEDIA_TYPE_VIDEO:
+ prefix = 'v';
+ flags |= AV_OPT_FLAG_VIDEO_PARAM;
+ break;
+ case AVMEDIA_TYPE_AUDIO:
+ prefix = 'a';
+ flags |= AV_OPT_FLAG_AUDIO_PARAM;
+ break;
+ case AVMEDIA_TYPE_SUBTITLE:
+ prefix = 's';
+ flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
+ break;
+ }
+
+ while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
+ char *p = strchr(t->key, ':');
+
+ /* check stream specification in opt name */
+ if (p)
+ switch (check_stream_specifier(s, st, p + 1)) {
+ case 1: *p = 0; break;
+ case 0: continue;
+ default: return NULL;
+ }
+
+ if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
+ (codec && codec->priv_class &&
+ av_opt_find(&codec->priv_class, t->key, NULL, flags,
+ AV_OPT_SEARCH_FAKE_OBJ)))
+ av_dict_set(&ret, t->key, t->value, 0);
+ else if (t->key[0] == prefix &&
+ av_opt_find(&cc, t->key + 1, NULL, flags,
+ AV_OPT_SEARCH_FAKE_OBJ))
+ av_dict_set(&ret, t->key + 1, t->value, 0);
+
+ if (p)
+ *p = ':';
+ }
+ return ret;
+}
+
+AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
+ AVDictionary *codec_opts)
+{
+ int i;
+ AVDictionary **opts;
+
+ if (!s->nb_streams)
+ return NULL;
+ opts = av_mallocz(s->nb_streams * sizeof(*opts));
+ if (!opts) {
+ av_log(NULL, AV_LOG_ERROR,
+ "Could not alloc memory for stream options.\n");
+ return NULL;
+ }
+ for (i = 0; i < s->nb_streams; i++)
+ opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
+ s, s->streams[i], NULL);
+ return opts;
+}
+
+void *grow_array(void *array, int elem_size, int *size, int new_size)
+{
+ if (new_size >= INT_MAX / elem_size) {
+ av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
+ exit_program(1);
+ }
+ if (*size < new_size) {
+ uint8_t *tmp = av_realloc(array, new_size*elem_size);
+ if (!tmp) {
+ av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
+ exit_program(1);
+ }
+ memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
+ *size = new_size;
+ return tmp;
+ }
+ return array;
+}
+
+const char *media_type_string(enum AVMediaType media_type)
+{
+ switch (media_type) {
+ case AVMEDIA_TYPE_VIDEO: return "video";
+ case AVMEDIA_TYPE_AUDIO: return "audio";
+ case AVMEDIA_TYPE_DATA: return "data";
+ case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
+ case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
+ default: return "unknown";
+ }
+}
diff --git a/avtools/cmdutils.h b/avtools/cmdutils.h
new file mode 100644
index 0000000000..cc78ac5911
--- /dev/null
+++ b/avtools/cmdutils.h
@@ -0,0 +1,566 @@
+/*
+ * Various utilities for command line tools
+ * 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
+ */
+
+#ifndef LIBAV_CMDUTILS_H
+#define LIBAV_CMDUTILS_H
+
+#include <stdint.h>
+
+#include "libavcodec/avcodec.h"
+#include "libavfilter/avfilter.h"
+#include "libavformat/avformat.h"
+#include "libswscale/swscale.h"
+
+/**
+ * program name, defined by the program for show_version().
+ */
+extern const char program_name[];
+
+/**
+ * program birth year, defined by the program for show_banner()
+ */
+extern const int program_birth_year;
+
+extern AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB];
+extern AVFormatContext *avformat_opts;
+extern struct SwsContext *sws_opts;
+extern AVDictionary *format_opts, *codec_opts, *resample_opts;
+
+/**
+ * Register a program-specific cleanup routine.
+ */
+void register_exit(void (*cb)(int ret));
+
+/**
+ * Wraps exit with a program-specific cleanup routine.
+ */
+void exit_program(int ret) av_noreturn;
+
+/**
+ * Initialize the cmdutils option system, in particular
+ * allocate the *_opts contexts.
+ */
+void init_opts(void);
+/**
+ * Uninitialize the cmdutils option system, in particular
+ * free the *_opts contexts and their contents.
+ */
+void uninit_opts(void);
+
+/**
+ * Trivial log callback.
+ * Only suitable for show_help and similar since it lacks prefix handling.
+ */
+void log_callback_help(void* ptr, int level, const char* fmt, va_list vl);
+
+/**
+ * Override the cpuflags mask.
+ */
+int opt_cpuflags(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Fallback for options that are not explicitly handled, these will be
+ * parsed through AVOptions.
+ */
+int opt_default(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Set the libav* libraries log level.
+ */
+int opt_loglevel(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Limit the execution time.
+ */
+int opt_timelimit(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Parse a string and return its corresponding value as a double.
+ * Exit from the application if the string cannot be correctly
+ * parsed or the corresponding value is invalid.
+ *
+ * @param context the context of the value to be set (e.g. the
+ * corresponding command line option name)
+ * @param numstr the string to be parsed
+ * @param type the type (OPT_INT64 or OPT_FLOAT) as which the
+ * string should be parsed
+ * @param min the minimum valid accepted value
+ * @param max the maximum valid accepted value
+ */
+double parse_number_or_die(const char *context, const char *numstr, int type,
+ double min, double max);
+
+/**
+ * Parse a string specifying a time and return its corresponding
+ * value as a number of microseconds. Exit from the application if
+ * the string cannot be correctly parsed.
+ *
+ * @param context the context of the value to be set (e.g. the
+ * corresponding command line option name)
+ * @param timestr the string to be parsed
+ * @param is_duration a flag which tells how to interpret timestr, if
+ * not zero timestr is interpreted as a duration, otherwise as a
+ * date
+ *
+ * @see parse_date()
+ */
+int64_t parse_time_or_die(const char *context, const char *timestr,
+ int is_duration);
+
+typedef struct SpecifierOpt {
+ char *specifier; /**< stream/chapter/program/... specifier */
+ union {
+ uint8_t *str;
+ int i;
+ int64_t i64;
+ float f;
+ double dbl;
+ } u;
+} SpecifierOpt;
+
+typedef struct OptionDef {
+ const char *name;
+ int flags;
+#define HAS_ARG 0x0001
+#define OPT_BOOL 0x0002
+#define OPT_EXPERT 0x0004
+#define OPT_STRING 0x0008
+#define OPT_VIDEO 0x0010
+#define OPT_AUDIO 0x0020
+#define OPT_INT 0x0080
+#define OPT_FLOAT 0x0100
+#define OPT_SUBTITLE 0x0200
+#define OPT_INT64 0x0400
+#define OPT_EXIT 0x0800
+#define OPT_DATA 0x1000
+#define OPT_PERFILE 0x2000 /* the option is per-file (currently avconv-only).
+ implied by OPT_OFFSET or OPT_SPEC */
+#define OPT_OFFSET 0x4000 /* option is specified as an offset in a passed optctx */
+#define OPT_SPEC 0x8000 /* option is to be stored in an array of SpecifierOpt.
+ Implies OPT_OFFSET. Next element after the offset is
+ an int containing element count in the array. */
+#define OPT_TIME 0x10000
+#define OPT_DOUBLE 0x20000
+#define OPT_INPUT 0x40000
+#define OPT_OUTPUT 0x80000
+ union {
+ void *dst_ptr;
+ int (*func_arg)(void *, const char *, const char *);
+ size_t off;
+ } u;
+ const char *help;
+ const char *argname;
+} OptionDef;
+
+/**
+ * Print help for all options matching specified flags.
+ *
+ * @param options a list of options
+ * @param msg title of this group. Only printed if at least one option matches.
+ * @param req_flags print only options which have all those flags set.
+ * @param rej_flags don't print options which have any of those flags set.
+ * @param alt_flags print only options that have at least one of those flags set
+ */
+void show_help_options(const OptionDef *options, const char *msg, int req_flags,
+ int rej_flags, int alt_flags);
+
+#define CMDUTILS_COMMON_OPTIONS \
+ { "L", OPT_EXIT, { .func_arg = show_license }, "show license" }, \
+ { "h", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" }, \
+ { "?", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" }, \
+ { "help", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" }, \
+ { "-help", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" }, \
+ { "version", OPT_EXIT, { .func_arg = show_version }, "show version" }, \
+ { "formats", OPT_EXIT, { .func_arg = show_formats }, "show available formats" }, \
+ { "codecs", OPT_EXIT, { .func_arg = show_codecs }, "show available codecs" }, \
+ { "decoders", OPT_EXIT, { .func_arg = show_decoders }, "show available decoders" }, \
+ { "encoders", OPT_EXIT, { .func_arg = show_encoders }, "show available encoders" }, \
+ { "bsfs", OPT_EXIT, { .func_arg = show_bsfs }, "show available bit stream filters" }, \
+ { "protocols", OPT_EXIT, { .func_arg = show_protocols }, "show available protocols" }, \
+ { "filters", OPT_EXIT, { .func_arg = show_filters }, "show available filters" }, \
+ { "pix_fmts", OPT_EXIT, { .func_arg = show_pix_fmts }, "show available pixel formats" }, \
+ { "sample_fmts", OPT_EXIT, { .func_arg = show_sample_fmts }, "show available audio sample formats" }, \
+ { "loglevel", HAS_ARG, { .func_arg = opt_loglevel }, "set libav* logging level", "loglevel" }, \
+ { "v", HAS_ARG, { .func_arg = opt_loglevel }, "set libav* logging level", "loglevel" }, \
+ { "cpuflags", HAS_ARG | OPT_EXPERT, { .func_arg = opt_cpuflags }, "set CPU flags mask", "mask" }, \
+
+/**
+ * Show help for all options with given flags in class and all its
+ * children.
+ */
+void show_help_children(const AVClass *class, int flags);
+
+/**
+ * Per-avtool specific help handler. Implemented in each
+ * avtool, called by show_help().
+ */
+void show_help_default(const char *opt, const char *arg);
+
+/**
+ * Generic -h handler common to all avtools.
+ */
+int show_help(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Parse the command line arguments.
+ *
+ * @param optctx an opaque options context
+ * @param argc number of command line arguments
+ * @param argv values of command line arguments
+ * @param options Array with the definitions required to interpret every
+ * option of the form: -option_name [argument]
+ * @param parse_arg_function Name of the function called to process every
+ * argument without a leading option name flag. NULL if such arguments do
+ * not have to be processed.
+ */
+void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
+ void (* parse_arg_function)(void *optctx, const char*));
+
+/**
+ * Parse one given option.
+ *
+ * @return on success 1 if arg was consumed, 0 otherwise; negative number on error
+ */
+int parse_option(void *optctx, const char *opt, const char *arg,
+ const OptionDef *options);
+
+/**
+ * An option extracted from the commandline.
+ * Cannot use AVDictionary because of options like -map which can be
+ * used multiple times.
+ */
+typedef struct Option {
+ const OptionDef *opt;
+ const char *key;
+ const char *val;
+} Option;
+
+typedef struct OptionGroupDef {
+ /**< group name */
+ const char *name;
+ /**
+ * Option to be used as group separator. Can be NULL for groups which
+ * are terminated by a non-option argument (e.g. avconv output files)
+ */
+ const char *sep;
+ /**
+ * Option flags that must be set on each option that is
+ * applied to this group
+ */
+ int flags;
+} OptionGroupDef;
+
+typedef struct OptionGroup {
+ const OptionGroupDef *group_def;
+ const char *arg;
+
+ Option *opts;
+ int nb_opts;
+
+ AVDictionary *codec_opts;
+ AVDictionary *format_opts;
+ AVDictionary *resample_opts;
+ struct SwsContext *sws_opts;
+} OptionGroup;
+
+/**
+ * A list of option groups that all have the same group type
+ * (e.g. input files or output files)
+ */
+typedef struct OptionGroupList {
+ const OptionGroupDef *group_def;
+
+ OptionGroup *groups;
+ int nb_groups;
+} OptionGroupList;
+
+typedef struct OptionParseContext {
+ OptionGroup global_opts;
+
+ OptionGroupList *groups;
+ int nb_groups;
+
+ /* parsing state */
+ OptionGroup cur_group;
+} OptionParseContext;
+
+/**
+ * Parse an options group and write results into optctx.
+ *
+ * @param optctx an app-specific options context. NULL for global options group
+ */
+int parse_optgroup(void *optctx, OptionGroup *g);
+
+/**
+ * Split the commandline into an intermediate form convenient for further
+ * processing.
+ *
+ * The commandline is assumed to be composed of options which either belong to a
+ * group (those with OPT_SPEC, OPT_OFFSET or OPT_PERFILE) or are global
+ * (everything else).
+ *
+ * A group (defined by an OptionGroupDef struct) is a sequence of options
+ * terminated by either a group separator option (e.g. -i) or a parameter that
+ * is not an option (doesn't start with -). A group without a separator option
+ * must always be first in the supplied groups list.
+ *
+ * All options within the same group are stored in one OptionGroup struct in an
+ * OptionGroupList, all groups with the same group definition are stored in one
+ * OptionGroupList in OptionParseContext.groups. The order of group lists is the
+ * same as the order of group definitions.
+ */
+int split_commandline(OptionParseContext *octx, int argc, char *argv[],
+ const OptionDef *options,
+ const OptionGroupDef *groups, int nb_groups);
+
+/**
+ * Free all allocated memory in an OptionParseContext.
+ */
+void uninit_parse_context(OptionParseContext *octx);
+
+/**
+ * Find the '-loglevel' option in the command line args and apply it.
+ */
+void parse_loglevel(int argc, char **argv, const OptionDef *options);
+
+/**
+ * Return index of option opt in argv or 0 if not found.
+ */
+int locate_option(int argc, char **argv, const OptionDef *options,
+ const char *optname);
+
+/**
+ * Check if the given stream matches a stream specifier.
+ *
+ * @param s Corresponding format context.
+ * @param st Stream from s to be checked.
+ * @param spec A stream specifier of the [v|a|s|d]:[\<stream index\>] form.
+ *
+ * @return 1 if the stream matches, 0 if it doesn't, <0 on error
+ */
+int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec);
+
+/**
+ * Filter out options for given codec.
+ *
+ * Create a new options dictionary containing only the options from
+ * opts which apply to the codec with ID codec_id.
+ *
+ * @param opts dictionary to place options in
+ * @param codec_id ID of the codec that should be filtered for
+ * @param s Corresponding format context.
+ * @param st A stream from s for which the options should be filtered.
+ * @param codec The particular codec for which the options should be filtered.
+ * If null, the default one is looked up according to the codec id.
+ * @return a pointer to the created dictionary
+ */
+AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
+ AVFormatContext *s, AVStream *st, AVCodec *codec);
+
+/**
+ * Setup AVCodecContext options for avformat_find_stream_info().
+ *
+ * Create an array of dictionaries, one dictionary for each stream
+ * contained in s.
+ * Each dictionary will contain the options from codec_opts which can
+ * be applied to the corresponding stream codec context.
+ *
+ * @return pointer to the created array of dictionaries, NULL if it
+ * cannot be created
+ */
+AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
+ AVDictionary *codec_opts);
+
+/**
+ * Print an error message to stderr, indicating filename and a human
+ * readable description of the error code err.
+ *
+ * If strerror_r() is not available the use of this function in a
+ * multithreaded application may be unsafe.
+ *
+ * @see av_strerror()
+ */
+void print_error(const char *filename, int err);
+
+/**
+ * Print the program banner to stderr. The banner contents depend on the
+ * current version of the repository and of the libav* libraries used by
+ * the program.
+ */
+void show_banner(void);
+
+/**
+ * Print the version of the program to stdout. The version message
+ * depends on the current versions of the repository and of the libav*
+ * libraries.
+ */
+int show_version(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Print the license of the program to stdout. The license depends on
+ * the license of the libraries compiled into the program.
+ */
+int show_license(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Print a listing containing all the formats supported by the
+ * program.
+ */
+int show_formats(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Print a listing containing all the codecs supported by the
+ * program.
+ */
+int show_codecs(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Print a listing containing all the decoders supported by the
+ * program.
+ */
+int show_decoders(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Print a listing containing all the encoders supported by the
+ * program.
+ */
+int show_encoders(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Print a listing containing all the filters supported by the
+ * program.
+ */
+int show_filters(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Print a listing containing all the bit stream filters supported by the
+ * program.
+ */
+int show_bsfs(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Print a listing containing all the protocols supported by the
+ * program.
+ */
+int show_protocols(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Print a listing containing all the pixel formats supported by the
+ * program.
+ */
+int show_pix_fmts(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Print a listing containing all the sample formats supported by the
+ * program.
+ */
+int show_sample_fmts(void *optctx, const char *opt, const char *arg);
+
+/**
+ * Return a positive value if a line read from standard input
+ * starts with [yY], otherwise return 0.
+ */
+int read_yesno(void);
+
+typedef struct PtsCorrectionContext {
+ int64_t num_faulty_pts; /// Number of incorrect PTS values so far
+ int64_t num_faulty_dts; /// Number of incorrect DTS values so far
+ int64_t last_pts; /// PTS of the last frame
+ int64_t last_dts; /// DTS of the last frame
+} PtsCorrectionContext;
+
+/**
+ * Reset the state of the PtsCorrectionContext.
+ */
+void init_pts_correction(PtsCorrectionContext *ctx);
+
+/**
+ * Attempt to guess proper monotonic timestamps for decoded video frames
+ * which might have incorrect times. Input timestamps may wrap around, in
+ * which case the output will as well.
+ *
+ * @param ctx the PtsCorrectionContext carrying stream pts information
+ * @param pts the pts field of the decoded AVPacket, as passed through
+ * AVCodecContext.reordered_opaque
+ * @param dts the dts field of the decoded AVPacket
+ * @return one of the input values, may be AV_NOPTS_VALUE
+ */
+int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t pts, int64_t dts);
+
+/**
+ * Get a file corresponding to a preset file.
+ *
+ * If is_path is non-zero, look for the file in the path preset_name.
+ * Otherwise search for a file named arg.avpreset in the directories
+ * $AVCONV_DATADIR (if set), $HOME/.avconv, and in the datadir defined
+ * at configuration time, in that order. If no such file is found and
+ * codec_name is defined, then search for a file named
+ * codec_name-preset_name.avpreset in the above-mentioned directories.
+ *
+ * @param filename buffer where the name of the found filename is written
+ * @param filename_size size in bytes of the filename buffer
+ * @param preset_name name of the preset to search
+ * @param is_path tell if preset_name is a filename path
+ * @param codec_name name of the codec for which to look for the
+ * preset, may be NULL
+ */
+FILE *get_preset_file(char *filename, size_t filename_size,
+ const char *preset_name, int is_path, const char *codec_name);
+
+/**
+ * Realloc array to hold new_size elements of elem_size.
+ * Calls exit() on failure.
+ *
+ * @param array array to reallocate
+ * @param elem_size size in bytes of each element
+ * @param size new element count will be written here
+ * @param new_size number of elements to place in reallocated array
+ * @return reallocated array
+ */
+void *grow_array(void *array, int elem_size, int *size, int new_size);
+
+/**
+ * Get a string describing a media type.
+ */
+const char *media_type_string(enum AVMediaType media_type);
+
+#define GROW_ARRAY(array, nb_elems)\
+ array = grow_array(array, sizeof(*array), &nb_elems, nb_elems + 1)
+
+#define GET_PIX_FMT_NAME(pix_fmt)\
+ const char *name = av_get_pix_fmt_name(pix_fmt);
+
+#define GET_SAMPLE_FMT_NAME(sample_fmt)\
+ const char *name = av_get_sample_fmt_name(sample_fmt)
+
+#define GET_SAMPLE_RATE_NAME(rate)\
+ char name[16];\
+ snprintf(name, sizeof(name), "%d", rate);
+
+#define GET_CH_LAYOUT_NAME(ch_layout)\
+ char name[16];\
+ snprintf(name, sizeof(name), "0x%"PRIx64, ch_layout);
+
+#define GET_CH_LAYOUT_DESC(ch_layout)\
+ char name[128];\
+ av_get_channel_layout_string(name, sizeof(name), 0, ch_layout);
+
+#endif /* LIBAV_CMDUTILS_H */