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