X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=avconv.c;h=974d464e34806b5ac94f635d27fdf7a07c846b5c;hb=231a6df9eaf438a7d3dc802fce8c094d979796e8;hp=0db47a847f607e79c143b0533dbe843f03e58cd2;hpb=d4863fc1a83ceab1d75469b406a2c67e5659b2a0;p=ffmpeg diff --git a/avconv.c b/avconv.c index 0db47a847f6..974d464e348 100644 --- a/avconv.c +++ b/avconv.c @@ -2,20 +2,20 @@ * avconv main * Copyright (c) 2000-2011 The libav developers. * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg 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, + * FFmpeg 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 + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -46,9 +46,13 @@ #include "libavutil/libm.h" #include "libavformat/os_support.h" +#include "libavformat/ffm.h" // not public API + #if CONFIG_AVFILTER +# include "libavfilter/avcodec.h" # include "libavfilter/avfilter.h" # include "libavfilter/avfiltergraph.h" +# include "libavfilter/vsink_buffer.h" # include "libavfilter/vsrc_buffer.h" #endif @@ -68,6 +72,14 @@ #include #endif +#if HAVE_TERMIOS_H +#include +#include +#include +#include +#elif HAVE_KBHIT +#include +#endif #include #include "cmdutils.h" @@ -79,6 +91,7 @@ const int program_birth_year = 2000; /* 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; @@ -89,15 +102,15 @@ typedef struct 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 + 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; static const OptionDef options[]; #define MAX_FILES 100 - +#define MAX_STREAMS 1024 /* arbitrary sanity check value */ static const char *last_asked_format = NULL; static double *ts_scale; static int nb_ts_scale; @@ -126,6 +139,7 @@ static int frame_width = 0; static int frame_height = 0; static float frame_aspect_ratio = 0; static enum PixelFormat frame_pix_fmt = PIX_FMT_NONE; +static int frame_bits_per_raw_sample = 0; static enum AVSampleFormat audio_sample_fmt = AV_SAMPLE_FMT_NONE; static int max_frames[4] = {INT_MAX, INT_MAX, INT_MAX, INT_MAX}; static AVRational frame_rate; @@ -180,7 +194,7 @@ static int do_hex_dump = 0; static int do_pkt_dump = 0; static int do_psnr = 0; static int do_pass = 0; -static char *pass_logfilename_prefix = NULL; +static const char *pass_logfilename_prefix; static int audio_stream_copy = 0; static int video_stream_copy = 0; static int subtitle_stream_copy = 0; @@ -189,7 +203,7 @@ static int video_sync_method= -1; static int audio_sync_method= 0; static float audio_drift_threshold= 0.1; static int copy_ts= 0; -static int copy_tb; +static int copy_tb= 0; static int opt_shortest = 0; static char *vstats_filename; static FILE *vstats_file; @@ -203,7 +217,9 @@ static int audio_volume = 256; static int exit_on_error = 0; static int using_stdin = 0; static int verbose = 1; +static int run_as_daemon = 0; static int thread_count= 1; +static int q_pressed = 0; static int64_t video_size = 0; static int64_t audio_size = 0; static int64_t extra_size = 0; @@ -249,7 +265,7 @@ typedef struct OutputStream { /* video only */ int video_resample; - AVFrame pict_tmp; /* temporary image for resampling */ + AVFrame resample_frame; /* temporary frame for image resampling */ struct SwsContext *img_resample_ctx; /* for image resampling */ int resample_height; int resample_width; @@ -300,7 +316,6 @@ typedef struct InputStream { 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; @@ -313,9 +328,16 @@ typedef struct InputFile { int eof_reached; /* true if eof reached */ int ist_index; /* index of first stream in ist_table */ int buffer_size; /* current total buffer size */ + int nb_streams; int64_t ts_offset; } InputFile; +#if HAVE_TERMIOS_H + +/* init terminal so that we can grab keys */ +static struct termios oldtty; +#endif + static InputStream *input_streams = NULL; static int nb_input_streams = 0; static InputFile *input_files = NULL; @@ -329,7 +351,7 @@ static int configure_video_filters(InputStream *ist, OutputStream *ost) /** filter graph containing all filters including input & output */ AVCodecContext *codec = ost->st->codec; AVCodecContext *icodec = ist->st->codec; - FFSinkContext ffsink_ctx = { .pix_fmt = codec->pix_fmt }; + enum PixelFormat pix_fmts[] = { codec->pix_fmt, PIX_FMT_NONE }; AVRational sample_aspect_ratio; char args[255]; int ret; @@ -349,8 +371,8 @@ static int configure_video_filters(InputStream *ist, OutputStream *ost) "src", args, NULL, ost->graph); if (ret < 0) return ret; - ret = avfilter_graph_create_filter(&ost->output_video_filter, &ffsink, - "out", NULL, &ffsink_ctx, ost->graph); + ret = avfilter_graph_create_filter(&ost->output_video_filter, avfilter_get_by_name("buffersink"), + "out", NULL, pix_fmts, ost->graph); if (ret < 0) return ret; last_filter = ost->input_video_filter; @@ -372,8 +394,8 @@ static int configure_video_filters(InputStream *ist, OutputStream *ost) ost->graph->scale_sws_opts = av_strdup(args); if (ost->avfilter) { - AVFilterInOut *outputs = av_malloc(sizeof(AVFilterInOut)); - AVFilterInOut *inputs = av_malloc(sizeof(AVFilterInOut)); + AVFilterInOut *outputs = avfilter_inout_alloc(); + AVFilterInOut *inputs = avfilter_inout_alloc(); outputs->name = av_strdup("in"); outputs->filter_ctx = last_filter; @@ -385,7 +407,7 @@ static int configure_video_filters(InputStream *ist, OutputStream *ost) inputs->pad_idx = 0; inputs->next = NULL; - if ((ret = avfilter_graph_parse(ost->graph, ost->avfilter, inputs, outputs, NULL)) < 0) + if ((ret = avfilter_graph_parse(ost->graph, ost->avfilter, &inputs, &outputs, NULL)) < 0) return ret; av_freep(&ost->avfilter); } else { @@ -409,22 +431,47 @@ static int configure_video_filters(InputStream *ist, OutputStream *ost) static void term_exit(void) { - av_log(NULL, AV_LOG_QUIET, ""); + av_log(NULL, AV_LOG_QUIET, "%s", ""); +#if HAVE_TERMIOS_H + if(!run_as_daemon) + tcsetattr (0, TCSANOW, &oldtty); +#endif } 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++; + q_pressed++; term_exit(); } static void term_init(void) { +#if HAVE_TERMIOS_H + if(!run_as_daemon){ + struct termios tty; + + tcgetattr (0, &tty); + oldtty = tty; + atexit(term_exit); + + tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP + |INLCR|IGNCR|ICRNL|IXON); + tty.c_oflag |= OPOST; + tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); + tty.c_cflag &= ~(CSIZE|PARENB); + tty.c_cflag |= CS8; + tty.c_cc[VMIN] = 1; + tty.c_cc[VTIME] = 0; + + tcsetattr (0, TCSANOW, &tty); + signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */ + } +#endif + signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */ signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */ #ifdef SIGXCPU @@ -432,9 +479,41 @@ static void term_init(void) #endif } +/* read a key without blocking */ +static int read_key(void) +{ +#if HAVE_TERMIOS_H + int n = 1; + unsigned char ch; + struct timeval tv; + fd_set rfds; + + if(run_as_daemon) + return -1; + + FD_ZERO(&rfds); + FD_SET(0, &rfds); + tv.tv_sec = 0; + tv.tv_usec = 0; + n = select(1, &rfds, NULL, NULL, &tv); + if (n > 0) { + n = read(0, &ch, 1); + if (n == 1) + return ch; + + return n; + } +#elif HAVE_KBHIT + if(kbhit()) + return(getch()); +#endif + return -1; +} + static int decode_interrupt_cb(void) { - return received_nb_signals > 1; + q_pressed += read_key() == 'q'; + return q_pressed > 1; } static int exit_program(int ret) @@ -464,7 +543,6 @@ static int exit_program(int ret) av_free(vstats_filename); av_free(streamid_map); - av_free(stream_maps); av_free(meta_data_maps); av_freep(&input_streams); @@ -551,6 +629,9 @@ static void choose_sample_fmt(AVStream *st, AVCodec *codec) break; } if (*p == -1) { + if((codec->capabilities & CODEC_CAP_LOSSLESS) && av_get_sample_fmt_name(st->codec->sample_fmt) > av_get_sample_fmt_name(codec->sample_fmts[0])) + av_log(NULL, AV_LOG_ERROR, "Convertion will not be lossless'\n"); + if(av_get_sample_fmt_name(st->codec->sample_fmt)) av_log(NULL, AV_LOG_WARNING, "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n", av_get_sample_fmt_name(st->codec->sample_fmt), @@ -561,46 +642,6 @@ static void choose_sample_fmt(AVStream *st, AVCodec *codec) } } -/** - * Update the requested input sample format based on the output sample format. - * This is currently only used to request float output from decoders which - * support multiple sample formats, one of which is AV_SAMPLE_FMT_FLT. - * Ideally this will be removed in the future when decoders do not do format - * conversion and only output in their native format. - */ -static void update_sample_fmt(AVCodecContext *dec, AVCodec *dec_codec, - AVCodecContext *enc) -{ - /* if sample formats match or a decoder sample format has already been - requested, just return */ - if (enc->sample_fmt == dec->sample_fmt || - dec->request_sample_fmt > AV_SAMPLE_FMT_NONE) - return; - - /* if decoder supports more than one output format */ - if (dec_codec && dec_codec->sample_fmts && - dec_codec->sample_fmts[0] != AV_SAMPLE_FMT_NONE && - dec_codec->sample_fmts[1] != AV_SAMPLE_FMT_NONE) { - const enum AVSampleFormat *p; - int min_dec = -1, min_inc = -1; - - /* find a matching sample format in the encoder */ - for (p = dec_codec->sample_fmts; *p != AV_SAMPLE_FMT_NONE; p++) { - if (*p == enc->sample_fmt) { - dec->request_sample_fmt = *p; - return; - } else if (*p > enc->sample_fmt) { - min_inc = FFMIN(min_inc, *p - enc->sample_fmt); - } else - min_dec = FFMIN(min_dec, enc->sample_fmt - *p); - } - - /* if none match, provide the one that matches quality closest */ - dec->request_sample_fmt = min_inc > 0 ? enc->sample_fmt + min_inc : - enc->sample_fmt - min_dec; - } -} - static void choose_sample_rate(AVStream *st, AVCodec *codec) { if(codec && codec->supported_samplerates){ @@ -685,7 +726,7 @@ static OutputStream *new_output_stream(AVFormatContext *oc, int file_idx, AVCode return ost; } -static int read_avserver_streams(AVFormatContext *s, const char *filename) +static int read_ffserver_streams(AVFormatContext *s, const char *filename) { int i, err; AVFormatContext *ic = NULL; @@ -706,7 +747,8 @@ static int read_avserver_streams(AVFormatContext *s, const char *filename) // FIXME: a more elegant solution is needed memcpy(st, ic->streams[i], sizeof(AVStream)); - st->info = NULL; + st->info = av_malloc(sizeof(*st->info)); + memcpy(st->info, ic->streams[i]->info, sizeof(*st->info)); avcodec_copy_context(st->codec, ic->streams[i]->codec); if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { @@ -810,7 +852,7 @@ need_realloc: exit_program(1); } - if (enc->channels != dec->channels || enc->sample_rate != dec->sample_rate) + if (enc->channels != dec->channels) ost->audio_resample = 1; resample_changed = ost->resample_sample_fmt != dec->sample_fmt || @@ -836,7 +878,7 @@ need_realloc: ost->resample_sample_rate == enc->sample_rate) { ost->resample = NULL; ost->audio_resample = 0; - } else if (ost->audio_resample) { + } else { if (dec->sample_fmt != AV_SAMPLE_FMT_S16) fprintf(stderr, "Warning, using s16 intermediate sample format for resampling\n"); ost->resample = av_audio_resample_init(enc->channels, dec->channels, @@ -1128,7 +1170,7 @@ static void do_video_out(AVFormatContext *s, AVFrame *in_picture, int *frame_size, float quality) { - int nb_frames, i, ret, resample_changed; + int nb_frames, i, ret, av_unused resample_changed; AVFrame *final_picture, *formatted_picture; AVCodecContext *enc, *dec; double sync_ipts; @@ -1175,6 +1217,7 @@ static void do_video_out(AVFormatContext *s, formatted_picture = in_picture; final_picture = formatted_picture; +#if !CONFIG_AVFILTER resample_changed = ost->resample_width != dec->width || ost->resample_height != dec->height || ost->resample_pix_fmt != dec->pix_fmt; @@ -1185,24 +1228,32 @@ static void do_video_out(AVFormatContext *s, ist->file_index, ist->st->index, ost->resample_width, ost->resample_height, av_get_pix_fmt_name(ost->resample_pix_fmt), dec->width , dec->height , av_get_pix_fmt_name(dec->pix_fmt)); - if(!ost->video_resample) - exit_program(1); + ost->resample_width = dec->width; + ost->resample_height = dec->height; + ost->resample_pix_fmt = dec->pix_fmt; } -#if !CONFIG_AVFILTER + ost->video_resample = dec->width != enc->width || + dec->height != enc->height || + dec->pix_fmt != enc->pix_fmt; + if (ost->video_resample) { - final_picture = &ost->pict_tmp; - if (resample_changed) { + final_picture = &ost->resample_frame; + if (!ost->img_resample_ctx || resample_changed) { + /* initialize the destination picture */ + if (!ost->resample_frame.data[0]) { + avcodec_get_frame_defaults(&ost->resample_frame); + if (avpicture_alloc((AVPicture *)&ost->resample_frame, enc->pix_fmt, + enc->width, enc->height)) { + fprintf(stderr, "Cannot allocate temp picture, check pix fmt\n"); + exit_program(1); + } + } /* initialize a new scaler context */ sws_freeContext(ost->img_resample_ctx); - ost->img_resample_ctx = sws_getContext( - ist->st->codec->width, - ist->st->codec->height, - ist->st->codec->pix_fmt, - ost->st->codec->width, - ost->st->codec->height, - ost->st->codec->pix_fmt, - ost->sws_flags, NULL, NULL, NULL); + ost->img_resample_ctx = sws_getContext(dec->width, dec->height, dec->pix_fmt, + enc->width, enc->height, enc->pix_fmt, + ost->sws_flags, NULL, NULL, NULL); if (ost->img_resample_ctx == NULL) { fprintf(stderr, "Cannot get resampling context\n"); exit_program(1); @@ -1346,7 +1397,8 @@ static void print_report(AVFormatContext **output_files, int64_t total_size; AVCodecContext *enc; int frame_number, vid, i; - double bitrate, ti1, pts; + double bitrate; + int64_t pts = INT64_MAX; static int64_t last_time = -1; static int qp_histogram[52]; @@ -1371,7 +1423,6 @@ static void print_report(AVFormatContext **output_files, total_size= avio_tell(oc->pb); buf[0] = '\0'; - ti1 = 1e10; vid = 0; for(i=0;ist->pts.val * av_q2d(ost->st->time_base); - if ((pts < ti1) && (pts > 0)) - ti1 = pts; + pts = FFMIN(pts, av_rescale_q(ost->st->pts.val, + ost->st->time_base, AV_TIME_BASE_Q)); } - if (ti1 < 0.01) - ti1 = 0.01; if (verbose > 0 || is_last_report) { - bitrate = (double)(total_size * 8) / ti1 / 1000.0; + int hours, mins, secs, us; + secs = pts / AV_TIME_BASE; + us = pts % AV_TIME_BASE; + mins = secs / 60; + secs %= 60; + hours = mins / 60; + mins %= 60; + + bitrate = pts ? total_size * 8 / (pts / 1000.0) : 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); + "size=%8.0fkB time=", total_size / 1024.0); + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), + "%02d:%02d:%02d.%02d ", hours, mins, secs, + (100 * us) / AV_TIME_BASE); + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), + "bitrate=%6.1fkbits/s", bitrate); if (nb_frames_dup || nb_frames_drop) snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d", @@ -1571,7 +1631,7 @@ static int output_packet(InputStream *ist, int ist_index, /* no picture yet */ goto discard_packet; } - ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, picture.pkt_pts, picture.pkt_dts); + ist->next_pts = ist->pts = picture.best_effort_timestamp; if (ist->st->codec->time_base.num != 0) { int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame; ist->next_pts += ((int64_t)AV_TIME_BASE * @@ -1616,19 +1676,16 @@ static int output_packet(InputStream *ist, int ist_index, } #if CONFIG_AVFILTER - if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { - for (i = 0; i < nb_ostreams; i++) { + if(ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) + if (start_time == 0 || ist->pts >= start_time) { + for(i=0;iinput_video_filter && ost->source_index == ist_index) { - AVRational sar; - if (ist->st->sample_aspect_ratio.num) - sar = ist->st->sample_aspect_ratio; - else - sar = ist->st->codec->sample_aspect_ratio; - // add it to be filtered - av_vsrc_buffer_add_frame(ost->input_video_filter, &picture, - ist->pts, - sar); + if (!picture.sample_aspect_ratio.num) + picture.sample_aspect_ratio = ist->st->sample_aspect_ratio; + picture.pts = ist->pts; + + av_vsrc_buffer_add_frame(ost->input_video_filter, &picture, AV_VSRC_BUF_FLAG_OVERWRITE); } } } @@ -1667,11 +1724,15 @@ static int output_packet(InputStream *ist, int ist_index, frame_available = ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO || !ost->output_video_filter || avfilter_poll_frame(ost->output_video_filter->inputs[0]); while (frame_available) { - AVRational ist_pts_tb; - if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter) - get_filtered_video_frame(ost->output_video_filter, &picture, &ost->picref, &ist_pts_tb); - if (ost->picref) - ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q); + if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter) { + AVRational ist_pts_tb = ost->output_video_filter->inputs[0]->time_base; + if (av_vsink_buffer_get_video_buffer_ref(ost->output_video_filter, &ost->picref, 0) < 0) + goto cont; + if (ost->picref) { + avfilter_fill_frame_from_video_buffer_ref(&picture, ost->picref); + ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q); + } + } #endif os = output_files[ost->file_index]; @@ -1687,7 +1748,7 @@ static int output_packet(InputStream *ist, int ist_index, case AVMEDIA_TYPE_VIDEO: #if CONFIG_AVFILTER if (ost->picref->video && !ost->frame_aspect_ratio) - ost->st->codec->sample_aspect_ratio = ost->picref->video->pixel_aspect; + ost->st->codec->sample_aspect_ratio = ost->picref->video->sample_aspect_ratio; #endif do_video_out(os, ost, ist, &picture, &frame_size, same_quality ? quality : ost->st->codec->global_quality); @@ -1703,6 +1764,7 @@ static int output_packet(InputStream *ist, int ist_index, } } else { AVFrame avframe; //FIXME/XXX remove this + AVPicture pict; AVPacket opkt; int64_t ost_tb_start_time= av_rescale_q(start_time, AV_TIME_BASE_Q, ost->st->time_base); @@ -1756,6 +1818,13 @@ static int output_packet(InputStream *ist, int ist_index, opkt.size = data_size; } + if (os->oformat->flags & AVFMT_RAWPICTURE) { + /* store AVPicture in AVPacket, as expected by the output format */ + avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height); + opkt.data = (uint8_t *)&pict; + opkt.size = sizeof(AVPicture); + opkt.flags |= AV_PKT_FLAG_KEY; + } write_frame(os, &opkt, ost->st->codec, ost->bitstream_filters); ost->st->codec->frame_number++; ost->frame_number++; @@ -1765,8 +1834,7 @@ static int output_packet(InputStream *ist, int ist_index, cont: frame_available = (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) && ost->output_video_filter && avfilter_poll_frame(ost->output_video_filter->inputs[0]); - if (ost->picref) - avfilter_unref_buffer(ost->picref); + avfilter_unref_buffer(ost->picref); } #endif } @@ -1946,18 +2014,21 @@ static void parse_forced_key_frames(char *kf, OutputStream *ost, static int transcode(AVFormatContext **output_files, int nb_output_files, InputFile *input_files, - int nb_input_files, - StreamMap *stream_maps, int nb_stream_maps) + int nb_input_files) { - int ret = 0, i, j, k, n, nb_ostreams = 0; + int ret = 0, i, j, k, n, nb_ostreams = 0, step; + AVFormatContext *is, *os; AVCodecContext *codec, *icodec; OutputStream *ost, **ost_table = NULL; InputStream *ist; char error[1024]; + int key; int want_sdp = 1; uint8_t no_packet[MAX_FILES]={0}; int no_packet_count=0; + int nb_frame_threshold[AVMEDIA_TYPE_NB]={0}; + int nb_streams[AVMEDIA_TYPE_NB]={0}; if (rate_emu) for (i = 0; i < nb_input_streams; i++) @@ -1975,113 +2046,52 @@ static int transcode(AVFormatContext **output_files, } nb_ostreams += os->nb_streams; } - if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) { - fprintf(stderr, "Number of stream maps must match number of output streams\n"); - ret = AVERROR(EINVAL); - goto fail; - } - - /* Sanity check the mapping args -- do the input files & streams exist? */ - for(i=0;i nb_input_files - 1 || - si < 0 || si > input_files[fi].ctx->nb_streams - 1) { - fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si); - ret = AVERROR(EINVAL); - goto fail; - } - fi = stream_maps[i].sync_file_index; - si = stream_maps[i].sync_stream_index; - if (fi < 0 || fi > nb_input_files - 1 || - si < 0 || si > input_files[fi].ctx->nb_streams - 1) { - fprintf(stderr,"Could not find sync stream #%d.%d\n", fi, si); - ret = AVERROR(EINVAL); - goto fail; - } - } ost_table = av_mallocz(sizeof(OutputStream *) * nb_ostreams); if (!ost_table) goto fail; - n = 0; + for(k=0;knb_streams;i++,n++) { - int found; - ost = ost_table[n] = output_streams_for_file[k][i]; - if (nb_stream_maps > 0) { - ost->source_index = input_files[stream_maps[n].file_index].ist_index + - stream_maps[n].stream_index; - - /* Sanity check that the stream types match */ - if (input_streams[ost->source_index].st->codec->codec_type != ost->st->codec->codec_type) { - int i= ost->file_index; - av_dump_format(output_files[i], i, output_files[i]->filename, 1); - fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n", - stream_maps[n].file_index, stream_maps[n].stream_index, - ost->file_index, ost->index); - exit_program(1); - } - - } else { - int best_nb_frames=-1; - /* get corresponding input stream index : we select the first one with the right type */ - found = 0; - for (j = 0; j < nb_input_streams; j++) { - int skip=0; - ist = &input_streams[j]; - if(opt_programid){ - int pi,si; - AVFormatContext *f = input_files[ist->file_index].ctx; - skip=1; - for(pi=0; pinb_programs; pi++){ - AVProgram *p= f->programs[pi]; - if(p->id == opt_programid) - for(si=0; sinb_stream_indexes; si++){ - if(f->streams[ p->stream_index[si] ] == ist->st) - skip=0; - } - } - } - if (ist->discard && ist->st->discard != AVDISCARD_ALL && !skip && - ist->st->codec->codec_type == ost->st->codec->codec_type) { - if(best_nb_frames < ist->st->codec_info_nb_frames){ - best_nb_frames= ist->st->codec_info_nb_frames; - ost->source_index = j; - found = 1; + nb_streams[os->streams[i]->codec->codec_type]++; + } + } + for(step=1<<30; step; step>>=1){ + int found_streams[AVMEDIA_TYPE_NB]={0}; + for(j=0; jfile_index ].ctx; + skip=1; + for(pi=0; pinb_programs; pi++){ + AVProgram *p= f->programs[pi]; + if(p->id == opt_programid) + for(si=0; sinb_stream_indexes; si++){ + if(f->streams[ p->stream_index[si] ] == ist->st) + skip=0; } - } - } - - if (!found) { - if(! opt_programid) { - /* try again and reuse existing stream */ - for (j = 0; j < nb_input_streams; j++) { - ist = &input_streams[j]; - if ( ist->st->codec->codec_type == ost->st->codec->codec_type - && ist->st->discard != AVDISCARD_ALL) { - ost->source_index = j; - found = 1; - } - } - } - if (!found) { - int i= ost->file_index; - av_dump_format(output_files[i], i, output_files[i]->filename, 1); - fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n", - ost->file_index, ost->index); - exit_program(1); - } } } - ist = &input_streams[ost->source_index]; - ist->discard = 0; - ost->sync_ist = (nb_stream_maps > 0) ? - &input_streams[input_files[stream_maps[n].sync_file_index].ist_index + - stream_maps[n].sync_stream_index] : ist; + if (ist->discard && ist->st->discard != AVDISCARD_ALL && !skip + && nb_frame_threshold[ist->st->codec->codec_type] <= ist->st->codec_info_nb_frames){ + found_streams[ist->st->codec->codec_type]++; + } } + for(j=0; jnb_streams; i++, n++) + ost_table[n] = output_streams_for_file[k][i]; } /* for each output stream, we compute the right encoding parameters */ @@ -2122,13 +2132,23 @@ static int transcode(AVFormatContext **output_files, goto fail; memcpy(codec->extradata, icodec->extradata, icodec->extradata_size); codec->extradata_size= icodec->extradata_size; - if(!copy_tb && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/500){ - codec->time_base = icodec->time_base; - codec->time_base.num *= icodec->ticks_per_frame; - av_reduce(&codec->time_base.num, &codec->time_base.den, - codec->time_base.num, codec->time_base.den, INT_MAX); - }else - codec->time_base = ist->st->time_base; + + codec->time_base = ist->st->time_base; + if(!strcmp(os->oformat->name, "avi")) { + if(!copy_tb && av_q2d(icodec->time_base)*icodec->ticks_per_frame > 2*av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/500){ + codec->time_base = icodec->time_base; + codec->time_base.num *= icodec->ticks_per_frame; + codec->time_base.den *= 2; + } + } else if(!(os->oformat->flags & AVFMT_VARIABLE_FPS)) { + if(!copy_tb && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/500){ + codec->time_base = icodec->time_base; + codec->time_base.num *= icodec->ticks_per_frame; + } + } + av_reduce(&codec->time_base.num, &codec->time_base.den, + codec->time_base.num, codec->time_base.den, INT_MAX); + switch(codec->codec_type) { case AVMEDIA_TYPE_AUDIO: if(audio_volume != 256) { @@ -2179,17 +2199,16 @@ static int transcode(AVFormatContext **output_files, ost->reformat_pair = MAKE_SFMT_PAIR(AV_SAMPLE_FMT_NONE,AV_SAMPLE_FMT_NONE); if (!codec->sample_rate) { codec->sample_rate = icodec->sample_rate; - if (icodec->lowres) - codec->sample_rate >>= icodec->lowres; } choose_sample_rate(ost->st, ost->enc); codec->time_base = (AVRational){1, codec->sample_rate}; if (codec->sample_fmt == AV_SAMPLE_FMT_NONE) codec->sample_fmt = icodec->sample_fmt; choose_sample_fmt(ost->st, ost->enc); - if (!codec->channels) + if (!codec->channels) { codec->channels = icodec->channels; - codec->channel_layout = icodec->channel_layout; + codec->channel_layout = icodec->channel_layout; + } if (av_get_channel_layout_nb_channels(codec->channel_layout) != codec->channels) codec->channel_layout = 0; ost->audio_resample = codec->sample_rate != icodec->sample_rate || audio_sync_method > 1; @@ -2219,27 +2238,7 @@ static int transcode(AVFormatContext **output_files, codec->height != icodec->height || codec->pix_fmt != icodec->pix_fmt; if (ost->video_resample) { -#if !CONFIG_AVFILTER - avcodec_get_frame_defaults(&ost->pict_tmp); - if(avpicture_alloc((AVPicture*)&ost->pict_tmp, codec->pix_fmt, - codec->width, codec->height)) { - fprintf(stderr, "Cannot allocate temp picture, check pix fmt\n"); - exit_program(1); - } - ost->img_resample_ctx = sws_getContext( - icodec->width, - icodec->height, - icodec->pix_fmt, - codec->width, - codec->height, - codec->pix_fmt, - ost->sws_flags, NULL, NULL, NULL); - if (ost->img_resample_ctx == NULL) { - fprintf(stderr, "Cannot get resampling context\n"); - exit_program(1); - } -#endif - codec->bits_per_raw_sample= 0; + codec->bits_per_raw_sample= frame_bits_per_raw_sample; } ost->resample_height = icodec->height; @@ -2255,6 +2254,11 @@ static int transcode(AVFormatContext **output_files, ost->frame_rate = ost->enc->supported_framerates[idx]; } codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num}; + if( av_q2d(codec->time_base) < 0.001 && video_sync_method + && (video_sync_method==1 || (video_sync_method<0 && !(os->oformat->flags & AVFMT_VARIABLE_FPS)))){ + av_log(os, AV_LOG_WARNING, "Frame rate very high for a muxer not effciciently supporting it.\n" + "Please consider specifiying a lower framerate, a different muxer or -vsync 2\n"); + } #if CONFIG_AVFILTER if (configure_video_filters(ist, ost)) { @@ -2272,7 +2276,7 @@ static int transcode(AVFormatContext **output_files, break; } /* two pass mode */ - if (ost->encoding_needed && + if (ost->encoding_needed && codec->codec_id != CODEC_ID_H264 && (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) { char logfilename[1024]; FILE *f; @@ -2299,8 +2303,9 @@ static int transcode(AVFormatContext **output_files, } } if(codec->codec_type == AVMEDIA_TYPE_VIDEO){ + /* maximum video buffer size is 6-bytes per pixel, plus DPX header size */ int size= codec->width * codec->height; - bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 200); + bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 1664); } } @@ -2362,17 +2367,6 @@ static int transcode(AVFormatContext **output_files, ret = AVERROR(EINVAL); goto dump_format; } - - /* update requested sample format for the decoder based on the - corresponding encoder sample format */ - for (j = 0; j < nb_ostreams; j++) { - ost = ost_table[j]; - if (ost->source_index == i) { - update_sample_fmt(ist->st->codec, codec, ost->st->codec); - break; - } - } - if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) { snprintf(error, sizeof(error), "Error while opening decoder for input stream #%d.%d", ist->file_index, ist->st->index); @@ -2393,7 +2387,6 @@ static int transcode(AVFormatContext **output_files, st= ist->st; ist->pts = st->avg_frame_rate.num ? - st->codec->has_b_frames*AV_TIME_BASE / av_q2d(st->avg_frame_rate) : 0; ist->next_pts = AV_NOPTS_VALUE; - init_pts_correction(&ist->pts_ctx); ist->is_start = 1; } @@ -2405,7 +2398,7 @@ static int transcode(AVFormatContext **output_files, ret = AVERROR(EINVAL); goto dump_format; } - assert_avoptions(output_opts[i]); +// assert_avoptions(output_opts[i]); if (strcmp(output_files[i]->oformat->name, "rtp")) { want_sdp = 0; } @@ -2445,8 +2438,11 @@ static int transcode(AVFormatContext **output_files, print_sdp(output_files, nb_output_files); } - if (verbose >= 0) - fprintf(stderr, "Press ctrl-c to stop encoding\n"); + if (!using_stdin) { + if(verbose >= 0) + fprintf(stderr, "Press [q] to stop, [?] for help\n"); + avio_set_interrupt_cb(decode_interrupt_cb); + } term_init(); timer_start = av_gettime(); @@ -2460,6 +2456,57 @@ static int transcode(AVFormatContext **output_files, redo: ipts_min= 1e100; opts_min= 1e100; + /* if 'q' pressed, exits */ + if (!using_stdin) { + if (q_pressed) + break; + /* read_key() returns 0 on EOF */ + key = read_key(); + if (key == 'q') + break; + if (key == '+') verbose++; + if (key == '-') verbose--; + if (key == 's') qp_hist ^= 1; + if (key == 'h'){ + if (do_hex_dump){ + do_hex_dump = do_pkt_dump = 0; + } else if(do_pkt_dump){ + do_hex_dump = 1; + } else + do_pkt_dump = 1; + av_log_set_level(AV_LOG_DEBUG); + } + if (key == 'd' || key == 'D'){ + int debug=0; + if(key == 'D') { + debug = input_streams[0].st->codec->debug<<1; + if(!debug) debug = 1; + while(debug & (FF_DEBUG_DCT_COEFF|FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) //unsupported, would just crash + debug += debug; + }else + scanf("%d", &debug); + for(i=0;icodec->debug = debug; + } + for(i=0;ist->codec->debug = debug; + } + if(debug) av_log_set_level(AV_LOG_DEBUG); + fprintf(stderr,"debug=%d\n", debug); + } + if (key == '?'){ + fprintf(stderr, "key function\n" + "? show this help\n" + "+ increase verbosity\n" + "- decrease verbosity\n" + "D cycle through available debug modes\n" + "h dump packets/hex press to cycle through the 3 states\n" + "q quit\n" + "s Show QP histogram\n" + ); + } + } /* select the stream that we must read now by looking at the smallest output pts */ @@ -2528,7 +2575,7 @@ static int transcode(AVFormatContext **output_files, } /* the following test is needed in case new streams appear dynamically in stream : we ignore them */ - if (pkt.stream_index >= input_files[file_index].ctx->nb_streams) + if (pkt.stream_index >= input_files[file_index].nb_streams) goto discard_packet; ist_index = input_files[file_index].ist_index + pkt.stream_index; ist = &input_streams[ist_index]; @@ -2565,7 +2612,11 @@ static int transcode(AVFormatContext **output_files, /* finish if recording time exhausted */ if (recording_time != INT64_MAX && - av_compare_ts(pkt.pts, ist->st->time_base, recording_time + start_time, (AVRational){1, 1000000}) >= 0) { + (pkt.pts != AV_NOPTS_VALUE ? + av_compare_ts(pkt.pts, ist->st->time_base, recording_time + start_time, (AVRational){1, 1000000}) + : + av_compare_ts(ist->pts, AV_TIME_BASE_Q, recording_time + start_time, (AVRational){1, 1000000}) + )>= 0) { ist->is_past_recording_time = 1; goto discard_packet; } @@ -2647,7 +2698,7 @@ static int transcode(AVFormatContext **output_files, av_fifo_free(ost->fifo); /* works even if fifo is not initialized but set to zero */ av_freep(&ost->st->codec->subtitle_header); - av_free(ost->pict_tmp.data[0]); + av_free(ost->resample_frame.data[0]); av_free(ost->forced_kf_pts); if (ost->video_resample) sws_freeContext(ost->img_resample_ctx); @@ -2726,7 +2777,7 @@ static int opt_frame_pix_fmt(const char *opt, const char *arg) return AVERROR(EINVAL); } } else { - show_pix_fmts(); + opt_pix_fmts(NULL, NULL); exit_program(0); } return 0; @@ -2775,7 +2826,7 @@ static int opt_metadata(const char *opt, const char *arg) static int opt_qscale(const char *opt, const char *arg) { video_qscale = parse_number_or_die(opt, arg, OPT_FLOAT, 0, 255); - if (video_qscale == 0) { + if (video_qscale <= 0 || video_qscale > 255) { fprintf(stderr, "qscale must be > 0.0 and <= 255\n"); return AVERROR(EINVAL); } @@ -2785,7 +2836,7 @@ static int opt_qscale(const char *opt, const char *arg) static int opt_top_field_first(const char *opt, const char *arg) { top_field_first = parse_number_or_die(opt, arg, OPT_INT, 0, 1); - return 0; + return opt_default(opt, arg); } static int opt_thread_count(const char *opt, const char *arg) @@ -2828,9 +2879,15 @@ static int opt_audio_channels(const char *opt, const char *arg) return 0; } -static int opt_codec(int *pstream_copy, char **pcodec_name, - int codec_type, const char *arg) +static int opt_codec(const char *opt, const char *arg) { + int *pstream_copy; char **pcodec_name; enum AVMediaType codec_type; + + if (!strcmp(opt, "acodec")) { pstream_copy = &audio_stream_copy; pcodec_name = &audio_codec_name; codec_type = AVMEDIA_TYPE_AUDIO; } + else if (!strcmp(opt, "vcodec")) { pstream_copy = &video_stream_copy; pcodec_name = &video_codec_name; codec_type = AVMEDIA_TYPE_VIDEO; } + else if (!strcmp(opt, "scodec")) { pstream_copy = &subtitle_stream_copy; pcodec_name = &subtitle_codec_name; codec_type = AVMEDIA_TYPE_SUBTITLE; } + else if (!strcmp(opt, "dcodec")) { pstream_copy = &data_stream_copy; pcodec_name = &data_codec_name; codec_type = AVMEDIA_TYPE_DATA; } + av_freep(pcodec_name); if (!strcmp(arg, "copy")) { *pstream_copy = 1; @@ -2840,26 +2897,6 @@ static int opt_codec(int *pstream_copy, char **pcodec_name, return 0; } -static int opt_audio_codec(const char *opt, const char *arg) -{ - return opt_codec(&audio_stream_copy, &audio_codec_name, AVMEDIA_TYPE_AUDIO, arg); -} - -static int opt_video_codec(const char *opt, const char *arg) -{ - return opt_codec(&video_stream_copy, &video_codec_name, AVMEDIA_TYPE_VIDEO, arg); -} - -static int opt_subtitle_codec(const char *opt, const char *arg) -{ - return opt_codec(&subtitle_stream_copy, &subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, arg); -} - -static int opt_data_codec(const char *opt, const char *arg) -{ - return opt_codec(&data_stream_copy, &data_codec_name, AVMEDIA_TYPE_DATA, arg); -} - static int opt_codec_tag(const char *opt, const char *arg) { char *tail; @@ -2880,27 +2917,82 @@ static int opt_codec_tag(const char *opt, const char *arg) static int opt_map(const char *opt, const char *arg) { - StreamMap *m; - char *p; + 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_ERROR, "Invalid sync file index: %d.\n", sync_file_idx); + exit_program(1); + } + if (*sync) + sync++; + for (i = 0; i < input_files[sync_file_idx].ctx->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].ctx->nb_streams) { + av_log(NULL, AV_LOG_ERROR, "Sync stream specification in map %s does not " + "match any streams.\n", arg); + exit_program(1); + } + } - 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++; + file_idx = strtol(map, &p, 0); + if (file_idx >= nb_input_files || file_idx < 0) { + av_log(NULL, AV_LOG_ERROR, "Invalid input file index: %d.\n", file_idx); + exit_program(1); + } + if (negative) + /* disable some already defined maps */ + for (i = 0; i < nb_stream_maps; i++) { + m = &stream_maps[i]; + if (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].ctx->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; + stream_maps = grow_array(stream_maps, sizeof(*stream_maps), &nb_stream_maps, nb_stream_maps + 1); + m = &stream_maps[nb_stream_maps - 1]; - 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; + 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_ERROR, "Stream map '%s' matches no streams.\n", arg); + exit_program(1); } + + av_freep(&map); return 0; } @@ -2964,6 +3056,9 @@ static int opt_input_ts_scale(const char *opt, const char *arg) p++; scale= strtod(p, &p); + if(stream >= MAX_STREAMS) + exit_program(1); + ts_scale = grow_array(ts_scale, sizeof(*ts_scale), &nb_ts_scale, stream + 1); ts_scale[stream] = scale; return 0; @@ -3147,17 +3242,19 @@ static int opt_input_file(const char *opt, const char *filename) switch (dec->codec_type) { case AVMEDIA_TYPE_AUDIO: ist->dec = avcodec_find_decoder_by_name(audio_codec_name); + if(!ist->dec) + ist->dec = avcodec_find_decoder(dec->codec_id); if(audio_disable) st->discard= AVDISCARD_ALL; break; case AVMEDIA_TYPE_VIDEO: - ist->dec = avcodec_find_decoder_by_name(video_codec_name); + ist->dec= avcodec_find_decoder_by_name(video_codec_name); + 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; - dec->height >>= dec->lowres; - dec->width >>= dec->lowres; } if(me_threshold) dec->debug |= FF_DEBUG_MV; @@ -3180,6 +3277,8 @@ static int opt_input_file(const char *opt, const char *filename) break; case AVMEDIA_TYPE_SUBTITLE: ist->dec = avcodec_find_decoder_by_name(subtitle_codec_name); + if(!ist->dec) + ist->dec = avcodec_find_decoder(dec->codec_id); if(subtitle_disable) st->discard = AVDISCARD_ALL; break; @@ -3199,7 +3298,9 @@ static int opt_input_file(const char *opt, const char *filename) 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].nb_streams = ic->nb_streams; + top_field_first = -1; frame_rate = (AVRational){0, 0}; frame_pix_fmt = PIX_FMT_NONE; frame_height = 0; @@ -3221,50 +3322,7 @@ static int opt_input_file(const char *opt, const char *filename) return 0; } -static void check_inputs(int *has_video_ptr, - int *has_audio_ptr, - int *has_subtitle_ptr, - int *has_data_ptr) -{ - int has_video, has_audio, has_subtitle, has_data, i, j; - AVFormatContext *ic; - - has_video = 0; - has_audio = 0; - has_subtitle = 0; - has_data = 0; - - for(j=0;jnb_streams;i++) { - AVCodecContext *enc = ic->streams[i]->codec; - switch(enc->codec_type) { - case AVMEDIA_TYPE_AUDIO: - has_audio = 1; - break; - case AVMEDIA_TYPE_VIDEO: - has_video = 1; - break; - case AVMEDIA_TYPE_SUBTITLE: - has_subtitle = 1; - break; - case AVMEDIA_TYPE_DATA: - case AVMEDIA_TYPE_ATTACHMENT: - case AVMEDIA_TYPE_UNKNOWN: - has_data = 1; - break; - default: - abort(); - } - } - } - *has_video_ptr = has_video; - *has_audio_ptr = has_audio; - *has_subtitle_ptr = has_subtitle; - *has_data_ptr = has_data; -} - -static void new_video_stream(AVFormatContext *oc, int file_idx) +static OutputStream *new_video_stream(AVFormatContext *oc, int file_idx) { AVStream *st; OutputStream *ost; @@ -3288,7 +3346,7 @@ static void new_video_stream(AVFormatContext *oc, int file_idx) ost->frame_aspect_ratio = frame_aspect_ratio; frame_aspect_ratio = 0; #if CONFIG_AVFILTER - ost->avfilter= vfilters; + ost->avfilter = vfilters; vfilters = NULL; #endif } @@ -3323,6 +3381,7 @@ static void new_video_stream(AVFormatContext *oc, int file_idx) video_enc->width = frame_width; video_enc->height = frame_height; video_enc->pix_fmt = frame_pix_fmt; + video_enc->bits_per_raw_sample = frame_bits_per_raw_sample; st->sample_aspect_ratio = video_enc->sample_aspect_ratio; if (intra_only) @@ -3393,9 +3452,10 @@ static void new_video_stream(AVFormatContext *oc, int file_idx) av_freep(&forced_key_frames); video_stream_copy = 0; frame_pix_fmt = PIX_FMT_NONE; + return ost; } -static void new_audio_stream(AVFormatContext *oc, int file_idx) +static OutputStream *new_audio_stream(AVFormatContext *oc, int file_idx) { AVStream *st; OutputStream *ost; @@ -3454,9 +3514,11 @@ static void new_audio_stream(AVFormatContext *oc, int file_idx) audio_disable = 0; av_freep(&audio_codec_name); audio_stream_copy = 0; + + return ost; } -static void new_data_stream(AVFormatContext *oc, int file_idx) +static OutputStream *new_data_stream(AVFormatContext *oc, int file_idx) { AVStream *st; OutputStream *ost; @@ -3485,9 +3547,10 @@ static void new_data_stream(AVFormatContext *oc, int file_idx) data_disable = 0; av_freep(&data_codec_name); data_stream_copy = 0; + return ost; } -static void new_subtitle_stream(AVFormatContext *oc, int file_idx) +static OutputStream *new_subtitle_stream(AVFormatContext *oc, int file_idx) { AVStream *st; OutputStream *ost; @@ -3533,24 +3596,7 @@ static void new_subtitle_stream(AVFormatContext *oc, int file_idx) subtitle_disable = 0; av_freep(&subtitle_codec_name); subtitle_stream_copy = 0; -} - -static int opt_new_stream(const char *opt, const char *arg) -{ - AVFormatContext *oc; - int file_idx = nb_output_files - 1; - if (nb_output_files <= 0) { - fprintf(stderr, "At least one output file must be specified\n"); - exit_program(1); - } - oc = output_files[file_idx]; - - if (!strcmp(opt, "newvideo" )) new_video_stream (oc, file_idx); - else if (!strcmp(opt, "newaudio" )) new_audio_stream (oc, file_idx); - else if (!strcmp(opt, "newsubtitle")) new_subtitle_stream(oc, file_idx); - else if (!strcmp(opt, "newdata" )) new_data_stream (oc, file_idx); - else av_assert0(0); - return 0; + return ost; } /* arg format is "output-stream-index:streamid-value". */ @@ -3569,92 +3615,122 @@ static int opt_streamid(const char *opt, const char *arg) exit_program(1); } *p++ = '\0'; - idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, INT_MAX); + idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, MAX_STREAMS-1); streamid_map = grow_array(streamid_map, sizeof(*streamid_map), &nb_streamid_map, idx+1); streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX); return 0; } -static void opt_output_file(const char *filename) +static int opt_output_file(const char *opt, const char *filename) { AVFormatContext *oc; - int i, err, use_video, use_audio, use_subtitle, use_data; - int input_has_video, input_has_audio, input_has_subtitle, input_has_data; + int i, err; AVOutputFormat *file_oformat; + OutputStream *ost; + InputStream *ist; + + if(nb_output_files >= FF_ARRAY_ELEMS(output_files)){ + fprintf(stderr, "Too many output files\n"); + exit_program(1); + } if (!strcmp(filename, "-")) filename = "pipe:"; - oc = avformat_alloc_context(); + err = avformat_alloc_output_context2(&oc, NULL, last_asked_format, filename); + last_asked_format = NULL; if (!oc) { - print_error(filename, AVERROR(ENOMEM)); + print_error(filename, err); exit_program(1); } - if (last_asked_format) { - file_oformat = av_guess_format(last_asked_format, NULL, NULL); - if (!file_oformat) { - fprintf(stderr, "Requested output format '%s' is not a suitable output format\n", last_asked_format); - exit_program(1); - } - last_asked_format = NULL; - } else { - file_oformat = av_guess_format(NULL, filename, NULL); - if (!file_oformat) { - fprintf(stderr, "Unable to find a suitable output format for '%s'\n", - filename); - exit_program(1); - } - } - - oc->oformat = file_oformat; - av_strlcpy(oc->filename, filename, sizeof(oc->filename)); + file_oformat= oc->oformat; 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); + /* special case for files sent to ffserver: we get the stream + parameters from ffserver */ + int err = read_ffserver_streams(oc, filename); if (err < 0) { print_error(filename, err); exit_program(1); } + } else if (!nb_stream_maps) { + /* pick the "best" stream of each type */ +#define NEW_STREAM(type, index)\ + if (index >= 0) {\ + ost = new_ ## type ## _stream(oc, nb_output_files);\ + ost->source_index = index;\ + ost->sync_ist = &input_streams[index];\ + input_streams[index].discard = 0;\ + } + + /* video: highest resolution */ + if (!video_disable && oc->oformat->video_codec != CODEC_ID_NONE) { + int area = 0, idx = -1; + for (i = 0; i < nb_input_streams; i++) { + ist = &input_streams[i]; + if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && + ist->st->codec->width * ist->st->codec->height > area) { + area = ist->st->codec->width * ist->st->codec->height; + idx = i; + } + } + NEW_STREAM(video, idx); + } + + /* audio: most channels */ + if (!audio_disable && oc->oformat->audio_codec != CODEC_ID_NONE) { + int channels = 0, idx = -1; + for (i = 0; i < nb_input_streams; i++) { + ist = &input_streams[i]; + if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && + ist->st->codec->channels > channels) { + channels = ist->st->codec->channels; + idx = i; + } + } + NEW_STREAM(audio, idx); + } + + /* subtitles: pick first */ + if (!subtitle_disable && oc->oformat->subtitle_codec != CODEC_ID_NONE) { + for (i = 0; i < nb_input_streams; i++) + if (input_streams[i].st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) { + NEW_STREAM(subtitle, i); + break; + } + } + /* do something with data? */ } else { - use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name; - use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name; - use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name; - use_data = data_stream_copy || data_codec_name; /* XXX once generic data codec will be available add a ->data_codec reference and use it here */ - - /* disable if no corresponding type found */ - check_inputs(&input_has_video, - &input_has_audio, - &input_has_subtitle, - &input_has_data); - - if (!input_has_video) - use_video = 0; - if (!input_has_audio) - use_audio = 0; - if (!input_has_subtitle) - use_subtitle = 0; - if (!input_has_data) - use_data = 0; - - /* manual disable */ - if (audio_disable) use_audio = 0; - if (video_disable) use_video = 0; - if (subtitle_disable) use_subtitle = 0; - if (data_disable) use_data = 0; - - if (use_video) new_video_stream(oc, nb_output_files); - if (use_audio) new_audio_stream(oc, nb_output_files); - if (use_subtitle) new_subtitle_stream(oc, nb_output_files); - if (use_data) new_data_stream(oc, nb_output_files); - - av_dict_copy(&oc->metadata, metadata, 0); - av_dict_free(&metadata); + for (i = 0; i < nb_stream_maps; i++) { + StreamMap *map = &stream_maps[i]; + + if (map->disabled) + continue; + + ist = &input_streams[input_files[map->file_index].ist_index + map->stream_index]; + switch (ist->st->codec->codec_type) { + case AVMEDIA_TYPE_VIDEO: ost = new_video_stream(oc, nb_output_files); break; + case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream(oc, nb_output_files); break; + case AVMEDIA_TYPE_SUBTITLE: ost = new_subtitle_stream(oc, nb_output_files); break; + case AVMEDIA_TYPE_DATA: ost = new_data_stream(oc, nb_output_files); break; + default: + av_log(NULL, AV_LOG_ERROR, "Cannot map stream #%d.%d - unsupported type.\n", + map->file_index, map->stream_index); + exit_program(1); + } + + ost->source_index = input_files[map->file_index].ist_index + map->stream_index; + ost->sync_ist = &input_streams[input_files[map->sync_file_index].ist_index + + map->sync_stream_index]; + ist->discard = 0; + } } + av_dict_copy(&oc->metadata, metadata, 0); + av_dict_free(&metadata); + av_dict_copy(&output_opts[nb_output_files], format_opts, 0); output_files[nb_output_files++] = oc; @@ -3697,7 +3773,6 @@ static void opt_output_file(const char *filename) oc->preload= (int)(mux_preload*AV_TIME_BASE); oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE); - oc->flags |= AVFMT_FLAG_NONBLOCK; /* copy chapters */ if (chapters_input_file >= nb_input_files) { @@ -3787,10 +3862,13 @@ static void opt_output_file(const char *filename) metadata_global_autocopy = 1; metadata_streams_autocopy = 1; metadata_chapters_autocopy = 1; + av_freep(&stream_maps); + nb_stream_maps = 0; av_freep(&forced_key_frames); uninit_opts(); init_opts(); + return 0; } /* same option as mencoder */ @@ -3853,16 +3931,18 @@ static void parse_matrix_coeffs(uint16_t *dest, const char *str) } } -static void opt_inter_matrix(const char *arg) +static int opt_inter_matrix(const char *opt, const char *arg) { inter_matrix = av_mallocz(sizeof(uint16_t) * 64); parse_matrix_coeffs(inter_matrix, arg); + return 0; } -static void opt_intra_matrix(const char *arg) +static int opt_intra_matrix(const char *opt, const char *arg) { intra_matrix = av_mallocz(sizeof(uint16_t) * 64); parse_matrix_coeffs(intra_matrix, arg); + return 0; } static void show_usage(void) @@ -3872,7 +3952,7 @@ static void show_usage(void) printf("\n"); } -static void show_help(void) +static int opt_help(const char *opt, const char *arg) { AVCodec *c; AVOutputFormat *oformat = NULL; @@ -3936,6 +4016,7 @@ static void show_help(void) } av_opt_show2(sws_opts, NULL, AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM, 0); + return 0; } static int opt_target(const char *opt, const char *arg) @@ -3995,8 +4076,8 @@ static int opt_target(const char *opt, const char *arg) } if(!strcmp(arg, "vcd")) { - opt_video_codec("vcodec", "mpeg1video"); - opt_audio_codec("vcodec", "mp2"); + opt_codec("vcodec", "mpeg1video"); + opt_codec("acodec", "mp2"); opt_format("f", "vcd"); opt_frame_size("s", norm == PAL ? "352x288" : "352x240"); @@ -4023,12 +4104,13 @@ static int opt_target(const char *opt, const char *arg) mux_preload= (36000+3*1200) / 90000.0; //0.44 } else if(!strcmp(arg, "svcd")) { - opt_video_codec("vcodec", "mpeg2video"); - opt_audio_codec("acodec", "mp2"); + opt_codec("vcodec", "mpeg2video"); + opt_codec("acodec", "mp2"); opt_format("f", "svcd"); opt_frame_size("s", norm == PAL ? "480x576" : "480x480"); opt_frame_rate("r", frame_rates[norm]); + opt_frame_pix_fmt("pix_fmt", "yuv420p"); opt_default("g", norm == PAL ? "15" : "18"); opt_default("b", "2040000"); @@ -4045,12 +4127,13 @@ static int opt_target(const char *opt, const char *arg) } else if(!strcmp(arg, "dvd")) { - opt_video_codec("vcodec", "mpeg2video"); - opt_audio_codec("vcodec", "ac3"); + opt_codec("vcodec", "mpeg2video"); + opt_codec("acodec", "ac3"); opt_format("f", "dvd"); opt_frame_size("vcodec", norm == PAL ? "720x576" : "720x480"); opt_frame_rate("r", frame_rates[norm]); + opt_frame_pix_fmt("pix_fmt", "yuv420p"); opt_default("g", norm == PAL ? "15" : "18"); opt_default("b", "6000000"); @@ -4144,14 +4227,11 @@ static int opt_preset(const char *opt, const char *arg) fprintf(stderr, "%s: Invalid syntax: '%s'\n", filename, line); exit_program(1); } - if(!strcmp(tmp, "acodec")){ - opt_audio_codec(tmp, tmp2); - }else if(!strcmp(tmp, "vcodec")){ - opt_video_codec(tmp, tmp2); - }else if(!strcmp(tmp, "scodec")){ - opt_subtitle_codec(tmp, tmp2); - }else if(!strcmp(tmp, "dcodec")){ - opt_data_codec(tmp, tmp2); + if (!strcmp(tmp, "acodec") || + !strcmp(tmp, "vcodec") || + !strcmp(tmp, "scodec") || + !strcmp(tmp, "dcodec")) { + opt_codec(tmp, tmp2); }else if(opt_default(tmp, tmp2) < 0){ fprintf(stderr, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n", filename, line, tmp, tmp2); exit_program(1); @@ -4163,6 +4243,20 @@ static int opt_preset(const char *opt, const char *arg) return 0; } +static void log_callback_null(void* ptr, int level, const char* fmt, va_list vl) +{ +} + +static int opt_passlogfile(const char *opt, const char *arg) +{ + pass_logfilename_prefix = arg; +#if CONFIG_LIBX264_ENCODER + return opt_default("passlogfile", arg); +#else + return 0; +#endif +} + static const OptionDef options[] = { /* main options */ #include "cmdutils_common_opts.h" @@ -4208,7 +4302,8 @@ static const OptionDef options[] = { { "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" }, + { "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" }, @@ -4222,12 +4317,12 @@ static const OptionDef options[] = { { "vdt", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)&video_discard}, "discard threshold", "n" }, { "qscale", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qscale}, "use fixed video quantizer scale (VBR)", "q" }, { "rc_override", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_video_rc_override_string}, "rate control override for specific intervals", "override" }, - { "vcodec", HAS_ARG | OPT_VIDEO, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" }, + { "vcodec", HAS_ARG | OPT_VIDEO, {(void*)opt_codec}, "force video codec ('copy' to copy stream)", "codec" }, { "me_threshold", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_me_threshold}, "motion estimaton threshold", "threshold" }, { "sameq", OPT_BOOL | OPT_VIDEO, {(void*)&same_quality}, "use same quantizer as source (implies VBR)" }, { "pass", HAS_ARG | OPT_VIDEO, {(void*)opt_pass}, "select the pass number (1 or 2)", "n" }, - { "passlogfile", HAS_ARG | OPT_STRING | OPT_VIDEO, {(void*)&pass_logfilename_prefix}, "select two pass log file name prefix", "prefix" }, + { "passlogfile", HAS_ARG | OPT_VIDEO, {(void*)&opt_passlogfile}, "select two pass log file name prefix", "prefix" }, { "deinterlace", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_deinterlace}, "deinterlace pictures" }, { "psnr", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_psnr}, "calculate PSNR of compressed frames" }, @@ -4241,7 +4336,6 @@ static const OptionDef options[] = { { "top", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_top_field_first}, "top=1/bottom=0/auto=-1 field first", "" }, { "dc", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)&intra_dc_precision}, "intra_dc_precision", "precision" }, { "vtag", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_codec_tag}, "force video tag/fourcc", "fourcc/tag" }, - { "newvideo", OPT_VIDEO, {(void*)opt_new_stream}, "add a new video stream to the current output stream" }, { "vlang", HAS_ARG | OPT_STRING | OPT_VIDEO, {(void *)&video_language}, "set the ISO 639 language code (3 letters) of the current video stream" , "code" }, { "qphist", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, { (void *)&qp_hist }, "show QP histogram" }, { "force_fps", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&force_fps}, "force the selected framerate, disable the best supported framerate selection" }, @@ -4254,17 +4348,15 @@ static const OptionDef options[] = { { "ar", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" }, { "ac", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_channels}, "set number of audio channels", "channels" }, { "an", OPT_BOOL | OPT_AUDIO, {(void*)&audio_disable}, "disable audio" }, - { "acodec", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" }, + { "acodec", HAS_ARG | OPT_AUDIO, {(void*)opt_codec}, "force audio codec ('copy' to copy stream)", "codec" }, { "atag", HAS_ARG | OPT_EXPERT | OPT_AUDIO, {(void*)opt_codec_tag}, "force audio tag/fourcc", "fourcc/tag" }, { "vol", OPT_INT | HAS_ARG | OPT_AUDIO, {(void*)&audio_volume}, "change audio volume (256=normal)" , "volume" }, // - { "newaudio", OPT_AUDIO, {(void*)opt_new_stream}, "add a new audio stream to the current output stream" }, { "alang", HAS_ARG | OPT_STRING | OPT_AUDIO, {(void *)&audio_language}, "set the ISO 639 language code (3 letters) of the current audio stream" , "code" }, { "sample_fmt", HAS_ARG | OPT_EXPERT | OPT_AUDIO, {(void*)opt_audio_sample_fmt}, "set sample format, 'list' as argument shows all the sample formats supported", "format" }, /* subtitle options */ { "sn", OPT_BOOL | OPT_SUBTITLE, {(void*)&subtitle_disable}, "disable subtitle" }, - { "scodec", HAS_ARG | OPT_SUBTITLE, {(void*)opt_subtitle_codec}, "force subtitle codec ('copy' to copy stream)", "codec" }, - { "newsubtitle", OPT_SUBTITLE, {(void*)opt_new_stream}, "add a new subtitle stream to the current output stream" }, + { "scodec", HAS_ARG | OPT_SUBTITLE, {(void*)opt_codec}, "force subtitle codec ('copy' to copy stream)", "codec" }, { "slang", HAS_ARG | OPT_STRING | OPT_SUBTITLE, {(void *)&subtitle_language}, "set the ISO 639 language code (3 letters) of the current subtitle stream" , "code" }, { "stag", HAS_ARG | OPT_EXPERT | OPT_SUBTITLE, {(void*)opt_codec_tag}, "force subtitle tag/fourcc", "fourcc/tag" }, @@ -4284,7 +4376,7 @@ static const OptionDef options[] = { { "spre", HAS_ARG | OPT_SUBTITLE | OPT_EXPERT, {(void*)opt_preset}, "set the subtitle options to the indicated preset", "preset" }, { "fpre", HAS_ARG | OPT_EXPERT, {(void*)opt_preset}, "set options from indicated preset file", "filename" }, /* data codec support */ - { "dcodec", HAS_ARG | OPT_DATA, {(void*)opt_data_codec}, "force data codec ('copy' to copy stream)", "codec" }, + { "dcodec", HAS_ARG | OPT_DATA, {(void*)opt_codec}, "force data codec ('copy' to copy stream)", "codec" }, { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" }, { NULL, }, @@ -4296,6 +4388,14 @@ int main(int argc, char **argv) av_log_set_flags(AV_LOG_SKIP_REPEATED); + if(argc>1 && !strcmp(argv[1], "-d")){ + run_as_daemon=1; + verbose=-1; + av_log_set_callback(log_callback_null); + argc--; + argv++; + } + avcodec_register_all(); #if CONFIG_AVDEVICE avdevice_register_all(); @@ -4305,11 +4405,15 @@ int main(int argc, char **argv) #endif av_register_all(); - avio_set_interrupt_cb(decode_interrupt_cb); +#if HAVE_ISATTY + if(isatty(STDIN_FILENO)) + avio_set_interrupt_cb(decode_interrupt_cb); +#endif init_opts(); - show_banner(); + if(verbose>=0) + show_banner(); /* parse options */ parse_options(argc, argv, options, opt_output_file); @@ -4332,8 +4436,7 @@ int main(int argc, char **argv) } ti = getutime(); - if (transcode(output_files, nb_output_files, input_files, nb_input_files, - stream_maps, nb_stream_maps) < 0) + if (transcode(output_files, nb_output_files, input_files, nb_input_files) < 0) exit_program(1); ti = getutime() - ti; if (do_benchmark) {