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