AVFilterGraph *graph;
#endif
- int sws_flags;
+ int64_t sws_flags;
AVDictionary *opts;
+ int is_past_recording_time;
+ int stream_copy;
++ const char *attachment_filename;
} OutputStream;
-static OutputStream **output_streams_for_file[MAX_FILES] = { NULL };
-static int nb_output_streams_for_file[MAX_FILES] = { 0 };
-typedef struct InputStream {
- int file_index;
- AVStream *st;
- int discard; /* true if stream data should be discarded */
- int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
- AVCodec *dec;
+#if HAVE_TERMIOS_H
- int64_t start; /* time when read started */
- int64_t next_pts; /* synthetic pts for cases where pkt.pts
- is not defined */
- int64_t pts; /* current pts */
- PtsCorrectionContext pts_ctx;
- double ts_scale;
- int is_start; /* is 1 at the start and after a discontinuity */
- int showed_multi_packet_warning;
- int is_past_recording_time;
- AVDictionary *opts;
-} InputStream;
+/* init terminal so that we can grab keys */
+static struct termios oldtty;
+#endif
-typedef struct InputFile {
+typedef struct OutputFile {
AVFormatContext *ctx;
- int eof_reached; /* true if eof reached */
- int ist_index; /* index of first stream in ist_table */
- int buffer_size; /* current total buffer size */
- int64_t ts_offset;
- int nb_streams; /* nb streams we are aware of */
-} InputFile;
+ AVDictionary *opts;
+ int ost_index; /* index of the first stream in output_streams */
+ int64_t recording_time; /* desired length of the resulting file in microseconds */
+ int64_t start_time; /* start time in microseconds */
+ uint64_t limit_filesize;
+} OutputFile;
static InputStream *input_streams = NULL;
static int nb_input_streams = 0;
static InputFile *input_files = NULL;
static int nb_input_files = 0;
+static OutputStream *output_streams = NULL;
+static int nb_output_streams = 0;
+static OutputFile *output_files = NULL;
+static int nb_output_files = 0;
+
+typedef struct OptionsContext {
+ /* input/output options */
+ int64_t start_time;
+ const char *format;
+
+ SpecifierOpt *codec_names;
+ int nb_codec_names;
+ SpecifierOpt *audio_channels;
+ int nb_audio_channels;
+ SpecifierOpt *audio_sample_rate;
+ int nb_audio_sample_rate;
+ SpecifierOpt *rematrix_volume;
+ int nb_rematrix_volume;
+ SpecifierOpt *frame_rates;
+ int nb_frame_rates;
+ SpecifierOpt *frame_sizes;
+ int nb_frame_sizes;
+ SpecifierOpt *frame_pix_fmts;
+ int nb_frame_pix_fmts;
+
+ /* input options */
+ int64_t input_ts_offset;
+ int rate_emu;
+
+ SpecifierOpt *ts_scale;
+ int nb_ts_scale;
++ SpecifierOpt *dump_attachment;
++ int nb_dump_attachment;
+
+ /* output options */
+ StreamMap *stream_maps;
+ int nb_stream_maps;
+ /* first item specifies output metadata, second is input */
+ MetadataMap (*meta_data_maps)[2];
+ int nb_meta_data_maps;
+ int metadata_global_manual;
+ int metadata_streams_manual;
+ int metadata_chapters_manual;
++ const char **attachments;
++ int nb_attachments;
+
+ int chapters_input_file;
+
+ int64_t recording_time;
+ uint64_t limit_filesize;
+ float mux_preload;
+ float mux_max_delay;
+
+ int video_disable;
+ int audio_disable;
+ int subtitle_disable;
+ int data_disable;
+
+ /* indexed by output file stream index */
+ int *streamid_map;
+ int nb_streamid_map;
+
+ SpecifierOpt *metadata;
+ int nb_metadata;
+ SpecifierOpt *max_frames;
+ int nb_max_frames;
+ SpecifierOpt *bitstream_filters;
+ int nb_bitstream_filters;
+ SpecifierOpt *codec_tags;
+ int nb_codec_tags;
+ SpecifierOpt *sample_fmts;
+ int nb_sample_fmts;
+ SpecifierOpt *qscale;
+ int nb_qscale;
+ SpecifierOpt *forced_key_frames;
+ int nb_forced_key_frames;
+ SpecifierOpt *force_fps;
+ int nb_force_fps;
+ SpecifierOpt *frame_aspect_ratios;
+ int nb_frame_aspect_ratios;
+ SpecifierOpt *rc_overrides;
+ int nb_rc_overrides;
+ SpecifierOpt *intra_matrices;
+ int nb_intra_matrices;
+ SpecifierOpt *inter_matrices;
+ int nb_inter_matrices;
+ SpecifierOpt *top_field_first;
+ int nb_top_field_first;
+ SpecifierOpt *presets;
+ int nb_presets;
+#if CONFIG_AVFILTER
+ SpecifierOpt *filters;
+ int nb_filters;
+#endif
+} OptionsContext;
+
+#define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
+{\
+ int i, ret;\
+ for (i = 0; i < o->nb_ ## name; i++) {\
+ char *spec = o->name[i].specifier;\
+ if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0)\
+ outvar = o->name[i].u.type;\
+ else if (ret < 0)\
+ exit_program(1);\
+ }\
+}
+
+static void reset_options(OptionsContext *o, int is_input)
+{
+ const OptionDef *po = options;
+ OptionsContext bak= *o;
+
+ /* all OPT_SPEC and OPT_STRING can be freed in generic way */
+ while (po->name) {
+ void *dst = (uint8_t*)o + po->u.off;
+
+ if (po->flags & OPT_SPEC) {
+ SpecifierOpt **so = dst;
+ int i, *count = (int*)(so + 1);
+ for (i = 0; i < *count; i++) {
+ av_freep(&(*so)[i].specifier);
+ if (po->flags & OPT_STRING)
+ av_freep(&(*so)[i].u.str);
+ }
+ av_freep(so);
+ *count = 0;
+ } else if (po->flags & OPT_OFFSET && po->flags & OPT_STRING)
+ av_freep(dst);
+ po++;
+ }
+
+ av_freep(&o->stream_maps);
+ av_freep(&o->meta_data_maps);
+ av_freep(&o->streamid_map);
+
+ memset(o, 0, sizeof(*o));
+
+ if(is_input) o->recording_time = bak.recording_time;
+ else o->recording_time = INT64_MAX;
+ o->mux_max_delay = 0.7;
+ o->limit_filesize = UINT64_MAX;
+ o->chapters_input_file = INT_MAX;
+
+ uninit_opts();
+ init_opts();
+}
+
#if CONFIG_AVFILTER
static int configure_video_filters(InputStream *ist, OutputStream *ost)
}
/* for each output stream, we compute the right encoding parameters */
- for(i=0;i<nb_ostreams;i++) {
- ost = ost_table[i];
- os = output_files[ost->file_index];
+ for (i = 0; i < nb_output_streams; i++) {
+ ost = &output_streams[i];
+ os = output_files[ost->file_index].ctx;
ist = &input_streams[ost->source_index];
++ if (ost->attachment_filename)
++ continue;
++
codec = ost->st->codec;
icodec = ist->st->codec;
}
/* dump the stream mapping */
- if (verbose >= 0) {
- fprintf(stderr, "Stream mapping:\n");
- for(i=0;i<nb_ostreams;i++) {
- ost = ost_table[i];
- fprintf(stderr, " Stream #%d.%d -> #%d.%d",
- input_streams[ost->source_index].file_index,
- input_streams[ost->source_index].st->index,
- ost->file_index,
- ost->index);
- if (ost->sync_ist != &input_streams[ost->source_index])
- fprintf(stderr, " [sync #%d.%d]",
- ost->sync_ist->file_index,
- ost->sync_ist->st->index);
- fprintf(stderr, "\n");
+ av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
+ for (i = 0; i < nb_output_streams; i++) {
+ ost = &output_streams[i];
++
++ if (ost->attachment_filename) {
++ /* an attached file */
++ av_log(NULL, AV_LOG_INFO, " File %s -> Stream #%d:%d\n",
++ ost->attachment_filename, ost->file_index, ost->index);
++ continue;
+ }
+ av_log(NULL, AV_LOG_INFO, " Stream #%d.%d -> #%d.%d",
+ input_streams[ost->source_index].file_index,
+ input_streams[ost->source_index].st->index,
+ ost->file_index,
+ ost->index);
+ if (ost->sync_ist != &input_streams[ost->source_index])
+ av_log(NULL, AV_LOG_INFO, " [sync #%d.%d]",
+ ost->sync_ist->file_index,
+ ost->sync_ist->st->index);
+ if (ost->stream_copy)
+ av_log(NULL, AV_LOG_INFO, " (copy)");
+ else
+ av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index].dec ?
+ input_streams[ost->source_index].dec->name : "?",
+ ost->enc ? ost->enc->name : "?");
+ av_log(NULL, AV_LOG_INFO, "\n");
}
if (ret) {
ar = strtod(arg, NULL);
if (!ar) {
- fprintf(stderr, "Incorrect aspect ratio specification.\n");
- return AVERROR(EINVAL);
- }
- frame_aspect_ratio = ar;
- return 0;
-}
-
-static int opt_metadata(const char *opt, const char *arg)
-{
- char *mid= strchr(arg, '=');
-
- if(!mid){
- fprintf(stderr, "Missing =\n");
+ av_log(NULL, AV_LOG_FATAL, "Incorrect aspect ratio specification.\n");
exit_program(1);
}
- *mid++= 0;
-
- av_dict_set(&metadata, arg, mid, 0);
-
- return 0;
+ return ar;
}
-static int opt_qscale(const char *opt, const char *arg)
+static int opt_video_channel(const char *opt, const char *arg)
{
- video_qscale = parse_number_or_die(opt, arg, OPT_FLOAT, 0, 255);
- if (video_qscale == 0) {
- fprintf(stderr, "qscale must be > 0.0 and <= 255\n");
- return AVERROR(EINVAL);
- }
- return 0;
+ av_log(NULL, AV_LOG_WARNING, "This option is deprecated, use -channel.\n");
+ return opt_default("channel", arg);
}
-static int opt_top_field_first(const char *opt, const char *arg)
+static int opt_video_standard(const char *opt, const char *arg)
{
- top_field_first = parse_number_or_die(opt, arg, OPT_INT, 0, 1);
- return 0;
+ av_log(NULL, AV_LOG_WARNING, "This option is deprecated, use -standard.\n");
+ return opt_default("standard", arg);
}
-static int opt_thread_count(const char *opt, const char *arg)
+static int opt_audio_codec(OptionsContext *o, const char *opt, const char *arg)
{
- thread_count= parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
-#if !HAVE_THREADS
- if (verbose >= 0)
- fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
-#endif
- return 0;
+ audio_codec_name = arg;
+ return parse_option(o, "codec:a", arg, options);
}
-static int opt_audio_sample_fmt(const char *opt, const char *arg)
+static int opt_video_codec(OptionsContext *o, const char *opt, const char *arg)
{
- if (strcmp(arg, "list")) {
- audio_sample_fmt = av_get_sample_fmt(arg);
- if (audio_sample_fmt == AV_SAMPLE_FMT_NONE) {
- av_log(NULL, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
- return AVERROR(EINVAL);
- }
- } else {
- int i;
- char fmt_str[128];
- for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
- printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
- exit_program(0);
- }
- return 0;
+ video_codec_name = arg;
+ return parse_option(o, "codec:v", arg, options);
}
-static int opt_audio_rate(const char *opt, const char *arg)
+static int opt_subtitle_codec(OptionsContext *o, const char *opt, const char *arg)
{
- audio_sample_rate = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
- return 0;
+ subtitle_codec_name = arg;
+ return parse_option(o, "codec:s", arg, options);
}
-static int opt_audio_channels(const char *opt, const char *arg)
+static int opt_data_codec(OptionsContext *o, const char *opt, const char *arg)
{
- audio_channels = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
- return 0;
+ return parse_option(o, "codec:d", arg, options);
}
-static int opt_video_channel(const char *opt, const char *arg)
+static int opt_map(OptionsContext *o, const char *opt, const char *arg)
{
- av_log(NULL, AV_LOG_WARNING, "This option is deprecated, use -channel.\n");
- opt_default("channel", arg);
- return 0;
-}
+ StreamMap *m = NULL;
+ int i, negative = 0, file_idx;
+ int sync_file_idx = -1, sync_stream_idx;
+ char *p, *sync;
+ char *map;
+
+ if (*arg == '-') {
+ negative = 1;
+ arg++;
+ }
+ map = av_strdup(arg);
+
+ /* parse sync stream first, just pick first matching stream */
+ if (sync = strchr(map, ',')) {
+ *sync = 0;
+ sync_file_idx = strtol(sync + 1, &sync, 0);
+ if (sync_file_idx >= nb_input_files || sync_file_idx < 0) {
+ av_log(NULL, AV_LOG_FATAL, "Invalid sync file index: %d.\n", sync_file_idx);
+ exit_program(1);
+ }
+ if (*sync)
+ sync++;
+ for (i = 0; i < input_files[sync_file_idx].nb_streams; i++)
+ if (check_stream_specifier(input_files[sync_file_idx].ctx,
+ input_files[sync_file_idx].ctx->streams[i], sync) == 1) {
+ sync_stream_idx = i;
+ break;
+ }
+ if (i == input_files[sync_file_idx].nb_streams) {
+ av_log(NULL, AV_LOG_FATAL, "Sync stream specification in map %s does not "
+ "match any streams.\n", arg);
+ exit_program(1);
+ }
+ }
-static int opt_video_standard(const char *opt, const char *arg)
-{
- av_log(NULL, AV_LOG_WARNING, "This option is deprecated, use -standard.\n");
- opt_default("standard", arg);
- return 0;
-}
-static int opt_codec(int *pstream_copy, char **pcodec_name,
- int codec_type, const char *arg)
-{
- av_freep(pcodec_name);
- if (!strcmp(arg, "copy")) {
- *pstream_copy = 1;
- } else {
- *pcodec_name = av_strdup(arg);
+ file_idx = strtol(map, &p, 0);
+ if (file_idx >= nb_input_files || file_idx < 0) {
+ av_log(NULL, AV_LOG_FATAL, "Invalid input file index: %d.\n", file_idx);
+ exit_program(1);
}
- return 0;
-}
+ if (negative)
+ /* disable some already defined maps */
+ for (i = 0; i < o->nb_stream_maps; i++) {
+ m = &o->stream_maps[i];
- if (check_stream_specifier(input_files[m->file_index].ctx,
++ if (file_idx == m->file_index &&
++ check_stream_specifier(input_files[m->file_index].ctx,
+ input_files[m->file_index].ctx->streams[m->stream_index],
+ *p == ':' ? p + 1 : p) > 0)
+ m->disabled = 1;
+ }
+ else
+ for (i = 0; i < input_files[file_idx].nb_streams; i++) {
+ if (check_stream_specifier(input_files[file_idx].ctx, input_files[file_idx].ctx->streams[i],
+ *p == ':' ? p + 1 : p) <= 0)
+ continue;
+ o->stream_maps = grow_array(o->stream_maps, sizeof(*o->stream_maps),
+ &o->nb_stream_maps, o->nb_stream_maps + 1);
+ m = &o->stream_maps[o->nb_stream_maps - 1];
-static int opt_audio_codec(const char *opt, const char *arg)
-{
- return opt_codec(&audio_stream_copy, &audio_codec_name, AVMEDIA_TYPE_AUDIO, arg);
-}
+ m->file_index = file_idx;
+ m->stream_index = i;
-static int opt_video_codec(const char *opt, const char *arg)
-{
- return opt_codec(&video_stream_copy, &video_codec_name, AVMEDIA_TYPE_VIDEO, arg);
-}
+ if (sync_file_idx >= 0) {
+ m->sync_file_index = sync_file_idx;
+ m->sync_stream_index = sync_stream_idx;
+ } else {
+ m->sync_file_index = file_idx;
+ m->sync_stream_index = i;
+ }
+ }
-static int opt_subtitle_codec(const char *opt, const char *arg)
-{
- return opt_codec(&subtitle_stream_copy, &subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, arg);
+ if (!m) {
+ av_log(NULL, AV_LOG_FATAL, "Stream map '%s' matches no streams.\n", arg);
+ exit_program(1);
+ }
+
+ av_freep(&map);
+ return 0;
}
-static int opt_data_codec(const char *opt, const char *arg)
++static int opt_attach(OptionsContext *o, const char *opt, const char *arg)
+ {
- return opt_codec(&data_stream_copy, &data_codec_name, AVMEDIA_TYPE_DATA, arg);
++ o->attachments = grow_array(o->attachments, sizeof(*o->attachments),
++ &o->nb_attachments, o->nb_attachments + 1);
++ o->attachments[o->nb_attachments - 1] = arg;
++ return 0;
+ }
+
-static int opt_codec_tag(const char *opt, const char *arg)
+static void parse_meta_type(char *arg, char *type, int *index)
{
- char *tail;
- uint32_t *codec_tag;
-
- codec_tag = !strcmp(opt, "atag") ? &audio_codec_tag :
- !strcmp(opt, "vtag") ? &video_codec_tag :
- !strcmp(opt, "stag") ? &subtitle_codec_tag : NULL;
- if (!codec_tag)
- return -1;
-
- *codec_tag = strtol(arg, &tail, 0);
- if (!tail || *tail)
- *codec_tag = AV_RL32(arg);
-
- return 0;
-}
-
-static int opt_map(const char *opt, const char *arg)
-{
- StreamMap *m;
- char *p;
-
- stream_maps = grow_array(stream_maps, sizeof(*stream_maps), &nb_stream_maps, nb_stream_maps + 1);
- m = &stream_maps[nb_stream_maps-1];
-
- m->file_index = strtol(arg, &p, 0);
- if (*p)
- p++;
-
- m->stream_index = strtol(p, &p, 0);
- if (*p) {
- p++;
- m->sync_file_index = strtol(p, &p, 0);
- if (*p)
- p++;
- m->sync_stream_index = strtol(p, &p, 0);
- } else {
- m->sync_file_index = m->file_index;
- m->sync_stream_index = m->stream_index;
- }
- return 0;
-}
-
-static void parse_meta_type(char *arg, char *type, int *index, char **endptr)
-{
- *endptr = arg;
- if (*arg == ',') {
- *type = *(++arg);
+ if (*arg) {
+ *type = *arg;
switch (*arg) {
case 'g':
break;
return 0;
}
-static int opt_input_ts_scale(const char *opt, const char *arg)
+static AVCodec *find_codec_or_die(const char *name, enum AVMediaType type, int encoder)
{
- unsigned int stream;
- double scale;
- char *p;
-
- stream = strtol(arg, &p, 0);
- if (*p)
- p++;
- scale= strtod(p, &p);
+ const char *codec_string = encoder ? "encoder" : "decoder";
+ AVCodec *codec;
- ts_scale = grow_array(ts_scale, sizeof(*ts_scale), &nb_ts_scale, stream + 1);
- ts_scale[stream] = scale;
- return 0;
+ codec = encoder ?
+ avcodec_find_encoder_by_name(name) :
+ avcodec_find_decoder_by_name(name);
+ if(!codec) {
+ av_log(NULL, AV_LOG_FATAL, "Unknown %s '%s'\n", codec_string, name);
+ exit_program(1);
+ }
+ if(codec->type != type) {
+ av_log(NULL, AV_LOG_FATAL, "Invalid %s type '%s'\n", codec_string, name);
+ exit_program(1);
+ }
+ return codec;
}
-static int opt_recording_time(const char *opt, const char *arg)
+static AVCodec *choose_decoder(OptionsContext *o, AVFormatContext *s, AVStream *st)
{
- recording_time = parse_time_or_die(opt, arg, 1);
- return 0;
-}
+ char *codec_name = NULL;
-static int opt_start_time(const char *opt, const char *arg)
-{
- start_time = parse_time_or_die(opt, arg, 1);
- return 0;
+ MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
+ if (codec_name) {
+ AVCodec *codec = find_codec_or_die(codec_name, st->codec->codec_type, 0);
+ st->codec->codec_id = codec->id;
+ return codec;
+ } else
+ return avcodec_find_decoder(st->codec->codec_id);
}
-static int opt_recording_timestamp(const char *opt, const char *arg)
+/**
+ * Add all the streams from the given input file to the global
+ * list of input streams.
+ */
+static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
{
- char buf[128];
- int64_t recording_timestamp = parse_time_or_die(opt, arg, 0) / 1E6;
- struct tm time = *gmtime((time_t*)&recording_timestamp);
- strftime(buf, sizeof(buf), "creation_time=%FT%T%z", &time);
- opt_metadata("metadata", buf);
+ int i, rfps, rfps_base;
+ char *next, *codec_tag = NULL;
- av_log(NULL, AV_LOG_WARNING, "%s is deprecated, set the 'creation_time' metadata "
- "tag instead.\n", opt);
- return 0;
+ for (i = 0; i < ic->nb_streams; i++) {
+ AVStream *st = ic->streams[i];
+ AVCodecContext *dec = st->codec;
+ InputStream *ist;
+ double scale = 1.0;
+
+ input_streams = grow_array(input_streams, sizeof(*input_streams), &nb_input_streams, nb_input_streams + 1);
+ ist = &input_streams[nb_input_streams - 1];
+ ist->st = st;
+ ist->file_index = nb_input_files;
+ ist->discard = 1;
+ ist->opts = filter_codec_opts(codec_opts, ist->st->codec->codec_id, ic, st);
+
+ MATCH_PER_STREAM_OPT(ts_scale, dbl, scale, ic, st);
+ ist->ts_scale = scale;
+
+ MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
+ if (codec_tag) {
+ uint32_t tag = strtol(codec_tag, &next, 0);
+ if (*next)
+ tag = AV_RL32(codec_tag);
+ st->codec->codec_tag = tag;
+ }
+
+ ist->dec = choose_decoder(o, ic, st);
+
+ switch (dec->codec_type) {
+ case AVMEDIA_TYPE_AUDIO:
+ if(!ist->dec)
+ ist->dec = avcodec_find_decoder(dec->codec_id);
+ if(o->audio_disable)
+ st->discard= AVDISCARD_ALL;
+ break;
+ case AVMEDIA_TYPE_VIDEO:
+ if(!ist->dec)
+ ist->dec = avcodec_find_decoder(dec->codec_id);
+ rfps = ic->streams[i]->r_frame_rate.num;
+ rfps_base = ic->streams[i]->r_frame_rate.den;
+ if (dec->lowres) {
+ dec->flags |= CODEC_FLAG_EMU_EDGE;
+ }
+
+ if (dec->time_base.den != rfps*dec->ticks_per_frame || dec->time_base.num != rfps_base) {
+
+ av_log(NULL, AV_LOG_INFO,"\nSeems stream %d codec frame rate differs from container frame rate: %2.2f (%d/%d) -> %2.2f (%d/%d)\n",
+ i, (float)dec->time_base.den / dec->time_base.num, dec->time_base.den, dec->time_base.num,
+ (float)rfps / rfps_base, rfps, rfps_base);
+ }
+
+ if (o->video_disable)
+ st->discard= AVDISCARD_ALL;
+ else if(video_discard)
+ st->discard= video_discard;
+ break;
+ case AVMEDIA_TYPE_DATA:
+ break;
+ case AVMEDIA_TYPE_SUBTITLE:
+ if(!ist->dec)
+ ist->dec = avcodec_find_decoder(dec->codec_id);
+ if(o->subtitle_disable)
+ st->discard = AVDISCARD_ALL;
+ break;
+ case AVMEDIA_TYPE_ATTACHMENT:
+ case AVMEDIA_TYPE_UNKNOWN:
+ break;
+ default:
+ abort();
+ }
+ }
}
-static int opt_input_ts_offset(const char *opt, const char *arg)
++static void assert_file_overwrite(const char *filename)
+ {
- input_ts_offset = parse_time_or_die(opt, arg, 1);
- return 0;
++ if (!file_overwrite &&
++ (strchr(filename, ':') == NULL || filename[1] == ':' ||
++ av_strstart(filename, "file:", NULL))) {
++ if (avio_check(filename, 0) == 0) {
++ if (!using_stdin) {
++ fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
++ fflush(stderr);
++ term_exit();
++ if (!read_yesno()) {
++ av_log(0, AV_LOG_FATAL, "Not overwriting - exiting\n");
++ exit_program(1);
++ }
++ term_init();
++ }
++ else {
++ av_log(0, AV_LOG_FATAL, "File '%s' already exists. Exiting.\n", filename);
++ exit_program(1);
++ }
++ }
++ }
+ }
+
-static enum CodecID find_codec_or_die(const char *name, int type, int encoder)
++static void dump_attachment(AVStream *st, const char *filename)
+ {
- const char *codec_string = encoder ? "encoder" : "decoder";
- AVCodec *codec;
++ int ret;
++ AVIOContext *out = NULL;
++ AVDictionaryEntry *e;
+
- if(!name)
- return CODEC_ID_NONE;
- codec = encoder ?
- avcodec_find_encoder_by_name(name) :
- avcodec_find_decoder_by_name(name);
- if(!codec) {
- fprintf(stderr, "Unknown %s '%s'\n", codec_string, name);
++ if (!st->codec->extradata_size) {
++ av_log(NULL, AV_LOG_WARNING, "No extradata to dump in stream #%d:%d.\n",
++ nb_input_files - 1, st->index);
++ return;
++ }
++ if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
++ filename = e->value;
++ if (!*filename) {
++ av_log(NULL, AV_LOG_FATAL, "No filename specified and no 'filename' tag"
++ "in stream #%d:%d.\n", nb_input_files - 1, st->index);
+ exit_program(1);
+ }
- if(codec->type != type) {
- fprintf(stderr, "Invalid %s type '%s'\n", codec_string, name);
++
++ assert_file_overwrite(filename);
++
++ if ((ret = avio_open (&out, filename, AVIO_FLAG_WRITE)) < 0) {
++ av_log(NULL, AV_LOG_FATAL, "Could not open file %s for writing.\n",
++ filename);
+ exit_program(1);
+ }
- return codec->id;
++
++ avio_write(out, st->codec->extradata, st->codec->extradata_size);
++ avio_flush(out);
++ avio_close(out);
+ }
+
-static int opt_input_file(const char *opt, const char *filename)
+static int opt_input_file(OptionsContext *o, const char *opt, const char *filename)
{
AVFormatContext *ic;
AVInputFormat *file_iformat = NULL;
input_files = grow_array(input_files, sizeof(*input_files), &nb_input_files, nb_input_files + 1);
input_files[nb_input_files - 1].ctx = ic;
input_files[nb_input_files - 1].ist_index = nb_input_streams - ic->nb_streams;
- input_files[nb_input_files - 1].ts_offset = input_ts_offset - (copy_ts ? 0 : timestamp);
+ input_files[nb_input_files - 1].ts_offset = o->input_ts_offset - (copy_ts ? 0 : timestamp);
input_files[nb_input_files - 1].nb_streams = ic->nb_streams;
+ input_files[nb_input_files - 1].rate_emu = o->rate_emu;
- frame_rate = (AVRational){0, 0};
- frame_pix_fmt = PIX_FMT_NONE;
- frame_height = 0;
- frame_width = 0;
- audio_sample_rate = 0;
- audio_channels = 0;
- audio_sample_fmt = AV_SAMPLE_FMT_NONE;
- av_freep(&ts_scale);
- nb_ts_scale = 0;
++ for (i = 0; i < o->nb_dump_attachment; i++) {
++ int j;
++
++ for (j = 0; j < ic->nb_streams; j++) {
++ AVStream *st = ic->streams[j];
++
++ if (check_stream_specifier(ic, st, o->dump_attachment[i].specifier) == 1)
++ dump_attachment(st, o->dump_attachment[i].u.str);
++ }
++ }
+
for (i = 0; i < orig_nb_streams; i++)
av_dict_free(&opts[i]);
av_freep(&opts);
}
}
- oc->oformat = file_oformat;
- av_strlcpy(oc->filename, filename, sizeof(oc->filename));
++ /* handle attached files */
++ for (i = 0; i < o->nb_attachments; i++) {
++ AVIOContext *pb;
++ uint8_t *attachment;
++ const char *p;
++ int64_t len;
+
- if (!strcmp(file_oformat->name, "ffm") &&
- av_strstart(filename, "http:", NULL)) {
- /* special case for files sent to avserver: we get the stream
- parameters from avserver */
- int err = read_avserver_streams(oc, filename);
- if (err < 0) {
- print_error(filename, err);
++ if ((err = avio_open(&pb, o->attachments[i], AVIO_FLAG_READ)) < 0) {
++ av_log(NULL, AV_LOG_FATAL, "Could not open attachment file %s.\n",
++ o->attachments[i]);
+ exit_program(1);
+ }
- } else {
- use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;
- use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;
- use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;
- use_data = data_stream_copy || data_codec_name; /* XXX once generic data codec will be available add a ->data_codec reference and use it here */
-
- /* disable if no corresponding type found */
- check_inputs(&input_has_video,
- &input_has_audio,
- &input_has_subtitle,
- &input_has_data);
-
- if (!input_has_video)
- use_video = 0;
- if (!input_has_audio)
- use_audio = 0;
- if (!input_has_subtitle)
- use_subtitle = 0;
- if (!input_has_data)
- use_data = 0;
-
- /* manual disable */
- if (audio_disable) use_audio = 0;
- if (video_disable) use_video = 0;
- if (subtitle_disable) use_subtitle = 0;
- if (data_disable) use_data = 0;
-
- if (use_video) new_video_stream(oc, nb_output_files);
- if (use_audio) new_audio_stream(oc, nb_output_files);
- if (use_subtitle) new_subtitle_stream(oc, nb_output_files);
- if (use_data) new_data_stream(oc, nb_output_files);
-
- av_dict_copy(&oc->metadata, metadata, 0);
- av_dict_free(&metadata);
++ if ((len = avio_size(pb)) <= 0) {
++ av_log(NULL, AV_LOG_FATAL, "Could not get size of the attachment %s.\n",
++ o->attachments[i]);
++ exit_program(1);
++ }
++ if (!(attachment = av_malloc(len))) {
++ av_log(NULL, AV_LOG_FATAL, "Attachment %s too large to fit into memory.\n",
++ o->attachments[i]);
++ exit_program(1);
++ }
++ avio_read(pb, attachment, len);
++
++ ost = new_attachment_stream(o, oc);
++ ost->stream_copy = 0;
++ ost->source_index = -1;
++ ost->attachment_filename = o->attachments[i];
++ ost->st->codec->extradata = attachment;
++ ost->st->codec->extradata_size = len;
++
++ p = strrchr(o->attachments[i], '/');
++ av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE);
++ avio_close(pb);
+ }
+
- av_dict_copy(&output_opts[nb_output_files], format_opts, 0);
- output_files[nb_output_files++] = oc;
+ output_files = grow_array(output_files, sizeof(*output_files), &nb_output_files, nb_output_files + 1);
+ output_files[nb_output_files - 1].ctx = oc;
+ output_files[nb_output_files - 1].ost_index = nb_output_streams - oc->nb_streams;
+ output_files[nb_output_files - 1].recording_time = o->recording_time;
+ output_files[nb_output_files - 1].start_time = o->start_time;
+ output_files[nb_output_files - 1].limit_filesize = o->limit_filesize;
+ av_dict_copy(&output_files[nb_output_files - 1].opts, format_opts, 0);
/* check filename in case of an image number is expected */
if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
if (!(oc->oformat->flags & AVFMT_NOFILE)) {
/* test if it already exists to avoid loosing precious files */
-- if (!file_overwrite &&
-- (strchr(filename, ':') == NULL ||
-- filename[1] == ':' ||
-- av_strstart(filename, "file:", NULL))) {
-- if (avio_check(filename, 0) == 0) {
-- if (!using_stdin) {
-- fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
-- fflush(stderr);
- term_exit();
-- if (!read_yesno()) {
- av_log(0, AV_LOG_FATAL, "Not overwriting - exiting\n");
- fprintf(stderr, "Not overwriting - exiting\n");
-- exit_program(1);
-- }
- term_init();
-- }
-- else {
- av_log(0, AV_LOG_FATAL,"File '%s' already exists. Exiting.\n", filename);
- fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
-- exit_program(1);
-- }
-- }
-- }
++ assert_file_overwrite(filename);
/* open the file */
if ((err = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE)) < 0) {
av_log(NULL, AV_LOG_WARNING, "-loop_output is deprecated, use -loop\n");
oc->loop_output = loop_output;
}
- oc->flags |= AVFMT_FLAG_NONBLOCK;
- frame_rate = (AVRational){0, 0};
- frame_width = 0;
- frame_height = 0;
- audio_sample_rate = 0;
- audio_channels = 0;
- audio_sample_fmt = AV_SAMPLE_FMT_NONE;
+ /* copy chapters */
+ if (o->chapters_input_file >= nb_input_files) {
+ if (o->chapters_input_file == INT_MAX) {
+ /* copy chapters from the first input file that has them*/
+ o->chapters_input_file = -1;
+ for (i = 0; i < nb_input_files; i++)
+ if (input_files[i].ctx->nb_chapters) {
+ o->chapters_input_file = i;
+ break;
+ }
+ } else {
+ av_log(NULL, AV_LOG_FATAL, "Invalid input file index %d in chapter mapping.\n",
+ o->chapters_input_file);
+ exit_program(1);
+ }
+ }
+ if (o->chapters_input_file >= 0)
+ copy_chapters(&input_files[o->chapters_input_file], &output_files[nb_output_files - 1],
+ !o->metadata_chapters_manual);
+
+ /* copy metadata */
+ for (i = 0; i < o->nb_meta_data_maps; i++) {
+ AVFormatContext *files[2];
+ AVDictionary **meta[2];
+ int j;
- av_freep(&forced_key_frames);
- uninit_opts();
- init_opts();
+#define METADATA_CHECK_INDEX(index, nb_elems, desc)\
+ if ((index) < 0 || (index) >= (nb_elems)) {\
+ av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps\n",\
+ (desc), (index));\
+ exit_program(1);\
+ }
+
+ int in_file_index = o->meta_data_maps[i][1].file;
+ if (in_file_index < 0)
+ continue;
+ METADATA_CHECK_INDEX(in_file_index, nb_input_files, "input file")
+
+ files[0] = oc;
+ files[1] = input_files[in_file_index].ctx;
+
+ for (j = 0; j < 2; j++) {
+ MetadataMap *map = &o->meta_data_maps[i][j];
+
+ switch (map->type) {
+ case 'g':
+ meta[j] = &files[j]->metadata;
+ break;
+ case 's':
+ METADATA_CHECK_INDEX(map->index, files[j]->nb_streams, "stream")
+ meta[j] = &files[j]->streams[map->index]->metadata;
+ break;
+ case 'c':
+ METADATA_CHECK_INDEX(map->index, files[j]->nb_chapters, "chapter")
+ meta[j] = &files[j]->chapters[map->index]->metadata;
+ break;
+ case 'p':
+ METADATA_CHECK_INDEX(map->index, files[j]->nb_programs, "program")
+ meta[j] = &files[j]->programs[map->index]->metadata;
+ break;
+ default:
+ abort();
+ }
+ }
+
+ av_dict_copy(meta[0], *meta[1], AV_DICT_DONT_OVERWRITE);
+ }
+
+ /* copy global metadata by default */
+ if (!o->metadata_global_manual && nb_input_files){
+ av_dict_copy(&oc->metadata, input_files[0].ctx->metadata,
+ AV_DICT_DONT_OVERWRITE);
+ if(o->recording_time != INT64_MAX)
+ av_dict_set(&oc->metadata, "duration", NULL, 0);
+ }
+ if (!o->metadata_streams_manual)
+ for (i = output_files[nb_output_files - 1].ost_index; i < nb_output_streams; i++) {
- InputStream *ist = &input_streams[output_streams[i].source_index];
++ InputStream *ist;
++ if (output_streams[i].source_index < 0) /* this is true e.g. for attached files */
++ continue;
++ ist = &input_streams[output_streams[i].source_index];
+ av_dict_copy(&output_streams[i].st->metadata, ist->st->metadata, AV_DICT_DONT_OVERWRITE);
+ }
+
+ /* process manually set metadata */
+ for (i = 0; i < o->nb_metadata; i++) {
+ AVDictionary **m;
+ char type, *val;
+ int index = 0;
+
+ val = strchr(o->metadata[i].u.str, '=');
+ if (!val) {
+ av_log(NULL, AV_LOG_FATAL, "No '=' character in metadata string %s.\n",
+ o->metadata[i].u.str);
+ exit_program(1);
+ }
+ *val++ = 0;
+
+ parse_meta_type(o->metadata[i].specifier, &type, &index);
+ switch (type) {
+ case 'g':
+ m = &oc->metadata;
+ break;
+ case 's':
+ if (index < 0 || index >= oc->nb_streams) {
+ av_log(NULL, AV_LOG_FATAL, "Invalid stream index %d in metadata specifier.\n", index);
+ exit_program(1);
+ }
+ m = &oc->streams[index]->metadata;
+ break;
+ case 'c':
+ if (index < 0 || index >= oc->nb_chapters) {
+ av_log(NULL, AV_LOG_FATAL, "Invalid chapter index %d in metadata specifier.\n", index);
+ exit_program(1);
+ }
+ m = &oc->chapters[index]->metadata;
+ break;
+ default:
+ av_log(NULL, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", o->metadata[i].specifier);
+ exit_program(1);
+ }
+
+ av_dict_set(m, o->metadata[i].u.str, *val ? val : NULL, 0);
+ }
+
+ reset_options(o, 0);
}
/* same option as mencoder */
{ "copytb", OPT_BOOL | OPT_EXPERT, {(void*)©_tb}, "copy input stream time base when stream copying" },
{ "shortest", OPT_BOOL | OPT_EXPERT, {(void*)&opt_shortest}, "finish encoding within shortest input" }, //
{ "dts_delta_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, {(void*)&dts_delta_threshold}, "timestamp discontinuity delta threshold", "threshold" },
- { "programid", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&opt_programid}, "desired program number", "" },
{ "xerror", OPT_BOOL, {(void*)&exit_on_error}, "exit on error", "error" },
{ "copyinkf", OPT_BOOL | OPT_EXPERT, {(void*)©_initial_nonkeyframes}, "copy initial non-keyframes" },
+ { "frames", OPT_INT64 | HAS_ARG | OPT_SPEC, {.off = OFFSET(max_frames)}, "set the number of frames to record", "number" },
+ { "tag", OPT_STRING | HAS_ARG | OPT_SPEC, {.off = OFFSET(codec_tags)}, "force codec tag/fourcc", "fourcc/tag" },
+ { "q", HAS_ARG | OPT_EXPERT | OPT_DOUBLE | OPT_SPEC, {.off = OFFSET(qscale)}, "use fixed quality scale (VBR)", "q" },
+ { "qscale", HAS_ARG | OPT_EXPERT | OPT_DOUBLE | OPT_SPEC, {.off = OFFSET(qscale)}, "use fixed quality scale (VBR)", "q" },
+#if CONFIG_AVFILTER
+ { "filter", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(filters)}, "set stream filterchain", "filter_list" },
+#endif
+ { "stats", OPT_BOOL, {&print_stats}, "print progress report during encoding", },
++ { "attach", HAS_ARG | OPT_FUNC2, {(void*)opt_attach}, "add an attachment to the output file", "filename" },
++ { "dump_attachment", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(dump_attachment)}, "extract an attachment into a file", "filename" },
/* video options */
- { "vframes", OPT_INT | HAS_ARG | OPT_VIDEO, {(void*)&max_frames[AVMEDIA_TYPE_VIDEO]}, "set the number of video frames to record", "number" },
- { "r", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_rate}, "set frame rate (Hz value, fraction or abbreviation)", "rate" },
- { "s", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
- { "aspect", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_aspect_ratio}, "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
- { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_frame_pix_fmt}, "set pixel format, 'list' as argument shows all the pixel formats supported", "format" },
- { "croptop", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Removed, use the crop filter instead", "size" },
+ { "vframes", HAS_ARG | OPT_VIDEO | OPT_FUNC2, {(void*)opt_video_frames}, "set the number of video frames to record", "number" },
+ { "r", HAS_ARG | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_rates)}, "set frame rate (Hz value, fraction or abbreviation)", "rate" },
+ { "s", HAS_ARG | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_sizes)}, "set frame size (WxH or abbreviation)", "size" },
+ { "aspect", HAS_ARG | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_aspect_ratios)}, "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
+ { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_pix_fmts)}, "set pixel format", "format" },
+ { "bits_per_raw_sample", OPT_INT | HAS_ARG | OPT_VIDEO, {(void*)&frame_bits_per_raw_sample}, "set the number of bits per raw sample", "number" },
+ { "croptop", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Removed, use the crop filter instead", "size" },
{ "cropbottom", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Removed, use the crop filter instead", "size" },
{ "cropleft", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Removed, use the crop filter instead", "size" },
{ "cropright", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Removed, use the crop filter instead", "size" },