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