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