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