00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023 #include <ctype.h>
00024 #include <string.h>
00025 #include <math.h>
00026 #include <stdlib.h>
00027 #include <errno.h>
00028 #include <signal.h>
00029 #include <limits.h>
00030 #include <unistd.h>
00031 #include "libavformat/avformat.h"
00032 #include "libavdevice/avdevice.h"
00033 #include "libswscale/swscale.h"
00034 #include "libavutil/opt.h"
00035 #include "libavcodec/audioconvert.h"
00036 #include "libavutil/audioconvert.h"
00037 #include "libavutil/parseutils.h"
00038 #include "libavutil/samplefmt.h"
00039 #include "libavutil/colorspace.h"
00040 #include "libavutil/fifo.h"
00041 #include "libavutil/intreadwrite.h"
00042 #include "libavutil/dict.h"
00043 #include "libavutil/mathematics.h"
00044 #include "libavutil/pixdesc.h"
00045 #include "libavutil/avstring.h"
00046 #include "libavutil/libm.h"
00047 #include "libavformat/os_support.h"
00048
00049 #include "libavformat/ffm.h"
00050
00051 #if CONFIG_AVFILTER
00052 # include "libavfilter/avcodec.h"
00053 # include "libavfilter/avfilter.h"
00054 # include "libavfilter/avfiltergraph.h"
00055 # include "libavfilter/buffersink.h"
00056 # include "libavfilter/vsrc_buffer.h"
00057 #endif
00058
00059 #if HAVE_SYS_RESOURCE_H
00060 #include <sys/types.h>
00061 #include <sys/time.h>
00062 #include <sys/resource.h>
00063 #elif HAVE_GETPROCESSTIMES
00064 #include <windows.h>
00065 #endif
00066 #if HAVE_GETPROCESSMEMORYINFO
00067 #include <windows.h>
00068 #include <psapi.h>
00069 #endif
00070
00071 #if HAVE_SYS_SELECT_H
00072 #include <sys/select.h>
00073 #endif
00074
00075 #if HAVE_TERMIOS_H
00076 #include <fcntl.h>
00077 #include <sys/ioctl.h>
00078 #include <sys/time.h>
00079 #include <termios.h>
00080 #elif HAVE_KBHIT
00081 #include <conio.h>
00082 #endif
00083 #include <time.h>
00084
00085 #include "cmdutils.h"
00086
00087 #include "libavutil/avassert.h"
00088
00089 const char program_name[] = "avconv";
00090 const int program_birth_year = 2000;
00091
00092
00093 typedef struct StreamMap {
00094 int disabled;
00095 int file_index;
00096 int stream_index;
00097 int sync_file_index;
00098 int sync_stream_index;
00099 } StreamMap;
00100
00104 typedef struct MetadataMap {
00105 int file;
00106 char type;
00107 int index;
00108 } MetadataMap;
00109
00110 static const OptionDef options[];
00111
00112 #define MAX_STREAMS 1024
00113
00114 static int frame_bits_per_raw_sample = 0;
00115 static int video_discard = 0;
00116 static int same_quant = 0;
00117 static int do_deinterlace = 0;
00118 static int intra_dc_precision = 8;
00119 static int qp_hist = 0;
00120
00121 static int file_overwrite = 0;
00122 static int no_file_overwrite = 0;
00123 static int do_benchmark = 0;
00124 static int do_hex_dump = 0;
00125 static int do_pkt_dump = 0;
00126 static int do_pass = 0;
00127 static const char *pass_logfilename_prefix;
00128 static int video_sync_method= -1;
00129 static int audio_sync_method= 0;
00130 static float audio_drift_threshold= 0.1;
00131 static int copy_ts= 0;
00132 static int copy_tb = 1;
00133 static int opt_shortest = 0;
00134 static char *vstats_filename;
00135 static FILE *vstats_file;
00136
00137 static int audio_volume = 256;
00138
00139 static int exit_on_error = 0;
00140 static int using_stdin = 0;
00141 static int run_as_daemon = 0;
00142 static int q_pressed = 0;
00143 static int64_t video_size = 0;
00144 static int64_t audio_size = 0;
00145 static int64_t extra_size = 0;
00146 static int nb_frames_dup = 0;
00147 static int nb_frames_drop = 0;
00148 static int input_sync;
00149
00150 static float dts_delta_threshold = 10;
00151
00152 static int print_stats = 1;
00153
00154 static uint8_t *audio_buf;
00155 static uint8_t *audio_out;
00156 static unsigned int allocated_audio_out_size, allocated_audio_buf_size;
00157
00158 #define DEFAULT_PASS_LOGFILENAME_PREFIX "av2pass"
00159
00160 typedef struct InputStream {
00161 int file_index;
00162 AVStream *st;
00163 int discard;
00164 int decoding_needed;
00165 AVCodec *dec;
00166 AVFrame *decoded_frame;
00167 AVFrame *filtered_frame;
00168
00169 int64_t start;
00170 int64_t next_pts;
00171
00172 int64_t pts;
00173 double ts_scale;
00174 int is_start;
00175 int showed_multi_packet_warning;
00176 AVDictionary *opts;
00177 } InputStream;
00178
00179 typedef struct InputFile {
00180 AVFormatContext *ctx;
00181 int eof_reached;
00182 int ist_index;
00183 int buffer_size;
00184 int64_t ts_offset;
00185 int nb_streams;
00186
00187 int rate_emu;
00188 } InputFile;
00189
00190 typedef struct OutputStream {
00191 int file_index;
00192 int index;
00193 int source_index;
00194 AVStream *st;
00195 int encoding_needed;
00196 int frame_number;
00197
00198
00199
00200 struct InputStream *sync_ist;
00201 int64_t sync_opts;
00202 AVBitStreamFilterContext *bitstream_filters;
00203 AVCodec *enc;
00204 int64_t max_frames;
00205
00206
00207 int video_resample;
00208 AVFrame resample_frame;
00209 struct SwsContext *img_resample_ctx;
00210 int resample_height;
00211 int resample_width;
00212 int resample_pix_fmt;
00213 AVRational frame_rate;
00214 int force_fps;
00215 int top_field_first;
00216
00217 float frame_aspect_ratio;
00218
00219
00220 int64_t *forced_kf_pts;
00221 int forced_kf_count;
00222 int forced_kf_index;
00223
00224
00225 int audio_resample;
00226 ReSampleContext *resample;
00227 int resample_sample_fmt;
00228 int resample_channels;
00229 int resample_sample_rate;
00230 int reformat_pair;
00231 AVAudioConvert *reformat_ctx;
00232 AVFifoBuffer *fifo;
00233 FILE *logfile;
00234
00235 #if CONFIG_AVFILTER
00236 AVFilterContext *output_video_filter;
00237 AVFilterContext *input_video_filter;
00238 AVFilterBufferRef *picref;
00239 char *avfilter;
00240 AVFilterGraph *graph;
00241 #endif
00242
00243 int64_t sws_flags;
00244 AVDictionary *opts;
00245 int is_past_recording_time;
00246 int stream_copy;
00247 const char *attachment_filename;
00248 int copy_initial_nonkeyframes;
00249 } OutputStream;
00250
00251 #if HAVE_TERMIOS_H
00252
00253
00254 static struct termios oldtty;
00255 #endif
00256
00257 typedef struct OutputFile {
00258 AVFormatContext *ctx;
00259 AVDictionary *opts;
00260 int ost_index;
00261 int64_t recording_time;
00262 int64_t start_time;
00263 uint64_t limit_filesize;
00264 } OutputFile;
00265
00266 static InputStream *input_streams = NULL;
00267 static int nb_input_streams = 0;
00268 static InputFile *input_files = NULL;
00269 static int nb_input_files = 0;
00270
00271 static OutputStream *output_streams = NULL;
00272 static int nb_output_streams = 0;
00273 static OutputFile *output_files = NULL;
00274 static int nb_output_files = 0;
00275
00276 typedef struct OptionsContext {
00277
00278 int64_t start_time;
00279 const char *format;
00280
00281 SpecifierOpt *codec_names;
00282 int nb_codec_names;
00283 SpecifierOpt *audio_channels;
00284 int nb_audio_channels;
00285 SpecifierOpt *audio_sample_rate;
00286 int nb_audio_sample_rate;
00287 SpecifierOpt *frame_rates;
00288 int nb_frame_rates;
00289 SpecifierOpt *frame_sizes;
00290 int nb_frame_sizes;
00291 SpecifierOpt *frame_pix_fmts;
00292 int nb_frame_pix_fmts;
00293
00294
00295 int64_t input_ts_offset;
00296 int rate_emu;
00297
00298 SpecifierOpt *ts_scale;
00299 int nb_ts_scale;
00300 SpecifierOpt *dump_attachment;
00301 int nb_dump_attachment;
00302
00303
00304 StreamMap *stream_maps;
00305 int nb_stream_maps;
00306
00307 MetadataMap (*meta_data_maps)[2];
00308 int nb_meta_data_maps;
00309 int metadata_global_manual;
00310 int metadata_streams_manual;
00311 int metadata_chapters_manual;
00312 const char **attachments;
00313 int nb_attachments;
00314
00315 int chapters_input_file;
00316
00317 int64_t recording_time;
00318 uint64_t limit_filesize;
00319 float mux_preload;
00320 float mux_max_delay;
00321
00322 int video_disable;
00323 int audio_disable;
00324 int subtitle_disable;
00325 int data_disable;
00326
00327
00328 int *streamid_map;
00329 int nb_streamid_map;
00330
00331 SpecifierOpt *metadata;
00332 int nb_metadata;
00333 SpecifierOpt *max_frames;
00334 int nb_max_frames;
00335 SpecifierOpt *bitstream_filters;
00336 int nb_bitstream_filters;
00337 SpecifierOpt *codec_tags;
00338 int nb_codec_tags;
00339 SpecifierOpt *sample_fmts;
00340 int nb_sample_fmts;
00341 SpecifierOpt *qscale;
00342 int nb_qscale;
00343 SpecifierOpt *forced_key_frames;
00344 int nb_forced_key_frames;
00345 SpecifierOpt *force_fps;
00346 int nb_force_fps;
00347 SpecifierOpt *frame_aspect_ratios;
00348 int nb_frame_aspect_ratios;
00349 SpecifierOpt *rc_overrides;
00350 int nb_rc_overrides;
00351 SpecifierOpt *intra_matrices;
00352 int nb_intra_matrices;
00353 SpecifierOpt *inter_matrices;
00354 int nb_inter_matrices;
00355 SpecifierOpt *top_field_first;
00356 int nb_top_field_first;
00357 SpecifierOpt *presets;
00358 int nb_presets;
00359 SpecifierOpt *copy_initial_nonkeyframes;
00360 int nb_copy_initial_nonkeyframes;
00361 #if CONFIG_AVFILTER
00362 SpecifierOpt *filters;
00363 int nb_filters;
00364 #endif
00365 } OptionsContext;
00366
00367 #define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
00368 {\
00369 int i, ret;\
00370 for (i = 0; i < o->nb_ ## name; i++) {\
00371 char *spec = o->name[i].specifier;\
00372 if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0)\
00373 outvar = o->name[i].u.type;\
00374 else if (ret < 0)\
00375 exit_program(1);\
00376 }\
00377 }
00378
00379 static void reset_options(OptionsContext *o)
00380 {
00381 const OptionDef *po = options;
00382
00383
00384 while (po->name) {
00385 void *dst = (uint8_t*)o + po->u.off;
00386
00387 if (po->flags & OPT_SPEC) {
00388 SpecifierOpt **so = dst;
00389 int i, *count = (int*)(so + 1);
00390 for (i = 0; i < *count; i++) {
00391 av_freep(&(*so)[i].specifier);
00392 if (po->flags & OPT_STRING)
00393 av_freep(&(*so)[i].u.str);
00394 }
00395 av_freep(so);
00396 *count = 0;
00397 } else if (po->flags & OPT_OFFSET && po->flags & OPT_STRING)
00398 av_freep(dst);
00399 po++;
00400 }
00401
00402 av_freep(&o->stream_maps);
00403 av_freep(&o->meta_data_maps);
00404 av_freep(&o->streamid_map);
00405
00406 memset(o, 0, sizeof(*o));
00407
00408 o->mux_max_delay = 0.7;
00409 o->recording_time = INT64_MAX;
00410 o->limit_filesize = UINT64_MAX;
00411 o->chapters_input_file = INT_MAX;
00412
00413 uninit_opts();
00414 init_opts();
00415 }
00416
00417 #if CONFIG_AVFILTER
00418
00419 static int configure_video_filters(InputStream *ist, OutputStream *ost)
00420 {
00421 AVFilterContext *last_filter, *filter;
00423 AVCodecContext *codec = ost->st->codec;
00424 AVCodecContext *icodec = ist->st->codec;
00425 enum PixelFormat pix_fmts[] = { codec->pix_fmt, PIX_FMT_NONE };
00426 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
00427 AVRational sample_aspect_ratio;
00428 char args[255];
00429 int ret;
00430
00431 ost->graph = avfilter_graph_alloc();
00432
00433 if (ist->st->sample_aspect_ratio.num){
00434 sample_aspect_ratio = ist->st->sample_aspect_ratio;
00435 }else
00436 sample_aspect_ratio = ist->st->codec->sample_aspect_ratio;
00437
00438 snprintf(args, 255, "%d:%d:%d:%d:%d:%d:%d", ist->st->codec->width,
00439 ist->st->codec->height, ist->st->codec->pix_fmt, 1, AV_TIME_BASE,
00440 sample_aspect_ratio.num, sample_aspect_ratio.den);
00441
00442 ret = avfilter_graph_create_filter(&ost->input_video_filter, avfilter_get_by_name("buffer"),
00443 "src", args, NULL, ost->graph);
00444 if (ret < 0)
00445 return ret;
00446 #if FF_API_OLD_VSINK_API
00447 ret = avfilter_graph_create_filter(&ost->output_video_filter, avfilter_get_by_name("buffersink"),
00448 "out", NULL, pix_fmts, ost->graph);
00449 #else
00450 buffersink_params->pixel_fmts = pix_fmts;
00451 ret = avfilter_graph_create_filter(&ost->output_video_filter, avfilter_get_by_name("buffersink"),
00452 "out", NULL, buffersink_params, ost->graph);
00453 #endif
00454 av_freep(&buffersink_params);
00455 if (ret < 0)
00456 return ret;
00457 last_filter = ost->input_video_filter;
00458
00459 if (codec->width != icodec->width || codec->height != icodec->height) {
00460 snprintf(args, 255, "%d:%d:flags=0x%X",
00461 codec->width,
00462 codec->height,
00463 (unsigned)ost->sws_flags);
00464 if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
00465 NULL, args, NULL, ost->graph)) < 0)
00466 return ret;
00467 if ((ret = avfilter_link(last_filter, 0, filter, 0)) < 0)
00468 return ret;
00469 last_filter = filter;
00470 }
00471
00472 snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
00473 ost->graph->scale_sws_opts = av_strdup(args);
00474
00475 if (ost->avfilter) {
00476 AVFilterInOut *outputs = avfilter_inout_alloc();
00477 AVFilterInOut *inputs = avfilter_inout_alloc();
00478
00479 outputs->name = av_strdup("in");
00480 outputs->filter_ctx = last_filter;
00481 outputs->pad_idx = 0;
00482 outputs->next = NULL;
00483
00484 inputs->name = av_strdup("out");
00485 inputs->filter_ctx = ost->output_video_filter;
00486 inputs->pad_idx = 0;
00487 inputs->next = NULL;
00488
00489 if ((ret = avfilter_graph_parse(ost->graph, ost->avfilter, &inputs, &outputs, NULL)) < 0)
00490 return ret;
00491 av_freep(&ost->avfilter);
00492 } else {
00493 if ((ret = avfilter_link(last_filter, 0, ost->output_video_filter, 0)) < 0)
00494 return ret;
00495 }
00496
00497 if ((ret = avfilter_graph_config(ost->graph, NULL)) < 0)
00498 return ret;
00499
00500 codec->width = ost->output_video_filter->inputs[0]->w;
00501 codec->height = ost->output_video_filter->inputs[0]->h;
00502 codec->sample_aspect_ratio = ost->st->sample_aspect_ratio =
00503 ost->frame_aspect_ratio ?
00504 av_d2q(ost->frame_aspect_ratio*codec->height/codec->width, 255) :
00505 ost->output_video_filter->inputs[0]->sample_aspect_ratio;
00506
00507 return 0;
00508 }
00509 #endif
00510
00511 static void term_exit(void)
00512 {
00513 av_log(NULL, AV_LOG_QUIET, "%s", "");
00514 #if HAVE_TERMIOS_H
00515 if(!run_as_daemon)
00516 tcsetattr (0, TCSANOW, &oldtty);
00517 #endif
00518 }
00519
00520 static volatile int received_sigterm = 0;
00521
00522 static void
00523 sigterm_handler(int sig)
00524 {
00525 received_sigterm = sig;
00526 q_pressed++;
00527 term_exit();
00528 }
00529
00530 static void term_init(void)
00531 {
00532 #if HAVE_TERMIOS_H
00533 if(!run_as_daemon){
00534 struct termios tty;
00535
00536 tcgetattr (0, &tty);
00537 oldtty = tty;
00538 atexit(term_exit);
00539
00540 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
00541 |INLCR|IGNCR|ICRNL|IXON);
00542 tty.c_oflag |= OPOST;
00543 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
00544 tty.c_cflag &= ~(CSIZE|PARENB);
00545 tty.c_cflag |= CS8;
00546 tty.c_cc[VMIN] = 1;
00547 tty.c_cc[VTIME] = 0;
00548
00549 tcsetattr (0, TCSANOW, &tty);
00550 signal(SIGQUIT, sigterm_handler);
00551 }
00552 #endif
00553
00554 signal(SIGINT , sigterm_handler);
00555 signal(SIGTERM, sigterm_handler);
00556 #ifdef SIGXCPU
00557 signal(SIGXCPU, sigterm_handler);
00558 #endif
00559 }
00560
00561
00562 static int read_key(void)
00563 {
00564 #if HAVE_TERMIOS_H
00565 int n = 1;
00566 unsigned char ch;
00567 struct timeval tv;
00568 fd_set rfds;
00569
00570 if(run_as_daemon)
00571 return -1;
00572
00573 FD_ZERO(&rfds);
00574 FD_SET(0, &rfds);
00575 tv.tv_sec = 0;
00576 tv.tv_usec = 0;
00577 n = select(1, &rfds, NULL, NULL, &tv);
00578 if (n > 0) {
00579 n = read(0, &ch, 1);
00580 if (n == 1)
00581 return ch;
00582
00583 return n;
00584 }
00585 #elif HAVE_KBHIT
00586 if(kbhit())
00587 return(getch());
00588 #endif
00589 return -1;
00590 }
00591
00592 static int decode_interrupt_cb(void *ctx)
00593 {
00594 q_pressed += read_key() == 'q';
00595 return q_pressed > 1;
00596 }
00597
00598 static const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
00599
00600 void exit_program(int ret)
00601 {
00602 int i;
00603
00604
00605 for(i=0;i<nb_output_files;i++) {
00606 AVFormatContext *s = output_files[i].ctx;
00607 if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
00608 avio_close(s->pb);
00609 avformat_free_context(s);
00610 av_dict_free(&output_files[i].opts);
00611 }
00612 for(i=0;i<nb_input_files;i++) {
00613 av_close_input_file(input_files[i].ctx);
00614 }
00615 for (i = 0; i < nb_input_streams; i++) {
00616 av_freep(&input_streams[i].decoded_frame);
00617 av_freep(&input_streams[i].filtered_frame);
00618 av_dict_free(&input_streams[i].opts);
00619 }
00620
00621 if (vstats_file)
00622 fclose(vstats_file);
00623 av_free(vstats_filename);
00624
00625 av_freep(&input_streams);
00626 av_freep(&input_files);
00627 av_freep(&output_streams);
00628 av_freep(&output_files);
00629
00630 uninit_opts();
00631 av_free(audio_buf);
00632 av_free(audio_out);
00633 allocated_audio_buf_size= allocated_audio_out_size= 0;
00634
00635 #if CONFIG_AVFILTER
00636 avfilter_uninit();
00637 #endif
00638 avformat_network_deinit();
00639
00640 if (received_sigterm) {
00641 av_log(NULL, AV_LOG_INFO, "Received signal %d: terminating.\n",
00642 (int) received_sigterm);
00643 exit (255);
00644 }
00645
00646 exit(ret);
00647 }
00648
00649 static void assert_avoptions(AVDictionary *m)
00650 {
00651 AVDictionaryEntry *t;
00652 if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
00653 av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
00654 exit_program(1);
00655 }
00656 }
00657
00658 static void assert_codec_experimental(AVCodecContext *c, int encoder)
00659 {
00660 const char *codec_string = encoder ? "encoder" : "decoder";
00661 AVCodec *codec;
00662 if (c->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
00663 c->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
00664 av_log(NULL, AV_LOG_FATAL, "%s '%s' is experimental and might produce bad "
00665 "results.\nAdd '-strict experimental' if you want to use it.\n",
00666 codec_string, c->codec->name);
00667 codec = encoder ? avcodec_find_encoder(c->codec->id) : avcodec_find_decoder(c->codec->id);
00668 if (!(codec->capabilities & CODEC_CAP_EXPERIMENTAL))
00669 av_log(NULL, AV_LOG_FATAL, "Or use the non experimental %s '%s'.\n",
00670 codec_string, codec->name);
00671 exit_program(1);
00672 }
00673 }
00674
00675 static void choose_sample_fmt(AVStream *st, AVCodec *codec)
00676 {
00677 if(codec && codec->sample_fmts){
00678 const enum AVSampleFormat *p= codec->sample_fmts;
00679 for(; *p!=-1; p++){
00680 if(*p == st->codec->sample_fmt)
00681 break;
00682 }
00683 if (*p == -1) {
00684 if((codec->capabilities & CODEC_CAP_LOSSLESS) && av_get_sample_fmt_name(st->codec->sample_fmt) > av_get_sample_fmt_name(codec->sample_fmts[0]))
00685 av_log(NULL, AV_LOG_ERROR, "Convertion will not be lossless'\n");
00686 if(av_get_sample_fmt_name(st->codec->sample_fmt))
00687 av_log(NULL, AV_LOG_WARNING,
00688 "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
00689 av_get_sample_fmt_name(st->codec->sample_fmt),
00690 codec->name,
00691 av_get_sample_fmt_name(codec->sample_fmts[0]));
00692 st->codec->sample_fmt = codec->sample_fmts[0];
00693 }
00694 }
00695 }
00696
00697 static void choose_sample_rate(AVStream *st, AVCodec *codec)
00698 {
00699 if(codec && codec->supported_samplerates){
00700 const int *p= codec->supported_samplerates;
00701 int best=0;
00702 int best_dist=INT_MAX;
00703 for(; *p; p++){
00704 int dist= abs(st->codec->sample_rate - *p);
00705 if(dist < best_dist){
00706 best_dist= dist;
00707 best= *p;
00708 }
00709 }
00710 if(best_dist){
00711 av_log(st->codec, AV_LOG_WARNING, "Requested sampling rate unsupported using closest supported (%d)\n", best);
00712 }
00713 st->codec->sample_rate= best;
00714 }
00715 }
00716
00717 static void choose_pixel_fmt(AVStream *st, AVCodec *codec)
00718 {
00719 if(codec && codec->pix_fmts){
00720 const enum PixelFormat *p= codec->pix_fmts;
00721 if(st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL){
00722 if(st->codec->codec_id==CODEC_ID_MJPEG){
00723 p= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE};
00724 }else if(st->codec->codec_id==CODEC_ID_LJPEG){
00725 p= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_BGRA, PIX_FMT_NONE};
00726 }
00727 }
00728 for (; *p != PIX_FMT_NONE; p++) {
00729 if(*p == st->codec->pix_fmt)
00730 break;
00731 }
00732 if (*p == PIX_FMT_NONE) {
00733 if(st->codec->pix_fmt != PIX_FMT_NONE)
00734 av_log(NULL, AV_LOG_WARNING,
00735 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
00736 av_pix_fmt_descriptors[st->codec->pix_fmt].name,
00737 codec->name,
00738 av_pix_fmt_descriptors[codec->pix_fmts[0]].name);
00739 st->codec->pix_fmt = codec->pix_fmts[0];
00740 }
00741 }
00742 }
00743
00744 static double
00745 get_sync_ipts(const OutputStream *ost)
00746 {
00747 const InputStream *ist = ost->sync_ist;
00748 OutputFile *of = &output_files[ost->file_index];
00749 return (double)(ist->pts - of->start_time)/AV_TIME_BASE;
00750 }
00751
00752 static void write_frame(AVFormatContext *s, AVPacket *pkt, AVCodecContext *avctx, AVBitStreamFilterContext *bsfc){
00753 int ret;
00754
00755 while(bsfc){
00756 AVPacket new_pkt= *pkt;
00757 int a= av_bitstream_filter_filter(bsfc, avctx, NULL,
00758 &new_pkt.data, &new_pkt.size,
00759 pkt->data, pkt->size,
00760 pkt->flags & AV_PKT_FLAG_KEY);
00761 if(a>0){
00762 av_free_packet(pkt);
00763 new_pkt.destruct= av_destruct_packet;
00764 } else if(a<0){
00765 av_log(NULL, AV_LOG_ERROR, "%s failed for stream %d, codec %s",
00766 bsfc->filter->name, pkt->stream_index,
00767 avctx->codec ? avctx->codec->name : "copy");
00768 print_error("", a);
00769 if (exit_on_error)
00770 exit_program(1);
00771 }
00772 *pkt= new_pkt;
00773
00774 bsfc= bsfc->next;
00775 }
00776
00777 ret= av_interleaved_write_frame(s, pkt);
00778 if(ret < 0){
00779 print_error("av_interleaved_write_frame()", ret);
00780 exit_program(1);
00781 }
00782 }
00783
00784 static void generate_silence(uint8_t* buf, enum AVSampleFormat sample_fmt, size_t size)
00785 {
00786 int fill_char = 0x00;
00787 if (sample_fmt == AV_SAMPLE_FMT_U8)
00788 fill_char = 0x80;
00789 memset(buf, fill_char, size);
00790 }
00791
00792 static void do_audio_out(AVFormatContext *s, OutputStream *ost,
00793 InputStream *ist, AVFrame *decoded_frame)
00794 {
00795 uint8_t *buftmp;
00796 int64_t audio_out_size, audio_buf_size;
00797
00798 int size_out, frame_bytes, ret, resample_changed;
00799 AVCodecContext *enc= ost->st->codec;
00800 AVCodecContext *dec= ist->st->codec;
00801 int osize = av_get_bytes_per_sample(enc->sample_fmt);
00802 int isize = av_get_bytes_per_sample(dec->sample_fmt);
00803 const int coded_bps = av_get_bits_per_sample(enc->codec->id);
00804 uint8_t *buf = decoded_frame->data[0];
00805 int size = decoded_frame->nb_samples * dec->channels * isize;
00806 int64_t allocated_for_size = size;
00807
00808 need_realloc:
00809 audio_buf_size= (allocated_for_size + isize*dec->channels - 1) / (isize*dec->channels);
00810 audio_buf_size= (audio_buf_size*enc->sample_rate + dec->sample_rate) / dec->sample_rate;
00811 audio_buf_size= audio_buf_size*2 + 10000;
00812 audio_buf_size= FFMAX(audio_buf_size, enc->frame_size);
00813 audio_buf_size*= osize*enc->channels;
00814
00815 audio_out_size= FFMAX(audio_buf_size, enc->frame_size * osize * enc->channels);
00816 if(coded_bps > 8*osize)
00817 audio_out_size= audio_out_size * coded_bps / (8*osize);
00818 audio_out_size += FF_MIN_BUFFER_SIZE;
00819
00820 if(audio_out_size > INT_MAX || audio_buf_size > INT_MAX){
00821 av_log(NULL, AV_LOG_FATAL, "Buffer sizes too large\n");
00822 exit_program(1);
00823 }
00824
00825 av_fast_malloc(&audio_buf, &allocated_audio_buf_size, audio_buf_size);
00826 av_fast_malloc(&audio_out, &allocated_audio_out_size, audio_out_size);
00827 if (!audio_buf || !audio_out){
00828 av_log(NULL, AV_LOG_FATAL, "Out of memory in do_audio_out\n");
00829 exit_program(1);
00830 }
00831
00832 if (enc->channels != dec->channels)
00833 ost->audio_resample = 1;
00834
00835 resample_changed = ost->resample_sample_fmt != dec->sample_fmt ||
00836 ost->resample_channels != dec->channels ||
00837 ost->resample_sample_rate != dec->sample_rate;
00838
00839 if ((ost->audio_resample && !ost->resample) || resample_changed) {
00840 if (resample_changed) {
00841 av_log(NULL, AV_LOG_INFO, "Input stream #%d:%d frame changed from rate:%d fmt:%s ch:%d to rate:%d fmt:%s ch:%d\n",
00842 ist->file_index, ist->st->index,
00843 ost->resample_sample_rate, av_get_sample_fmt_name(ost->resample_sample_fmt), ost->resample_channels,
00844 dec->sample_rate, av_get_sample_fmt_name(dec->sample_fmt), dec->channels);
00845 ost->resample_sample_fmt = dec->sample_fmt;
00846 ost->resample_channels = dec->channels;
00847 ost->resample_sample_rate = dec->sample_rate;
00848 if (ost->resample)
00849 audio_resample_close(ost->resample);
00850 }
00851
00852 if (audio_sync_method <= 1 &&
00853 ost->resample_sample_fmt == enc->sample_fmt &&
00854 ost->resample_channels == enc->channels &&
00855 ost->resample_sample_rate == enc->sample_rate) {
00856 ost->resample = NULL;
00857 ost->audio_resample = 0;
00858 } else {
00859 if (dec->sample_fmt != AV_SAMPLE_FMT_S16)
00860 av_log(NULL, AV_LOG_WARNING, "Using s16 intermediate sample format for resampling\n");
00861 ost->resample = av_audio_resample_init(enc->channels, dec->channels,
00862 enc->sample_rate, dec->sample_rate,
00863 enc->sample_fmt, dec->sample_fmt,
00864 16, 10, 0, 0.8);
00865 if (!ost->resample) {
00866 av_log(NULL, AV_LOG_FATAL, "Can not resample %d channels @ %d Hz to %d channels @ %d Hz\n",
00867 dec->channels, dec->sample_rate,
00868 enc->channels, enc->sample_rate);
00869 exit_program(1);
00870 }
00871 }
00872 }
00873
00874 #define MAKE_SFMT_PAIR(a,b) ((a)+AV_SAMPLE_FMT_NB*(b))
00875 if (!ost->audio_resample && dec->sample_fmt!=enc->sample_fmt &&
00876 MAKE_SFMT_PAIR(enc->sample_fmt,dec->sample_fmt)!=ost->reformat_pair) {
00877 if (ost->reformat_ctx)
00878 av_audio_convert_free(ost->reformat_ctx);
00879 ost->reformat_ctx = av_audio_convert_alloc(enc->sample_fmt, 1,
00880 dec->sample_fmt, 1, NULL, 0);
00881 if (!ost->reformat_ctx) {
00882 av_log(NULL, AV_LOG_FATAL, "Cannot convert %s sample format to %s sample format\n",
00883 av_get_sample_fmt_name(dec->sample_fmt),
00884 av_get_sample_fmt_name(enc->sample_fmt));
00885 exit_program(1);
00886 }
00887 ost->reformat_pair=MAKE_SFMT_PAIR(enc->sample_fmt,dec->sample_fmt);
00888 }
00889
00890 if(audio_sync_method){
00891 double delta = get_sync_ipts(ost) * enc->sample_rate - ost->sync_opts
00892 - av_fifo_size(ost->fifo)/(enc->channels * osize);
00893 int idelta = delta * dec->sample_rate / enc->sample_rate;
00894 int byte_delta = idelta * isize * dec->channels;
00895
00896
00897 if(fabs(delta) > 50){
00898 if(ist->is_start || fabs(delta) > audio_drift_threshold*enc->sample_rate){
00899 if(byte_delta < 0){
00900 byte_delta= FFMAX(byte_delta, -size);
00901 size += byte_delta;
00902 buf -= byte_delta;
00903 av_log(NULL, AV_LOG_VERBOSE, "discarding %d audio samples\n",
00904 -byte_delta / (isize * dec->channels));
00905 if(!size)
00906 return;
00907 ist->is_start=0;
00908 }else{
00909 static uint8_t *input_tmp= NULL;
00910 input_tmp= av_realloc(input_tmp, byte_delta + size);
00911
00912 if(byte_delta > allocated_for_size - size){
00913 allocated_for_size= byte_delta + (int64_t)size;
00914 goto need_realloc;
00915 }
00916 ist->is_start=0;
00917
00918 generate_silence(input_tmp, dec->sample_fmt, byte_delta);
00919 memcpy(input_tmp + byte_delta, buf, size);
00920 buf= input_tmp;
00921 size += byte_delta;
00922 av_log(NULL, AV_LOG_VERBOSE, "adding %d audio samples of silence\n", idelta);
00923 }
00924 }else if(audio_sync_method>1){
00925 int comp= av_clip(delta, -audio_sync_method, audio_sync_method);
00926 av_assert0(ost->audio_resample);
00927 av_log(NULL, AV_LOG_VERBOSE, "compensating audio timestamp drift:%f compensation:%d in:%d\n",
00928 delta, comp, enc->sample_rate);
00929
00930 av_resample_compensate(*(struct AVResampleContext**)ost->resample, comp, enc->sample_rate);
00931 }
00932 }
00933 }else
00934 ost->sync_opts= lrintf(get_sync_ipts(ost) * enc->sample_rate)
00935 - av_fifo_size(ost->fifo)/(enc->channels * osize);
00936
00937 if (ost->audio_resample) {
00938 buftmp = audio_buf;
00939 size_out = audio_resample(ost->resample,
00940 (short *)buftmp, (short *)buf,
00941 size / (dec->channels * isize));
00942 size_out = size_out * enc->channels * osize;
00943 } else {
00944 buftmp = buf;
00945 size_out = size;
00946 }
00947
00948 if (!ost->audio_resample && dec->sample_fmt!=enc->sample_fmt) {
00949 const void *ibuf[6]= {buftmp};
00950 void *obuf[6]= {audio_buf};
00951 int istride[6]= {isize};
00952 int ostride[6]= {osize};
00953 int len= size_out/istride[0];
00954 if (av_audio_convert(ost->reformat_ctx, obuf, ostride, ibuf, istride, len)<0) {
00955 printf("av_audio_convert() failed\n");
00956 if (exit_on_error)
00957 exit_program(1);
00958 return;
00959 }
00960 buftmp = audio_buf;
00961 size_out = len*osize;
00962 }
00963
00964
00965 if (enc->frame_size > 1) {
00966
00967 if (av_fifo_realloc2(ost->fifo, av_fifo_size(ost->fifo) + size_out) < 0) {
00968 av_log(NULL, AV_LOG_FATAL, "av_fifo_realloc2() failed\n");
00969 exit_program(1);
00970 }
00971 av_fifo_generic_write(ost->fifo, buftmp, size_out, NULL);
00972
00973 frame_bytes = enc->frame_size * osize * enc->channels;
00974
00975 while (av_fifo_size(ost->fifo) >= frame_bytes) {
00976 AVPacket pkt;
00977 av_init_packet(&pkt);
00978
00979 av_fifo_generic_read(ost->fifo, audio_buf, frame_bytes, NULL);
00980
00981
00982
00983 ret = avcodec_encode_audio(enc, audio_out, audio_out_size,
00984 (short *)audio_buf);
00985 if (ret < 0) {
00986 av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
00987 exit_program(1);
00988 }
00989 audio_size += ret;
00990 pkt.stream_index= ost->index;
00991 pkt.data= audio_out;
00992 pkt.size= ret;
00993 if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
00994 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
00995 pkt.flags |= AV_PKT_FLAG_KEY;
00996 write_frame(s, &pkt, enc, ost->bitstream_filters);
00997
00998 ost->sync_opts += enc->frame_size;
00999 }
01000 } else {
01001 AVPacket pkt;
01002 av_init_packet(&pkt);
01003
01004 ost->sync_opts += size_out / (osize * enc->channels);
01005
01006
01007
01008 size_out /= osize;
01009 if (coded_bps)
01010 size_out = size_out*coded_bps/8;
01011
01012 if(size_out > audio_out_size){
01013 av_log(NULL, AV_LOG_FATAL, "Internal error, buffer size too small\n");
01014 exit_program(1);
01015 }
01016
01017
01018 ret = avcodec_encode_audio(enc, audio_out, size_out,
01019 (short *)buftmp);
01020 if (ret < 0) {
01021 av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
01022 exit_program(1);
01023 }
01024 audio_size += ret;
01025 pkt.stream_index= ost->index;
01026 pkt.data= audio_out;
01027 pkt.size= ret;
01028 if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
01029 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
01030 pkt.flags |= AV_PKT_FLAG_KEY;
01031 write_frame(s, &pkt, enc, ost->bitstream_filters);
01032 }
01033 }
01034
01035 static void pre_process_video_frame(InputStream *ist, AVPicture *picture, void **bufp)
01036 {
01037 AVCodecContext *dec;
01038 AVPicture *picture2;
01039 AVPicture picture_tmp;
01040 uint8_t *buf = 0;
01041
01042 dec = ist->st->codec;
01043
01044
01045 if (do_deinterlace) {
01046 int size;
01047
01048
01049 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
01050 buf = av_malloc(size);
01051 if (!buf)
01052 return;
01053
01054 picture2 = &picture_tmp;
01055 avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
01056
01057 if(avpicture_deinterlace(picture2, picture,
01058 dec->pix_fmt, dec->width, dec->height) < 0) {
01059
01060 av_log(NULL, AV_LOG_WARNING, "Deinterlacing failed\n");
01061 av_free(buf);
01062 buf = NULL;
01063 picture2 = picture;
01064 }
01065 } else {
01066 picture2 = picture;
01067 }
01068
01069 if (picture != picture2)
01070 *picture = *picture2;
01071 *bufp = buf;
01072 }
01073
01074 static void do_subtitle_out(AVFormatContext *s,
01075 OutputStream *ost,
01076 InputStream *ist,
01077 AVSubtitle *sub,
01078 int64_t pts)
01079 {
01080 static uint8_t *subtitle_out = NULL;
01081 int subtitle_out_max_size = 1024 * 1024;
01082 int subtitle_out_size, nb, i;
01083 AVCodecContext *enc;
01084 AVPacket pkt;
01085
01086 if (pts == AV_NOPTS_VALUE) {
01087 av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
01088 if (exit_on_error)
01089 exit_program(1);
01090 return;
01091 }
01092
01093 enc = ost->st->codec;
01094
01095 if (!subtitle_out) {
01096 subtitle_out = av_malloc(subtitle_out_max_size);
01097 }
01098
01099
01100
01101
01102 if (enc->codec_id == CODEC_ID_DVB_SUBTITLE)
01103 nb = 2;
01104 else
01105 nb = 1;
01106
01107 for(i = 0; i < nb; i++) {
01108 sub->pts = av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q);
01109
01110 sub->pts += av_rescale_q(sub->start_display_time, (AVRational){1, 1000}, AV_TIME_BASE_Q);
01111 sub->end_display_time -= sub->start_display_time;
01112 sub->start_display_time = 0;
01113 subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
01114 subtitle_out_max_size, sub);
01115 if (subtitle_out_size < 0) {
01116 av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
01117 exit_program(1);
01118 }
01119
01120 av_init_packet(&pkt);
01121 pkt.stream_index = ost->index;
01122 pkt.data = subtitle_out;
01123 pkt.size = subtitle_out_size;
01124 pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base);
01125 if (enc->codec_id == CODEC_ID_DVB_SUBTITLE) {
01126
01127
01128 if (i == 0)
01129 pkt.pts += 90 * sub->start_display_time;
01130 else
01131 pkt.pts += 90 * sub->end_display_time;
01132 }
01133 write_frame(s, &pkt, ost->st->codec, ost->bitstream_filters);
01134 }
01135 }
01136
01137 static int bit_buffer_size= 1024*256;
01138 static uint8_t *bit_buffer= NULL;
01139
01140 static void do_video_resample(OutputStream *ost,
01141 InputStream *ist,
01142 AVFrame *in_picture,
01143 AVFrame **out_picture)
01144 {
01145 int resample_changed = 0;
01146 AVCodecContext *dec = ist->st->codec;
01147 AVCodecContext *enc = ost->st->codec;
01148 *out_picture = in_picture;
01149
01150 resample_changed = ost->resample_width != dec->width ||
01151 ost->resample_height != dec->height ||
01152 ost->resample_pix_fmt != dec->pix_fmt;
01153
01154 #if !CONFIG_AVFILTER
01155 if (resample_changed) {
01156 av_log(NULL, AV_LOG_INFO,
01157 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
01158 ist->file_index, ist->st->index,
01159 ost->resample_width, ost->resample_height, av_get_pix_fmt_name(ost->resample_pix_fmt),
01160 dec->width , dec->height , av_get_pix_fmt_name(dec->pix_fmt));
01161 ost->resample_width = dec->width;
01162 ost->resample_height = dec->height;
01163 ost->resample_pix_fmt = dec->pix_fmt;
01164 }
01165
01166 ost->video_resample = dec->width != enc->width ||
01167 dec->height != enc->height ||
01168 dec->pix_fmt != enc->pix_fmt;
01169
01170 if (ost->video_resample) {
01171 *out_picture = &ost->resample_frame;
01172 if (!ost->img_resample_ctx || resample_changed) {
01173
01174 if (!ost->resample_frame.data[0]) {
01175 avcodec_get_frame_defaults(&ost->resample_frame);
01176 if (avpicture_alloc((AVPicture *)&ost->resample_frame, enc->pix_fmt,
01177 enc->width, enc->height)) {
01178 fprintf(stderr, "Cannot allocate temp picture, check pix fmt\n");
01179 exit_program(1);
01180 }
01181 }
01182
01183 sws_freeContext(ost->img_resample_ctx);
01184 ost->img_resample_ctx = sws_getContext(dec->width, dec->height, dec->pix_fmt,
01185 enc->width, enc->height, enc->pix_fmt,
01186 ost->sws_flags, NULL, NULL, NULL);
01187 if (ost->img_resample_ctx == NULL) {
01188 av_log(NULL, AV_LOG_FATAL, "Cannot get resampling context\n");
01189 exit_program(1);
01190 }
01191 }
01192 sws_scale(ost->img_resample_ctx, in_picture->data, in_picture->linesize,
01193 0, ost->resample_height, (*out_picture)->data, (*out_picture)->linesize);
01194 }
01195 #else
01196 if (resample_changed) {
01197 avfilter_graph_free(&ost->graph);
01198 if (configure_video_filters(ist, ost)) {
01199 av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n");
01200 exit_program(1);
01201 }
01202 }
01203 #endif
01204 if (resample_changed) {
01205 ost->resample_width = dec->width;
01206 ost->resample_height = dec->height;
01207 ost->resample_pix_fmt = dec->pix_fmt;
01208 }
01209 }
01210
01211
01212 static void do_video_out(AVFormatContext *s,
01213 OutputStream *ost,
01214 InputStream *ist,
01215 AVFrame *in_picture,
01216 int *frame_size, float quality)
01217 {
01218 int nb_frames, i, ret, format_video_sync;
01219 AVFrame *final_picture;
01220 AVCodecContext *enc;
01221 double sync_ipts;
01222
01223 enc = ost->st->codec;
01224
01225 sync_ipts = get_sync_ipts(ost) / av_q2d(enc->time_base);
01226
01227
01228 nb_frames = 1;
01229
01230 *frame_size = 0;
01231
01232 format_video_sync = video_sync_method;
01233 if (format_video_sync < 0)
01234 format_video_sync = (s->oformat->flags & AVFMT_NOTIMESTAMPS) ? 0 :
01235 (s->oformat->flags & AVFMT_VARIABLE_FPS) ? 2 : 1;
01236
01237 if (format_video_sync) {
01238 double vdelta = sync_ipts - ost->sync_opts;
01239
01240 if (vdelta < -1.1)
01241 nb_frames = 0;
01242 else if (format_video_sync == 2) {
01243 if(vdelta<=-0.6){
01244 nb_frames=0;
01245 }else if(vdelta>0.6)
01246 ost->sync_opts= lrintf(sync_ipts);
01247 }else if (vdelta > 1.1)
01248 nb_frames = lrintf(vdelta);
01249
01250 if (nb_frames == 0){
01251 ++nb_frames_drop;
01252 av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n");
01253 }else if (nb_frames > 1) {
01254 nb_frames_dup += nb_frames - 1;
01255 av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", nb_frames-1);
01256 }
01257 }else
01258 ost->sync_opts= lrintf(sync_ipts);
01259
01260 nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number);
01261 if (nb_frames <= 0)
01262 return;
01263
01264 do_video_resample(ost, ist, in_picture, &final_picture);
01265
01266
01267 for(i=0;i<nb_frames;i++) {
01268 AVPacket pkt;
01269 av_init_packet(&pkt);
01270 pkt.stream_index= ost->index;
01271
01272 if (s->oformat->flags & AVFMT_RAWPICTURE &&
01273 enc->codec->id == CODEC_ID_RAWVIDEO) {
01274
01275
01276
01277 enc->coded_frame->interlaced_frame = in_picture->interlaced_frame;
01278 enc->coded_frame->top_field_first = in_picture->top_field_first;
01279 pkt.data= (uint8_t *)final_picture;
01280 pkt.size= sizeof(AVPicture);
01281 pkt.pts= av_rescale_q(ost->sync_opts, enc->time_base, ost->st->time_base);
01282 pkt.flags |= AV_PKT_FLAG_KEY;
01283
01284 write_frame(s, &pkt, ost->st->codec, ost->bitstream_filters);
01285 } else {
01286 AVFrame big_picture;
01287
01288 big_picture= *final_picture;
01289
01290
01291 big_picture.interlaced_frame = in_picture->interlaced_frame;
01292 if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)) {
01293 if (ost->top_field_first == -1)
01294 big_picture.top_field_first = in_picture->top_field_first;
01295 else
01296 big_picture.top_field_first = !!ost->top_field_first;
01297 }
01298
01299
01300
01301 big_picture.quality = quality;
01302 if (!enc->me_threshold)
01303 big_picture.pict_type = 0;
01304
01305 big_picture.pts= ost->sync_opts;
01306
01307
01308 if (ost->forced_kf_index < ost->forced_kf_count &&
01309 big_picture.pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
01310 big_picture.pict_type = AV_PICTURE_TYPE_I;
01311 ost->forced_kf_index++;
01312 }
01313 ret = avcodec_encode_video(enc,
01314 bit_buffer, bit_buffer_size,
01315 &big_picture);
01316 if (ret < 0) {
01317 av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
01318 exit_program(1);
01319 }
01320
01321 if(ret>0){
01322 pkt.data= bit_buffer;
01323 pkt.size= ret;
01324 if(enc->coded_frame->pts != AV_NOPTS_VALUE)
01325 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
01326
01327
01328
01329
01330 if(enc->coded_frame->key_frame)
01331 pkt.flags |= AV_PKT_FLAG_KEY;
01332 write_frame(s, &pkt, ost->st->codec, ost->bitstream_filters);
01333 *frame_size = ret;
01334 video_size += ret;
01335
01336
01337
01338 if (ost->logfile && enc->stats_out) {
01339 fprintf(ost->logfile, "%s", enc->stats_out);
01340 }
01341 }
01342 }
01343 ost->sync_opts++;
01344 ost->frame_number++;
01345 }
01346 }
01347
01348 static double psnr(double d){
01349 return -10.0*log(d)/log(10.0);
01350 }
01351
01352 static void do_video_stats(AVFormatContext *os, OutputStream *ost,
01353 int frame_size)
01354 {
01355 AVCodecContext *enc;
01356 int frame_number;
01357 double ti1, bitrate, avg_bitrate;
01358
01359
01360 if (!vstats_file) {
01361 vstats_file = fopen(vstats_filename, "w");
01362 if (!vstats_file) {
01363 perror("fopen");
01364 exit_program(1);
01365 }
01366 }
01367
01368 enc = ost->st->codec;
01369 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
01370 frame_number = ost->frame_number;
01371 fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);
01372 if (enc->flags&CODEC_FLAG_PSNR)
01373 fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
01374
01375 fprintf(vstats_file,"f_size= %6d ", frame_size);
01376
01377 ti1 = ost->sync_opts * av_q2d(enc->time_base);
01378 if (ti1 < 0.01)
01379 ti1 = 0.01;
01380
01381 bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
01382 avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
01383 fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
01384 (double)video_size / 1024, ti1, bitrate, avg_bitrate);
01385 fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type));
01386 }
01387 }
01388
01389 static void print_report(OutputFile *output_files,
01390 OutputStream *ost_table, int nb_ostreams,
01391 int is_last_report, int64_t timer_start)
01392 {
01393 char buf[1024];
01394 OutputStream *ost;
01395 AVFormatContext *oc;
01396 int64_t total_size;
01397 AVCodecContext *enc;
01398 int frame_number, vid, i;
01399 double bitrate;
01400 int64_t pts = INT64_MAX;
01401 static int64_t last_time = -1;
01402 static int qp_histogram[52];
01403 int hours, mins, secs, us;
01404
01405 if (!print_stats && !is_last_report)
01406 return;
01407
01408 if (!is_last_report) {
01409 int64_t cur_time;
01410
01411 cur_time = av_gettime();
01412 if (last_time == -1) {
01413 last_time = cur_time;
01414 return;
01415 }
01416 if ((cur_time - last_time) < 500000)
01417 return;
01418 last_time = cur_time;
01419 }
01420
01421
01422 oc = output_files[0].ctx;
01423
01424 total_size = avio_size(oc->pb);
01425 if(total_size<0)
01426 total_size= avio_tell(oc->pb);
01427
01428 buf[0] = '\0';
01429 vid = 0;
01430 for(i=0;i<nb_ostreams;i++) {
01431 float q = -1;
01432 ost = &ost_table[i];
01433 enc = ost->st->codec;
01434 if (!ost->stream_copy && enc->coded_frame)
01435 q = enc->coded_frame->quality/(float)FF_QP2LAMBDA;
01436 if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
01437 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q);
01438 }
01439 if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
01440 float t = (av_gettime()-timer_start) / 1000000.0;
01441
01442 frame_number = ost->frame_number;
01443 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3d q=%3.1f ",
01444 frame_number, (t>1)?(int)(frame_number/t+0.5) : 0, q);
01445 if(is_last_report)
01446 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
01447 if(qp_hist){
01448 int j;
01449 int qp = lrintf(q);
01450 if(qp>=0 && qp<FF_ARRAY_ELEMS(qp_histogram))
01451 qp_histogram[qp]++;
01452 for(j=0; j<32; j++)
01453 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log(qp_histogram[j]+1)/log(2)));
01454 }
01455 if (enc->flags&CODEC_FLAG_PSNR){
01456 int j;
01457 double error, error_sum=0;
01458 double scale, scale_sum=0;
01459 char type[3]= {'Y','U','V'};
01460 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
01461 for(j=0; j<3; j++){
01462 if(is_last_report){
01463 error= enc->error[j];
01464 scale= enc->width*enc->height*255.0*255.0*frame_number;
01465 }else{
01466 error= enc->coded_frame->error[j];
01467 scale= enc->width*enc->height*255.0*255.0;
01468 }
01469 if(j) scale/=4;
01470 error_sum += error;
01471 scale_sum += scale;
01472 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error/scale));
01473 }
01474 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum/scale_sum));
01475 }
01476 vid = 1;
01477 }
01478
01479 pts = FFMIN(pts, av_rescale_q(ost->st->pts.val,
01480 ost->st->time_base, AV_TIME_BASE_Q));
01481 }
01482
01483 secs = pts / AV_TIME_BASE;
01484 us = pts % AV_TIME_BASE;
01485 mins = secs / 60;
01486 secs %= 60;
01487 hours = mins / 60;
01488 mins %= 60;
01489
01490 bitrate = pts ? total_size * 8 / (pts / 1000.0) : 0;
01491
01492 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
01493 "size=%8.0fkB time=", total_size / 1024.0);
01494 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
01495 "%02d:%02d:%02d.%02d ", hours, mins, secs,
01496 (100 * us) / AV_TIME_BASE);
01497 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
01498 "bitrate=%6.1fkbits/s", bitrate);
01499
01500 if (nb_frames_dup || nb_frames_drop)
01501 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
01502 nb_frames_dup, nb_frames_drop);
01503
01504 av_log(NULL, AV_LOG_INFO, "%s \r", buf);
01505
01506 fflush(stderr);
01507
01508 if (is_last_report) {
01509 int64_t raw= audio_size + video_size + extra_size;
01510 av_log(NULL, AV_LOG_INFO, "\n");
01511 av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
01512 video_size/1024.0,
01513 audio_size/1024.0,
01514 extra_size/1024.0,
01515 100.0*(total_size - raw)/raw
01516 );
01517 }
01518 }
01519
01520 static void flush_encoders(OutputStream *ost_table, int nb_ostreams)
01521 {
01522 int i, ret;
01523
01524 for (i = 0; i < nb_ostreams; i++) {
01525 OutputStream *ost = &ost_table[i];
01526 AVCodecContext *enc = ost->st->codec;
01527 AVFormatContext *os = output_files[ost->file_index].ctx;
01528
01529 if (!ost->encoding_needed)
01530 continue;
01531
01532 if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <=1)
01533 continue;
01534 if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == CODEC_ID_RAWVIDEO)
01535 continue;
01536
01537 for(;;) {
01538 AVPacket pkt;
01539 int fifo_bytes;
01540 av_init_packet(&pkt);
01541 pkt.stream_index= ost->index;
01542
01543 switch (ost->st->codec->codec_type) {
01544 case AVMEDIA_TYPE_AUDIO:
01545 fifo_bytes = av_fifo_size(ost->fifo);
01546 ret = 0;
01547
01548 if (fifo_bytes > 0) {
01549 int osize = av_get_bytes_per_sample(enc->sample_fmt);
01550 int fs_tmp = enc->frame_size;
01551
01552 av_fifo_generic_read(ost->fifo, audio_buf, fifo_bytes, NULL);
01553 if (enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
01554 enc->frame_size = fifo_bytes / (osize * enc->channels);
01555 } else {
01556 int frame_bytes = enc->frame_size*osize*enc->channels;
01557 if (allocated_audio_buf_size < frame_bytes)
01558 exit_program(1);
01559 generate_silence(audio_buf+fifo_bytes, enc->sample_fmt, frame_bytes - fifo_bytes);
01560 }
01561
01562 ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, (short *)audio_buf);
01563 pkt.duration = av_rescale((int64_t)enc->frame_size*ost->st->time_base.den,
01564 ost->st->time_base.num, enc->sample_rate);
01565 enc->frame_size = fs_tmp;
01566 }
01567 if (ret <= 0) {
01568 ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL);
01569 }
01570 if (ret < 0) {
01571 av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
01572 exit_program(1);
01573 }
01574 audio_size += ret;
01575 pkt.flags |= AV_PKT_FLAG_KEY;
01576 break;
01577 case AVMEDIA_TYPE_VIDEO:
01578 ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL);
01579 if (ret < 0) {
01580 av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
01581 exit_program(1);
01582 }
01583 video_size += ret;
01584 if(enc->coded_frame && enc->coded_frame->key_frame)
01585 pkt.flags |= AV_PKT_FLAG_KEY;
01586 if (ost->logfile && enc->stats_out) {
01587 fprintf(ost->logfile, "%s", enc->stats_out);
01588 }
01589 break;
01590 default:
01591 ret=-1;
01592 }
01593
01594 if (ret <= 0)
01595 break;
01596 pkt.data = bit_buffer;
01597 pkt.size = ret;
01598 if (enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
01599 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
01600 write_frame(os, &pkt, ost->st->codec, ost->bitstream_filters);
01601 }
01602 }
01603 }
01604
01605
01606
01607
01608 static int check_output_constraints(InputStream *ist, OutputStream *ost)
01609 {
01610 OutputFile *of = &output_files[ost->file_index];
01611 int ist_index = ist - input_streams;
01612
01613 if (ost->source_index != ist_index)
01614 return 0;
01615
01616 if (of->start_time && ist->pts < of->start_time)
01617 return 0;
01618
01619 if (of->recording_time != INT64_MAX &&
01620 av_compare_ts(ist->pts, AV_TIME_BASE_Q, of->recording_time + of->start_time,
01621 (AVRational){1, 1000000}) >= 0) {
01622 ost->is_past_recording_time = 1;
01623 return 0;
01624 }
01625
01626 return 1;
01627 }
01628
01629 static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
01630 {
01631 OutputFile *of = &output_files[ost->file_index];
01632 int64_t ost_tb_start_time = av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base);
01633 AVPicture pict;
01634 AVPacket opkt;
01635
01636 av_init_packet(&opkt);
01637
01638 if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
01639 !ost->copy_initial_nonkeyframes)
01640 return;
01641
01642
01643 if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
01644 audio_size += pkt->size;
01645 else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01646 video_size += pkt->size;
01647 ost->sync_opts++;
01648 }
01649
01650 opkt.stream_index = ost->index;
01651 if (pkt->pts != AV_NOPTS_VALUE)
01652 opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;
01653 else
01654 opkt.pts = AV_NOPTS_VALUE;
01655
01656 if (pkt->dts == AV_NOPTS_VALUE)
01657 opkt.dts = av_rescale_q(ist->pts, AV_TIME_BASE_Q, ost->st->time_base);
01658 else
01659 opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);
01660 opkt.dts -= ost_tb_start_time;
01661
01662 opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base);
01663 opkt.flags = pkt->flags;
01664
01665
01666 if( ost->st->codec->codec_id != CODEC_ID_H264
01667 && ost->st->codec->codec_id != CODEC_ID_MPEG1VIDEO
01668 && ost->st->codec->codec_id != CODEC_ID_MPEG2VIDEO
01669 ) {
01670 if (av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY))
01671 opkt.destruct = av_destruct_packet;
01672 } else {
01673 opkt.data = pkt->data;
01674 opkt.size = pkt->size;
01675 }
01676 if (of->ctx->oformat->flags & AVFMT_RAWPICTURE) {
01677
01678 avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height);
01679 opkt.data = (uint8_t *)&pict;
01680 opkt.size = sizeof(AVPicture);
01681 opkt.flags |= AV_PKT_FLAG_KEY;
01682 }
01683
01684 write_frame(of->ctx, &opkt, ost->st->codec, ost->bitstream_filters);
01685 ost->st->codec->frame_number++;
01686 ost->frame_number++;
01687 av_free_packet(&opkt);
01688 }
01689
01690 static void rate_emu_sleep(InputStream *ist)
01691 {
01692 if (input_files[ist->file_index].rate_emu) {
01693 int64_t pts = av_rescale(ist->pts, 1000000, AV_TIME_BASE);
01694 int64_t now = av_gettime() - ist->start;
01695 if (pts > now)
01696 usleep(pts - now);
01697 }
01698 }
01699
01700 static int transcode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
01701 {
01702 AVFrame *decoded_frame;
01703 AVCodecContext *avctx = ist->st->codec;
01704 int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt);
01705 int i, ret;
01706
01707 if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
01708 return AVERROR(ENOMEM);
01709 else
01710 avcodec_get_frame_defaults(ist->decoded_frame);
01711 decoded_frame = ist->decoded_frame;
01712
01713 ret = avcodec_decode_audio4(avctx, decoded_frame, got_output, pkt);
01714 if (ret < 0) {
01715 return ret;
01716 }
01717
01718 if (!*got_output) {
01719
01720 return ret;
01721 }
01722
01723
01724
01725 if (decoded_frame->pts != AV_NOPTS_VALUE)
01726 ist->next_pts = decoded_frame->pts;
01727
01728
01729
01730 ist->next_pts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
01731 avctx->sample_rate;
01732
01733
01734 if (audio_volume != 256) {
01735 int decoded_data_size = decoded_frame->nb_samples * avctx->channels * bps;
01736 void *samples = decoded_frame->data[0];
01737 switch (avctx->sample_fmt) {
01738 case AV_SAMPLE_FMT_U8:
01739 {
01740 uint8_t *volp = samples;
01741 for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
01742 int v = (((*volp - 128) * audio_volume + 128) >> 8) + 128;
01743 *volp++ = av_clip_uint8(v);
01744 }
01745 break;
01746 }
01747 case AV_SAMPLE_FMT_S16:
01748 {
01749 int16_t *volp = samples;
01750 for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
01751 int v = ((*volp) * audio_volume + 128) >> 8;
01752 *volp++ = av_clip_int16(v);
01753 }
01754 break;
01755 }
01756 case AV_SAMPLE_FMT_S32:
01757 {
01758 int32_t *volp = samples;
01759 for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
01760 int64_t v = (((int64_t)*volp * audio_volume + 128) >> 8);
01761 *volp++ = av_clipl_int32(v);
01762 }
01763 break;
01764 }
01765 case AV_SAMPLE_FMT_FLT:
01766 {
01767 float *volp = samples;
01768 float scale = audio_volume / 256.f;
01769 for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
01770 *volp++ *= scale;
01771 }
01772 break;
01773 }
01774 case AV_SAMPLE_FMT_DBL:
01775 {
01776 double *volp = samples;
01777 double scale = audio_volume / 256.;
01778 for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
01779 *volp++ *= scale;
01780 }
01781 break;
01782 }
01783 default:
01784 av_log(NULL, AV_LOG_FATAL,
01785 "Audio volume adjustment on sample format %s is not supported.\n",
01786 av_get_sample_fmt_name(ist->st->codec->sample_fmt));
01787 exit_program(1);
01788 }
01789 }
01790
01791 rate_emu_sleep(ist);
01792
01793 for (i = 0; i < nb_output_streams; i++) {
01794 OutputStream *ost = &output_streams[i];
01795
01796 if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
01797 continue;
01798 do_audio_out(output_files[ost->file_index].ctx, ost, ist, decoded_frame);
01799 }
01800
01801 return ret;
01802 }
01803
01804 static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int64_t *pkt_pts)
01805 {
01806 AVFrame *decoded_frame, *filtered_frame = NULL;
01807 void *buffer_to_free = NULL;
01808 int i, ret = 0;
01809 float quality;
01810 #if CONFIG_AVFILTER
01811 int frame_available = 1;
01812 #endif
01813
01814 if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
01815 return AVERROR(ENOMEM);
01816 else
01817 avcodec_get_frame_defaults(ist->decoded_frame);
01818 decoded_frame = ist->decoded_frame;
01819 pkt->pts = *pkt_pts;
01820 pkt->dts = ist->pts;
01821 *pkt_pts = AV_NOPTS_VALUE;
01822
01823 ret = avcodec_decode_video2(ist->st->codec,
01824 decoded_frame, got_output, pkt);
01825 if (ret < 0)
01826 return ret;
01827
01828 quality = same_quant ? decoded_frame->quality : 0;
01829 if (!*got_output) {
01830
01831 return ret;
01832 }
01833 ist->next_pts = ist->pts = decoded_frame->best_effort_timestamp;
01834 if (pkt->duration)
01835 ist->next_pts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
01836 else if (ist->st->codec->time_base.num != 0) {
01837 int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 :
01838 ist->st->codec->ticks_per_frame;
01839 ist->next_pts += ((int64_t)AV_TIME_BASE *
01840 ist->st->codec->time_base.num * ticks) /
01841 ist->st->codec->time_base.den;
01842 }
01843 pkt->size = 0;
01844 pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
01845
01846 rate_emu_sleep(ist);
01847
01848 for (i = 0; i < nb_output_streams; i++) {
01849 OutputStream *ost = &output_streams[i];
01850 int frame_size;
01851
01852 if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
01853 continue;
01854
01855 #if CONFIG_AVFILTER
01856 if (ost->input_video_filter) {
01857 if (!decoded_frame->sample_aspect_ratio.num)
01858 decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
01859 decoded_frame->pts = ist->pts;
01860
01861 av_vsrc_buffer_add_frame(ost->input_video_filter, decoded_frame, AV_VSRC_BUF_FLAG_OVERWRITE);
01862 if (!ist->filtered_frame && !(ist->filtered_frame = avcodec_alloc_frame())) {
01863 av_free(buffer_to_free);
01864 return AVERROR(ENOMEM);
01865 } else
01866 avcodec_get_frame_defaults(ist->filtered_frame);
01867 filtered_frame = ist->filtered_frame;
01868 frame_available = avfilter_poll_frame(ost->output_video_filter->inputs[0]);
01869 }
01870 while (frame_available) {
01871 if (ost->output_video_filter) {
01872 AVRational ist_pts_tb = ost->output_video_filter->inputs[0]->time_base;
01873 if (av_buffersink_get_buffer_ref(ost->output_video_filter, &ost->picref, 0) < 0)
01874 goto cont;
01875 if (ost->picref) {
01876 avfilter_fill_frame_from_video_buffer_ref(filtered_frame, ost->picref);
01877 ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
01878 }
01879 }
01880 if (ost->picref->video && !ost->frame_aspect_ratio)
01881 ost->st->codec->sample_aspect_ratio = ost->picref->video->sample_aspect_ratio;
01882 #else
01883 filtered_frame = decoded_frame;
01884 #endif
01885
01886 do_video_out(output_files[ost->file_index].ctx, ost, ist, filtered_frame, &frame_size,
01887 same_quant ? quality : ost->st->codec->global_quality);
01888 if (vstats_filename && frame_size)
01889 do_video_stats(output_files[ost->file_index].ctx, ost, frame_size);
01890 #if CONFIG_AVFILTER
01891 cont:
01892 frame_available = ost->output_video_filter && avfilter_poll_frame(ost->output_video_filter->inputs[0]);
01893 if (ost->picref)
01894 avfilter_unref_buffer(ost->picref);
01895 }
01896 #endif
01897 }
01898
01899 av_free(buffer_to_free);
01900 return ret;
01901 }
01902
01903 static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
01904 {
01905 AVSubtitle subtitle;
01906 int i, ret = avcodec_decode_subtitle2(ist->st->codec,
01907 &subtitle, got_output, pkt);
01908 if (ret < 0)
01909 return ret;
01910 if (!*got_output)
01911 return ret;
01912
01913 rate_emu_sleep(ist);
01914
01915 for (i = 0; i < nb_output_streams; i++) {
01916 OutputStream *ost = &output_streams[i];
01917
01918 if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
01919 continue;
01920
01921 do_subtitle_out(output_files[ost->file_index].ctx, ost, ist, &subtitle, pkt->pts);
01922 }
01923
01924 avsubtitle_free(&subtitle);
01925 return ret;
01926 }
01927
01928
01929 static int output_packet(InputStream *ist,
01930 OutputStream *ost_table, int nb_ostreams,
01931 const AVPacket *pkt)
01932 {
01933 int i;
01934 int got_output;
01935 int64_t pkt_pts = AV_NOPTS_VALUE;
01936 AVPacket avpkt;
01937
01938 if (ist->next_pts == AV_NOPTS_VALUE)
01939 ist->next_pts = ist->pts;
01940
01941 if (pkt == NULL) {
01942
01943 av_init_packet(&avpkt);
01944 avpkt.data = NULL;
01945 avpkt.size = 0;
01946 goto handle_eof;
01947 } else {
01948 avpkt = *pkt;
01949 }
01950
01951 if(pkt->dts != AV_NOPTS_VALUE)
01952 ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
01953 if(pkt->pts != AV_NOPTS_VALUE)
01954 pkt_pts = av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
01955
01956
01957 while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {
01958 int ret = 0;
01959 handle_eof:
01960
01961 ist->pts = ist->next_pts;
01962
01963 if (avpkt.size && avpkt.size != pkt->size) {
01964 av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,
01965 "Multiple frames in a packet from stream %d\n", pkt->stream_index);
01966 ist->showed_multi_packet_warning = 1;
01967 }
01968
01969 switch(ist->st->codec->codec_type) {
01970 case AVMEDIA_TYPE_AUDIO:
01971 ret = transcode_audio (ist, &avpkt, &got_output);
01972 break;
01973 case AVMEDIA_TYPE_VIDEO:
01974 ret = transcode_video (ist, &avpkt, &got_output, &pkt_pts);
01975 break;
01976 case AVMEDIA_TYPE_SUBTITLE:
01977 ret = transcode_subtitles(ist, &avpkt, &got_output);
01978 break;
01979 default:
01980 return -1;
01981 }
01982
01983 if (ret < 0)
01984 return ret;
01985
01986 if (pkt) {
01987 avpkt.data += ret;
01988 avpkt.size -= ret;
01989 }
01990 if (!got_output) {
01991 continue;
01992 }
01993 }
01994
01995
01996 if (!ist->decoding_needed) {
01997 rate_emu_sleep(ist);
01998 ist->pts = ist->next_pts;
01999 switch (ist->st->codec->codec_type) {
02000 case AVMEDIA_TYPE_AUDIO:
02001 ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
02002 ist->st->codec->sample_rate;
02003 break;
02004 case AVMEDIA_TYPE_VIDEO:
02005 if (ist->st->codec->time_base.num != 0) {
02006 int ticks = ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
02007 ist->next_pts += ((int64_t)AV_TIME_BASE *
02008 ist->st->codec->time_base.num * ticks) /
02009 ist->st->codec->time_base.den;
02010 }
02011 break;
02012 }
02013 }
02014 for (i = 0; pkt && i < nb_ostreams; i++) {
02015 OutputStream *ost = &ost_table[i];
02016
02017 if (!check_output_constraints(ist, ost) || ost->encoding_needed)
02018 continue;
02019
02020 do_streamcopy(ist, ost, pkt);
02021 }
02022
02023 return 0;
02024 }
02025
02026 static void print_sdp(OutputFile *output_files, int n)
02027 {
02028 char sdp[2048];
02029 int i;
02030 AVFormatContext **avc = av_malloc(sizeof(*avc)*n);
02031
02032 if (!avc)
02033 exit_program(1);
02034 for (i = 0; i < n; i++)
02035 avc[i] = output_files[i].ctx;
02036
02037 av_sdp_create(avc, n, sdp, sizeof(sdp));
02038 printf("SDP:\n%s\n", sdp);
02039 fflush(stdout);
02040 av_freep(&avc);
02041 }
02042
02043 static int init_input_stream(int ist_index, OutputStream *output_streams, int nb_output_streams,
02044 char *error, int error_len)
02045 {
02046 int i;
02047 InputStream *ist = &input_streams[ist_index];
02048 if (ist->decoding_needed) {
02049 AVCodec *codec = ist->dec;
02050 if (!codec) {
02051 snprintf(error, error_len, "Decoder (codec id %d) not found for input stream #%d:%d",
02052 ist->st->codec->codec_id, ist->file_index, ist->st->index);
02053 return AVERROR(EINVAL);
02054 }
02055
02056 if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) {
02057 snprintf(error, error_len, "Error while opening decoder for input stream #%d:%d",
02058 ist->file_index, ist->st->index);
02059 return AVERROR(EINVAL);
02060 }
02061 assert_codec_experimental(ist->st->codec, 0);
02062 assert_avoptions(ist->opts);
02063 }
02064
02065 ist->pts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames*AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
02066 ist->next_pts = AV_NOPTS_VALUE;
02067 ist->is_start = 1;
02068
02069 return 0;
02070 }
02071
02072 static int transcode_init(OutputFile *output_files,
02073 int nb_output_files,
02074 InputFile *input_files,
02075 int nb_input_files)
02076 {
02077 int ret = 0, i, j, k;
02078 AVFormatContext *oc;
02079 AVCodecContext *codec, *icodec;
02080 OutputStream *ost;
02081 InputStream *ist;
02082 char error[1024];
02083 int want_sdp = 1;
02084
02085
02086 for (i = 0; i < nb_input_files; i++) {
02087 InputFile *ifile = &input_files[i];
02088 if (ifile->rate_emu)
02089 for (j = 0; j < ifile->nb_streams; j++)
02090 input_streams[j + ifile->ist_index].start = av_gettime();
02091 }
02092
02093
02094 for (i = 0; i < nb_output_files; i++) {
02095 oc = output_files[i].ctx;
02096 if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
02097 av_dump_format(oc, i, oc->filename, 1);
02098 av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", i);
02099 return AVERROR(EINVAL);
02100 }
02101 }
02102
02103
02104 for (i = 0; i < nb_output_streams; i++) {
02105 ost = &output_streams[i];
02106 oc = output_files[ost->file_index].ctx;
02107 ist = &input_streams[ost->source_index];
02108
02109 if (ost->attachment_filename)
02110 continue;
02111
02112 codec = ost->st->codec;
02113 icodec = ist->st->codec;
02114
02115 ost->st->disposition = ist->st->disposition;
02116 codec->bits_per_raw_sample = icodec->bits_per_raw_sample;
02117 codec->chroma_sample_location = icodec->chroma_sample_location;
02118
02119 if (ost->stream_copy) {
02120 uint64_t extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
02121
02122 if (extra_size > INT_MAX) {
02123 return AVERROR(EINVAL);
02124 }
02125
02126
02127 codec->codec_id = icodec->codec_id;
02128 codec->codec_type = icodec->codec_type;
02129
02130 if (!codec->codec_tag) {
02131 if (!oc->oformat->codec_tag ||
02132 av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == codec->codec_id ||
02133 av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0)
02134 codec->codec_tag = icodec->codec_tag;
02135 }
02136
02137 codec->bit_rate = icodec->bit_rate;
02138 codec->rc_max_rate = icodec->rc_max_rate;
02139 codec->rc_buffer_size = icodec->rc_buffer_size;
02140 codec->extradata = av_mallocz(extra_size);
02141 if (!codec->extradata) {
02142 return AVERROR(ENOMEM);
02143 }
02144 memcpy(codec->extradata, icodec->extradata, icodec->extradata_size);
02145
02146 codec->extradata_size = icodec->extradata_size;
02147 if (!copy_tb) {
02148 codec->time_base = icodec->time_base;
02149 codec->time_base.num *= icodec->ticks_per_frame;
02150 av_reduce(&codec->time_base.num, &codec->time_base.den,
02151 codec->time_base.num, codec->time_base.den, INT_MAX);
02152 } else
02153 codec->time_base = ist->st->time_base;
02154
02155 switch(codec->codec_type) {
02156 case AVMEDIA_TYPE_AUDIO:
02157 if(audio_volume != 256) {
02158 av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
02159 exit_program(1);
02160 }
02161 codec->channel_layout = icodec->channel_layout;
02162 codec->sample_rate = icodec->sample_rate;
02163 codec->channels = icodec->channels;
02164 codec->frame_size = icodec->frame_size;
02165 codec->audio_service_type = icodec->audio_service_type;
02166 codec->block_align = icodec->block_align;
02167 break;
02168 case AVMEDIA_TYPE_VIDEO:
02169 codec->pix_fmt = icodec->pix_fmt;
02170 codec->width = icodec->width;
02171 codec->height = icodec->height;
02172 codec->has_b_frames = icodec->has_b_frames;
02173 if (!codec->sample_aspect_ratio.num) {
02174 codec->sample_aspect_ratio =
02175 ost->st->sample_aspect_ratio =
02176 ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio :
02177 ist->st->codec->sample_aspect_ratio.num ?
02178 ist->st->codec->sample_aspect_ratio : (AVRational){0, 1};
02179 }
02180 break;
02181 case AVMEDIA_TYPE_SUBTITLE:
02182 codec->width = icodec->width;
02183 codec->height = icodec->height;
02184 break;
02185 case AVMEDIA_TYPE_DATA:
02186 case AVMEDIA_TYPE_ATTACHMENT:
02187 break;
02188 default:
02189 abort();
02190 }
02191 } else {
02192 if (!ost->enc)
02193 ost->enc = avcodec_find_encoder(ost->st->codec->codec_id);
02194
02195 ist->decoding_needed = 1;
02196 ost->encoding_needed = 1;
02197
02198 switch(codec->codec_type) {
02199 case AVMEDIA_TYPE_AUDIO:
02200 ost->fifo = av_fifo_alloc(1024);
02201 if (!ost->fifo) {
02202 return AVERROR(ENOMEM);
02203 }
02204 ost->reformat_pair = MAKE_SFMT_PAIR(AV_SAMPLE_FMT_NONE,AV_SAMPLE_FMT_NONE);
02205
02206 if (!codec->sample_rate)
02207 codec->sample_rate = icodec->sample_rate;
02208 choose_sample_rate(ost->st, ost->enc);
02209 codec->time_base = (AVRational){1, codec->sample_rate};
02210
02211 if (codec->sample_fmt == AV_SAMPLE_FMT_NONE)
02212 codec->sample_fmt = icodec->sample_fmt;
02213 choose_sample_fmt(ost->st, ost->enc);
02214
02215 if (!codec->channels) {
02216 codec->channels = icodec->channels;
02217 codec->channel_layout = icodec->channel_layout;
02218 }
02219 if (av_get_channel_layout_nb_channels(codec->channel_layout) != codec->channels)
02220 codec->channel_layout = 0;
02221
02222 ost->audio_resample = codec-> sample_rate != icodec->sample_rate || audio_sync_method > 1;
02223 icodec->request_channels = codec-> channels;
02224 ost->resample_sample_fmt = icodec->sample_fmt;
02225 ost->resample_sample_rate = icodec->sample_rate;
02226 ost->resample_channels = icodec->channels;
02227 break;
02228 case AVMEDIA_TYPE_VIDEO:
02229 if (codec->pix_fmt == PIX_FMT_NONE)
02230 codec->pix_fmt = icodec->pix_fmt;
02231 choose_pixel_fmt(ost->st, ost->enc);
02232
02233 if (ost->st->codec->pix_fmt == PIX_FMT_NONE) {
02234 av_log(NULL, AV_LOG_FATAL, "Video pixel format is unknown, stream cannot be encoded\n");
02235 exit_program(1);
02236 }
02237
02238 if (!codec->width || !codec->height) {
02239 codec->width = icodec->width;
02240 codec->height = icodec->height;
02241 }
02242
02243 ost->video_resample = codec->width != icodec->width ||
02244 codec->height != icodec->height ||
02245 codec->pix_fmt != icodec->pix_fmt;
02246 if (ost->video_resample) {
02247 codec->bits_per_raw_sample= frame_bits_per_raw_sample;
02248 }
02249
02250 ost->resample_height = icodec->height;
02251 ost->resample_width = icodec->width;
02252 ost->resample_pix_fmt = icodec->pix_fmt;
02253
02254 if (!ost->frame_rate.num)
02255 ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25,1};
02256 if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) {
02257 int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
02258 ost->frame_rate = ost->enc->supported_framerates[idx];
02259 }
02260 codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num};
02261 if( av_q2d(codec->time_base) < 0.001 && video_sync_method
02262 && (video_sync_method==1 || (video_sync_method<0 && !(oc->oformat->flags & AVFMT_VARIABLE_FPS)))){
02263 av_log(oc, AV_LOG_WARNING, "Frame rate very high for a muxer not effciciently supporting it.\n"
02264 "Please consider specifiying a lower framerate, a different muxer or -vsync 2\n");
02265 }
02266
02267 #if CONFIG_AVFILTER
02268 if (configure_video_filters(ist, ost)) {
02269 av_log(NULL, AV_LOG_FATAL, "Error opening filters!\n");
02270 exit(1);
02271 }
02272 #endif
02273 break;
02274 case AVMEDIA_TYPE_SUBTITLE:
02275 break;
02276 default:
02277 abort();
02278 break;
02279 }
02280
02281 if (codec->codec_id != CODEC_ID_H264 &&
02282 (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
02283 char logfilename[1024];
02284 FILE *f;
02285
02286 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
02287 pass_logfilename_prefix ? pass_logfilename_prefix : DEFAULT_PASS_LOGFILENAME_PREFIX,
02288 i);
02289 if (codec->flags & CODEC_FLAG_PASS1) {
02290 f = fopen(logfilename, "wb");
02291 if (!f) {
02292 av_log(NULL, AV_LOG_FATAL, "Cannot write log file '%s' for pass-1 encoding: %s\n",
02293 logfilename, strerror(errno));
02294 exit_program(1);
02295 }
02296 ost->logfile = f;
02297 } else {
02298 char *logbuffer;
02299 size_t logbuffer_size;
02300 if (cmdutils_read_file(logfilename, &logbuffer, &logbuffer_size) < 0) {
02301 av_log(NULL, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
02302 logfilename);
02303 exit_program(1);
02304 }
02305 codec->stats_in = logbuffer;
02306 }
02307 }
02308 }
02309 if(codec->codec_type == AVMEDIA_TYPE_VIDEO){
02310
02311 int size = codec->width * codec->height;
02312 bit_buffer_size = FFMAX(bit_buffer_size, 6*size + 1664);
02313 }
02314 }
02315
02316 if (!bit_buffer)
02317 bit_buffer = av_malloc(bit_buffer_size);
02318 if (!bit_buffer) {
02319 av_log(NULL, AV_LOG_ERROR, "Cannot allocate %d bytes output buffer\n",
02320 bit_buffer_size);
02321 return AVERROR(ENOMEM);
02322 }
02323
02324
02325 for (i = 0; i < nb_output_streams; i++) {
02326 ost = &output_streams[i];
02327 if (ost->encoding_needed) {
02328 AVCodec *codec = ost->enc;
02329 AVCodecContext *dec = input_streams[ost->source_index].st->codec;
02330 if (!codec) {
02331 snprintf(error, sizeof(error), "Encoder (codec id %d) not found for output stream #%d:%d",
02332 ost->st->codec->codec_id, ost->file_index, ost->index);
02333 ret = AVERROR(EINVAL);
02334 goto dump_format;
02335 }
02336 if (dec->subtitle_header) {
02337 ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size);
02338 if (!ost->st->codec->subtitle_header) {
02339 ret = AVERROR(ENOMEM);
02340 goto dump_format;
02341 }
02342 memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
02343 ost->st->codec->subtitle_header_size = dec->subtitle_header_size;
02344 }
02345 if (avcodec_open2(ost->st->codec, codec, &ost->opts) < 0) {
02346 snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
02347 ost->file_index, ost->index);
02348 ret = AVERROR(EINVAL);
02349 goto dump_format;
02350 }
02351 assert_codec_experimental(ost->st->codec, 1);
02352 assert_avoptions(ost->opts);
02353 if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
02354 av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
02355 "It takes bits/s as argument, not kbits/s\n");
02356 extra_size += ost->st->codec->extradata_size;
02357
02358 if (ost->st->codec->me_threshold)
02359 input_streams[ost->source_index].st->codec->debug |= FF_DEBUG_MV;
02360 }
02361 }
02362
02363
02364 for (i = 0; i < nb_input_streams; i++)
02365 if ((ret = init_input_stream(i, output_streams, nb_output_streams, error, sizeof(error))) < 0)
02366 goto dump_format;
02367
02368
02369 for (i = 0; i < nb_input_files; i++) {
02370 InputFile *ifile = &input_files[i];
02371 for (j = 0; j < ifile->ctx->nb_programs; j++) {
02372 AVProgram *p = ifile->ctx->programs[j];
02373 int discard = AVDISCARD_ALL;
02374
02375 for (k = 0; k < p->nb_stream_indexes; k++)
02376 if (!input_streams[ifile->ist_index + p->stream_index[k]].discard) {
02377 discard = AVDISCARD_DEFAULT;
02378 break;
02379 }
02380 p->discard = discard;
02381 }
02382 }
02383
02384
02385 for (i = 0; i < nb_output_files; i++) {
02386 oc = output_files[i].ctx;
02387 oc->interrupt_callback = int_cb;
02388 if (avformat_write_header(oc, &output_files[i].opts) < 0) {
02389 snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?)", i);
02390 ret = AVERROR(EINVAL);
02391 goto dump_format;
02392 }
02393
02394 if (strcmp(oc->oformat->name, "rtp")) {
02395 want_sdp = 0;
02396 }
02397 }
02398
02399 dump_format:
02400
02401
02402 for (i = 0; i < nb_output_files; i++) {
02403 av_dump_format(output_files[i].ctx, i, output_files[i].ctx->filename, 1);
02404 }
02405
02406
02407 av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
02408 for (i = 0; i < nb_output_streams; i++) {
02409 ost = &output_streams[i];
02410
02411 if (ost->attachment_filename) {
02412
02413 av_log(NULL, AV_LOG_INFO, " File %s -> Stream #%d:%d\n",
02414 ost->attachment_filename, ost->file_index, ost->index);
02415 continue;
02416 }
02417 av_log(NULL, AV_LOG_INFO, " Stream #%d:%d -> #%d:%d",
02418 input_streams[ost->source_index].file_index,
02419 input_streams[ost->source_index].st->index,
02420 ost->file_index,
02421 ost->index);
02422 if (ost->sync_ist != &input_streams[ost->source_index])
02423 av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]",
02424 ost->sync_ist->file_index,
02425 ost->sync_ist->st->index);
02426 if (ost->stream_copy)
02427 av_log(NULL, AV_LOG_INFO, " (copy)");
02428 else
02429 av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index].dec ?
02430 input_streams[ost->source_index].dec->name : "?",
02431 ost->enc ? ost->enc->name : "?");
02432 av_log(NULL, AV_LOG_INFO, "\n");
02433 }
02434
02435 if (ret) {
02436 av_log(NULL, AV_LOG_ERROR, "%s\n", error);
02437 return ret;
02438 }
02439
02440 if (want_sdp) {
02441 print_sdp(output_files, nb_output_files);
02442 }
02443
02444 return 0;
02445 }
02446
02447
02448
02449
02450 static int transcode(OutputFile *output_files,
02451 int nb_output_files,
02452 InputFile *input_files,
02453 int nb_input_files)
02454 {
02455 int ret, i;
02456 AVFormatContext *is, *os;
02457 OutputStream *ost;
02458 InputStream *ist;
02459 uint8_t *no_packet;
02460 int no_packet_count=0;
02461 int64_t timer_start;
02462 int key;
02463
02464 if (!(no_packet = av_mallocz(nb_input_files)))
02465 exit_program(1);
02466
02467 ret = transcode_init(output_files, nb_output_files, input_files, nb_input_files);
02468 if (ret < 0)
02469 goto fail;
02470
02471 if (!using_stdin) {
02472 av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");
02473 avio_set_interrupt_cb(decode_interrupt_cb);
02474 }
02475 term_init();
02476
02477 timer_start = av_gettime();
02478
02479 for(; received_sigterm == 0;) {
02480 int file_index, ist_index;
02481 AVPacket pkt;
02482 int64_t ipts_min;
02483 double opts_min;
02484
02485 ipts_min = INT64_MAX;
02486 opts_min= 1e100;
02487
02488 if (!using_stdin) {
02489 if (q_pressed)
02490 break;
02491
02492 key = read_key();
02493 if (key == 'q')
02494 break;
02495 if (key == '+') av_log_set_level(av_log_get_level()+10);
02496 if (key == '-') av_log_set_level(av_log_get_level()-10);
02497 if (key == 's') qp_hist ^= 1;
02498 if (key == 'h'){
02499 if (do_hex_dump){
02500 do_hex_dump = do_pkt_dump = 0;
02501 } else if(do_pkt_dump){
02502 do_hex_dump = 1;
02503 } else
02504 do_pkt_dump = 1;
02505 av_log_set_level(AV_LOG_DEBUG);
02506 }
02507 if (key == 'd' || key == 'D'){
02508 int debug=0;
02509 if(key == 'D') {
02510 debug = input_streams[0].st->codec->debug<<1;
02511 if(!debug) debug = 1;
02512 while(debug & (FF_DEBUG_DCT_COEFF|FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE))
02513 debug += debug;
02514 }else
02515 scanf("%d", &debug);
02516 for(i=0;i<nb_input_streams;i++) {
02517 input_streams[i].st->codec->debug = debug;
02518 }
02519 for(i=0;i<nb_output_streams;i++) {
02520 ost = &output_streams[i];
02521 ost->st->codec->debug = debug;
02522 }
02523 if(debug) av_log_set_level(AV_LOG_DEBUG);
02524 fprintf(stderr,"debug=%d\n", debug);
02525 }
02526 if (key == '?'){
02527 fprintf(stderr, "key function\n"
02528 "? show this help\n"
02529 "+ increase verbosity\n"
02530 "- decrease verbosity\n"
02531 "D cycle through available debug modes\n"
02532 "h dump packets/hex press to cycle through the 3 states\n"
02533 "q quit\n"
02534 "s Show QP histogram\n"
02535 );
02536 }
02537 }
02538
02539
02540
02541 file_index = -1;
02542 for (i = 0; i < nb_output_streams; i++) {
02543 OutputFile *of;
02544 int64_t ipts;
02545 double opts;
02546 ost = &output_streams[i];
02547 of = &output_files[ost->file_index];
02548 os = output_files[ost->file_index].ctx;
02549 ist = &input_streams[ost->source_index];
02550 if (ost->is_past_recording_time || no_packet[ist->file_index] ||
02551 (os->pb && avio_tell(os->pb) >= of->limit_filesize))
02552 continue;
02553 opts = ost->st->pts.val * av_q2d(ost->st->time_base);
02554 ipts = ist->pts;
02555 if (!input_files[ist->file_index].eof_reached){
02556 if(ipts < ipts_min) {
02557 ipts_min = ipts;
02558 if(input_sync ) file_index = ist->file_index;
02559 }
02560 if(opts < opts_min) {
02561 opts_min = opts;
02562 if(!input_sync) file_index = ist->file_index;
02563 }
02564 }
02565 if (ost->frame_number >= ost->max_frames) {
02566 int j;
02567 for (j = 0; j < of->ctx->nb_streams; j++)
02568 output_streams[of->ost_index + j].is_past_recording_time = 1;
02569 continue;
02570 }
02571 }
02572
02573 if (file_index < 0) {
02574 if(no_packet_count){
02575 no_packet_count=0;
02576 memset(no_packet, 0, nb_input_files);
02577 usleep(10000);
02578 continue;
02579 }
02580 break;
02581 }
02582
02583
02584 is = input_files[file_index].ctx;
02585 ret= av_read_frame(is, &pkt);
02586 if(ret == AVERROR(EAGAIN)){
02587 no_packet[file_index]=1;
02588 no_packet_count++;
02589 continue;
02590 }
02591 if (ret < 0) {
02592 input_files[file_index].eof_reached = 1;
02593 if (opt_shortest)
02594 break;
02595 else
02596 continue;
02597 }
02598
02599 no_packet_count=0;
02600 memset(no_packet, 0, nb_input_files);
02601
02602 if (do_pkt_dump) {
02603 av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump,
02604 is->streams[pkt.stream_index]);
02605 }
02606
02607
02608 if (pkt.stream_index >= input_files[file_index].nb_streams)
02609 goto discard_packet;
02610 ist_index = input_files[file_index].ist_index + pkt.stream_index;
02611 ist = &input_streams[ist_index];
02612 if (ist->discard)
02613 goto discard_packet;
02614
02615 if (pkt.dts != AV_NOPTS_VALUE)
02616 pkt.dts += av_rescale_q(input_files[ist->file_index].ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
02617 if (pkt.pts != AV_NOPTS_VALUE)
02618 pkt.pts += av_rescale_q(input_files[ist->file_index].ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
02619
02620 if(pkt.pts != AV_NOPTS_VALUE)
02621 pkt.pts *= ist->ts_scale;
02622 if(pkt.dts != AV_NOPTS_VALUE)
02623 pkt.dts *= ist->ts_scale;
02624
02625
02626 if (pkt.dts != AV_NOPTS_VALUE && ist->next_pts != AV_NOPTS_VALUE
02627 && (is->iformat->flags & AVFMT_TS_DISCONT)) {
02628 int64_t pkt_dts= av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
02629 int64_t delta= pkt_dts - ist->next_pts;
02630 if((FFABS(delta) > 1LL*dts_delta_threshold*AV_TIME_BASE || pkt_dts+1<ist->pts)&& !copy_ts){
02631 input_files[ist->file_index].ts_offset -= delta;
02632 av_log(NULL, AV_LOG_DEBUG, "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
02633 delta, input_files[ist->file_index].ts_offset);
02634 pkt.dts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
02635 if(pkt.pts != AV_NOPTS_VALUE)
02636 pkt.pts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
02637 }
02638 }
02639
02640
02641 if (output_packet(ist, output_streams, nb_output_streams, &pkt) < 0) {
02642
02643 av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d\n",
02644 ist->file_index, ist->st->index);
02645 if (exit_on_error)
02646 exit_program(1);
02647 av_free_packet(&pkt);
02648 continue;
02649 }
02650
02651 discard_packet:
02652 av_free_packet(&pkt);
02653
02654
02655 print_report(output_files, output_streams, nb_output_streams, 0, timer_start);
02656 }
02657
02658
02659 for (i = 0; i < nb_input_streams; i++) {
02660 ist = &input_streams[i];
02661 if (ist->decoding_needed) {
02662 output_packet(ist, output_streams, nb_output_streams, NULL);
02663 }
02664 }
02665 flush_encoders(output_streams, nb_output_streams);
02666
02667 term_exit();
02668
02669
02670 for(i=0;i<nb_output_files;i++) {
02671 os = output_files[i].ctx;
02672 av_write_trailer(os);
02673 }
02674
02675
02676 print_report(output_files, output_streams, nb_output_streams, 1, timer_start);
02677
02678
02679 for (i = 0; i < nb_output_streams; i++) {
02680 ost = &output_streams[i];
02681 if (ost->encoding_needed) {
02682 av_freep(&ost->st->codec->stats_in);
02683 avcodec_close(ost->st->codec);
02684 }
02685 #if CONFIG_AVFILTER
02686 avfilter_graph_free(&ost->graph);
02687 #endif
02688 }
02689
02690
02691 for (i = 0; i < nb_input_streams; i++) {
02692 ist = &input_streams[i];
02693 if (ist->decoding_needed) {
02694 avcodec_close(ist->st->codec);
02695 }
02696 }
02697
02698
02699 ret = 0;
02700
02701 fail:
02702 av_freep(&bit_buffer);
02703 av_freep(&no_packet);
02704
02705 if (output_streams) {
02706 for (i = 0; i < nb_output_streams; i++) {
02707 ost = &output_streams[i];
02708 if (ost) {
02709 if (ost->stream_copy)
02710 av_freep(&ost->st->codec->extradata);
02711 if (ost->logfile) {
02712 fclose(ost->logfile);
02713 ost->logfile = NULL;
02714 }
02715 av_fifo_free(ost->fifo);
02716
02717 av_freep(&ost->st->codec->subtitle_header);
02718 av_free(ost->resample_frame.data[0]);
02719 av_free(ost->forced_kf_pts);
02720 if (ost->video_resample)
02721 sws_freeContext(ost->img_resample_ctx);
02722 if (ost->resample)
02723 audio_resample_close(ost->resample);
02724 if (ost->reformat_ctx)
02725 av_audio_convert_free(ost->reformat_ctx);
02726 av_dict_free(&ost->opts);
02727 }
02728 }
02729 }
02730 return ret;
02731 }
02732
02733 static double parse_frame_aspect_ratio(const char *arg)
02734 {
02735 int x = 0, y = 0;
02736 double ar = 0;
02737 const char *p;
02738 char *end;
02739
02740 p = strchr(arg, ':');
02741 if (p) {
02742 x = strtol(arg, &end, 10);
02743 if (end == p)
02744 y = strtol(end+1, &end, 10);
02745 if (x > 0 && y > 0)
02746 ar = (double)x / (double)y;
02747 } else
02748 ar = strtod(arg, NULL);
02749
02750 if (!ar) {
02751 av_log(NULL, AV_LOG_FATAL, "Incorrect aspect ratio specification.\n");
02752 exit_program(1);
02753 }
02754 return ar;
02755 }
02756
02757 static int opt_audio_codec(OptionsContext *o, const char *opt, const char *arg)
02758 {
02759 return parse_option(o, "codec:a", arg, options);
02760 }
02761
02762 static int opt_video_codec(OptionsContext *o, const char *opt, const char *arg)
02763 {
02764 return parse_option(o, "codec:v", arg, options);
02765 }
02766
02767 static int opt_subtitle_codec(OptionsContext *o, const char *opt, const char *arg)
02768 {
02769 return parse_option(o, "codec:s", arg, options);
02770 }
02771
02772 static int opt_data_codec(OptionsContext *o, const char *opt, const char *arg)
02773 {
02774 return parse_option(o, "codec:d", arg, options);
02775 }
02776
02777 static int opt_map(OptionsContext *o, const char *opt, const char *arg)
02778 {
02779 StreamMap *m = NULL;
02780 int i, negative = 0, file_idx;
02781 int sync_file_idx = -1, sync_stream_idx;
02782 char *p, *sync;
02783 char *map;
02784
02785 if (*arg == '-') {
02786 negative = 1;
02787 arg++;
02788 }
02789 map = av_strdup(arg);
02790
02791
02792 if (sync = strchr(map, ',')) {
02793 *sync = 0;
02794 sync_file_idx = strtol(sync + 1, &sync, 0);
02795 if (sync_file_idx >= nb_input_files || sync_file_idx < 0) {
02796 av_log(NULL, AV_LOG_FATAL, "Invalid sync file index: %d.\n", sync_file_idx);
02797 exit_program(1);
02798 }
02799 if (*sync)
02800 sync++;
02801 for (i = 0; i < input_files[sync_file_idx].nb_streams; i++)
02802 if (check_stream_specifier(input_files[sync_file_idx].ctx,
02803 input_files[sync_file_idx].ctx->streams[i], sync) == 1) {
02804 sync_stream_idx = i;
02805 break;
02806 }
02807 if (i == input_files[sync_file_idx].nb_streams) {
02808 av_log(NULL, AV_LOG_FATAL, "Sync stream specification in map %s does not "
02809 "match any streams.\n", arg);
02810 exit_program(1);
02811 }
02812 }
02813
02814
02815 file_idx = strtol(map, &p, 0);
02816 if (file_idx >= nb_input_files || file_idx < 0) {
02817 av_log(NULL, AV_LOG_FATAL, "Invalid input file index: %d.\n", file_idx);
02818 exit_program(1);
02819 }
02820 if (negative)
02821
02822 for (i = 0; i < o->nb_stream_maps; i++) {
02823 m = &o->stream_maps[i];
02824 if (file_idx == m->file_index &&
02825 check_stream_specifier(input_files[m->file_index].ctx,
02826 input_files[m->file_index].ctx->streams[m->stream_index],
02827 *p == ':' ? p + 1 : p) > 0)
02828 m->disabled = 1;
02829 }
02830 else
02831 for (i = 0; i < input_files[file_idx].nb_streams; i++) {
02832 if (check_stream_specifier(input_files[file_idx].ctx, input_files[file_idx].ctx->streams[i],
02833 *p == ':' ? p + 1 : p) <= 0)
02834 continue;
02835 o->stream_maps = grow_array(o->stream_maps, sizeof(*o->stream_maps),
02836 &o->nb_stream_maps, o->nb_stream_maps + 1);
02837 m = &o->stream_maps[o->nb_stream_maps - 1];
02838
02839 m->file_index = file_idx;
02840 m->stream_index = i;
02841
02842 if (sync_file_idx >= 0) {
02843 m->sync_file_index = sync_file_idx;
02844 m->sync_stream_index = sync_stream_idx;
02845 } else {
02846 m->sync_file_index = file_idx;
02847 m->sync_stream_index = i;
02848 }
02849 }
02850
02851 if (!m) {
02852 av_log(NULL, AV_LOG_FATAL, "Stream map '%s' matches no streams.\n", arg);
02853 exit_program(1);
02854 }
02855
02856 av_freep(&map);
02857 return 0;
02858 }
02859
02860 static int opt_attach(OptionsContext *o, const char *opt, const char *arg)
02861 {
02862 o->attachments = grow_array(o->attachments, sizeof(*o->attachments),
02863 &o->nb_attachments, o->nb_attachments + 1);
02864 o->attachments[o->nb_attachments - 1] = arg;
02865 return 0;
02866 }
02867
02868 static void parse_meta_type(char *arg, char *type, int *index)
02869 {
02870 if (*arg) {
02871 *type = *arg;
02872 switch (*arg) {
02873 case 'g':
02874 break;
02875 case 's':
02876 case 'c':
02877 case 'p':
02878 if (*(++arg) == ':')
02879 *index = strtol(++arg, NULL, 0);
02880 break;
02881 default:
02882 av_log(NULL, AV_LOG_FATAL, "Invalid metadata type %c.\n", *arg);
02883 exit_program(1);
02884 }
02885 } else
02886 *type = 'g';
02887 }
02888
02889 static int opt_map_metadata(OptionsContext *o, const char *opt, const char *arg)
02890 {
02891 MetadataMap *m, *m1;
02892 char *p;
02893
02894 o->meta_data_maps = grow_array(o->meta_data_maps, sizeof(*o->meta_data_maps),
02895 &o->nb_meta_data_maps, o->nb_meta_data_maps + 1);
02896
02897 m = &o->meta_data_maps[o->nb_meta_data_maps - 1][1];
02898 m->file = strtol(arg, &p, 0);
02899 parse_meta_type(*p ? p + 1 : p, &m->type, &m->index);
02900
02901 m1 = &o->meta_data_maps[o->nb_meta_data_maps - 1][0];
02902 if (p = strchr(opt, ':'))
02903 parse_meta_type(p + 1, &m1->type, &m1->index);
02904 else
02905 m1->type = 'g';
02906
02907 if (m->type == 'g' || m1->type == 'g')
02908 o->metadata_global_manual = 1;
02909 if (m->type == 's' || m1->type == 's')
02910 o->metadata_streams_manual = 1;
02911 if (m->type == 'c' || m1->type == 'c')
02912 o->metadata_chapters_manual = 1;
02913
02914 return 0;
02915 }
02916
02917 static AVCodec *find_codec_or_die(const char *name, enum AVMediaType type, int encoder)
02918 {
02919 const char *codec_string = encoder ? "encoder" : "decoder";
02920 AVCodec *codec;
02921
02922 codec = encoder ?
02923 avcodec_find_encoder_by_name(name) :
02924 avcodec_find_decoder_by_name(name);
02925 if(!codec) {
02926 av_log(NULL, AV_LOG_FATAL, "Unknown %s '%s'\n", codec_string, name);
02927 exit_program(1);
02928 }
02929 if(codec->type != type) {
02930 av_log(NULL, AV_LOG_FATAL, "Invalid %s type '%s'\n", codec_string, name);
02931 exit_program(1);
02932 }
02933 return codec;
02934 }
02935
02936 static AVCodec *choose_decoder(OptionsContext *o, AVFormatContext *s, AVStream *st)
02937 {
02938 char *codec_name = NULL;
02939
02940 MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
02941 if (codec_name) {
02942 AVCodec *codec = find_codec_or_die(codec_name, st->codec->codec_type, 0);
02943 st->codec->codec_id = codec->id;
02944 return codec;
02945 } else
02946 return avcodec_find_decoder(st->codec->codec_id);
02947 }
02948
02953 static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
02954 {
02955 int i, rfps, rfps_base;
02956
02957 for (i = 0; i < ic->nb_streams; i++) {
02958 AVStream *st = ic->streams[i];
02959 AVCodecContext *dec = st->codec;
02960 InputStream *ist;
02961
02962 input_streams = grow_array(input_streams, sizeof(*input_streams), &nb_input_streams, nb_input_streams + 1);
02963 ist = &input_streams[nb_input_streams - 1];
02964 ist->st = st;
02965 ist->file_index = nb_input_files;
02966 ist->discard = 1;
02967 ist->opts = filter_codec_opts(codec_opts, choose_decoder(o, ic, st), ic, st);
02968
02969 ist->ts_scale = 1.0;
02970 MATCH_PER_STREAM_OPT(ts_scale, dbl, ist->ts_scale, ic, st);
02971
02972 ist->dec = choose_decoder(o, ic, st);
02973
02974 switch (dec->codec_type) {
02975 case AVMEDIA_TYPE_AUDIO:
02976 if(!ist->dec)
02977 ist->dec = avcodec_find_decoder(dec->codec_id);
02978 if(o->audio_disable)
02979 st->discard= AVDISCARD_ALL;
02980 break;
02981 case AVMEDIA_TYPE_VIDEO:
02982 if(!ist->dec)
02983 ist->dec = avcodec_find_decoder(dec->codec_id);
02984 rfps = ic->streams[i]->r_frame_rate.num;
02985 rfps_base = ic->streams[i]->r_frame_rate.den;
02986 if (dec->lowres) {
02987 dec->flags |= CODEC_FLAG_EMU_EDGE;
02988 }
02989
02990 if (dec->time_base.den != rfps*dec->ticks_per_frame || dec->time_base.num != rfps_base) {
02991
02992 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",
02993 i, (float)dec->time_base.den / dec->time_base.num, dec->time_base.den, dec->time_base.num,
02994 (float)rfps / rfps_base, rfps, rfps_base);
02995 }
02996
02997 if (o->video_disable)
02998 st->discard= AVDISCARD_ALL;
02999 else if(video_discard)
03000 st->discard= video_discard;
03001 break;
03002 case AVMEDIA_TYPE_DATA:
03003 break;
03004 case AVMEDIA_TYPE_SUBTITLE:
03005 if(!ist->dec)
03006 ist->dec = avcodec_find_decoder(dec->codec_id);
03007 if(o->subtitle_disable)
03008 st->discard = AVDISCARD_ALL;
03009 break;
03010 case AVMEDIA_TYPE_ATTACHMENT:
03011 case AVMEDIA_TYPE_UNKNOWN:
03012 break;
03013 default:
03014 abort();
03015 }
03016 }
03017 }
03018
03019 static void assert_file_overwrite(const char *filename)
03020 {
03021 if ((!file_overwrite || no_file_overwrite) &&
03022 (strchr(filename, ':') == NULL || filename[1] == ':' ||
03023 av_strstart(filename, "file:", NULL))) {
03024 if (avio_check(filename, 0) == 0) {
03025 if (!using_stdin && (!no_file_overwrite || file_overwrite)) {
03026 fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
03027 fflush(stderr);
03028 if (!read_yesno()) {
03029 fprintf(stderr, "Not overwriting - exiting\n");
03030 exit_program(1);
03031 }
03032 }
03033 else {
03034 fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
03035 exit_program(1);
03036 }
03037 }
03038 }
03039 }
03040
03041 static void dump_attachment(AVStream *st, const char *filename)
03042 {
03043 int ret;
03044 AVIOContext *out = NULL;
03045 AVDictionaryEntry *e;
03046
03047 if (!st->codec->extradata_size) {
03048 av_log(NULL, AV_LOG_WARNING, "No extradata to dump in stream #%d:%d.\n",
03049 nb_input_files - 1, st->index);
03050 return;
03051 }
03052 if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
03053 filename = e->value;
03054 if (!*filename) {
03055 av_log(NULL, AV_LOG_FATAL, "No filename specified and no 'filename' tag"
03056 "in stream #%d:%d.\n", nb_input_files - 1, st->index);
03057 exit_program(1);
03058 }
03059
03060 assert_file_overwrite(filename);
03061
03062 if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
03063 av_log(NULL, AV_LOG_FATAL, "Could not open file %s for writing.\n",
03064 filename);
03065 exit_program(1);
03066 }
03067
03068 avio_write(out, st->codec->extradata, st->codec->extradata_size);
03069 avio_flush(out);
03070 avio_close(out);
03071 }
03072
03073 static int opt_input_file(OptionsContext *o, const char *opt, const char *filename)
03074 {
03075 AVFormatContext *ic;
03076 AVInputFormat *file_iformat = NULL;
03077 int err, i, ret;
03078 int64_t timestamp;
03079 uint8_t buf[128];
03080 AVDictionary **opts;
03081 int orig_nb_streams;
03082
03083 if (o->format) {
03084 if (!(file_iformat = av_find_input_format(o->format))) {
03085 av_log(NULL, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
03086 exit_program(1);
03087 }
03088 }
03089
03090 if (!strcmp(filename, "-"))
03091 filename = "pipe:";
03092
03093 using_stdin |= !strncmp(filename, "pipe:", 5) ||
03094 !strcmp(filename, "/dev/stdin");
03095
03096
03097 ic = avformat_alloc_context();
03098 if (!ic) {
03099 print_error(filename, AVERROR(ENOMEM));
03100 exit_program(1);
03101 }
03102 if (o->nb_audio_sample_rate) {
03103 snprintf(buf, sizeof(buf), "%d", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i);
03104 av_dict_set(&format_opts, "sample_rate", buf, 0);
03105 }
03106 if (o->nb_audio_channels) {
03107 snprintf(buf, sizeof(buf), "%d", o->audio_channels[o->nb_audio_channels - 1].u.i);
03108 av_dict_set(&format_opts, "channels", buf, 0);
03109 }
03110 if (o->nb_frame_rates) {
03111 av_dict_set(&format_opts, "framerate", o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
03112 }
03113 if (o->nb_frame_sizes) {
03114 av_dict_set(&format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
03115 }
03116 if (o->nb_frame_pix_fmts)
03117 av_dict_set(&format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
03118
03119 ic->flags |= AVFMT_FLAG_NONBLOCK;
03120 ic->interrupt_callback = int_cb;
03121
03122
03123 err = avformat_open_input(&ic, filename, file_iformat, &format_opts);
03124 if (err < 0) {
03125 print_error(filename, err);
03126 exit_program(1);
03127 }
03128 assert_avoptions(format_opts);
03129
03130
03131 for (i = 0; i < ic->nb_streams; i++)
03132 choose_decoder(o, ic, ic->streams[i]);
03133
03134
03135 opts = setup_find_stream_info_opts(ic, codec_opts);
03136 orig_nb_streams = ic->nb_streams;
03137
03138
03139
03140 ret = avformat_find_stream_info(ic, opts);
03141 if (ret < 0) {
03142 av_log(NULL, AV_LOG_FATAL, "%s: could not find codec parameters\n", filename);
03143 av_close_input_file(ic);
03144 exit_program(1);
03145 }
03146
03147 timestamp = o->start_time;
03148
03149 if (ic->start_time != AV_NOPTS_VALUE)
03150 timestamp += ic->start_time;
03151
03152
03153 if (o->start_time != 0) {
03154 ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);
03155 if (ret < 0) {
03156 av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
03157 filename, (double)timestamp / AV_TIME_BASE);
03158 }
03159 }
03160
03161
03162 add_input_streams(o, ic);
03163
03164
03165 av_dump_format(ic, nb_input_files, filename, 0);
03166
03167 input_files = grow_array(input_files, sizeof(*input_files), &nb_input_files, nb_input_files + 1);
03168 input_files[nb_input_files - 1].ctx = ic;
03169 input_files[nb_input_files - 1].ist_index = nb_input_streams - ic->nb_streams;
03170 input_files[nb_input_files - 1].ts_offset = o->input_ts_offset - (copy_ts ? 0 : timestamp);
03171 input_files[nb_input_files - 1].nb_streams = ic->nb_streams;
03172 input_files[nb_input_files - 1].rate_emu = o->rate_emu;
03173
03174 for (i = 0; i < o->nb_dump_attachment; i++) {
03175 int j;
03176
03177 for (j = 0; j < ic->nb_streams; j++) {
03178 AVStream *st = ic->streams[j];
03179
03180 if (check_stream_specifier(ic, st, o->dump_attachment[i].specifier) == 1)
03181 dump_attachment(st, o->dump_attachment[i].u.str);
03182 }
03183 }
03184
03185 for (i = 0; i < orig_nb_streams; i++)
03186 av_dict_free(&opts[i]);
03187 av_freep(&opts);
03188
03189 reset_options(o);
03190 return 0;
03191 }
03192
03193 static void parse_forced_key_frames(char *kf, OutputStream *ost,
03194 AVCodecContext *avctx)
03195 {
03196 char *p;
03197 int n = 1, i;
03198 int64_t t;
03199
03200 for (p = kf; *p; p++)
03201 if (*p == ',')
03202 n++;
03203 ost->forced_kf_count = n;
03204 ost->forced_kf_pts = av_malloc(sizeof(*ost->forced_kf_pts) * n);
03205 if (!ost->forced_kf_pts) {
03206 av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
03207 exit_program(1);
03208 }
03209 for (i = 0; i < n; i++) {
03210 p = i ? strchr(p, ',') + 1 : kf;
03211 t = parse_time_or_die("force_key_frames", p, 1);
03212 ost->forced_kf_pts[i] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
03213 }
03214 }
03215
03216 static uint8_t *get_line(AVIOContext *s)
03217 {
03218 AVIOContext *line;
03219 uint8_t *buf;
03220 char c;
03221
03222 if (avio_open_dyn_buf(&line) < 0) {
03223 av_log(NULL, AV_LOG_FATAL, "Could not alloc buffer for reading preset.\n");
03224 exit_program(1);
03225 }
03226
03227 while ((c = avio_r8(s)) && c != '\n')
03228 avio_w8(line, c);
03229 avio_w8(line, 0);
03230 avio_close_dyn_buf(line, &buf);
03231
03232 return buf;
03233 }
03234
03235 static int get_preset_file_2(const char *preset_name, const char *codec_name, AVIOContext **s)
03236 {
03237 int i, ret = 1;
03238 char filename[1000];
03239 const char *base[3] = { getenv("AVCONV_DATADIR"),
03240 getenv("HOME"),
03241 AVCONV_DATADIR,
03242 };
03243
03244 for (i = 0; i < FF_ARRAY_ELEMS(base) && ret; i++) {
03245 if (!base[i])
03246 continue;
03247 if (codec_name) {
03248 snprintf(filename, sizeof(filename), "%s%s/%s-%s.avpreset", base[i],
03249 i != 1 ? "" : "/.avconv", codec_name, preset_name);
03250 ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
03251 }
03252 if (ret) {
03253 snprintf(filename, sizeof(filename), "%s%s/%s.avpreset", base[i],
03254 i != 1 ? "" : "/.avconv", preset_name);
03255 ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
03256 }
03257 }
03258 return ret;
03259 }
03260
03261 static void choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *ost)
03262 {
03263 char *codec_name = NULL;
03264
03265 MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st);
03266 if (!codec_name) {
03267 ost->st->codec->codec_id = av_guess_codec(s->oformat, NULL, s->filename,
03268 NULL, ost->st->codec->codec_type);
03269 ost->enc = avcodec_find_encoder(ost->st->codec->codec_id);
03270 } else if (!strcmp(codec_name, "copy"))
03271 ost->stream_copy = 1;
03272 else {
03273 ost->enc = find_codec_or_die(codec_name, ost->st->codec->codec_type, 1);
03274 ost->st->codec->codec_id = ost->enc->id;
03275 }
03276 }
03277
03278 static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type)
03279 {
03280 OutputStream *ost;
03281 AVStream *st = avformat_new_stream(oc, NULL);
03282 int idx = oc->nb_streams - 1, ret = 0;
03283 char *bsf = NULL, *next, *codec_tag = NULL;
03284 AVBitStreamFilterContext *bsfc, *bsfc_prev = NULL;
03285 double qscale = -1;
03286 char *buf = NULL, *arg = NULL, *preset = NULL;
03287 AVIOContext *s = NULL;
03288
03289 if (!st) {
03290 av_log(NULL, AV_LOG_FATAL, "Could not alloc stream.\n");
03291 exit_program(1);
03292 }
03293
03294 if (oc->nb_streams - 1 < o->nb_streamid_map)
03295 st->id = o->streamid_map[oc->nb_streams - 1];
03296
03297 output_streams = grow_array(output_streams, sizeof(*output_streams), &nb_output_streams,
03298 nb_output_streams + 1);
03299 ost = &output_streams[nb_output_streams - 1];
03300 ost->file_index = nb_output_files;
03301 ost->index = idx;
03302 ost->st = st;
03303 st->codec->codec_type = type;
03304 choose_encoder(o, oc, ost);
03305 if (ost->enc) {
03306 ost->opts = filter_codec_opts(codec_opts, ost->enc, oc, st);
03307 }
03308
03309 avcodec_get_context_defaults3(st->codec, ost->enc);
03310 st->codec->codec_type = type;
03311
03312 MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);
03313 if (preset && (!(ret = get_preset_file_2(preset, ost->enc->name, &s)))) {
03314 do {
03315 buf = get_line(s);
03316 if (!buf[0] || buf[0] == '#') {
03317 av_free(buf);
03318 continue;
03319 }
03320 if (!(arg = strchr(buf, '='))) {
03321 av_log(NULL, AV_LOG_FATAL, "Invalid line found in the preset file.\n");
03322 exit_program(1);
03323 }
03324 *arg++ = 0;
03325 av_dict_set(&ost->opts, buf, arg, AV_DICT_DONT_OVERWRITE);
03326 av_free(buf);
03327 } while (!s->eof_reached);
03328 avio_close(s);
03329 }
03330 if (ret) {
03331 av_log(NULL, AV_LOG_FATAL,
03332 "Preset %s specified for stream %d:%d, but could not be opened.\n",
03333 preset, ost->file_index, ost->index);
03334 exit_program(1);
03335 }
03336
03337 ost->max_frames = INT64_MAX;
03338 MATCH_PER_STREAM_OPT(max_frames, i64, ost->max_frames, oc, st);
03339
03340 MATCH_PER_STREAM_OPT(bitstream_filters, str, bsf, oc, st);
03341 while (bsf) {
03342 if (next = strchr(bsf, ','))
03343 *next++ = 0;
03344 if (!(bsfc = av_bitstream_filter_init(bsf))) {
03345 av_log(NULL, AV_LOG_FATAL, "Unknown bitstream filter %s\n", bsf);
03346 exit_program(1);
03347 }
03348 if (bsfc_prev)
03349 bsfc_prev->next = bsfc;
03350 else
03351 ost->bitstream_filters = bsfc;
03352
03353 bsfc_prev = bsfc;
03354 bsf = next;
03355 }
03356
03357 MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st);
03358 if (codec_tag) {
03359 uint32_t tag = strtol(codec_tag, &next, 0);
03360 if (*next)
03361 tag = AV_RL32(codec_tag);
03362 st->codec->codec_tag = tag;
03363 }
03364
03365 MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st);
03366 if (qscale >= 0 || same_quant) {
03367 st->codec->flags |= CODEC_FLAG_QSCALE;
03368 st->codec->global_quality = FF_QP2LAMBDA * qscale;
03369 }
03370
03371 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
03372 st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
03373
03374 av_opt_get_int(sws_opts, "sws_flags", 0, &ost->sws_flags);
03375 return ost;
03376 }
03377
03378 static void parse_matrix_coeffs(uint16_t *dest, const char *str)
03379 {
03380 int i;
03381 const char *p = str;
03382 for(i = 0;; i++) {
03383 dest[i] = atoi(p);
03384 if(i == 63)
03385 break;
03386 p = strchr(p, ',');
03387 if(!p) {
03388 av_log(NULL, AV_LOG_FATAL, "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
03389 exit_program(1);
03390 }
03391 p++;
03392 }
03393 }
03394
03395 static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc)
03396 {
03397 AVStream *st;
03398 OutputStream *ost;
03399 AVCodecContext *video_enc;
03400
03401 ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO);
03402 st = ost->st;
03403 video_enc = st->codec;
03404
03405 if (!ost->stream_copy) {
03406 const char *p = NULL;
03407 char *forced_key_frames = NULL, *frame_rate = NULL, *frame_size = NULL;
03408 char *frame_aspect_ratio = NULL, *frame_pix_fmt = NULL;
03409 char *intra_matrix = NULL, *inter_matrix = NULL, *filters = NULL;
03410 int i;
03411
03412 MATCH_PER_STREAM_OPT(frame_rates, str, frame_rate, oc, st);
03413 if (frame_rate && av_parse_video_rate(&ost->frame_rate, frame_rate) < 0) {
03414 av_log(NULL, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);
03415 exit_program(1);
03416 }
03417
03418 MATCH_PER_STREAM_OPT(frame_sizes, str, frame_size, oc, st);
03419 if (frame_size && av_parse_video_size(&video_enc->width, &video_enc->height, frame_size) < 0) {
03420 av_log(NULL, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
03421 exit_program(1);
03422 }
03423
03424 MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);
03425 if (frame_aspect_ratio)
03426 ost->frame_aspect_ratio = parse_frame_aspect_ratio(frame_aspect_ratio);
03427
03428 MATCH_PER_STREAM_OPT(frame_pix_fmts, str, frame_pix_fmt, oc, st);
03429 if (frame_pix_fmt && (video_enc->pix_fmt = av_get_pix_fmt(frame_pix_fmt)) == PIX_FMT_NONE) {
03430 av_log(NULL, AV_LOG_FATAL, "Unknown pixel format requested: %s.\n", frame_pix_fmt);
03431 exit_program(1);
03432 }
03433 st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
03434
03435 MATCH_PER_STREAM_OPT(intra_matrices, str, intra_matrix, oc, st);
03436 if (intra_matrix) {
03437 if (!(video_enc->intra_matrix = av_mallocz(sizeof(*video_enc->intra_matrix) * 64))) {
03438 av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for intra matrix.\n");
03439 exit_program(1);
03440 }
03441 parse_matrix_coeffs(video_enc->intra_matrix, intra_matrix);
03442 }
03443 MATCH_PER_STREAM_OPT(inter_matrices, str, inter_matrix, oc, st);
03444 if (inter_matrix) {
03445 if (!(video_enc->inter_matrix = av_mallocz(sizeof(*video_enc->inter_matrix) * 64))) {
03446 av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for inter matrix.\n");
03447 exit_program(1);
03448 }
03449 parse_matrix_coeffs(video_enc->inter_matrix, inter_matrix);
03450 }
03451
03452 MATCH_PER_STREAM_OPT(rc_overrides, str, p, oc, st);
03453 for(i=0; p; i++){
03454 int start, end, q;
03455 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
03456 if(e!=3){
03457 av_log(NULL, AV_LOG_FATAL, "error parsing rc_override\n");
03458 exit_program(1);
03459 }
03460 video_enc->rc_override=
03461 av_realloc(video_enc->rc_override,
03462 sizeof(RcOverride)*(i+1));
03463 video_enc->rc_override[i].start_frame= start;
03464 video_enc->rc_override[i].end_frame = end;
03465 if(q>0){
03466 video_enc->rc_override[i].qscale= q;
03467 video_enc->rc_override[i].quality_factor= 1.0;
03468 }
03469 else{
03470 video_enc->rc_override[i].qscale= 0;
03471 video_enc->rc_override[i].quality_factor= -q/100.0;
03472 }
03473 p= strchr(p, '/');
03474 if(p) p++;
03475 }
03476 video_enc->rc_override_count=i;
03477 if (!video_enc->rc_initial_buffer_occupancy)
03478 video_enc->rc_initial_buffer_occupancy = video_enc->rc_buffer_size*3/4;
03479 video_enc->intra_dc_precision= intra_dc_precision - 8;
03480
03481
03482 if (do_pass) {
03483 if (do_pass == 1) {
03484 video_enc->flags |= CODEC_FLAG_PASS1;
03485 } else {
03486 video_enc->flags |= CODEC_FLAG_PASS2;
03487 }
03488 }
03489
03490 MATCH_PER_STREAM_OPT(forced_key_frames, str, forced_key_frames, oc, st);
03491 if (forced_key_frames)
03492 parse_forced_key_frames(forced_key_frames, ost, video_enc);
03493
03494 MATCH_PER_STREAM_OPT(force_fps, i, ost->force_fps, oc, st);
03495
03496 ost->top_field_first = -1;
03497 MATCH_PER_STREAM_OPT(top_field_first, i, ost->top_field_first, oc, st);
03498
03499 MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, ost->copy_initial_nonkeyframes, oc ,st);
03500
03501 #if CONFIG_AVFILTER
03502 MATCH_PER_STREAM_OPT(filters, str, filters, oc, st);
03503 if (filters)
03504 ost->avfilter = av_strdup(filters);
03505 #endif
03506 }
03507
03508 return ost;
03509 }
03510
03511 static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc)
03512 {
03513 AVStream *st;
03514 OutputStream *ost;
03515 AVCodecContext *audio_enc;
03516
03517 ost = new_output_stream(o, oc, AVMEDIA_TYPE_AUDIO);
03518 st = ost->st;
03519
03520 audio_enc = st->codec;
03521 audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
03522
03523 if (!ost->stream_copy) {
03524 char *sample_fmt = NULL;
03525
03526 MATCH_PER_STREAM_OPT(audio_channels, i, audio_enc->channels, oc, st);
03527
03528 MATCH_PER_STREAM_OPT(sample_fmts, str, sample_fmt, oc, st);
03529 if (sample_fmt &&
03530 (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
03531 av_log(NULL, AV_LOG_FATAL, "Invalid sample format '%s'\n", sample_fmt);
03532 exit_program(1);
03533 }
03534
03535 MATCH_PER_STREAM_OPT(audio_sample_rate, i, audio_enc->sample_rate, oc, st);
03536 }
03537
03538 return ost;
03539 }
03540
03541 static OutputStream *new_data_stream(OptionsContext *o, AVFormatContext *oc)
03542 {
03543 OutputStream *ost;
03544
03545 ost = new_output_stream(o, oc, AVMEDIA_TYPE_DATA);
03546 if (!ost->stream_copy) {
03547 av_log(NULL, AV_LOG_FATAL, "Data stream encoding not supported yet (only streamcopy)\n");
03548 exit_program(1);
03549 }
03550
03551 return ost;
03552 }
03553
03554 static OutputStream *new_attachment_stream(OptionsContext *o, AVFormatContext *oc)
03555 {
03556 OutputStream *ost = new_output_stream(o, oc, AVMEDIA_TYPE_ATTACHMENT);
03557 ost->stream_copy = 1;
03558 return ost;
03559 }
03560
03561 static OutputStream *new_subtitle_stream(OptionsContext *o, AVFormatContext *oc)
03562 {
03563 AVStream *st;
03564 OutputStream *ost;
03565 AVCodecContext *subtitle_enc;
03566
03567 ost = new_output_stream(o, oc, AVMEDIA_TYPE_SUBTITLE);
03568 st = ost->st;
03569 subtitle_enc = st->codec;
03570
03571 subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
03572
03573 return ost;
03574 }
03575
03576
03577 static int opt_streamid(OptionsContext *o, const char *opt, const char *arg)
03578 {
03579 int idx;
03580 char *p;
03581 char idx_str[16];
03582
03583 av_strlcpy(idx_str, arg, sizeof(idx_str));
03584 p = strchr(idx_str, ':');
03585 if (!p) {
03586 av_log(NULL, AV_LOG_FATAL,
03587 "Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
03588 arg, opt);
03589 exit_program(1);
03590 }
03591 *p++ = '\0';
03592 idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, MAX_STREAMS-1);
03593 o->streamid_map = grow_array(o->streamid_map, sizeof(*o->streamid_map), &o->nb_streamid_map, idx+1);
03594 o->streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);
03595 return 0;
03596 }
03597
03598 static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata)
03599 {
03600 AVFormatContext *is = ifile->ctx;
03601 AVFormatContext *os = ofile->ctx;
03602 int i;
03603
03604 for (i = 0; i < is->nb_chapters; i++) {
03605 AVChapter *in_ch = is->chapters[i], *out_ch;
03606 int64_t ts_off = av_rescale_q(ofile->start_time - ifile->ts_offset,
03607 AV_TIME_BASE_Q, in_ch->time_base);
03608 int64_t rt = (ofile->recording_time == INT64_MAX) ? INT64_MAX :
03609 av_rescale_q(ofile->recording_time, AV_TIME_BASE_Q, in_ch->time_base);
03610
03611
03612 if (in_ch->end < ts_off)
03613 continue;
03614 if (rt != INT64_MAX && in_ch->start > rt + ts_off)
03615 break;
03616
03617 out_ch = av_mallocz(sizeof(AVChapter));
03618 if (!out_ch)
03619 return AVERROR(ENOMEM);
03620
03621 out_ch->id = in_ch->id;
03622 out_ch->time_base = in_ch->time_base;
03623 out_ch->start = FFMAX(0, in_ch->start - ts_off);
03624 out_ch->end = FFMIN(rt, in_ch->end - ts_off);
03625
03626 if (copy_metadata)
03627 av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
03628
03629 os->nb_chapters++;
03630 os->chapters = av_realloc(os->chapters, sizeof(AVChapter)*os->nb_chapters);
03631 if (!os->chapters)
03632 return AVERROR(ENOMEM);
03633 os->chapters[os->nb_chapters - 1] = out_ch;
03634 }
03635 return 0;
03636 }
03637
03638 static void opt_output_file(void *optctx, const char *filename)
03639 {
03640 OptionsContext *o = optctx;
03641 AVFormatContext *oc;
03642 int i, err;
03643 AVOutputFormat *file_oformat;
03644 OutputStream *ost;
03645 InputStream *ist;
03646
03647 if (!strcmp(filename, "-"))
03648 filename = "pipe:";
03649
03650 err = avformat_alloc_output_context2(&oc, NULL, o->format, filename);
03651 if (!oc) {
03652 print_error(filename, err);
03653 exit_program(1);
03654 }
03655
03656 file_oformat= oc->oformat;
03657 oc->interrupt_callback = int_cb;
03658
03659 if (!o->nb_stream_maps) {
03660
03661 #define NEW_STREAM(type, index)\
03662 if (index >= 0) {\
03663 ost = new_ ## type ## _stream(o, oc);\
03664 ost->source_index = index;\
03665 ost->sync_ist = &input_streams[index];\
03666 input_streams[index].discard = 0;\
03667 }
03668
03669
03670 if (!o->video_disable && oc->oformat->video_codec != CODEC_ID_NONE) {
03671 int area = 0, idx = -1;
03672 for (i = 0; i < nb_input_streams; i++) {
03673 ist = &input_streams[i];
03674 if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
03675 ist->st->codec->width * ist->st->codec->height > area) {
03676 area = ist->st->codec->width * ist->st->codec->height;
03677 idx = i;
03678 }
03679 }
03680 NEW_STREAM(video, idx);
03681 }
03682
03683
03684 if (!o->audio_disable && oc->oformat->audio_codec != CODEC_ID_NONE) {
03685 int channels = 0, idx = -1;
03686 for (i = 0; i < nb_input_streams; i++) {
03687 ist = &input_streams[i];
03688 if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
03689 ist->st->codec->channels > channels) {
03690 channels = ist->st->codec->channels;
03691 idx = i;
03692 }
03693 }
03694 NEW_STREAM(audio, idx);
03695 }
03696
03697
03698 if (!o->subtitle_disable && oc->oformat->subtitle_codec != CODEC_ID_NONE) {
03699 for (i = 0; i < nb_input_streams; i++)
03700 if (input_streams[i].st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
03701 NEW_STREAM(subtitle, i);
03702 break;
03703 }
03704 }
03705
03706 } else {
03707 for (i = 0; i < o->nb_stream_maps; i++) {
03708 StreamMap *map = &o->stream_maps[i];
03709
03710 if (map->disabled)
03711 continue;
03712
03713 ist = &input_streams[input_files[map->file_index].ist_index + map->stream_index];
03714 switch (ist->st->codec->codec_type) {
03715 case AVMEDIA_TYPE_VIDEO: ost = new_video_stream(o, oc); break;
03716 case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream(o, oc); break;
03717 case AVMEDIA_TYPE_SUBTITLE: ost = new_subtitle_stream(o, oc); break;
03718 case AVMEDIA_TYPE_DATA: ost = new_data_stream(o, oc); break;
03719 case AVMEDIA_TYPE_ATTACHMENT: ost = new_attachment_stream(o, oc); break;
03720 default:
03721 av_log(NULL, AV_LOG_FATAL, "Cannot map stream #%d:%d - unsupported type.\n",
03722 map->file_index, map->stream_index);
03723 exit_program(1);
03724 }
03725
03726 ost->source_index = input_files[map->file_index].ist_index + map->stream_index;
03727 ost->sync_ist = &input_streams[input_files[map->sync_file_index].ist_index +
03728 map->sync_stream_index];
03729 ist->discard = 0;
03730 }
03731 }
03732
03733
03734 for (i = 0; i < o->nb_attachments; i++) {
03735 AVIOContext *pb;
03736 uint8_t *attachment;
03737 const char *p;
03738 int64_t len;
03739
03740 if ((err = avio_open2(&pb, o->attachments[i], AVIO_FLAG_READ, &int_cb, NULL)) < 0) {
03741 av_log(NULL, AV_LOG_FATAL, "Could not open attachment file %s.\n",
03742 o->attachments[i]);
03743 exit_program(1);
03744 }
03745 if ((len = avio_size(pb)) <= 0) {
03746 av_log(NULL, AV_LOG_FATAL, "Could not get size of the attachment %s.\n",
03747 o->attachments[i]);
03748 exit_program(1);
03749 }
03750 if (!(attachment = av_malloc(len))) {
03751 av_log(NULL, AV_LOG_FATAL, "Attachment %s too large to fit into memory.\n",
03752 o->attachments[i]);
03753 exit_program(1);
03754 }
03755 avio_read(pb, attachment, len);
03756
03757 ost = new_attachment_stream(o, oc);
03758 ost->stream_copy = 0;
03759 ost->source_index = -1;
03760 ost->attachment_filename = o->attachments[i];
03761 ost->st->codec->extradata = attachment;
03762 ost->st->codec->extradata_size = len;
03763
03764 p = strrchr(o->attachments[i], '/');
03765 av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE);
03766 avio_close(pb);
03767 }
03768
03769 output_files = grow_array(output_files, sizeof(*output_files), &nb_output_files, nb_output_files + 1);
03770 output_files[nb_output_files - 1].ctx = oc;
03771 output_files[nb_output_files - 1].ost_index = nb_output_streams - oc->nb_streams;
03772 output_files[nb_output_files - 1].recording_time = o->recording_time;
03773 output_files[nb_output_files - 1].start_time = o->start_time;
03774 output_files[nb_output_files - 1].limit_filesize = o->limit_filesize;
03775 av_dict_copy(&output_files[nb_output_files - 1].opts, format_opts, 0);
03776
03777
03778 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
03779 if (!av_filename_number_test(oc->filename)) {
03780 print_error(oc->filename, AVERROR(EINVAL));
03781 exit_program(1);
03782 }
03783 }
03784
03785 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
03786
03787 assert_file_overwrite(filename);
03788
03789
03790 if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
03791 &oc->interrupt_callback,
03792 &output_files[nb_output_files - 1].opts)) < 0) {
03793 print_error(filename, err);
03794 exit_program(1);
03795 }
03796 }
03797
03798 if (o->mux_preload) {
03799 uint8_t buf[64];
03800 snprintf(buf, sizeof(buf), "%d", (int)(o->mux_preload*AV_TIME_BASE));
03801 av_dict_set(&output_files[nb_output_files - 1].opts, "preload", buf, 0);
03802 }
03803 oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
03804
03805
03806 if (o->chapters_input_file >= nb_input_files) {
03807 if (o->chapters_input_file == INT_MAX) {
03808
03809 o->chapters_input_file = -1;
03810 for (i = 0; i < nb_input_files; i++)
03811 if (input_files[i].ctx->nb_chapters) {
03812 o->chapters_input_file = i;
03813 break;
03814 }
03815 } else {
03816 av_log(NULL, AV_LOG_FATAL, "Invalid input file index %d in chapter mapping.\n",
03817 o->chapters_input_file);
03818 exit_program(1);
03819 }
03820 }
03821 if (o->chapters_input_file >= 0)
03822 copy_chapters(&input_files[o->chapters_input_file], &output_files[nb_output_files - 1],
03823 !o->metadata_chapters_manual);
03824
03825
03826 for (i = 0; i < o->nb_meta_data_maps; i++) {
03827 AVFormatContext *files[2];
03828 AVDictionary **meta[2];
03829 int j;
03830
03831 #define METADATA_CHECK_INDEX(index, nb_elems, desc)\
03832 if ((index) < 0 || (index) >= (nb_elems)) {\
03833 av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps\n",\
03834 (desc), (index));\
03835 exit_program(1);\
03836 }
03837
03838 int in_file_index = o->meta_data_maps[i][1].file;
03839 if (in_file_index < 0)
03840 continue;
03841 METADATA_CHECK_INDEX(in_file_index, nb_input_files, "input file")
03842
03843 files[0] = oc;
03844 files[1] = input_files[in_file_index].ctx;
03845
03846 for (j = 0; j < 2; j++) {
03847 MetadataMap *map = &o->meta_data_maps[i][j];
03848
03849 switch (map->type) {
03850 case 'g':
03851 meta[j] = &files[j]->metadata;
03852 break;
03853 case 's':
03854 METADATA_CHECK_INDEX(map->index, files[j]->nb_streams, "stream")
03855 meta[j] = &files[j]->streams[map->index]->metadata;
03856 break;
03857 case 'c':
03858 METADATA_CHECK_INDEX(map->index, files[j]->nb_chapters, "chapter")
03859 meta[j] = &files[j]->chapters[map->index]->metadata;
03860 break;
03861 case 'p':
03862 METADATA_CHECK_INDEX(map->index, files[j]->nb_programs, "program")
03863 meta[j] = &files[j]->programs[map->index]->metadata;
03864 break;
03865 }
03866 }
03867
03868 av_dict_copy(meta[0], *meta[1], AV_DICT_DONT_OVERWRITE);
03869 }
03870
03871
03872 if (!o->metadata_global_manual && nb_input_files)
03873 av_dict_copy(&oc->metadata, input_files[0].ctx->metadata,
03874 AV_DICT_DONT_OVERWRITE);
03875 if (!o->metadata_streams_manual)
03876 for (i = output_files[nb_output_files - 1].ost_index; i < nb_output_streams; i++) {
03877 InputStream *ist;
03878 if (output_streams[i].source_index < 0)
03879 continue;
03880 ist = &input_streams[output_streams[i].source_index];
03881 av_dict_copy(&output_streams[i].st->metadata, ist->st->metadata, AV_DICT_DONT_OVERWRITE);
03882 }
03883
03884
03885 for (i = 0; i < o->nb_metadata; i++) {
03886 AVDictionary **m;
03887 char type, *val;
03888 int index = 0;
03889
03890 val = strchr(o->metadata[i].u.str, '=');
03891 if (!val) {
03892 av_log(NULL, AV_LOG_FATAL, "No '=' character in metadata string %s.\n",
03893 o->metadata[i].u.str);
03894 exit_program(1);
03895 }
03896 *val++ = 0;
03897
03898 parse_meta_type(o->metadata[i].specifier, &type, &index);
03899 switch (type) {
03900 case 'g':
03901 m = &oc->metadata;
03902 break;
03903 case 's':
03904 if (index < 0 || index >= oc->nb_streams) {
03905 av_log(NULL, AV_LOG_FATAL, "Invalid stream index %d in metadata specifier.\n", index);
03906 exit_program(1);
03907 }
03908 m = &oc->streams[index]->metadata;
03909 break;
03910 case 'c':
03911 if (index < 0 || index >= oc->nb_chapters) {
03912 av_log(NULL, AV_LOG_FATAL, "Invalid chapter index %d in metadata specifier.\n", index);
03913 exit_program(1);
03914 }
03915 m = &oc->chapters[index]->metadata;
03916 break;
03917 default:
03918 av_log(NULL, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", o->metadata[i].specifier);
03919 exit_program(1);
03920 }
03921
03922 av_dict_set(m, o->metadata[i].u.str, *val ? val : NULL, 0);
03923 }
03924
03925 reset_options(o);
03926 }
03927
03928
03929 static int opt_pass(const char *opt, const char *arg)
03930 {
03931 do_pass = parse_number_or_die(opt, arg, OPT_INT, 1, 2);
03932 return 0;
03933 }
03934
03935 static int64_t getutime(void)
03936 {
03937 #if HAVE_GETRUSAGE
03938 struct rusage rusage;
03939
03940 getrusage(RUSAGE_SELF, &rusage);
03941 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
03942 #elif HAVE_GETPROCESSTIMES
03943 HANDLE proc;
03944 FILETIME c, e, k, u;
03945 proc = GetCurrentProcess();
03946 GetProcessTimes(proc, &c, &e, &k, &u);
03947 return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
03948 #else
03949 return av_gettime();
03950 #endif
03951 }
03952
03953 static int64_t getmaxrss(void)
03954 {
03955 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
03956 struct rusage rusage;
03957 getrusage(RUSAGE_SELF, &rusage);
03958 return (int64_t)rusage.ru_maxrss * 1024;
03959 #elif HAVE_GETPROCESSMEMORYINFO
03960 HANDLE proc;
03961 PROCESS_MEMORY_COUNTERS memcounters;
03962 proc = GetCurrentProcess();
03963 memcounters.cb = sizeof(memcounters);
03964 GetProcessMemoryInfo(proc, &memcounters, sizeof(memcounters));
03965 return memcounters.PeakPagefileUsage;
03966 #else
03967 return 0;
03968 #endif
03969 }
03970
03971 static int opt_audio_qscale(OptionsContext *o, const char *opt, const char *arg)
03972 {
03973 return parse_option(o, "q:a", arg, options);
03974 }
03975
03976 static void show_usage(void)
03977 {
03978 printf("Hyper fast Audio and Video encoder\n");
03979 printf("usage: %s [options] [[infile options] -i infile]... {[outfile options] outfile}...\n", program_name);
03980 printf("\n");
03981 }
03982
03983 static int opt_help(const char *opt, const char *arg)
03984 {
03985 int flags = AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM;
03986 av_log_set_callback(log_callback_help);
03987 show_usage();
03988 show_help_options(options, "Main options:\n",
03989 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_SUBTITLE | OPT_GRAB, 0);
03990 show_help_options(options, "\nAdvanced options:\n",
03991 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_SUBTITLE | OPT_GRAB,
03992 OPT_EXPERT);
03993 show_help_options(options, "\nVideo options:\n",
03994 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
03995 OPT_VIDEO);
03996 show_help_options(options, "\nAdvanced Video options:\n",
03997 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
03998 OPT_VIDEO | OPT_EXPERT);
03999 show_help_options(options, "\nAudio options:\n",
04000 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
04001 OPT_AUDIO);
04002 show_help_options(options, "\nAdvanced Audio options:\n",
04003 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
04004 OPT_AUDIO | OPT_EXPERT);
04005 show_help_options(options, "\nSubtitle options:\n",
04006 OPT_SUBTITLE | OPT_GRAB,
04007 OPT_SUBTITLE);
04008 show_help_options(options, "\nAudio/Video grab options:\n",
04009 OPT_GRAB,
04010 OPT_GRAB);
04011 printf("\n");
04012 show_help_children(avcodec_get_class(), flags);
04013 show_help_children(avformat_get_class(), flags);
04014 show_help_children(sws_get_class(), flags);
04015
04016 return 0;
04017 }
04018
04019 static int opt_target(OptionsContext *o, const char *opt, const char *arg)
04020 {
04021 enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
04022 static const char *const frame_rates[] = {"25", "30000/1001", "24000/1001"};
04023
04024 if(!strncmp(arg, "pal-", 4)) {
04025 norm = PAL;
04026 arg += 4;
04027 } else if(!strncmp(arg, "ntsc-", 5)) {
04028 norm = NTSC;
04029 arg += 5;
04030 } else if(!strncmp(arg, "film-", 5)) {
04031 norm = FILM;
04032 arg += 5;
04033 } else {
04034
04035 if(nb_input_files) {
04036 int i, j, fr;
04037 for (j = 0; j < nb_input_files; j++) {
04038 for (i = 0; i < input_files[j].nb_streams; i++) {
04039 AVCodecContext *c = input_files[j].ctx->streams[i]->codec;
04040 if(c->codec_type != AVMEDIA_TYPE_VIDEO)
04041 continue;
04042 fr = c->time_base.den * 1000 / c->time_base.num;
04043 if(fr == 25000) {
04044 norm = PAL;
04045 break;
04046 } else if((fr == 29970) || (fr == 23976)) {
04047 norm = NTSC;
04048 break;
04049 }
04050 }
04051 if(norm != UNKNOWN)
04052 break;
04053 }
04054 }
04055 if (norm != UNKNOWN)
04056 av_log(NULL, AV_LOG_INFO, "Assuming %s for target.\n", norm == PAL ? "PAL" : "NTSC");
04057 }
04058
04059 if(norm == UNKNOWN) {
04060 av_log(NULL, AV_LOG_FATAL, "Could not determine norm (PAL/NTSC/NTSC-Film) for target.\n");
04061 av_log(NULL, AV_LOG_FATAL, "Please prefix target with \"pal-\", \"ntsc-\" or \"film-\",\n");
04062 av_log(NULL, AV_LOG_FATAL, "or set a framerate with \"-r xxx\".\n");
04063 exit_program(1);
04064 }
04065
04066 if(!strcmp(arg, "vcd")) {
04067 opt_video_codec(o, "c:v", "mpeg1video");
04068 opt_audio_codec(o, "c:a", "mp2");
04069 parse_option(o, "f", "vcd", options);
04070
04071 parse_option(o, "s", norm == PAL ? "352x288" : "352x240", options);
04072 parse_option(o, "r", frame_rates[norm], options);
04073 opt_default("g", norm == PAL ? "15" : "18");
04074
04075 opt_default("b", "1150000");
04076 opt_default("maxrate", "1150000");
04077 opt_default("minrate", "1150000");
04078 opt_default("bufsize", "327680");
04079
04080 opt_default("b:a", "224000");
04081 parse_option(o, "ar", "44100", options);
04082 parse_option(o, "ac", "2", options);
04083
04084 opt_default("packetsize", "2324");
04085 opt_default("muxrate", "1411200");
04086
04087
04088
04089
04090
04091
04092 o->mux_preload = (36000+3*1200) / 90000.0;
04093 } else if(!strcmp(arg, "svcd")) {
04094
04095 opt_video_codec(o, "c:v", "mpeg2video");
04096 opt_audio_codec(o, "c:a", "mp2");
04097 parse_option(o, "f", "svcd", options);
04098
04099 parse_option(o, "s", norm == PAL ? "480x576" : "480x480", options);
04100 parse_option(o, "r", frame_rates[norm], options);
04101 opt_default("g", norm == PAL ? "15" : "18");
04102
04103 opt_default("b", "2040000");
04104 opt_default("maxrate", "2516000");
04105 opt_default("minrate", "0");
04106 opt_default("bufsize", "1835008");
04107 opt_default("flags", "+scan_offset");
04108
04109
04110 opt_default("b:a", "224000");
04111 parse_option(o, "ar", "44100", options);
04112
04113 opt_default("packetsize", "2324");
04114
04115 } else if(!strcmp(arg, "dvd")) {
04116
04117 opt_video_codec(o, "c:v", "mpeg2video");
04118 opt_audio_codec(o, "c:a", "ac3");
04119 parse_option(o, "f", "dvd", options);
04120
04121 parse_option(o, "s", norm == PAL ? "720x576" : "720x480", options);
04122 parse_option(o, "r", frame_rates[norm], options);
04123 opt_default("g", norm == PAL ? "15" : "18");
04124
04125 opt_default("b", "6000000");
04126 opt_default("maxrate", "9000000");
04127 opt_default("minrate", "0");
04128 opt_default("bufsize", "1835008");
04129
04130 opt_default("packetsize", "2048");
04131 opt_default("muxrate", "10080000");
04132
04133 opt_default("b:a", "448000");
04134 parse_option(o, "ar", "48000", options);
04135
04136 } else if(!strncmp(arg, "dv", 2)) {
04137
04138 parse_option(o, "f", "dv", options);
04139
04140 parse_option(o, "s", norm == PAL ? "720x576" : "720x480", options);
04141 parse_option(o, "pix_fmt", !strncmp(arg, "dv50", 4) ? "yuv422p" :
04142 norm == PAL ? "yuv420p" : "yuv411p", options);
04143 parse_option(o, "r", frame_rates[norm], options);
04144
04145 parse_option(o, "ar", "48000", options);
04146 parse_option(o, "ac", "2", options);
04147
04148 } else {
04149 av_log(NULL, AV_LOG_ERROR, "Unknown target: %s\n", arg);
04150 return AVERROR(EINVAL);
04151 }
04152 return 0;
04153 }
04154
04155 static int opt_vstats_file(const char *opt, const char *arg)
04156 {
04157 av_free (vstats_filename);
04158 vstats_filename=av_strdup (arg);
04159 return 0;
04160 }
04161
04162 static int opt_vstats(const char *opt, const char *arg)
04163 {
04164 char filename[40];
04165 time_t today2 = time(NULL);
04166 struct tm *today = localtime(&today2);
04167
04168 snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
04169 today->tm_sec);
04170 return opt_vstats_file(opt, filename);
04171 }
04172
04173 static int opt_video_frames(OptionsContext *o, const char *opt, const char *arg)
04174 {
04175 return parse_option(o, "frames:v", arg, options);
04176 }
04177
04178 static int opt_audio_frames(OptionsContext *o, const char *opt, const char *arg)
04179 {
04180 return parse_option(o, "frames:a", arg, options);
04181 }
04182
04183 static int opt_data_frames(OptionsContext *o, const char *opt, const char *arg)
04184 {
04185 return parse_option(o, "frames:d", arg, options);
04186 }
04187
04188 static void log_callback_null(void* ptr, int level, const char* fmt, va_list vl)
04189 {
04190 }
04191
04192 static int opt_passlogfile(const char *opt, const char *arg)
04193 {
04194 pass_logfilename_prefix = arg;
04195 #if CONFIG_LIBX264_ENCODER
04196 return opt_default("passlogfile", arg);
04197 #else
04198 return 0;
04199 #endif
04200 }
04201
04202 static int opt_video_tag(OptionsContext *o, const char *opt, const char *arg)
04203 {
04204 return parse_option(o, "tag:v", arg, options);
04205 }
04206
04207 static int opt_audio_tag(OptionsContext *o, const char *opt, const char *arg)
04208 {
04209 return parse_option(o, "tag:a", arg, options);
04210 }
04211
04212 static int opt_subtitle_tag(OptionsContext *o, const char *opt, const char *arg)
04213 {
04214 return parse_option(o, "tag:s", arg, options);
04215 }
04216
04217 static int opt_video_filters(OptionsContext *o, const char *opt, const char *arg)
04218 {
04219 return parse_option(o, "filter:v", arg, options);
04220 }
04221
04222 #define OFFSET(x) offsetof(OptionsContext, x)
04223 static const OptionDef options[] = {
04224
04225 #include "cmdutils_common_opts.h"
04226 { "f", HAS_ARG | OPT_STRING | OPT_OFFSET, {.off = OFFSET(format)}, "force format", "fmt" },
04227 { "i", HAS_ARG | OPT_FUNC2, {(void*)opt_input_file}, "input file name", "filename" },
04228 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
04229 { "n", OPT_BOOL, {(void*)&no_file_overwrite}, "do not overwrite output files" },
04230 { "c", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(codec_names)}, "codec name", "codec" },
04231 { "codec", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(codec_names)}, "codec name", "codec" },
04232 { "pre", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(presets)}, "preset name", "preset" },
04233 { "map", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map}, "set input stream mapping", "file.stream[:syncfile.syncstream]" },
04234 { "map_metadata", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map_metadata}, "set metadata information of outfile from infile",
04235 "outfile[,metadata]:infile[,metadata]" },
04236 { "map_chapters", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_OFFSET, {.off = OFFSET(chapters_input_file)}, "set chapters mapping", "input_file_index" },
04237 { "t", HAS_ARG | OPT_TIME | OPT_OFFSET, {.off = OFFSET(recording_time)}, "record or transcode \"duration\" seconds of audio/video", "duration" },
04238 { "fs", HAS_ARG | OPT_INT64 | OPT_OFFSET, {.off = OFFSET(limit_filesize)}, "set the limit file size in bytes", "limit_size" },
04239 { "ss", HAS_ARG | OPT_TIME | OPT_OFFSET, {.off = OFFSET(start_time)}, "set the start time offset", "time_off" },
04240 { "itsoffset", HAS_ARG | OPT_TIME | OPT_OFFSET, {.off = OFFSET(input_ts_offset)}, "set the input ts offset", "time_off" },
04241 { "itsscale", HAS_ARG | OPT_DOUBLE | OPT_SPEC, {.off = OFFSET(ts_scale)}, "set the input ts scale", "scale" },
04242 { "metadata", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(metadata)}, "add metadata", "string=string" },
04243 { "dframes", HAS_ARG | OPT_FUNC2, {(void*)opt_data_frames}, "set the number of data frames to record", "number" },
04244 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
04245 "add timings for benchmarking" },
04246 { "timelimit", HAS_ARG, {(void*)opt_timelimit}, "set max runtime in seconds", "limit" },
04247 { "dump", OPT_BOOL | OPT_EXPERT, {(void*)&do_pkt_dump},
04248 "dump each input packet" },
04249 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
04250 "when dumping packets, also dump the payload" },
04251 { "re", OPT_BOOL | OPT_EXPERT | OPT_OFFSET, {.off = OFFSET(rate_emu)}, "read input at native frame rate", "" },
04252 { "target", HAS_ARG | OPT_FUNC2, {(void*)opt_target}, "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\", \"dv50\", \"pal-vcd\", \"ntsc-svcd\", ...)", "type" },
04253 { "vsync", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&video_sync_method}, "video sync method", "" },
04254 { "async", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&audio_sync_method}, "audio sync method", "" },
04255 { "adrift_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, {(void*)&audio_drift_threshold}, "audio drift threshold", "threshold" },
04256 { "copyts", OPT_BOOL | OPT_EXPERT, {(void*)©_ts}, "copy timestamps" },
04257 { "copytb", OPT_BOOL | OPT_EXPERT, {(void*)©_tb}, "copy input stream time base when stream copying" },
04258 { "shortest", OPT_BOOL | OPT_EXPERT, {(void*)&opt_shortest}, "finish encoding within shortest input" },
04259 { "dts_delta_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, {(void*)&dts_delta_threshold}, "timestamp discontinuity delta threshold", "threshold" },
04260 { "xerror", OPT_BOOL, {(void*)&exit_on_error}, "exit on error", "error" },
04261 { "copyinkf", OPT_BOOL | OPT_EXPERT | OPT_SPEC, {.off = OFFSET(copy_initial_nonkeyframes)}, "copy initial non-keyframes" },
04262 { "frames", OPT_INT64 | HAS_ARG | OPT_SPEC, {.off = OFFSET(max_frames)}, "set the number of frames to record", "number" },
04263 { "tag", OPT_STRING | HAS_ARG | OPT_SPEC, {.off = OFFSET(codec_tags)}, "force codec tag/fourcc", "fourcc/tag" },
04264 { "q", HAS_ARG | OPT_EXPERT | OPT_DOUBLE | OPT_SPEC, {.off = OFFSET(qscale)}, "use fixed quality scale (VBR)", "q" },
04265 { "qscale", HAS_ARG | OPT_EXPERT | OPT_DOUBLE | OPT_SPEC, {.off = OFFSET(qscale)}, "use fixed quality scale (VBR)", "q" },
04266 #if CONFIG_AVFILTER
04267 { "filter", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(filters)}, "set stream filterchain", "filter_list" },
04268 #endif
04269 { "stats", OPT_BOOL, {&print_stats}, "print progress report during encoding", },
04270 { "attach", HAS_ARG | OPT_FUNC2, {(void*)opt_attach}, "add an attachment to the output file", "filename" },
04271 { "dump_attachment", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(dump_attachment)}, "extract an attachment into a file", "filename" },
04272
04273
04274 { "vframes", HAS_ARG | OPT_VIDEO | OPT_FUNC2, {(void*)opt_video_frames}, "set the number of video frames to record", "number" },
04275 { "r", HAS_ARG | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_rates)}, "set frame rate (Hz value, fraction or abbreviation)", "rate" },
04276 { "s", HAS_ARG | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_sizes)}, "set frame size (WxH or abbreviation)", "size" },
04277 { "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" },
04278 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(frame_pix_fmts)}, "set pixel format", "format" },
04279 { "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" },
04280 { "vn", OPT_BOOL | OPT_VIDEO | OPT_OFFSET, {.off = OFFSET(video_disable)}, "disable video" },
04281 { "vdt", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)&video_discard}, "discard threshold", "n" },
04282 { "rc_override", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(rc_overrides)}, "rate control override for specific intervals", "override" },
04283 { "vcodec", HAS_ARG | OPT_VIDEO | OPT_FUNC2, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
04284 { "same_quant", OPT_BOOL | OPT_VIDEO, {(void*)&same_quant},
04285 "use same quantizer as source (implies VBR)" },
04286 { "pass", HAS_ARG | OPT_VIDEO, {(void*)opt_pass}, "select the pass number (1 or 2)", "n" },
04287 { "passlogfile", HAS_ARG | OPT_VIDEO, {(void*)&opt_passlogfile}, "select two pass log file name prefix", "prefix" },
04288 { "deinterlace", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_deinterlace},
04289 "deinterlace pictures" },
04290 { "vstats", OPT_EXPERT | OPT_VIDEO, {(void*)&opt_vstats}, "dump video coding statistics to file" },
04291 { "vstats_file", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_vstats_file}, "dump video coding statistics to file", "file" },
04292 #if CONFIG_AVFILTER
04293 { "vf", HAS_ARG | OPT_VIDEO | OPT_FUNC2, {(void*)opt_video_filters}, "video filters", "filter list" },
04294 #endif
04295 { "intra_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(intra_matrices)}, "specify intra matrix coeffs", "matrix" },
04296 { "inter_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_STRING | OPT_SPEC, {.off = OFFSET(inter_matrices)}, "specify inter matrix coeffs", "matrix" },
04297 { "top", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_INT| OPT_SPEC, {.off = OFFSET(top_field_first)}, "top=1/bottom=0/auto=-1 field first", "" },
04298 { "dc", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)&intra_dc_precision}, "intra_dc_precision", "precision" },
04299 { "vtag", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_FUNC2, {(void*)opt_video_tag}, "force video tag/fourcc", "fourcc/tag" },
04300 { "qphist", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, { (void *)&qp_hist }, "show QP histogram" },
04301 { "force_fps", OPT_BOOL | OPT_EXPERT | OPT_VIDEO | OPT_SPEC, {.off = OFFSET(force_fps)}, "force the selected framerate, disable the best supported framerate selection" },
04302 { "streamid", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_streamid}, "set the value of an outfile streamid", "streamIndex:value" },
04303 { "force_key_frames", OPT_STRING | HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_SPEC, {.off = OFFSET(forced_key_frames)}, "force key frames at specified timestamps", "timestamps" },
04304
04305
04306 { "aframes", HAS_ARG | OPT_AUDIO | OPT_FUNC2, {(void*)opt_audio_frames}, "set the number of audio frames to record", "number" },
04307 { "aq", HAS_ARG | OPT_AUDIO | OPT_FUNC2, {(void*)opt_audio_qscale}, "set audio quality (codec-specific)", "quality", },
04308 { "ar", HAS_ARG | OPT_AUDIO | OPT_INT | OPT_SPEC, {.off = OFFSET(audio_sample_rate)}, "set audio sampling rate (in Hz)", "rate" },
04309 { "ac", HAS_ARG | OPT_AUDIO | OPT_INT | OPT_SPEC, {.off = OFFSET(audio_channels)}, "set number of audio channels", "channels" },
04310 { "an", OPT_BOOL | OPT_AUDIO | OPT_OFFSET, {.off = OFFSET(audio_disable)}, "disable audio" },
04311 { "acodec", HAS_ARG | OPT_AUDIO | OPT_FUNC2, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
04312 { "atag", HAS_ARG | OPT_EXPERT | OPT_AUDIO | OPT_FUNC2, {(void*)opt_audio_tag}, "force audio tag/fourcc", "fourcc/tag" },
04313 { "vol", OPT_INT | HAS_ARG | OPT_AUDIO, {(void*)&audio_volume}, "change audio volume (256=normal)" , "volume" },
04314 { "sample_fmt", HAS_ARG | OPT_EXPERT | OPT_AUDIO | OPT_SPEC | OPT_STRING, {.off = OFFSET(sample_fmts)}, "set sample format", "format" },
04315
04316
04317 { "sn", OPT_BOOL | OPT_SUBTITLE | OPT_OFFSET, {.off = OFFSET(subtitle_disable)}, "disable subtitle" },
04318 { "scodec", HAS_ARG | OPT_SUBTITLE | OPT_FUNC2, {(void*)opt_subtitle_codec}, "force subtitle codec ('copy' to copy stream)", "codec" },
04319 { "stag", HAS_ARG | OPT_EXPERT | OPT_SUBTITLE | OPT_FUNC2, {(void*)opt_subtitle_tag}, "force subtitle tag/fourcc", "fourcc/tag" },
04320
04321
04322 { "isync", OPT_BOOL | OPT_EXPERT | OPT_GRAB, {(void*)&input_sync}, "sync read on input", "" },
04323
04324
04325 { "muxdelay", OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET, {.off = OFFSET(mux_max_delay)}, "set the maximum demux-decode delay", "seconds" },
04326 { "muxpreload", OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET, {.off = OFFSET(mux_preload)}, "set the initial demux-decode delay", "seconds" },
04327
04328 { "bsf", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(bitstream_filters)}, "A comma-separated list of bitstream filters", "bitstream_filters" },
04329
04330
04331 { "dcodec", HAS_ARG | OPT_DATA | OPT_FUNC2, {(void*)opt_data_codec}, "force data codec ('copy' to copy stream)", "codec" },
04332
04333 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
04334 { NULL, },
04335 };
04336
04337 int main(int argc, char **argv)
04338 {
04339 OptionsContext o = { 0 };
04340 int64_t ti;
04341
04342 reset_options(&o);
04343
04344 av_log_set_flags(AV_LOG_SKIP_REPEATED);
04345 parse_loglevel(argc, argv, options);
04346
04347 if(argc>1 && !strcmp(argv[1], "-d")){
04348 run_as_daemon=1;
04349 av_log_set_callback(log_callback_null);
04350 argc--;
04351 argv++;
04352 }
04353
04354 avcodec_register_all();
04355 #if CONFIG_AVDEVICE
04356 avdevice_register_all();
04357 #endif
04358 #if CONFIG_AVFILTER
04359 avfilter_register_all();
04360 #endif
04361 av_register_all();
04362 avformat_network_init();
04363
04364 show_banner(argc, argv, options);
04365
04366
04367 parse_options(&o, argc, argv, options, opt_output_file);
04368
04369 if(nb_output_files <= 0 && nb_input_files == 0) {
04370 show_usage();
04371 av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
04372 exit_program(1);
04373 }
04374
04375
04376 if (nb_output_files <= 0) {
04377 fprintf(stderr, "At least one output file must be specified\n");
04378 exit_program(1);
04379 }
04380
04381 if (nb_input_files == 0) {
04382 av_log(NULL, AV_LOG_FATAL, "At least one input file must be specified\n");
04383 exit_program(1);
04384 }
04385
04386 ti = getutime();
04387 if (transcode(output_files, nb_output_files, input_files, nb_input_files) < 0)
04388 exit_program(1);
04389 ti = getutime() - ti;
04390 if (do_benchmark) {
04391 int maxrss = getmaxrss() / 1024;
04392 printf("bench: utime=%0.3fs maxrss=%ikB\n", ti / 1000000.0, maxrss);
04393 }
04394
04395 exit_program(0);
04396 return 0;
04397 }