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