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 "libavcore/audioconvert.h"
00040 #include "libavcore/parseutils.h"
00041 #include "libavcore/samplefmt.h"
00042 #include "libavutil/colorspace.h"
00043 #include "libavutil/fifo.h"
00044 #include "libavutil/intreadwrite.h"
00045 #include "libavutil/pixdesc.h"
00046 #include "libavutil/avstring.h"
00047 #include "libavutil/libm.h"
00048 #include "libavformat/os_support.h"
00049
00050 #if CONFIG_AVFILTER
00051 # include "libavfilter/avfilter.h"
00052 # include "libavfilter/avfiltergraph.h"
00053 # include "libavfilter/vsrc_buffer.h"
00054 #endif
00055
00056 #if HAVE_SYS_RESOURCE_H
00057 #include <sys/types.h>
00058 #include <sys/time.h>
00059 #include <sys/resource.h>
00060 #elif HAVE_GETPROCESSTIMES
00061 #include <windows.h>
00062 #endif
00063 #if HAVE_GETPROCESSMEMORYINFO
00064 #include <windows.h>
00065 #include <psapi.h>
00066 #endif
00067
00068 #if HAVE_SYS_SELECT_H
00069 #include <sys/select.h>
00070 #endif
00071
00072 #if HAVE_TERMIOS_H
00073 #include <fcntl.h>
00074 #include <sys/ioctl.h>
00075 #include <sys/time.h>
00076 #include <termios.h>
00077 #elif HAVE_CONIO_H
00078 #include <conio.h>
00079 #endif
00080 #include <time.h>
00081
00082 #include "cmdutils.h"
00083
00084 #include "libavutil/avassert.h"
00085
00086 const char program_name[] = "FFmpeg";
00087 const int program_birth_year = 2000;
00088
00089
00090 typedef struct AVStreamMap {
00091 int file_index;
00092 int stream_index;
00093 int sync_file_index;
00094 int sync_stream_index;
00095 } AVStreamMap;
00096
00100 typedef struct AVMetaDataMap {
00101 int file;
00102 char type;
00103 int index;
00104 } AVMetaDataMap;
00105
00106 typedef struct AVChapterMap {
00107 int in_file;
00108 int out_file;
00109 } AVChapterMap;
00110
00111 static const OptionDef options[];
00112
00113 #define MAX_FILES 100
00114 #if !FF_API_MAX_STREAMS
00115 #define MAX_STREAMS 1024
00116 #endif
00117
00118 static const char *last_asked_format = NULL;
00119 static AVFormatContext *input_files[MAX_FILES];
00120 static int64_t input_files_ts_offset[MAX_FILES];
00121 static double *input_files_ts_scale[MAX_FILES] = {NULL};
00122 static AVCodec **input_codecs = NULL;
00123 static int nb_input_files = 0;
00124 static int nb_input_codecs = 0;
00125 static int nb_input_files_ts_scale[MAX_FILES] = {0};
00126
00127 static AVFormatContext *output_files[MAX_FILES];
00128 static AVCodec **output_codecs = NULL;
00129 static int nb_output_files = 0;
00130 static int nb_output_codecs = 0;
00131
00132 static AVStreamMap *stream_maps = NULL;
00133 static int nb_stream_maps;
00134
00135
00136 static AVMetaDataMap (*meta_data_maps)[2] = NULL;
00137 static int nb_meta_data_maps;
00138 static int metadata_global_autocopy = 1;
00139 static int metadata_streams_autocopy = 1;
00140 static int metadata_chapters_autocopy = 1;
00141
00142 static AVChapterMap *chapter_maps = NULL;
00143 static int nb_chapter_maps;
00144
00145
00146 static int *streamid_map = NULL;
00147 static int nb_streamid_map = 0;
00148
00149 static int frame_width = 0;
00150 static int frame_height = 0;
00151 static float frame_aspect_ratio = 0;
00152 static enum PixelFormat frame_pix_fmt = PIX_FMT_NONE;
00153 static enum AVSampleFormat audio_sample_fmt = AV_SAMPLE_FMT_NONE;
00154 static int max_frames[4] = {INT_MAX, INT_MAX, INT_MAX, INT_MAX};
00155 static AVRational frame_rate;
00156 static float video_qscale = 0;
00157 static uint16_t *intra_matrix = NULL;
00158 static uint16_t *inter_matrix = NULL;
00159 static const char *video_rc_override_string=NULL;
00160 static int video_disable = 0;
00161 static int video_discard = 0;
00162 static char *video_codec_name = NULL;
00163 static unsigned int video_codec_tag = 0;
00164 static char *video_language = NULL;
00165 static int same_quality = 0;
00166 static int do_deinterlace = 0;
00167 static int top_field_first = -1;
00168 static int me_threshold = 0;
00169 static int intra_dc_precision = 8;
00170 static int loop_input = 0;
00171 static int loop_output = AVFMT_NOOUTPUTLOOP;
00172 static int qp_hist = 0;
00173 #if CONFIG_AVFILTER
00174 static char *vfilters = NULL;
00175 AVFilterGraph *graph = NULL;
00176 #endif
00177
00178 static int intra_only = 0;
00179 static int audio_sample_rate = 44100;
00180 static int64_t channel_layout = 0;
00181 #define QSCALE_NONE -99999
00182 static float audio_qscale = QSCALE_NONE;
00183 static int audio_disable = 0;
00184 static int audio_channels = 1;
00185 static char *audio_codec_name = NULL;
00186 static unsigned int audio_codec_tag = 0;
00187 static char *audio_language = NULL;
00188
00189 static int subtitle_disable = 0;
00190 static char *subtitle_codec_name = NULL;
00191 static char *subtitle_language = NULL;
00192 static unsigned int subtitle_codec_tag = 0;
00193
00194 static float mux_preload= 0.5;
00195 static float mux_max_delay= 0.7;
00196
00197 static int64_t recording_time = INT64_MAX;
00198 static int64_t start_time = 0;
00199 static int64_t recording_timestamp = 0;
00200 static int64_t input_ts_offset = 0;
00201 static int file_overwrite = 0;
00202 static AVMetadata *metadata;
00203 static int do_benchmark = 0;
00204 static int do_hex_dump = 0;
00205 static int do_pkt_dump = 0;
00206 static int do_psnr = 0;
00207 static int do_pass = 0;
00208 static char *pass_logfilename_prefix = NULL;
00209 static int audio_stream_copy = 0;
00210 static int video_stream_copy = 0;
00211 static int subtitle_stream_copy = 0;
00212 static int video_sync_method= -1;
00213 static int audio_sync_method= 0;
00214 static float audio_drift_threshold= 0.1;
00215 static int copy_ts= 0;
00216 static int copy_tb;
00217 static int opt_shortest = 0;
00218 static int video_global_header = 0;
00219 static char *vstats_filename;
00220 static FILE *vstats_file;
00221 static int opt_programid = 0;
00222 static int copy_initial_nonkeyframes = 0;
00223
00224 static int rate_emu = 0;
00225
00226 static int video_channel = 0;
00227 static char *video_standard;
00228
00229 static int audio_volume = 256;
00230
00231 static int exit_on_error = 0;
00232 static int using_stdin = 0;
00233 static int verbose = 1;
00234 static int thread_count= 1;
00235 static int q_pressed = 0;
00236 static int64_t video_size = 0;
00237 static int64_t audio_size = 0;
00238 static int64_t extra_size = 0;
00239 static int nb_frames_dup = 0;
00240 static int nb_frames_drop = 0;
00241 static int input_sync;
00242 static uint64_t limit_filesize = 0;
00243 static int force_fps = 0;
00244 static char *forced_key_frames = NULL;
00245
00246 static float dts_delta_threshold = 10;
00247
00248 static unsigned int sws_flags = SWS_BICUBIC;
00249
00250 static int64_t timer_start;
00251
00252 static uint8_t *audio_buf;
00253 static uint8_t *audio_out;
00254 unsigned int allocated_audio_out_size, allocated_audio_buf_size;
00255
00256 static short *samples;
00257
00258 static AVBitStreamFilterContext *video_bitstream_filters=NULL;
00259 static AVBitStreamFilterContext *audio_bitstream_filters=NULL;
00260 static AVBitStreamFilterContext *subtitle_bitstream_filters=NULL;
00261
00262 #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
00263
00264 struct AVInputStream;
00265
00266 typedef struct AVOutputStream {
00267 int file_index;
00268 int index;
00269 int source_index;
00270 AVStream *st;
00271 int encoding_needed;
00272 int frame_number;
00273
00274
00275
00276 struct AVInputStream *sync_ist;
00277 int64_t sync_opts;
00278 AVBitStreamFilterContext *bitstream_filters;
00279
00280 int video_resample;
00281 AVFrame pict_tmp;
00282 struct SwsContext *img_resample_ctx;
00283 int resample_height;
00284 int resample_width;
00285 int resample_pix_fmt;
00286
00287
00288 int original_height;
00289 int original_width;
00290
00291
00292 int64_t *forced_kf_pts;
00293 int forced_kf_count;
00294 int forced_kf_index;
00295
00296
00297 int audio_resample;
00298 ReSampleContext *resample;
00299 int resample_sample_fmt;
00300 int resample_channels;
00301 int resample_sample_rate;
00302 int reformat_pair;
00303 AVAudioConvert *reformat_ctx;
00304 AVFifoBuffer *fifo;
00305 FILE *logfile;
00306 } AVOutputStream;
00307
00308 static AVOutputStream **output_streams_for_file[MAX_FILES] = { NULL };
00309 static int nb_output_streams_for_file[MAX_FILES] = { 0 };
00310
00311 typedef struct AVInputStream {
00312 int file_index;
00313 int index;
00314 AVStream *st;
00315 int discard;
00316 int decoding_needed;
00317 int64_t sample_index;
00318
00319 int64_t start;
00320 int64_t next_pts;
00321
00322 int64_t pts;
00323 PtsCorrectionContext pts_ctx;
00324 int is_start;
00325 int showed_multi_packet_warning;
00326 int is_past_recording_time;
00327 #if CONFIG_AVFILTER
00328 AVFilterContext *output_video_filter;
00329 AVFilterContext *input_video_filter;
00330 AVFrame *filter_frame;
00331 int has_filter_frame;
00332 AVFilterBufferRef *picref;
00333 #endif
00334 } AVInputStream;
00335
00336 typedef struct AVInputFile {
00337 int eof_reached;
00338 int ist_index;
00339 int buffer_size;
00340 int nb_streams;
00341 } AVInputFile;
00342
00343 #if HAVE_TERMIOS_H
00344
00345
00346 static struct termios oldtty;
00347 #endif
00348
00349 #if CONFIG_AVFILTER
00350
00351 static int configure_filters(AVInputStream *ist, AVOutputStream *ost)
00352 {
00353 AVFilterContext *last_filter, *filter;
00355 AVCodecContext *codec = ost->st->codec;
00356 AVCodecContext *icodec = ist->st->codec;
00357 FFSinkContext ffsink_ctx = { .pix_fmt = codec->pix_fmt };
00358 char args[255];
00359 int ret;
00360
00361 graph = avfilter_graph_alloc();
00362
00363 snprintf(args, 255, "%d:%d:%d:%d:%d", ist->st->codec->width,
00364 ist->st->codec->height, ist->st->codec->pix_fmt, 1, AV_TIME_BASE);
00365 ret = avfilter_graph_create_filter(&ist->input_video_filter, avfilter_get_by_name("buffer"),
00366 "src", args, NULL, graph);
00367 if (ret < 0)
00368 return ret;
00369 ret = avfilter_graph_create_filter(&ist->output_video_filter, &ffsink,
00370 "out", NULL, &ffsink_ctx, graph);
00371 if (ret < 0)
00372 return ret;
00373 last_filter = ist->input_video_filter;
00374
00375 if (codec->width != icodec->width || codec->height != icodec->height) {
00376 snprintf(args, 255, "%d:%d:flags=0x%X",
00377 codec->width,
00378 codec->height,
00379 (int)av_get_int(sws_opts, "sws_flags", NULL));
00380 if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
00381 NULL, args, NULL, graph)) < 0)
00382 return ret;
00383 if ((ret = avfilter_link(last_filter, 0, filter, 0)) < 0)
00384 return ret;
00385 last_filter = filter;
00386 }
00387
00388 snprintf(args, sizeof(args), "flags=0x%X", (int)av_get_int(sws_opts, "sws_flags", NULL));
00389 graph->scale_sws_opts = av_strdup(args);
00390
00391 if (vfilters) {
00392 AVFilterInOut *outputs = av_malloc(sizeof(AVFilterInOut));
00393 AVFilterInOut *inputs = av_malloc(sizeof(AVFilterInOut));
00394
00395 outputs->name = av_strdup("in");
00396 outputs->filter_ctx = last_filter;
00397 outputs->pad_idx = 0;
00398 outputs->next = NULL;
00399
00400 inputs->name = av_strdup("out");
00401 inputs->filter_ctx = ist->output_video_filter;
00402 inputs->pad_idx = 0;
00403 inputs->next = NULL;
00404
00405 if ((ret = avfilter_graph_parse(graph, vfilters, inputs, outputs, NULL)) < 0)
00406 return ret;
00407 av_freep(&vfilters);
00408 } else {
00409 if ((ret = avfilter_link(last_filter, 0, ist->output_video_filter, 0)) < 0)
00410 return ret;
00411 }
00412
00413 if ((ret = avfilter_graph_config(graph, NULL)) < 0)
00414 return ret;
00415
00416 codec->width = ist->output_video_filter->inputs[0]->w;
00417 codec->height = ist->output_video_filter->inputs[0]->h;
00418
00419 return 0;
00420 }
00421 #endif
00422
00423 static void term_exit(void)
00424 {
00425 av_log(NULL, AV_LOG_QUIET, "");
00426 #if HAVE_TERMIOS_H
00427 tcsetattr (0, TCSANOW, &oldtty);
00428 #endif
00429 }
00430
00431 static volatile int received_sigterm = 0;
00432
00433 static void
00434 sigterm_handler(int sig)
00435 {
00436 received_sigterm = sig;
00437 term_exit();
00438 }
00439
00440 static void term_init(void)
00441 {
00442 #if HAVE_TERMIOS_H
00443 struct termios tty;
00444
00445 tcgetattr (0, &tty);
00446 oldtty = tty;
00447 atexit(term_exit);
00448
00449 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
00450 |INLCR|IGNCR|ICRNL|IXON);
00451 tty.c_oflag |= OPOST;
00452 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
00453 tty.c_cflag &= ~(CSIZE|PARENB);
00454 tty.c_cflag |= CS8;
00455 tty.c_cc[VMIN] = 1;
00456 tty.c_cc[VTIME] = 0;
00457
00458 tcsetattr (0, TCSANOW, &tty);
00459 signal(SIGQUIT, sigterm_handler);
00460 #endif
00461
00462 signal(SIGINT , sigterm_handler);
00463 signal(SIGTERM, sigterm_handler);
00464 #ifdef SIGXCPU
00465 signal(SIGXCPU, sigterm_handler);
00466 #endif
00467 }
00468
00469
00470 static int read_key(void)
00471 {
00472 #if HAVE_TERMIOS_H
00473 int n = 1;
00474 unsigned char ch;
00475 struct timeval tv;
00476 fd_set rfds;
00477
00478 FD_ZERO(&rfds);
00479 FD_SET(0, &rfds);
00480 tv.tv_sec = 0;
00481 tv.tv_usec = 0;
00482 n = select(1, &rfds, NULL, NULL, &tv);
00483 if (n > 0) {
00484 n = read(0, &ch, 1);
00485 if (n == 1)
00486 return ch;
00487
00488 return n;
00489 }
00490 #elif HAVE_CONIO_H
00491 if(kbhit())
00492 return(getch());
00493 #endif
00494 return -1;
00495 }
00496
00497 static int decode_interrupt_cb(void)
00498 {
00499 return q_pressed || (q_pressed = read_key() == 'q');
00500 }
00501
00502 static int ffmpeg_exit(int ret)
00503 {
00504 int i;
00505
00506
00507 for(i=0;i<nb_output_files;i++) {
00508
00509 AVFormatContext *s = output_files[i];
00510 int j;
00511 if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
00512 url_fclose(s->pb);
00513 for(j=0;j<s->nb_streams;j++) {
00514 av_metadata_free(&s->streams[j]->metadata);
00515 av_free(s->streams[j]->codec);
00516 av_free(s->streams[j]->info);
00517 av_free(s->streams[j]);
00518 }
00519 for(j=0;j<s->nb_programs;j++) {
00520 av_metadata_free(&s->programs[j]->metadata);
00521 }
00522 for(j=0;j<s->nb_chapters;j++) {
00523 av_metadata_free(&s->chapters[j]->metadata);
00524 }
00525 av_metadata_free(&s->metadata);
00526 av_free(s);
00527 av_free(output_streams_for_file[i]);
00528 }
00529 for(i=0;i<nb_input_files;i++) {
00530 av_close_input_file(input_files[i]);
00531 av_free(input_files_ts_scale[i]);
00532 }
00533
00534 av_free(intra_matrix);
00535 av_free(inter_matrix);
00536
00537 if (vstats_file)
00538 fclose(vstats_file);
00539 av_free(vstats_filename);
00540
00541 av_free(opt_names);
00542 av_free(streamid_map);
00543 av_free(input_codecs);
00544 av_free(output_codecs);
00545 av_free(stream_maps);
00546 av_free(meta_data_maps);
00547
00548 av_free(video_codec_name);
00549 av_free(audio_codec_name);
00550 av_free(subtitle_codec_name);
00551
00552 av_free(video_standard);
00553
00554 uninit_opts();
00555 av_free(audio_buf);
00556 av_free(audio_out);
00557 allocated_audio_buf_size= allocated_audio_out_size= 0;
00558 av_free(samples);
00559
00560 #if CONFIG_AVFILTER
00561 avfilter_uninit();
00562 #endif
00563
00564 if (received_sigterm) {
00565 fprintf(stderr,
00566 "Received signal %d: terminating.\n",
00567 (int) received_sigterm);
00568 exit (255);
00569 }
00570
00571 exit(ret);
00572 return ret;
00573 }
00574
00575
00576 static void *grow_array(void *array, int elem_size, int *size, int new_size)
00577 {
00578 if (new_size >= INT_MAX / elem_size) {
00579 fprintf(stderr, "Array too big.\n");
00580 ffmpeg_exit(1);
00581 }
00582 if (*size < new_size) {
00583 uint8_t *tmp = av_realloc(array, new_size*elem_size);
00584 if (!tmp) {
00585 fprintf(stderr, "Could not alloc buffer.\n");
00586 ffmpeg_exit(1);
00587 }
00588 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
00589 *size = new_size;
00590 return tmp;
00591 }
00592 return array;
00593 }
00594
00595 static void choose_sample_fmt(AVStream *st, AVCodec *codec)
00596 {
00597 if(codec && codec->sample_fmts){
00598 const enum AVSampleFormat *p= codec->sample_fmts;
00599 for(; *p!=-1; p++){
00600 if(*p == st->codec->sample_fmt)
00601 break;
00602 }
00603 if(*p == -1)
00604 st->codec->sample_fmt = codec->sample_fmts[0];
00605 }
00606 }
00607
00608 static void choose_sample_rate(AVStream *st, AVCodec *codec)
00609 {
00610 if(codec && codec->supported_samplerates){
00611 const int *p= codec->supported_samplerates;
00612 int best=0;
00613 int best_dist=INT_MAX;
00614 for(; *p; p++){
00615 int dist= abs(st->codec->sample_rate - *p);
00616 if(dist < best_dist){
00617 best_dist= dist;
00618 best= *p;
00619 }
00620 }
00621 if(best_dist){
00622 av_log(st->codec, AV_LOG_WARNING, "Requested sampling rate unsupported using closest supported (%d)\n", best);
00623 }
00624 st->codec->sample_rate= best;
00625 }
00626 }
00627
00628 static void choose_pixel_fmt(AVStream *st, AVCodec *codec)
00629 {
00630 if(codec && codec->pix_fmts){
00631 const enum PixelFormat *p= codec->pix_fmts;
00632 if(st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL){
00633 if(st->codec->codec_id==CODEC_ID_MJPEG){
00634 p= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE};
00635 }else if(st->codec->codec_id==CODEC_ID_LJPEG){
00636 p= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_BGRA, PIX_FMT_NONE};
00637 }
00638 }
00639 for(; *p!=-1; p++){
00640 if(*p == st->codec->pix_fmt)
00641 break;
00642 }
00643 if(*p == -1)
00644 st->codec->pix_fmt = codec->pix_fmts[0];
00645 }
00646 }
00647
00648 static AVOutputStream *new_output_stream(AVFormatContext *oc, int file_idx)
00649 {
00650 int idx = oc->nb_streams - 1;
00651 AVOutputStream *ost;
00652
00653 output_streams_for_file[file_idx] =
00654 grow_array(output_streams_for_file[file_idx],
00655 sizeof(*output_streams_for_file[file_idx]),
00656 &nb_output_streams_for_file[file_idx],
00657 oc->nb_streams);
00658 ost = output_streams_for_file[file_idx][idx] =
00659 av_mallocz(sizeof(AVOutputStream));
00660 if (!ost) {
00661 fprintf(stderr, "Could not alloc output stream\n");
00662 ffmpeg_exit(1);
00663 }
00664 ost->file_index = file_idx;
00665 ost->index = idx;
00666 return ost;
00667 }
00668
00669 static int read_ffserver_streams(AVFormatContext *s, const char *filename)
00670 {
00671 int i, err;
00672 AVFormatContext *ic;
00673 int nopts = 0;
00674
00675 err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
00676 if (err < 0)
00677 return err;
00678
00679 s->nb_streams = 0;
00680 for(i=0;i<ic->nb_streams;i++) {
00681 AVStream *st;
00682 AVCodec *codec;
00683
00684 s->nb_streams++;
00685
00686
00687 st = av_mallocz(sizeof(AVStream));
00688 memcpy(st, ic->streams[i], sizeof(AVStream));
00689 st->codec = avcodec_alloc_context();
00690 if (!st->codec) {
00691 print_error(filename, AVERROR(ENOMEM));
00692 ffmpeg_exit(1);
00693 }
00694 avcodec_copy_context(st->codec, ic->streams[i]->codec);
00695 s->streams[i] = st;
00696
00697 codec = avcodec_find_encoder(st->codec->codec_id);
00698 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00699 if (audio_stream_copy) {
00700 st->stream_copy = 1;
00701 } else
00702 choose_sample_fmt(st, codec);
00703 } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00704 if (video_stream_copy) {
00705 st->stream_copy = 1;
00706 } else
00707 choose_pixel_fmt(st, codec);
00708 }
00709
00710 if(!st->codec->thread_count)
00711 st->codec->thread_count = 1;
00712 if(st->codec->thread_count>1)
00713 avcodec_thread_init(st->codec, st->codec->thread_count);
00714
00715 if(st->codec->flags & CODEC_FLAG_BITEXACT)
00716 nopts = 1;
00717
00718 new_output_stream(s, nb_output_files);
00719 }
00720
00721 if (!nopts)
00722 s->timestamp = av_gettime();
00723
00724 av_close_input_file(ic);
00725 return 0;
00726 }
00727
00728 static double
00729 get_sync_ipts(const AVOutputStream *ost)
00730 {
00731 const AVInputStream *ist = ost->sync_ist;
00732 return (double)(ist->pts - start_time)/AV_TIME_BASE;
00733 }
00734
00735 static void write_frame(AVFormatContext *s, AVPacket *pkt, AVCodecContext *avctx, AVBitStreamFilterContext *bsfc){
00736 int ret;
00737
00738 while(bsfc){
00739 AVPacket new_pkt= *pkt;
00740 int a= av_bitstream_filter_filter(bsfc, avctx, NULL,
00741 &new_pkt.data, &new_pkt.size,
00742 pkt->data, pkt->size,
00743 pkt->flags & AV_PKT_FLAG_KEY);
00744 if(a>0){
00745 av_free_packet(pkt);
00746 new_pkt.destruct= av_destruct_packet;
00747 } else if(a<0){
00748 fprintf(stderr, "%s failed for stream %d, codec %s",
00749 bsfc->filter->name, pkt->stream_index,
00750 avctx->codec ? avctx->codec->name : "copy");
00751 print_error("", a);
00752 if (exit_on_error)
00753 ffmpeg_exit(1);
00754 }
00755 *pkt= new_pkt;
00756
00757 bsfc= bsfc->next;
00758 }
00759
00760 ret= av_interleaved_write_frame(s, pkt);
00761 if(ret < 0){
00762 print_error("av_interleaved_write_frame()", ret);
00763 ffmpeg_exit(1);
00764 }
00765 }
00766
00767 #define MAX_AUDIO_PACKET_SIZE (128 * 1024)
00768
00769 static void do_audio_out(AVFormatContext *s,
00770 AVOutputStream *ost,
00771 AVInputStream *ist,
00772 unsigned char *buf, int size)
00773 {
00774 uint8_t *buftmp;
00775 int64_t audio_out_size, audio_buf_size;
00776 int64_t allocated_for_size= size;
00777
00778 int size_out, frame_bytes, ret, resample_changed;
00779 AVCodecContext *enc= ost->st->codec;
00780 AVCodecContext *dec= ist->st->codec;
00781 int osize= av_get_bits_per_sample_fmt(enc->sample_fmt)/8;
00782 int isize= av_get_bits_per_sample_fmt(dec->sample_fmt)/8;
00783 const int coded_bps = av_get_bits_per_sample(enc->codec->id);
00784
00785 need_realloc:
00786 audio_buf_size= (allocated_for_size + isize*dec->channels - 1) / (isize*dec->channels);
00787 audio_buf_size= (audio_buf_size*enc->sample_rate + dec->sample_rate) / dec->sample_rate;
00788 audio_buf_size= audio_buf_size*2 + 10000;
00789 audio_buf_size= FFMAX(audio_buf_size, enc->frame_size);
00790 audio_buf_size*= osize*enc->channels;
00791
00792 audio_out_size= FFMAX(audio_buf_size, enc->frame_size * osize * enc->channels);
00793 if(coded_bps > 8*osize)
00794 audio_out_size= audio_out_size * coded_bps / (8*osize);
00795 audio_out_size += FF_MIN_BUFFER_SIZE;
00796
00797 if(audio_out_size > INT_MAX || audio_buf_size > INT_MAX){
00798 fprintf(stderr, "Buffer sizes too large\n");
00799 ffmpeg_exit(1);
00800 }
00801
00802 av_fast_malloc(&audio_buf, &allocated_audio_buf_size, audio_buf_size);
00803 av_fast_malloc(&audio_out, &allocated_audio_out_size, audio_out_size);
00804 if (!audio_buf || !audio_out){
00805 fprintf(stderr, "Out of memory in do_audio_out\n");
00806 ffmpeg_exit(1);
00807 }
00808
00809 if (enc->channels != dec->channels)
00810 ost->audio_resample = 1;
00811
00812 resample_changed = ost->resample_sample_fmt != dec->sample_fmt ||
00813 ost->resample_channels != dec->channels ||
00814 ost->resample_sample_rate != dec->sample_rate;
00815
00816 if ((ost->audio_resample && !ost->resample) || resample_changed) {
00817 if (resample_changed) {
00818 av_log(NULL, AV_LOG_INFO, "Input stream #%d.%d frame changed from rate:%d fmt:%s ch:%d to rate:%d fmt:%s ch:%d\n",
00819 ist->file_index, ist->index,
00820 ost->resample_sample_rate, av_get_sample_fmt_name(ost->resample_sample_fmt), ost->resample_channels,
00821 dec->sample_rate, av_get_sample_fmt_name(dec->sample_fmt), dec->channels);
00822 ost->resample_sample_fmt = dec->sample_fmt;
00823 ost->resample_channels = dec->channels;
00824 ost->resample_sample_rate = dec->sample_rate;
00825 if (ost->resample)
00826 audio_resample_close(ost->resample);
00827 }
00828 if (ost->resample_sample_fmt == enc->sample_fmt &&
00829 ost->resample_channels == enc->channels &&
00830 ost->resample_sample_rate == enc->sample_rate) {
00831 ost->resample = NULL;
00832 ost->audio_resample = 0;
00833 } else {
00834 if (dec->sample_fmt != AV_SAMPLE_FMT_S16)
00835 fprintf(stderr, "Warning, using s16 intermediate sample format for resampling\n");
00836 ost->resample = av_audio_resample_init(enc->channels, dec->channels,
00837 enc->sample_rate, dec->sample_rate,
00838 enc->sample_fmt, dec->sample_fmt,
00839 16, 10, 0, 0.8);
00840 if (!ost->resample) {
00841 fprintf(stderr, "Can not resample %d channels @ %d Hz to %d channels @ %d Hz\n",
00842 dec->channels, dec->sample_rate,
00843 enc->channels, enc->sample_rate);
00844 ffmpeg_exit(1);
00845 }
00846 }
00847 }
00848
00849 #define MAKE_SFMT_PAIR(a,b) ((a)+AV_SAMPLE_FMT_NB*(b))
00850 if (!ost->audio_resample && dec->sample_fmt!=enc->sample_fmt &&
00851 MAKE_SFMT_PAIR(enc->sample_fmt,dec->sample_fmt)!=ost->reformat_pair) {
00852 if (ost->reformat_ctx)
00853 av_audio_convert_free(ost->reformat_ctx);
00854 ost->reformat_ctx = av_audio_convert_alloc(enc->sample_fmt, 1,
00855 dec->sample_fmt, 1, NULL, 0);
00856 if (!ost->reformat_ctx) {
00857 fprintf(stderr, "Cannot convert %s sample format to %s sample format\n",
00858 av_get_sample_fmt_name(dec->sample_fmt),
00859 av_get_sample_fmt_name(enc->sample_fmt));
00860 ffmpeg_exit(1);
00861 }
00862 ost->reformat_pair=MAKE_SFMT_PAIR(enc->sample_fmt,dec->sample_fmt);
00863 }
00864
00865 if(audio_sync_method){
00866 double delta = get_sync_ipts(ost) * enc->sample_rate - ost->sync_opts
00867 - av_fifo_size(ost->fifo)/(enc->channels * 2);
00868 double idelta= delta*dec->sample_rate / enc->sample_rate;
00869 int byte_delta= ((int)idelta)*2*dec->channels;
00870
00871
00872 if(fabs(delta) > 50){
00873 if(ist->is_start || fabs(delta) > audio_drift_threshold*enc->sample_rate){
00874 if(byte_delta < 0){
00875 byte_delta= FFMAX(byte_delta, -size);
00876 size += byte_delta;
00877 buf -= byte_delta;
00878 if(verbose > 2)
00879 fprintf(stderr, "discarding %d audio samples\n", (int)-delta);
00880 if(!size)
00881 return;
00882 ist->is_start=0;
00883 }else{
00884 static uint8_t *input_tmp= NULL;
00885 input_tmp= av_realloc(input_tmp, byte_delta + size);
00886
00887 if(byte_delta > allocated_for_size - size){
00888 allocated_for_size= byte_delta + (int64_t)size;
00889 goto need_realloc;
00890 }
00891 ist->is_start=0;
00892
00893 memset(input_tmp, 0, byte_delta);
00894 memcpy(input_tmp + byte_delta, buf, size);
00895 buf= input_tmp;
00896 size += byte_delta;
00897 if(verbose > 2)
00898 fprintf(stderr, "adding %d audio samples of silence\n", (int)delta);
00899 }
00900 }else if(audio_sync_method>1){
00901 int comp= av_clip(delta, -audio_sync_method, audio_sync_method);
00902 av_assert0(ost->audio_resample);
00903 if(verbose > 2)
00904 fprintf(stderr, "compensating audio timestamp drift:%f compensation:%d in:%d\n", delta, comp, enc->sample_rate);
00905
00906 av_resample_compensate(*(struct AVResampleContext**)ost->resample, comp, enc->sample_rate);
00907 }
00908 }
00909 }else
00910 ost->sync_opts= lrintf(get_sync_ipts(ost) * enc->sample_rate)
00911 - av_fifo_size(ost->fifo)/(enc->channels * 2);
00912
00913 if (ost->audio_resample) {
00914 buftmp = audio_buf;
00915 size_out = audio_resample(ost->resample,
00916 (short *)buftmp, (short *)buf,
00917 size / (dec->channels * isize));
00918 size_out = size_out * enc->channels * osize;
00919 } else {
00920 buftmp = buf;
00921 size_out = size;
00922 }
00923
00924 if (!ost->audio_resample && dec->sample_fmt!=enc->sample_fmt) {
00925 const void *ibuf[6]= {buftmp};
00926 void *obuf[6]= {audio_buf};
00927 int istride[6]= {isize};
00928 int ostride[6]= {osize};
00929 int len= size_out/istride[0];
00930 if (av_audio_convert(ost->reformat_ctx, obuf, ostride, ibuf, istride, len)<0) {
00931 printf("av_audio_convert() failed\n");
00932 if (exit_on_error)
00933 ffmpeg_exit(1);
00934 return;
00935 }
00936 buftmp = audio_buf;
00937 size_out = len*osize;
00938 }
00939
00940
00941 if (enc->frame_size > 1) {
00942
00943 if (av_fifo_realloc2(ost->fifo, av_fifo_size(ost->fifo) + size_out) < 0) {
00944 fprintf(stderr, "av_fifo_realloc2() failed\n");
00945 ffmpeg_exit(1);
00946 }
00947 av_fifo_generic_write(ost->fifo, buftmp, size_out, NULL);
00948
00949 frame_bytes = enc->frame_size * osize * enc->channels;
00950
00951 while (av_fifo_size(ost->fifo) >= frame_bytes) {
00952 AVPacket pkt;
00953 av_init_packet(&pkt);
00954
00955 av_fifo_generic_read(ost->fifo, audio_buf, frame_bytes, NULL);
00956
00957
00958
00959 ret = avcodec_encode_audio(enc, audio_out, audio_out_size,
00960 (short *)audio_buf);
00961 if (ret < 0) {
00962 fprintf(stderr, "Audio encoding failed\n");
00963 ffmpeg_exit(1);
00964 }
00965 audio_size += ret;
00966 pkt.stream_index= ost->index;
00967 pkt.data= audio_out;
00968 pkt.size= ret;
00969 if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
00970 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
00971 pkt.flags |= AV_PKT_FLAG_KEY;
00972 write_frame(s, &pkt, enc, ost->bitstream_filters);
00973
00974 ost->sync_opts += enc->frame_size;
00975 }
00976 } else {
00977 AVPacket pkt;
00978 av_init_packet(&pkt);
00979
00980 ost->sync_opts += size_out / (osize * enc->channels);
00981
00982
00983
00984 size_out /= osize;
00985 if (coded_bps)
00986 size_out = size_out*coded_bps/8;
00987
00988 if(size_out > audio_out_size){
00989 fprintf(stderr, "Internal error, buffer size too small\n");
00990 ffmpeg_exit(1);
00991 }
00992
00993
00994 ret = avcodec_encode_audio(enc, audio_out, size_out,
00995 (short *)buftmp);
00996 if (ret < 0) {
00997 fprintf(stderr, "Audio encoding failed\n");
00998 ffmpeg_exit(1);
00999 }
01000 audio_size += ret;
01001 pkt.stream_index= ost->index;
01002 pkt.data= audio_out;
01003 pkt.size= ret;
01004 if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
01005 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
01006 pkt.flags |= AV_PKT_FLAG_KEY;
01007 write_frame(s, &pkt, enc, ost->bitstream_filters);
01008 }
01009 }
01010
01011 static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp)
01012 {
01013 AVCodecContext *dec;
01014 AVPicture *picture2;
01015 AVPicture picture_tmp;
01016 uint8_t *buf = 0;
01017
01018 dec = ist->st->codec;
01019
01020
01021 if (do_deinterlace) {
01022 int size;
01023
01024
01025 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
01026 buf = av_malloc(size);
01027 if (!buf)
01028 return;
01029
01030 picture2 = &picture_tmp;
01031 avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
01032
01033 if(avpicture_deinterlace(picture2, picture,
01034 dec->pix_fmt, dec->width, dec->height) < 0) {
01035
01036 fprintf(stderr, "Deinterlacing failed\n");
01037 av_free(buf);
01038 buf = NULL;
01039 picture2 = picture;
01040 }
01041 } else {
01042 picture2 = picture;
01043 }
01044
01045 if (picture != picture2)
01046 *picture = *picture2;
01047 *bufp = buf;
01048 }
01049
01050
01051 #define AV_DELAY_MAX 0.100
01052
01053 static void do_subtitle_out(AVFormatContext *s,
01054 AVOutputStream *ost,
01055 AVInputStream *ist,
01056 AVSubtitle *sub,
01057 int64_t pts)
01058 {
01059 static uint8_t *subtitle_out = NULL;
01060 int subtitle_out_max_size = 1024 * 1024;
01061 int subtitle_out_size, nb, i;
01062 AVCodecContext *enc;
01063 AVPacket pkt;
01064
01065 if (pts == AV_NOPTS_VALUE) {
01066 fprintf(stderr, "Subtitle packets must have a pts\n");
01067 if (exit_on_error)
01068 ffmpeg_exit(1);
01069 return;
01070 }
01071
01072 enc = ost->st->codec;
01073
01074 if (!subtitle_out) {
01075 subtitle_out = av_malloc(subtitle_out_max_size);
01076 }
01077
01078
01079
01080
01081 if (enc->codec_id == CODEC_ID_DVB_SUBTITLE)
01082 nb = 2;
01083 else
01084 nb = 1;
01085
01086 for(i = 0; i < nb; i++) {
01087 sub->pts = av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q);
01088
01089 sub->pts += av_rescale_q(sub->start_display_time, (AVRational){1, 1000}, AV_TIME_BASE_Q);
01090 sub->end_display_time -= sub->start_display_time;
01091 sub->start_display_time = 0;
01092 subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
01093 subtitle_out_max_size, sub);
01094 if (subtitle_out_size < 0) {
01095 fprintf(stderr, "Subtitle encoding failed\n");
01096 ffmpeg_exit(1);
01097 }
01098
01099 av_init_packet(&pkt);
01100 pkt.stream_index = ost->index;
01101 pkt.data = subtitle_out;
01102 pkt.size = subtitle_out_size;
01103 pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base);
01104 if (enc->codec_id == CODEC_ID_DVB_SUBTITLE) {
01105
01106
01107 if (i == 0)
01108 pkt.pts += 90 * sub->start_display_time;
01109 else
01110 pkt.pts += 90 * sub->end_display_time;
01111 }
01112 write_frame(s, &pkt, ost->st->codec, ost->bitstream_filters);
01113 }
01114 }
01115
01116 static int bit_buffer_size= 1024*256;
01117 static uint8_t *bit_buffer= NULL;
01118
01119 static void do_video_out(AVFormatContext *s,
01120 AVOutputStream *ost,
01121 AVInputStream *ist,
01122 AVFrame *in_picture,
01123 int *frame_size)
01124 {
01125 int nb_frames, i, ret;
01126 AVFrame *final_picture, *formatted_picture, *resampling_dst, *padding_src;
01127 AVCodecContext *enc, *dec;
01128 double sync_ipts;
01129
01130 enc = ost->st->codec;
01131 dec = ist->st->codec;
01132
01133 sync_ipts = get_sync_ipts(ost) / av_q2d(enc->time_base);
01134
01135
01136 nb_frames = 1;
01137
01138 *frame_size = 0;
01139
01140 if(video_sync_method){
01141 double vdelta = sync_ipts - ost->sync_opts;
01142
01143 if (vdelta < -1.1)
01144 nb_frames = 0;
01145 else if (video_sync_method == 2 || (video_sync_method<0 && (s->oformat->flags & AVFMT_VARIABLE_FPS))){
01146 if(vdelta<=-0.6){
01147 nb_frames=0;
01148 }else if(vdelta>0.6)
01149 ost->sync_opts= lrintf(sync_ipts);
01150 }else if (vdelta > 1.1)
01151 nb_frames = lrintf(vdelta);
01152
01153 if (nb_frames == 0){
01154 ++nb_frames_drop;
01155 if (verbose>2)
01156 fprintf(stderr, "*** drop!\n");
01157 }else if (nb_frames > 1) {
01158 nb_frames_dup += nb_frames - 1;
01159 if (verbose>2)
01160 fprintf(stderr, "*** %d dup!\n", nb_frames-1);
01161 }
01162 }else
01163 ost->sync_opts= lrintf(sync_ipts);
01164
01165 nb_frames= FFMIN(nb_frames, max_frames[AVMEDIA_TYPE_VIDEO] - ost->frame_number);
01166 if (nb_frames <= 0)
01167 return;
01168
01169 formatted_picture = in_picture;
01170 final_picture = formatted_picture;
01171 padding_src = formatted_picture;
01172 resampling_dst = &ost->pict_tmp;
01173
01174 if ( ost->resample_height != ist->st->codec->height
01175 || ost->resample_width != ist->st->codec->width
01176 || (ost->resample_pix_fmt!= ist->st->codec->pix_fmt) ) {
01177
01178 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));
01179 if(!ost->video_resample)
01180 ffmpeg_exit(1);
01181 }
01182
01183 #if !CONFIG_AVFILTER
01184 if (ost->video_resample) {
01185 padding_src = NULL;
01186 final_picture = &ost->pict_tmp;
01187 if( ost->resample_height != ist->st->codec->height
01188 || ost->resample_width != ist->st->codec->width
01189 || (ost->resample_pix_fmt!= ist->st->codec->pix_fmt) ) {
01190
01191
01192 sws_freeContext(ost->img_resample_ctx);
01193 sws_flags = av_get_int(sws_opts, "sws_flags", NULL);
01194 ost->img_resample_ctx = sws_getContext(
01195 ist->st->codec->width,
01196 ist->st->codec->height,
01197 ist->st->codec->pix_fmt,
01198 ost->st->codec->width,
01199 ost->st->codec->height,
01200 ost->st->codec->pix_fmt,
01201 sws_flags, NULL, NULL, NULL);
01202 if (ost->img_resample_ctx == NULL) {
01203 fprintf(stderr, "Cannot get resampling context\n");
01204 ffmpeg_exit(1);
01205 }
01206 }
01207 sws_scale(ost->img_resample_ctx, formatted_picture->data, formatted_picture->linesize,
01208 0, ost->resample_height, resampling_dst->data, resampling_dst->linesize);
01209 }
01210 #endif
01211
01212
01213 for(i=0;i<nb_frames;i++) {
01214 AVPacket pkt;
01215 av_init_packet(&pkt);
01216 pkt.stream_index= ost->index;
01217
01218 if (s->oformat->flags & AVFMT_RAWPICTURE) {
01219
01220
01221
01222 AVFrame* old_frame = enc->coded_frame;
01223 enc->coded_frame = dec->coded_frame;
01224 pkt.data= (uint8_t *)final_picture;
01225 pkt.size= sizeof(AVPicture);
01226 pkt.pts= av_rescale_q(ost->sync_opts, enc->time_base, ost->st->time_base);
01227 pkt.flags |= AV_PKT_FLAG_KEY;
01228
01229 write_frame(s, &pkt, ost->st->codec, ost->bitstream_filters);
01230 enc->coded_frame = old_frame;
01231 } else {
01232 AVFrame big_picture;
01233
01234 big_picture= *final_picture;
01235
01236
01237 big_picture.interlaced_frame = in_picture->interlaced_frame;
01238 if(avcodec_opts[AVMEDIA_TYPE_VIDEO]->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)){
01239 if(top_field_first == -1)
01240 big_picture.top_field_first = in_picture->top_field_first;
01241 else
01242 big_picture.top_field_first = top_field_first;
01243 }
01244
01245
01246
01247 big_picture.quality = same_quality ? ist->st->quality : ost->st->quality;
01248 if(!me_threshold)
01249 big_picture.pict_type = 0;
01250
01251 big_picture.pts= ost->sync_opts;
01252
01253
01254 if (ost->forced_kf_index < ost->forced_kf_count &&
01255 big_picture.pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
01256 big_picture.pict_type = FF_I_TYPE;
01257 ost->forced_kf_index++;
01258 }
01259 ret = avcodec_encode_video(enc,
01260 bit_buffer, bit_buffer_size,
01261 &big_picture);
01262 if (ret < 0) {
01263 fprintf(stderr, "Video encoding failed\n");
01264 ffmpeg_exit(1);
01265 }
01266
01267 if(ret>0){
01268 pkt.data= bit_buffer;
01269 pkt.size= ret;
01270 if(enc->coded_frame->pts != AV_NOPTS_VALUE)
01271 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
01272
01273
01274
01275
01276 if(enc->coded_frame->key_frame)
01277 pkt.flags |= AV_PKT_FLAG_KEY;
01278 write_frame(s, &pkt, ost->st->codec, ost->bitstream_filters);
01279 *frame_size = ret;
01280 video_size += ret;
01281
01282
01283
01284 if (ost->logfile && enc->stats_out) {
01285 fprintf(ost->logfile, "%s", enc->stats_out);
01286 }
01287 }
01288 }
01289 ost->sync_opts++;
01290 ost->frame_number++;
01291 }
01292 }
01293
01294 static double psnr(double d){
01295 return -10.0*log(d)/log(10.0);
01296 }
01297
01298 static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
01299 int frame_size)
01300 {
01301 AVCodecContext *enc;
01302 int frame_number;
01303 double ti1, bitrate, avg_bitrate;
01304
01305
01306 if (!vstats_file) {
01307 vstats_file = fopen(vstats_filename, "w");
01308 if (!vstats_file) {
01309 perror("fopen");
01310 ffmpeg_exit(1);
01311 }
01312 }
01313
01314 enc = ost->st->codec;
01315 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
01316 frame_number = ost->frame_number;
01317 fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);
01318 if (enc->flags&CODEC_FLAG_PSNR)
01319 fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
01320
01321 fprintf(vstats_file,"f_size= %6d ", frame_size);
01322
01323 ti1 = ost->sync_opts * av_q2d(enc->time_base);
01324 if (ti1 < 0.01)
01325 ti1 = 0.01;
01326
01327 bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
01328 avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
01329 fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
01330 (double)video_size / 1024, ti1, bitrate, avg_bitrate);
01331 fprintf(vstats_file,"type= %c\n", av_get_pict_type_char(enc->coded_frame->pict_type));
01332 }
01333 }
01334
01335 static void print_report(AVFormatContext **output_files,
01336 AVOutputStream **ost_table, int nb_ostreams,
01337 int is_last_report)
01338 {
01339 char buf[1024];
01340 AVOutputStream *ost;
01341 AVFormatContext *oc;
01342 int64_t total_size;
01343 AVCodecContext *enc;
01344 int frame_number, vid, i;
01345 double bitrate, ti1, pts;
01346 static int64_t last_time = -1;
01347 static int qp_histogram[52];
01348
01349 if (!is_last_report) {
01350 int64_t cur_time;
01351
01352 cur_time = av_gettime();
01353 if (last_time == -1) {
01354 last_time = cur_time;
01355 return;
01356 }
01357 if ((cur_time - last_time) < 500000)
01358 return;
01359 last_time = cur_time;
01360 }
01361
01362
01363 oc = output_files[0];
01364
01365 total_size = url_fsize(oc->pb);
01366 if(total_size<0)
01367 total_size= url_ftell(oc->pb);
01368
01369 buf[0] = '\0';
01370 ti1 = 1e10;
01371 vid = 0;
01372 for(i=0;i<nb_ostreams;i++) {
01373 ost = ost_table[i];
01374 enc = ost->st->codec;
01375 if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
01376 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ",
01377 !ost->st->stream_copy ?
01378 enc->coded_frame->quality/(float)FF_QP2LAMBDA : -1);
01379 }
01380 if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
01381 float t = (av_gettime()-timer_start) / 1000000.0;
01382
01383 frame_number = ost->frame_number;
01384 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3d q=%3.1f ",
01385 frame_number, (t>1)?(int)(frame_number/t+0.5) : 0,
01386 !ost->st->stream_copy ?
01387 enc->coded_frame->quality/(float)FF_QP2LAMBDA : -1);
01388 if(is_last_report)
01389 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
01390 if(qp_hist){
01391 int j;
01392 int qp= lrintf(enc->coded_frame->quality/(float)FF_QP2LAMBDA);
01393 if(qp>=0 && qp<FF_ARRAY_ELEMS(qp_histogram))
01394 qp_histogram[qp]++;
01395 for(j=0; j<32; j++)
01396 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log(qp_histogram[j]+1)/log(2)));
01397 }
01398 if (enc->flags&CODEC_FLAG_PSNR){
01399 int j;
01400 double error, error_sum=0;
01401 double scale, scale_sum=0;
01402 char type[3]= {'Y','U','V'};
01403 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
01404 for(j=0; j<3; j++){
01405 if(is_last_report){
01406 error= enc->error[j];
01407 scale= enc->width*enc->height*255.0*255.0*frame_number;
01408 }else{
01409 error= enc->coded_frame->error[j];
01410 scale= enc->width*enc->height*255.0*255.0;
01411 }
01412 if(j) scale/=4;
01413 error_sum += error;
01414 scale_sum += scale;
01415 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error/scale));
01416 }
01417 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum/scale_sum));
01418 }
01419 vid = 1;
01420 }
01421
01422 pts = (double)ost->st->pts.val * av_q2d(ost->st->time_base);
01423 if ((pts < ti1) && (pts > 0))
01424 ti1 = pts;
01425 }
01426 if (ti1 < 0.01)
01427 ti1 = 0.01;
01428
01429 if (verbose || is_last_report) {
01430 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
01431
01432 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
01433 "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
01434 (double)total_size / 1024, ti1, bitrate);
01435
01436 if (nb_frames_dup || nb_frames_drop)
01437 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
01438 nb_frames_dup, nb_frames_drop);
01439
01440 if (verbose >= 0)
01441 fprintf(stderr, "%s \r", buf);
01442
01443 fflush(stderr);
01444 }
01445
01446 if (is_last_report && verbose >= 0){
01447 int64_t raw= audio_size + video_size + extra_size;
01448 fprintf(stderr, "\n");
01449 fprintf(stderr, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
01450 video_size/1024.0,
01451 audio_size/1024.0,
01452 extra_size/1024.0,
01453 100.0*(total_size - raw)/raw
01454 );
01455 }
01456 }
01457
01458
01459 static int output_packet(AVInputStream *ist, int ist_index,
01460 AVOutputStream **ost_table, int nb_ostreams,
01461 const AVPacket *pkt)
01462 {
01463 AVFormatContext *os;
01464 AVOutputStream *ost;
01465 int ret, i;
01466 int got_picture;
01467 AVFrame picture;
01468 void *buffer_to_free;
01469 static unsigned int samples_size= 0;
01470 AVSubtitle subtitle, *subtitle_to_free;
01471 int64_t pkt_pts = AV_NOPTS_VALUE;
01472 #if CONFIG_AVFILTER
01473 int frame_available;
01474 #endif
01475
01476 AVPacket avpkt;
01477 int bps = av_get_bits_per_sample_fmt(ist->st->codec->sample_fmt)>>3;
01478
01479 if(ist->next_pts == AV_NOPTS_VALUE)
01480 ist->next_pts= ist->pts;
01481
01482 if (pkt == NULL) {
01483
01484 av_init_packet(&avpkt);
01485 avpkt.data = NULL;
01486 avpkt.size = 0;
01487 goto handle_eof;
01488 } else {
01489 avpkt = *pkt;
01490 }
01491
01492 if(pkt->dts != AV_NOPTS_VALUE)
01493 ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
01494 if(pkt->pts != AV_NOPTS_VALUE)
01495 pkt_pts = av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
01496
01497
01498 while (avpkt.size > 0 || (!pkt && ist->next_pts != ist->pts)) {
01499 uint8_t *data_buf, *decoded_data_buf;
01500 int data_size, decoded_data_size;
01501 handle_eof:
01502 ist->pts= ist->next_pts;
01503
01504 if(avpkt.size && avpkt.size != pkt->size &&
01505 ((!ist->showed_multi_packet_warning && verbose>0) || verbose>1)){
01506 fprintf(stderr, "Multiple frames in a packet from stream %d\n", pkt->stream_index);
01507 ist->showed_multi_packet_warning=1;
01508 }
01509
01510
01511 decoded_data_buf = NULL;
01512 decoded_data_size= 0;
01513 data_buf = avpkt.data;
01514 data_size = avpkt.size;
01515 subtitle_to_free = NULL;
01516 if (ist->decoding_needed) {
01517 switch(ist->st->codec->codec_type) {
01518 case AVMEDIA_TYPE_AUDIO:{
01519 if(pkt && samples_size < FFMAX(pkt->size*sizeof(*samples), AVCODEC_MAX_AUDIO_FRAME_SIZE)) {
01520 samples_size = FFMAX(pkt->size*sizeof(*samples), AVCODEC_MAX_AUDIO_FRAME_SIZE);
01521 av_free(samples);
01522 samples= av_malloc(samples_size);
01523 }
01524 decoded_data_size= samples_size;
01525
01526
01527 ret = avcodec_decode_audio3(ist->st->codec, samples, &decoded_data_size,
01528 &avpkt);
01529 if (ret < 0)
01530 goto fail_decode;
01531 avpkt.data += ret;
01532 avpkt.size -= ret;
01533 data_size = ret;
01534
01535
01536 if (decoded_data_size <= 0) {
01537
01538 continue;
01539 }
01540 decoded_data_buf = (uint8_t *)samples;
01541 ist->next_pts += ((int64_t)AV_TIME_BASE/bps * decoded_data_size) /
01542 (ist->st->codec->sample_rate * ist->st->codec->channels);
01543 break;}
01544 case AVMEDIA_TYPE_VIDEO:
01545 decoded_data_size = (ist->st->codec->width * ist->st->codec->height * 3) / 2;
01546
01547 avcodec_get_frame_defaults(&picture);
01548 ist->st->codec->reordered_opaque = pkt_pts;
01549 pkt_pts = AV_NOPTS_VALUE;
01550
01551 ret = avcodec_decode_video2(ist->st->codec,
01552 &picture, &got_picture, &avpkt);
01553 ist->st->quality= picture.quality;
01554 if (ret < 0)
01555 goto fail_decode;
01556 if (!got_picture) {
01557
01558 goto discard_packet;
01559 }
01560 ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, picture.reordered_opaque, ist->pts);
01561 if (ist->st->codec->time_base.num != 0) {
01562 int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
01563 ist->next_pts += ((int64_t)AV_TIME_BASE *
01564 ist->st->codec->time_base.num * ticks) /
01565 ist->st->codec->time_base.den;
01566 }
01567 avpkt.size = 0;
01568 break;
01569 case AVMEDIA_TYPE_SUBTITLE:
01570 ret = avcodec_decode_subtitle2(ist->st->codec,
01571 &subtitle, &got_picture, &avpkt);
01572 if (ret < 0)
01573 goto fail_decode;
01574 if (!got_picture) {
01575 goto discard_packet;
01576 }
01577 subtitle_to_free = &subtitle;
01578 avpkt.size = 0;
01579 break;
01580 default:
01581 goto fail_decode;
01582 }
01583 } else {
01584 switch(ist->st->codec->codec_type) {
01585 case AVMEDIA_TYPE_AUDIO:
01586 ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
01587 ist->st->codec->sample_rate;
01588 break;
01589 case AVMEDIA_TYPE_VIDEO:
01590 if (ist->st->codec->time_base.num != 0) {
01591 int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
01592 ist->next_pts += ((int64_t)AV_TIME_BASE *
01593 ist->st->codec->time_base.num * ticks) /
01594 ist->st->codec->time_base.den;
01595 }
01596 break;
01597 }
01598 ret = avpkt.size;
01599 avpkt.size = 0;
01600 }
01601
01602 buffer_to_free = NULL;
01603 if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01604 pre_process_video_frame(ist, (AVPicture *)&picture,
01605 &buffer_to_free);
01606 }
01607
01608 #if CONFIG_AVFILTER
01609 if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ist->input_video_filter) {
01610
01611 av_vsrc_buffer_add_frame(ist->input_video_filter, &picture,
01612 ist->pts,
01613 ist->st->codec->sample_aspect_ratio);
01614 }
01615 #endif
01616
01617
01618 if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
01619 if (audio_volume != 256) {
01620 short *volp;
01621 volp = samples;
01622 for(i=0;i<(decoded_data_size / sizeof(short));i++) {
01623 int v = ((*volp) * audio_volume + 128) >> 8;
01624 if (v < -32768) v = -32768;
01625 if (v > 32767) v = 32767;
01626 *volp++ = v;
01627 }
01628 }
01629 }
01630
01631
01632 if (rate_emu) {
01633 int64_t pts = av_rescale(ist->pts, 1000000, AV_TIME_BASE);
01634 int64_t now = av_gettime() - ist->start;
01635 if (pts > now)
01636 usleep(pts - now);
01637 }
01638 #if CONFIG_AVFILTER
01639 frame_available = ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
01640 !ist->output_video_filter || avfilter_poll_frame(ist->output_video_filter->inputs[0]);
01641 #endif
01642
01643
01644 if (start_time == 0 || ist->pts >= start_time)
01645 #if CONFIG_AVFILTER
01646 while (frame_available) {
01647 AVRational ist_pts_tb;
01648 if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ist->output_video_filter)
01649 get_filtered_video_frame(ist->output_video_filter, &picture, &ist->picref, &ist_pts_tb);
01650 if (ist->picref)
01651 ist->pts = av_rescale_q(ist->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
01652 #endif
01653 for(i=0;i<nb_ostreams;i++) {
01654 int frame_size;
01655
01656 ost = ost_table[i];
01657 if (ost->source_index == ist_index) {
01658 os = output_files[ost->file_index];
01659
01660
01661
01662
01663 if (ost->encoding_needed) {
01664 av_assert0(ist->decoding_needed);
01665 switch(ost->st->codec->codec_type) {
01666 case AVMEDIA_TYPE_AUDIO:
01667 do_audio_out(os, ost, ist, decoded_data_buf, decoded_data_size);
01668 break;
01669 case AVMEDIA_TYPE_VIDEO:
01670 #if CONFIG_AVFILTER
01671 if (ist->picref->video)
01672 ost->st->codec->sample_aspect_ratio = ist->picref->video->pixel_aspect;
01673 #endif
01674 do_video_out(os, ost, ist, &picture, &frame_size);
01675 if (vstats_filename && frame_size)
01676 do_video_stats(os, ost, frame_size);
01677 break;
01678 case AVMEDIA_TYPE_SUBTITLE:
01679 do_subtitle_out(os, ost, ist, &subtitle,
01680 pkt->pts);
01681 break;
01682 default:
01683 abort();
01684 }
01685 } else {
01686 AVFrame avframe;
01687 AVPacket opkt;
01688 int64_t ost_tb_start_time= av_rescale_q(start_time, AV_TIME_BASE_Q, ost->st->time_base);
01689
01690 av_init_packet(&opkt);
01691
01692 if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) && !copy_initial_nonkeyframes)
01693 continue;
01694
01695
01696
01697
01698 avcodec_get_frame_defaults(&avframe);
01699 ost->st->codec->coded_frame= &avframe;
01700 avframe.key_frame = pkt->flags & AV_PKT_FLAG_KEY;
01701
01702 if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
01703 audio_size += data_size;
01704 else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01705 video_size += data_size;
01706 ost->sync_opts++;
01707 }
01708
01709 opkt.stream_index= ost->index;
01710 if(pkt->pts != AV_NOPTS_VALUE)
01711 opkt.pts= av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;
01712 else
01713 opkt.pts= AV_NOPTS_VALUE;
01714
01715 if (pkt->dts == AV_NOPTS_VALUE)
01716 opkt.dts = av_rescale_q(ist->pts, AV_TIME_BASE_Q, ost->st->time_base);
01717 else
01718 opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);
01719 opkt.dts -= ost_tb_start_time;
01720
01721 opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base);
01722 opkt.flags= pkt->flags;
01723
01724
01725 if( ost->st->codec->codec_id != CODEC_ID_H264
01726 && ost->st->codec->codec_id != CODEC_ID_MPEG1VIDEO
01727 && ost->st->codec->codec_id != CODEC_ID_MPEG2VIDEO
01728 ) {
01729 if(av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, data_buf, data_size, pkt->flags & AV_PKT_FLAG_KEY))
01730 opkt.destruct= av_destruct_packet;
01731 } else {
01732 opkt.data = data_buf;
01733 opkt.size = data_size;
01734 }
01735
01736 write_frame(os, &opkt, ost->st->codec, ost->bitstream_filters);
01737 ost->st->codec->frame_number++;
01738 ost->frame_number++;
01739 av_free_packet(&opkt);
01740 }
01741 }
01742 }
01743
01744 #if CONFIG_AVFILTER
01745 frame_available = (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
01746 ist->output_video_filter && avfilter_poll_frame(ist->output_video_filter->inputs[0]);
01747 if(ist->picref)
01748 avfilter_unref_buffer(ist->picref);
01749 }
01750 #endif
01751 av_free(buffer_to_free);
01752
01753 if (subtitle_to_free) {
01754 avsubtitle_free(subtitle_to_free);
01755 subtitle_to_free = NULL;
01756 }
01757 }
01758 discard_packet:
01759 if (pkt == NULL) {
01760
01761
01762 for(i=0;i<nb_ostreams;i++) {
01763 ost = ost_table[i];
01764 if (ost->source_index == ist_index) {
01765 AVCodecContext *enc= ost->st->codec;
01766 os = output_files[ost->file_index];
01767
01768 if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <=1)
01769 continue;
01770 if(ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE))
01771 continue;
01772
01773 if (ost->encoding_needed) {
01774 for(;;) {
01775 AVPacket pkt;
01776 int fifo_bytes;
01777 av_init_packet(&pkt);
01778 pkt.stream_index= ost->index;
01779
01780 switch(ost->st->codec->codec_type) {
01781 case AVMEDIA_TYPE_AUDIO:
01782 fifo_bytes = av_fifo_size(ost->fifo);
01783 ret = 0;
01784
01785 if (fifo_bytes > 0) {
01786 int osize = av_get_bits_per_sample_fmt(enc->sample_fmt) >> 3;
01787 int fs_tmp = enc->frame_size;
01788
01789 av_fifo_generic_read(ost->fifo, audio_buf, fifo_bytes, NULL);
01790 if (enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
01791 enc->frame_size = fifo_bytes / (osize * enc->channels);
01792 } else {
01793 int frame_bytes = enc->frame_size*osize*enc->channels;
01794 if (allocated_audio_buf_size < frame_bytes)
01795 ffmpeg_exit(1);
01796 memset(audio_buf+fifo_bytes, 0, frame_bytes - fifo_bytes);
01797 }
01798
01799 ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, (short *)audio_buf);
01800 pkt.duration = av_rescale((int64_t)enc->frame_size*ost->st->time_base.den,
01801 ost->st->time_base.num, enc->sample_rate);
01802 enc->frame_size = fs_tmp;
01803 }
01804 if(ret <= 0) {
01805 ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL);
01806 }
01807 if (ret < 0) {
01808 fprintf(stderr, "Audio encoding failed\n");
01809 ffmpeg_exit(1);
01810 }
01811 audio_size += ret;
01812 pkt.flags |= AV_PKT_FLAG_KEY;
01813 break;
01814 case AVMEDIA_TYPE_VIDEO:
01815 ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL);
01816 if (ret < 0) {
01817 fprintf(stderr, "Video encoding failed\n");
01818 ffmpeg_exit(1);
01819 }
01820 video_size += ret;
01821 if(enc->coded_frame && enc->coded_frame->key_frame)
01822 pkt.flags |= AV_PKT_FLAG_KEY;
01823 if (ost->logfile && enc->stats_out) {
01824 fprintf(ost->logfile, "%s", enc->stats_out);
01825 }
01826 break;
01827 default:
01828 ret=-1;
01829 }
01830
01831 if(ret<=0)
01832 break;
01833 pkt.data= bit_buffer;
01834 pkt.size= ret;
01835 if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
01836 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
01837 write_frame(os, &pkt, ost->st->codec, ost->bitstream_filters);
01838 }
01839 }
01840 }
01841 }
01842 }
01843
01844 return 0;
01845 fail_decode:
01846 return -1;
01847 }
01848
01849 static void print_sdp(AVFormatContext **avc, int n)
01850 {
01851 char sdp[2048];
01852
01853 avf_sdp_create(avc, n, sdp, sizeof(sdp));
01854 printf("SDP:\n%s\n", sdp);
01855 fflush(stdout);
01856 }
01857
01858 static int copy_chapters(int infile, int outfile)
01859 {
01860 AVFormatContext *is = input_files[infile];
01861 AVFormatContext *os = output_files[outfile];
01862 int i;
01863
01864 for (i = 0; i < is->nb_chapters; i++) {
01865 AVChapter *in_ch = is->chapters[i], *out_ch;
01866 int64_t ts_off = av_rescale_q(start_time - input_files_ts_offset[infile],
01867 AV_TIME_BASE_Q, in_ch->time_base);
01868 int64_t rt = (recording_time == INT64_MAX) ? INT64_MAX :
01869 av_rescale_q(recording_time, AV_TIME_BASE_Q, in_ch->time_base);
01870
01871
01872 if (in_ch->end < ts_off)
01873 continue;
01874 if (rt != INT64_MAX && in_ch->start > rt + ts_off)
01875 break;
01876
01877 out_ch = av_mallocz(sizeof(AVChapter));
01878 if (!out_ch)
01879 return AVERROR(ENOMEM);
01880
01881 out_ch->id = in_ch->id;
01882 out_ch->time_base = in_ch->time_base;
01883 out_ch->start = FFMAX(0, in_ch->start - ts_off);
01884 out_ch->end = FFMIN(rt, in_ch->end - ts_off);
01885
01886 if (metadata_chapters_autocopy)
01887 av_metadata_copy(&out_ch->metadata, in_ch->metadata, 0);
01888
01889 os->nb_chapters++;
01890 os->chapters = av_realloc(os->chapters, sizeof(AVChapter)*os->nb_chapters);
01891 if (!os->chapters)
01892 return AVERROR(ENOMEM);
01893 os->chapters[os->nb_chapters - 1] = out_ch;
01894 }
01895 return 0;
01896 }
01897
01898 static void parse_forced_key_frames(char *kf, AVOutputStream *ost,
01899 AVCodecContext *avctx)
01900 {
01901 char *p;
01902 int n = 1, i;
01903 int64_t t;
01904
01905 for (p = kf; *p; p++)
01906 if (*p == ',')
01907 n++;
01908 ost->forced_kf_count = n;
01909 ost->forced_kf_pts = av_malloc(sizeof(*ost->forced_kf_pts) * n);
01910 if (!ost->forced_kf_pts) {
01911 av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
01912 ffmpeg_exit(1);
01913 }
01914 for (i = 0; i < n; i++) {
01915 p = i ? strchr(p, ',') + 1 : kf;
01916 t = parse_time_or_die("force_key_frames", p, 1);
01917 ost->forced_kf_pts[i] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
01918 }
01919 }
01920
01921
01922
01923
01924 static int transcode(AVFormatContext **output_files,
01925 int nb_output_files,
01926 AVFormatContext **input_files,
01927 int nb_input_files,
01928 AVStreamMap *stream_maps, int nb_stream_maps)
01929 {
01930 int ret = 0, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
01931 AVFormatContext *is, *os;
01932 AVCodecContext *codec, *icodec;
01933 AVOutputStream *ost, **ost_table = NULL;
01934 AVInputStream *ist, **ist_table = NULL;
01935 AVInputFile *file_table;
01936 char error[1024];
01937 int key;
01938 int want_sdp = 1;
01939 uint8_t no_packet[MAX_FILES]={0};
01940 int no_packet_count=0;
01941
01942 file_table= av_mallocz(nb_input_files * sizeof(AVInputFile));
01943 if (!file_table)
01944 goto fail;
01945
01946
01947 j = 0;
01948 for(i=0;i<nb_input_files;i++) {
01949 is = input_files[i];
01950 file_table[i].ist_index = j;
01951 file_table[i].nb_streams = is->nb_streams;
01952 j += is->nb_streams;
01953 }
01954 nb_istreams = j;
01955
01956 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
01957 if (!ist_table)
01958 goto fail;
01959
01960 for(i=0;i<nb_istreams;i++) {
01961 ist = av_mallocz(sizeof(AVInputStream));
01962 if (!ist)
01963 goto fail;
01964 ist_table[i] = ist;
01965 }
01966 j = 0;
01967 for(i=0;i<nb_input_files;i++) {
01968 is = input_files[i];
01969 for(k=0;k<is->nb_streams;k++) {
01970 ist = ist_table[j++];
01971 ist->st = is->streams[k];
01972 ist->file_index = i;
01973 ist->index = k;
01974 ist->discard = 1;
01975
01976
01977 if (rate_emu) {
01978 ist->start = av_gettime();
01979 }
01980 }
01981 }
01982
01983
01984 nb_ostreams = 0;
01985 for(i=0;i<nb_output_files;i++) {
01986 os = output_files[i];
01987 if (!os->nb_streams && !(os->oformat->flags & AVFMT_NOSTREAMS)) {
01988 dump_format(output_files[i], i, output_files[i]->filename, 1);
01989 fprintf(stderr, "Output file #%d does not contain any stream\n", i);
01990 ret = AVERROR(EINVAL);
01991 goto fail;
01992 }
01993 nb_ostreams += os->nb_streams;
01994 }
01995 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
01996 fprintf(stderr, "Number of stream maps must match number of output streams\n");
01997 ret = AVERROR(EINVAL);
01998 goto fail;
01999 }
02000
02001
02002 for(i=0;i<nb_stream_maps;i++) {
02003 int fi = stream_maps[i].file_index;
02004 int si = stream_maps[i].stream_index;
02005
02006 if (fi < 0 || fi > nb_input_files - 1 ||
02007 si < 0 || si > file_table[fi].nb_streams - 1) {
02008 fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si);
02009 ret = AVERROR(EINVAL);
02010 goto fail;
02011 }
02012 fi = stream_maps[i].sync_file_index;
02013 si = stream_maps[i].sync_stream_index;
02014 if (fi < 0 || fi > nb_input_files - 1 ||
02015 si < 0 || si > file_table[fi].nb_streams - 1) {
02016 fprintf(stderr,"Could not find sync stream #%d.%d\n", fi, si);
02017 ret = AVERROR(EINVAL);
02018 goto fail;
02019 }
02020 }
02021
02022 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
02023 if (!ost_table)
02024 goto fail;
02025 n = 0;
02026 for(k=0;k<nb_output_files;k++) {
02027 os = output_files[k];
02028 for(i=0;i<os->nb_streams;i++,n++) {
02029 int found;
02030 ost = ost_table[n] = output_streams_for_file[k][i];
02031 ost->st = os->streams[i];
02032 if (nb_stream_maps > 0) {
02033 ost->source_index = file_table[stream_maps[n].file_index].ist_index +
02034 stream_maps[n].stream_index;
02035
02036
02037 if (ist_table[ost->source_index]->st->codec->codec_type != ost->st->codec->codec_type) {
02038 int i= ost->file_index;
02039 dump_format(output_files[i], i, output_files[i]->filename, 1);
02040 fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n",
02041 stream_maps[n].file_index, stream_maps[n].stream_index,
02042 ost->file_index, ost->index);
02043 ffmpeg_exit(1);
02044 }
02045
02046 } else {
02047 int best_nb_frames=-1;
02048
02049 found = 0;
02050 for(j=0;j<nb_istreams;j++) {
02051 int skip=0;
02052 ist = ist_table[j];
02053 if(opt_programid){
02054 int pi,si;
02055 AVFormatContext *f= input_files[ ist->file_index ];
02056 skip=1;
02057 for(pi=0; pi<f->nb_programs; pi++){
02058 AVProgram *p= f->programs[pi];
02059 if(p->id == opt_programid)
02060 for(si=0; si<p->nb_stream_indexes; si++){
02061 if(f->streams[ p->stream_index[si] ] == ist->st)
02062 skip=0;
02063 }
02064 }
02065 }
02066 if (ist->discard && ist->st->discard != AVDISCARD_ALL && !skip &&
02067 ist->st->codec->codec_type == ost->st->codec->codec_type) {
02068 if(best_nb_frames < ist->st->codec_info_nb_frames){
02069 best_nb_frames= ist->st->codec_info_nb_frames;
02070 ost->source_index = j;
02071 found = 1;
02072 }
02073 }
02074 }
02075
02076 if (!found) {
02077 if(! opt_programid) {
02078
02079 for(j=0;j<nb_istreams;j++) {
02080 ist = ist_table[j];
02081 if ( ist->st->codec->codec_type == ost->st->codec->codec_type
02082 && ist->st->discard != AVDISCARD_ALL) {
02083 ost->source_index = j;
02084 found = 1;
02085 }
02086 }
02087 }
02088 if (!found) {
02089 int i= ost->file_index;
02090 dump_format(output_files[i], i, output_files[i]->filename, 1);
02091 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
02092 ost->file_index, ost->index);
02093 ffmpeg_exit(1);
02094 }
02095 }
02096 }
02097 ist = ist_table[ost->source_index];
02098 ist->discard = 0;
02099 ost->sync_ist = (nb_stream_maps > 0) ?
02100 ist_table[file_table[stream_maps[n].sync_file_index].ist_index +
02101 stream_maps[n].sync_stream_index] : ist;
02102 }
02103 }
02104
02105
02106 for(i=0;i<nb_ostreams;i++) {
02107 ost = ost_table[i];
02108 os = output_files[ost->file_index];
02109 ist = ist_table[ost->source_index];
02110
02111 codec = ost->st->codec;
02112 icodec = ist->st->codec;
02113
02114 if (metadata_streams_autocopy)
02115 av_metadata_copy(&ost->st->metadata, ist->st->metadata,
02116 AV_METADATA_DONT_OVERWRITE);
02117
02118 ost->st->disposition = ist->st->disposition;
02119 codec->bits_per_raw_sample= icodec->bits_per_raw_sample;
02120 codec->chroma_sample_location = icodec->chroma_sample_location;
02121
02122 if (ost->st->stream_copy) {
02123 uint64_t extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
02124
02125 if (extra_size > INT_MAX)
02126 goto fail;
02127
02128
02129 codec->codec_id = icodec->codec_id;
02130 codec->codec_type = icodec->codec_type;
02131
02132 if(!codec->codec_tag){
02133 if( !os->oformat->codec_tag
02134 || av_codec_get_id (os->oformat->codec_tag, icodec->codec_tag) == codec->codec_id
02135 || av_codec_get_tag(os->oformat->codec_tag, icodec->codec_id) <= 0)
02136 codec->codec_tag = icodec->codec_tag;
02137 }
02138
02139 codec->bit_rate = icodec->bit_rate;
02140 codec->rc_max_rate = icodec->rc_max_rate;
02141 codec->rc_buffer_size = icodec->rc_buffer_size;
02142 codec->extradata= av_mallocz(extra_size);
02143 if (!codec->extradata)
02144 goto fail;
02145 memcpy(codec->extradata, icodec->extradata, icodec->extradata_size);
02146 codec->extradata_size= icodec->extradata_size;
02147 if(!copy_tb && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/1000){
02148 codec->time_base = icodec->time_base;
02149 codec->time_base.num *= icodec->ticks_per_frame;
02150 av_reduce(&codec->time_base.num, &codec->time_base.den,
02151 codec->time_base.num, codec->time_base.den, INT_MAX);
02152 }else
02153 codec->time_base = ist->st->time_base;
02154 switch(codec->codec_type) {
02155 case AVMEDIA_TYPE_AUDIO:
02156 if(audio_volume != 256) {
02157 fprintf(stderr,"-acodec copy and -vol are incompatible (frames are not decoded)\n");
02158 ffmpeg_exit(1);
02159 }
02160 codec->channel_layout = icodec->channel_layout;
02161 codec->sample_rate = icodec->sample_rate;
02162 codec->channels = icodec->channels;
02163 codec->frame_size = icodec->frame_size;
02164 codec->block_align= icodec->block_align;
02165 if(codec->block_align == 1 && codec->codec_id == CODEC_ID_MP3)
02166 codec->block_align= 0;
02167 if(codec->codec_id == CODEC_ID_AC3)
02168 codec->block_align= 0;
02169 break;
02170 case AVMEDIA_TYPE_VIDEO:
02171 codec->pix_fmt = icodec->pix_fmt;
02172 codec->width = icodec->width;
02173 codec->height = icodec->height;
02174 codec->has_b_frames = icodec->has_b_frames;
02175 break;
02176 case AVMEDIA_TYPE_SUBTITLE:
02177 codec->width = icodec->width;
02178 codec->height = icodec->height;
02179 break;
02180 default:
02181 abort();
02182 }
02183 } else {
02184 switch(codec->codec_type) {
02185 case AVMEDIA_TYPE_AUDIO:
02186 ost->fifo= av_fifo_alloc(1024);
02187 if(!ost->fifo)
02188 goto fail;
02189 ost->reformat_pair = MAKE_SFMT_PAIR(AV_SAMPLE_FMT_NONE,AV_SAMPLE_FMT_NONE);
02190 ost->audio_resample = codec->sample_rate != icodec->sample_rate || audio_sync_method > 1;
02191 icodec->request_channels = codec->channels;
02192 ist->decoding_needed = 1;
02193 ost->encoding_needed = 1;
02194 ost->resample_sample_fmt = icodec->sample_fmt;
02195 ost->resample_sample_rate = icodec->sample_rate;
02196 ost->resample_channels = icodec->channels;
02197 break;
02198 case AVMEDIA_TYPE_VIDEO:
02199 if (ost->st->codec->pix_fmt == PIX_FMT_NONE) {
02200 fprintf(stderr, "Video pixel format is unknown, stream cannot be encoded\n");
02201 ffmpeg_exit(1);
02202 }
02203 ost->video_resample = (codec->width != icodec->width ||
02204 codec->height != icodec->height ||
02205 (codec->pix_fmt != icodec->pix_fmt));
02206 if (ost->video_resample) {
02207 #if !CONFIG_AVFILTER
02208 avcodec_get_frame_defaults(&ost->pict_tmp);
02209 if(avpicture_alloc((AVPicture*)&ost->pict_tmp, codec->pix_fmt,
02210 codec->width, codec->height)) {
02211 fprintf(stderr, "Cannot allocate temp picture, check pix fmt\n");
02212 ffmpeg_exit(1);
02213 }
02214 sws_flags = av_get_int(sws_opts, "sws_flags", NULL);
02215 ost->img_resample_ctx = sws_getContext(
02216 icodec->width,
02217 icodec->height,
02218 icodec->pix_fmt,
02219 codec->width,
02220 codec->height,
02221 codec->pix_fmt,
02222 sws_flags, NULL, NULL, NULL);
02223 if (ost->img_resample_ctx == NULL) {
02224 fprintf(stderr, "Cannot get resampling context\n");
02225 ffmpeg_exit(1);
02226 }
02227
02228 ost->original_height = icodec->height;
02229 ost->original_width = icodec->width;
02230 #endif
02231 codec->bits_per_raw_sample= 0;
02232 }
02233 ost->resample_height = icodec->height;
02234 ost->resample_width = icodec->width;
02235 ost->resample_pix_fmt= icodec->pix_fmt;
02236 ost->encoding_needed = 1;
02237 ist->decoding_needed = 1;
02238
02239 #if CONFIG_AVFILTER
02240 if (configure_filters(ist, ost)) {
02241 fprintf(stderr, "Error opening filters!\n");
02242 exit(1);
02243 }
02244 #endif
02245 break;
02246 case AVMEDIA_TYPE_SUBTITLE:
02247 ost->encoding_needed = 1;
02248 ist->decoding_needed = 1;
02249 break;
02250 default:
02251 abort();
02252 break;
02253 }
02254
02255 if (ost->encoding_needed &&
02256 (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
02257 char logfilename[1024];
02258 FILE *f;
02259
02260 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
02261 pass_logfilename_prefix ? pass_logfilename_prefix : DEFAULT_PASS_LOGFILENAME_PREFIX,
02262 i);
02263 if (codec->flags & CODEC_FLAG_PASS1) {
02264 f = fopen(logfilename, "wb");
02265 if (!f) {
02266 fprintf(stderr, "Cannot write log file '%s' for pass-1 encoding: %s\n", logfilename, strerror(errno));
02267 ffmpeg_exit(1);
02268 }
02269 ost->logfile = f;
02270 } else {
02271 char *logbuffer;
02272 size_t logbuffer_size;
02273 if (read_file(logfilename, &logbuffer, &logbuffer_size) < 0) {
02274 fprintf(stderr, "Error reading log file '%s' for pass-2 encoding\n", logfilename);
02275 ffmpeg_exit(1);
02276 }
02277 codec->stats_in = logbuffer;
02278 }
02279 }
02280 }
02281 if(codec->codec_type == AVMEDIA_TYPE_VIDEO){
02282 int size= codec->width * codec->height;
02283 bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 200);
02284 }
02285 }
02286
02287 if (!bit_buffer)
02288 bit_buffer = av_malloc(bit_buffer_size);
02289 if (!bit_buffer) {
02290 fprintf(stderr, "Cannot allocate %d bytes output buffer\n",
02291 bit_buffer_size);
02292 ret = AVERROR(ENOMEM);
02293 goto fail;
02294 }
02295
02296
02297 for(i=0;i<nb_ostreams;i++) {
02298 ost = ost_table[i];
02299 if (ost->encoding_needed) {
02300 AVCodec *codec = i < nb_output_codecs ? output_codecs[i] : NULL;
02301 AVCodecContext *dec = ist_table[ost->source_index]->st->codec;
02302 if (!codec)
02303 codec = avcodec_find_encoder(ost->st->codec->codec_id);
02304 if (!codec) {
02305 snprintf(error, sizeof(error), "Encoder (codec id %d) not found for output stream #%d.%d",
02306 ost->st->codec->codec_id, ost->file_index, ost->index);
02307 ret = AVERROR(EINVAL);
02308 goto dump_format;
02309 }
02310 if (dec->subtitle_header) {
02311 ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size);
02312 if (!ost->st->codec->subtitle_header) {
02313 ret = AVERROR(ENOMEM);
02314 goto dump_format;
02315 }
02316 memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
02317 ost->st->codec->subtitle_header_size = dec->subtitle_header_size;
02318 }
02319 if (avcodec_open(ost->st->codec, codec) < 0) {
02320 snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height",
02321 ost->file_index, ost->index);
02322 ret = AVERROR(EINVAL);
02323 goto dump_format;
02324 }
02325 extra_size += ost->st->codec->extradata_size;
02326 }
02327 }
02328
02329
02330 for(i=0;i<nb_istreams;i++) {
02331 ist = ist_table[i];
02332 if (ist->decoding_needed) {
02333 AVCodec *codec = i < nb_input_codecs ? input_codecs[i] : NULL;
02334 if (!codec)
02335 codec = avcodec_find_decoder(ist->st->codec->codec_id);
02336 if (!codec) {
02337 snprintf(error, sizeof(error), "Decoder (codec id %d) not found for input stream #%d.%d",
02338 ist->st->codec->codec_id, ist->file_index, ist->index);
02339 ret = AVERROR(EINVAL);
02340 goto dump_format;
02341 }
02342 if (avcodec_open(ist->st->codec, codec) < 0) {
02343 snprintf(error, sizeof(error), "Error while opening decoder for input stream #%d.%d",
02344 ist->file_index, ist->index);
02345 ret = AVERROR(EINVAL);
02346 goto dump_format;
02347 }
02348
02349
02350 }
02351 }
02352
02353
02354 for(i=0;i<nb_istreams;i++) {
02355 AVStream *st;
02356 ist = ist_table[i];
02357 st= ist->st;
02358 ist->pts = st->avg_frame_rate.num ? - st->codec->has_b_frames*AV_TIME_BASE / av_q2d(st->avg_frame_rate) : 0;
02359 ist->next_pts = AV_NOPTS_VALUE;
02360 init_pts_correction(&ist->pts_ctx);
02361 ist->is_start = 1;
02362 }
02363
02364
02365 for (i=0;i<nb_meta_data_maps;i++) {
02366 AVFormatContext *files[2];
02367 AVMetadata **meta[2];
02368 int j;
02369
02370 #define METADATA_CHECK_INDEX(index, nb_elems, desc)\
02371 if ((index) < 0 || (index) >= (nb_elems)) {\
02372 snprintf(error, sizeof(error), "Invalid %s index %d while processing metadata maps\n",\
02373 (desc), (index));\
02374 ret = AVERROR(EINVAL);\
02375 goto dump_format;\
02376 }
02377
02378 int out_file_index = meta_data_maps[i][0].file;
02379 int in_file_index = meta_data_maps[i][1].file;
02380 if (in_file_index < 0 || out_file_index < 0)
02381 continue;
02382 METADATA_CHECK_INDEX(out_file_index, nb_output_files, "output file")
02383 METADATA_CHECK_INDEX(in_file_index, nb_input_files, "input file")
02384
02385 files[0] = output_files[out_file_index];
02386 files[1] = input_files[in_file_index];
02387
02388 for (j = 0; j < 2; j++) {
02389 AVMetaDataMap *map = &meta_data_maps[i][j];
02390
02391 switch (map->type) {
02392 case 'g':
02393 meta[j] = &files[j]->metadata;
02394 break;
02395 case 's':
02396 METADATA_CHECK_INDEX(map->index, files[j]->nb_streams, "stream")
02397 meta[j] = &files[j]->streams[map->index]->metadata;
02398 break;
02399 case 'c':
02400 METADATA_CHECK_INDEX(map->index, files[j]->nb_chapters, "chapter")
02401 meta[j] = &files[j]->chapters[map->index]->metadata;
02402 break;
02403 case 'p':
02404 METADATA_CHECK_INDEX(map->index, files[j]->nb_programs, "program")
02405 meta[j] = &files[j]->programs[map->index]->metadata;
02406 break;
02407 }
02408 }
02409
02410 av_metadata_copy(meta[0], *meta[1], AV_METADATA_DONT_OVERWRITE);
02411 }
02412
02413
02414 if (metadata_global_autocopy) {
02415
02416 for (i = 0; i < nb_output_files; i++)
02417 av_metadata_copy(&output_files[i]->metadata, input_files[0]->metadata,
02418 AV_METADATA_DONT_OVERWRITE);
02419 }
02420
02421
02422 for (i = 0; i < nb_chapter_maps; i++) {
02423 int infile = chapter_maps[i].in_file;
02424 int outfile = chapter_maps[i].out_file;
02425
02426 if (infile < 0 || outfile < 0)
02427 continue;
02428 if (infile >= nb_input_files) {
02429 snprintf(error, sizeof(error), "Invalid input file index %d in chapter mapping.\n", infile);
02430 ret = AVERROR(EINVAL);
02431 goto dump_format;
02432 }
02433 if (outfile >= nb_output_files) {
02434 snprintf(error, sizeof(error), "Invalid output file index %d in chapter mapping.\n",outfile);
02435 ret = AVERROR(EINVAL);
02436 goto dump_format;
02437 }
02438 copy_chapters(infile, outfile);
02439 }
02440
02441
02442 if (!nb_chapter_maps)
02443 for (i = 0; i < nb_input_files; i++) {
02444 if (!input_files[i]->nb_chapters)
02445 continue;
02446
02447 for (j = 0; j < nb_output_files; j++)
02448 if ((ret = copy_chapters(i, j)) < 0)
02449 goto dump_format;
02450 break;
02451 }
02452
02453
02454 for(i=0;i<nb_output_files;i++) {
02455 os = output_files[i];
02456 if (av_write_header(os) < 0) {
02457 snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?)", i);
02458 ret = AVERROR(EINVAL);
02459 goto dump_format;
02460 }
02461 if (strcmp(output_files[i]->oformat->name, "rtp")) {
02462 want_sdp = 0;
02463 }
02464 }
02465
02466 dump_format:
02467
02468
02469 for(i=0;i<nb_output_files;i++) {
02470 dump_format(output_files[i], i, output_files[i]->filename, 1);
02471 }
02472
02473
02474 if (verbose >= 0) {
02475 fprintf(stderr, "Stream mapping:\n");
02476 for(i=0;i<nb_ostreams;i++) {
02477 ost = ost_table[i];
02478 fprintf(stderr, " Stream #%d.%d -> #%d.%d",
02479 ist_table[ost->source_index]->file_index,
02480 ist_table[ost->source_index]->index,
02481 ost->file_index,
02482 ost->index);
02483 if (ost->sync_ist != ist_table[ost->source_index])
02484 fprintf(stderr, " [sync #%d.%d]",
02485 ost->sync_ist->file_index,
02486 ost->sync_ist->index);
02487 fprintf(stderr, "\n");
02488 }
02489 }
02490
02491 if (ret) {
02492 fprintf(stderr, "%s\n", error);
02493 goto fail;
02494 }
02495
02496 if (want_sdp) {
02497 print_sdp(output_files, nb_output_files);
02498 }
02499
02500 if (!using_stdin && verbose >= 0) {
02501 fprintf(stderr, "Press [q] to stop encoding\n");
02502 url_set_interrupt_cb(decode_interrupt_cb);
02503 }
02504 term_init();
02505
02506 timer_start = av_gettime();
02507
02508 for(; received_sigterm == 0;) {
02509 int file_index, ist_index;
02510 AVPacket pkt;
02511 double ipts_min;
02512 double opts_min;
02513
02514 redo:
02515 ipts_min= 1e100;
02516 opts_min= 1e100;
02517
02518 if (!using_stdin) {
02519 if (q_pressed)
02520 break;
02521
02522 key = read_key();
02523 if (key == 'q')
02524 break;
02525 }
02526
02527
02528
02529 file_index = -1;
02530 for(i=0;i<nb_ostreams;i++) {
02531 double ipts, opts;
02532 ost = ost_table[i];
02533 os = output_files[ost->file_index];
02534 ist = ist_table[ost->source_index];
02535 if(ist->is_past_recording_time || no_packet[ist->file_index])
02536 continue;
02537 opts = ost->st->pts.val * av_q2d(ost->st->time_base);
02538 ipts = (double)ist->pts;
02539 if (!file_table[ist->file_index].eof_reached){
02540 if(ipts < ipts_min) {
02541 ipts_min = ipts;
02542 if(input_sync ) file_index = ist->file_index;
02543 }
02544 if(opts < opts_min) {
02545 opts_min = opts;
02546 if(!input_sync) file_index = ist->file_index;
02547 }
02548 }
02549 if(ost->frame_number >= max_frames[ost->st->codec->codec_type]){
02550 file_index= -1;
02551 break;
02552 }
02553 }
02554
02555 if (file_index < 0) {
02556 if(no_packet_count){
02557 no_packet_count=0;
02558 memset(no_packet, 0, sizeof(no_packet));
02559 usleep(10000);
02560 continue;
02561 }
02562 break;
02563 }
02564
02565
02566 if (limit_filesize != 0 && limit_filesize <= url_ftell(output_files[0]->pb))
02567 break;
02568
02569
02570 is = input_files[file_index];
02571 ret= av_read_frame(is, &pkt);
02572 if(ret == AVERROR(EAGAIN)){
02573 no_packet[file_index]=1;
02574 no_packet_count++;
02575 continue;
02576 }
02577 if (ret < 0) {
02578 file_table[file_index].eof_reached = 1;
02579 if (opt_shortest)
02580 break;
02581 else
02582 continue;
02583 }
02584
02585 no_packet_count=0;
02586 memset(no_packet, 0, sizeof(no_packet));
02587
02588 if (do_pkt_dump) {
02589 av_pkt_dump_log(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump);
02590 }
02591
02592
02593 if (pkt.stream_index >= file_table[file_index].nb_streams)
02594 goto discard_packet;
02595 ist_index = file_table[file_index].ist_index + pkt.stream_index;
02596 ist = ist_table[ist_index];
02597 if (ist->discard)
02598 goto discard_packet;
02599
02600 if (pkt.dts != AV_NOPTS_VALUE)
02601 pkt.dts += av_rescale_q(input_files_ts_offset[ist->file_index], AV_TIME_BASE_Q, ist->st->time_base);
02602 if (pkt.pts != AV_NOPTS_VALUE)
02603 pkt.pts += av_rescale_q(input_files_ts_offset[ist->file_index], AV_TIME_BASE_Q, ist->st->time_base);
02604
02605 if (pkt.stream_index < nb_input_files_ts_scale[file_index]
02606 && input_files_ts_scale[file_index][pkt.stream_index]){
02607 if(pkt.pts != AV_NOPTS_VALUE)
02608 pkt.pts *= input_files_ts_scale[file_index][pkt.stream_index];
02609 if(pkt.dts != AV_NOPTS_VALUE)
02610 pkt.dts *= input_files_ts_scale[file_index][pkt.stream_index];
02611 }
02612
02613
02614 if (pkt.dts != AV_NOPTS_VALUE && ist->next_pts != AV_NOPTS_VALUE
02615 && (is->iformat->flags & AVFMT_TS_DISCONT)) {
02616 int64_t pkt_dts= av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
02617 int64_t delta= pkt_dts - ist->next_pts;
02618 if((FFABS(delta) > 1LL*dts_delta_threshold*AV_TIME_BASE || pkt_dts+1<ist->pts)&& !copy_ts){
02619 input_files_ts_offset[ist->file_index]-= delta;
02620 if (verbose > 2)
02621 fprintf(stderr, "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n", delta, input_files_ts_offset[ist->file_index]);
02622 pkt.dts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
02623 if(pkt.pts != AV_NOPTS_VALUE)
02624 pkt.pts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
02625 }
02626 }
02627
02628
02629 if (recording_time != INT64_MAX &&
02630 av_compare_ts(pkt.pts, ist->st->time_base, recording_time + start_time, (AVRational){1, 1000000}) >= 0) {
02631 ist->is_past_recording_time = 1;
02632 goto discard_packet;
02633 }
02634
02635
02636 if (output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt) < 0) {
02637
02638 if (verbose >= 0)
02639 fprintf(stderr, "Error while decoding stream #%d.%d\n",
02640 ist->file_index, ist->index);
02641 if (exit_on_error)
02642 ffmpeg_exit(1);
02643 av_free_packet(&pkt);
02644 goto redo;
02645 }
02646
02647 discard_packet:
02648 av_free_packet(&pkt);
02649
02650
02651 print_report(output_files, ost_table, nb_ostreams, 0);
02652 }
02653
02654
02655 for(i=0;i<nb_istreams;i++) {
02656 ist = ist_table[i];
02657 if (ist->decoding_needed) {
02658 output_packet(ist, i, ost_table, nb_ostreams, NULL);
02659 }
02660 }
02661
02662 term_exit();
02663
02664
02665 for(i=0;i<nb_output_files;i++) {
02666 os = output_files[i];
02667 av_write_trailer(os);
02668 }
02669
02670
02671 print_report(output_files, ost_table, nb_ostreams, 1);
02672
02673
02674 for(i=0;i<nb_ostreams;i++) {
02675 ost = ost_table[i];
02676 if (ost->encoding_needed) {
02677 av_freep(&ost->st->codec->stats_in);
02678 avcodec_close(ost->st->codec);
02679 }
02680 }
02681
02682
02683 for(i=0;i<nb_istreams;i++) {
02684 ist = ist_table[i];
02685 if (ist->decoding_needed) {
02686 avcodec_close(ist->st->codec);
02687 }
02688 }
02689 #if CONFIG_AVFILTER
02690 if (graph) {
02691 avfilter_graph_free(graph);
02692 av_freep(&graph);
02693 }
02694 #endif
02695
02696
02697 ret = 0;
02698
02699 fail:
02700 av_freep(&bit_buffer);
02701 av_free(file_table);
02702
02703 if (ist_table) {
02704 for(i=0;i<nb_istreams;i++) {
02705 ist = ist_table[i];
02706 av_free(ist);
02707 }
02708 av_free(ist_table);
02709 }
02710 if (ost_table) {
02711 for(i=0;i<nb_ostreams;i++) {
02712 ost = ost_table[i];
02713 if (ost) {
02714 if (ost->st->stream_copy)
02715 av_freep(&ost->st->codec->extradata);
02716 if (ost->logfile) {
02717 fclose(ost->logfile);
02718 ost->logfile = NULL;
02719 }
02720 av_fifo_free(ost->fifo);
02721
02722 av_freep(&ost->st->codec->subtitle_header);
02723 av_free(ost->pict_tmp.data[0]);
02724 av_free(ost->forced_kf_pts);
02725 if (ost->video_resample)
02726 sws_freeContext(ost->img_resample_ctx);
02727 if (ost->resample)
02728 audio_resample_close(ost->resample);
02729 if (ost->reformat_ctx)
02730 av_audio_convert_free(ost->reformat_ctx);
02731 av_free(ost);
02732 }
02733 }
02734 av_free(ost_table);
02735 }
02736 return ret;
02737 }
02738
02739 static void opt_format(const char *arg)
02740 {
02741 last_asked_format = arg;
02742 }
02743
02744 static void opt_video_rc_override_string(const char *arg)
02745 {
02746 video_rc_override_string = arg;
02747 }
02748
02749 static int opt_me_threshold(const char *opt, const char *arg)
02750 {
02751 me_threshold = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
02752 return 0;
02753 }
02754
02755 static int opt_verbose(const char *opt, const char *arg)
02756 {
02757 verbose = parse_number_or_die(opt, arg, OPT_INT64, -10, 10);
02758 return 0;
02759 }
02760
02761 static int opt_frame_rate(const char *opt, const char *arg)
02762 {
02763 if (av_parse_video_rate(&frame_rate, arg) < 0) {
02764 fprintf(stderr, "Incorrect value for %s: %s\n", opt, arg);
02765 ffmpeg_exit(1);
02766 }
02767 return 0;
02768 }
02769
02770 static int opt_bitrate(const char *opt, const char *arg)
02771 {
02772 int codec_type = opt[0]=='a' ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
02773
02774 opt_default(opt, arg);
02775
02776 if (av_get_int(avcodec_opts[codec_type], "b", NULL) < 1000)
02777 fprintf(stderr, "WARNING: The bitrate parameter is set too low. It takes bits/s as argument, not kbits/s\n");
02778
02779 return 0;
02780 }
02781
02782 static int opt_frame_crop(const char *opt, const char *arg)
02783 {
02784 fprintf(stderr, "Option '%s' has been removed, use the crop filter instead\n", opt);
02785 return AVERROR(EINVAL);
02786 }
02787
02788 static void opt_frame_size(const char *arg)
02789 {
02790 if (av_parse_video_size(&frame_width, &frame_height, arg) < 0) {
02791 fprintf(stderr, "Incorrect frame size\n");
02792 ffmpeg_exit(1);
02793 }
02794 }
02795
02796 static int opt_pad(const char *opt, const char *arg) {
02797 fprintf(stderr, "Option '%s' has been removed, use the pad filter instead\n", opt);
02798 return -1;
02799 }
02800
02801 static void opt_frame_pix_fmt(const char *arg)
02802 {
02803 if (strcmp(arg, "list")) {
02804 frame_pix_fmt = av_get_pix_fmt(arg);
02805 if (frame_pix_fmt == PIX_FMT_NONE) {
02806 fprintf(stderr, "Unknown pixel format requested: %s\n", arg);
02807 ffmpeg_exit(1);
02808 }
02809 } else {
02810 show_pix_fmts();
02811 ffmpeg_exit(0);
02812 }
02813 }
02814
02815 static void opt_frame_aspect_ratio(const char *arg)
02816 {
02817 int x = 0, y = 0;
02818 double ar = 0;
02819 const char *p;
02820 char *end;
02821
02822 p = strchr(arg, ':');
02823 if (p) {
02824 x = strtol(arg, &end, 10);
02825 if (end == p)
02826 y = strtol(end+1, &end, 10);
02827 if (x > 0 && y > 0)
02828 ar = (double)x / (double)y;
02829 } else
02830 ar = strtod(arg, NULL);
02831
02832 if (!ar) {
02833 fprintf(stderr, "Incorrect aspect ratio specification.\n");
02834 ffmpeg_exit(1);
02835 }
02836 frame_aspect_ratio = ar;
02837 }
02838
02839 static int opt_metadata(const char *opt, const char *arg)
02840 {
02841 char *mid= strchr(arg, '=');
02842
02843 if(!mid){
02844 fprintf(stderr, "Missing =\n");
02845 ffmpeg_exit(1);
02846 }
02847 *mid++= 0;
02848
02849 av_metadata_set2(&metadata, arg, mid, 0);
02850
02851 return 0;
02852 }
02853
02854 static void opt_qscale(const char *arg)
02855 {
02856 video_qscale = atof(arg);
02857 if (video_qscale <= 0 ||
02858 video_qscale > 255) {
02859 fprintf(stderr, "qscale must be > 0.0 and <= 255\n");
02860 ffmpeg_exit(1);
02861 }
02862 }
02863
02864 static void opt_top_field_first(const char *arg)
02865 {
02866 top_field_first= atoi(arg);
02867 }
02868
02869 static int opt_thread_count(const char *opt, const char *arg)
02870 {
02871 thread_count= parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
02872 #if !HAVE_THREADS
02873 if (verbose >= 0)
02874 fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
02875 #endif
02876 return 0;
02877 }
02878
02879 static void opt_audio_sample_fmt(const char *arg)
02880 {
02881 if (strcmp(arg, "list")) {
02882 audio_sample_fmt = av_get_sample_fmt(arg);
02883 if (audio_sample_fmt == AV_SAMPLE_FMT_NONE) {
02884 av_log(NULL, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
02885 ffmpeg_exit(1);
02886 }
02887 } else {
02888 list_fmts(av_get_sample_fmt_string, AV_SAMPLE_FMT_NB);
02889 ffmpeg_exit(0);
02890 }
02891 }
02892
02893 static int opt_audio_rate(const char *opt, const char *arg)
02894 {
02895 audio_sample_rate = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
02896 return 0;
02897 }
02898
02899 static int opt_audio_channels(const char *opt, const char *arg)
02900 {
02901 audio_channels = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
02902 return 0;
02903 }
02904
02905 static void opt_video_channel(const char *arg)
02906 {
02907 video_channel = strtol(arg, NULL, 0);
02908 }
02909
02910 static void opt_video_standard(const char *arg)
02911 {
02912 video_standard = av_strdup(arg);
02913 }
02914
02915 static void opt_codec(int *pstream_copy, char **pcodec_name,
02916 int codec_type, const char *arg)
02917 {
02918 av_freep(pcodec_name);
02919 if (!strcmp(arg, "copy")) {
02920 *pstream_copy = 1;
02921 } else {
02922 *pcodec_name = av_strdup(arg);
02923 }
02924 }
02925
02926 static void opt_audio_codec(const char *arg)
02927 {
02928 opt_codec(&audio_stream_copy, &audio_codec_name, AVMEDIA_TYPE_AUDIO, arg);
02929 }
02930
02931 static void opt_video_codec(const char *arg)
02932 {
02933 opt_codec(&video_stream_copy, &video_codec_name, AVMEDIA_TYPE_VIDEO, arg);
02934 }
02935
02936 static void opt_subtitle_codec(const char *arg)
02937 {
02938 opt_codec(&subtitle_stream_copy, &subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, arg);
02939 }
02940
02941 static int opt_codec_tag(const char *opt, const char *arg)
02942 {
02943 char *tail;
02944 uint32_t *codec_tag;
02945
02946 codec_tag = !strcmp(opt, "atag") ? &audio_codec_tag :
02947 !strcmp(opt, "vtag") ? &video_codec_ta