00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "config.h"
00027 #include <ctype.h>
00028 #include <string.h>
00029 #include <math.h>
00030 #include <stdlib.h>
00031 #include <errno.h>
00032 #include <limits.h>
00033 #if HAVE_ISATTY
00034 #if HAVE_IO_H
00035 #include <io.h>
00036 #endif
00037 #if HAVE_UNISTD_H
00038 #include <unistd.h>
00039 #endif
00040 #endif
00041 #include "libavformat/avformat.h"
00042 #include "libavdevice/avdevice.h"
00043 #include "libswscale/swscale.h"
00044 #include "libswresample/swresample.h"
00045 #include "libavutil/opt.h"
00046 #include "libavutil/audioconvert.h"
00047 #include "libavutil/parseutils.h"
00048 #include "libavutil/samplefmt.h"
00049 #include "libavutil/colorspace.h"
00050 #include "libavutil/fifo.h"
00051 #include "libavutil/intreadwrite.h"
00052 #include "libavutil/dict.h"
00053 #include "libavutil/mathematics.h"
00054 #include "libavutil/pixdesc.h"
00055 #include "libavutil/avstring.h"
00056 #include "libavutil/libm.h"
00057 #include "libavutil/imgutils.h"
00058 #include "libavutil/timestamp.h"
00059 #include "libavutil/bprint.h"
00060 #include "libavutil/time.h"
00061 #include "libavformat/os_support.h"
00062
00063 #include "libavformat/ffm.h"
00064
00065 # include "libavfilter/avcodec.h"
00066 # include "libavfilter/avfilter.h"
00067 # include "libavfilter/avfiltergraph.h"
00068 # include "libavfilter/buffersrc.h"
00069 # include "libavfilter/buffersink.h"
00070
00071 #if HAVE_SYS_RESOURCE_H
00072 #include <sys/types.h>
00073 #include <sys/resource.h>
00074 #elif HAVE_GETPROCESSTIMES
00075 #include <windows.h>
00076 #endif
00077 #if HAVE_GETPROCESSMEMORYINFO
00078 #include <windows.h>
00079 #include <psapi.h>
00080 #endif
00081
00082 #if HAVE_SYS_SELECT_H
00083 #include <sys/select.h>
00084 #endif
00085
00086 #if HAVE_TERMIOS_H
00087 #include <fcntl.h>
00088 #include <sys/ioctl.h>
00089 #include <sys/time.h>
00090 #include <termios.h>
00091 #elif HAVE_KBHIT
00092 #include <conio.h>
00093 #endif
00094
00095 #if HAVE_PTHREADS
00096 #include <pthread.h>
00097 #endif
00098
00099 #include <time.h>
00100
00101 #include "ffmpeg.h"
00102 #include "cmdutils.h"
00103
00104 #include "libavutil/avassert.h"
00105
00106 const char program_name[] = "ffmpeg";
00107 const int program_birth_year = 2000;
00108
00109 static FILE *vstats_file;
00110
00111 static void do_video_stats(AVFormatContext *os, OutputStream *ost, int frame_size);
00112 static int64_t getutime(void);
00113
00114 static int run_as_daemon = 0;
00115 static int64_t video_size = 0;
00116 static int64_t audio_size = 0;
00117 static int64_t subtitle_size = 0;
00118 static int64_t extra_size = 0;
00119 static int nb_frames_dup = 0;
00120 static int nb_frames_drop = 0;
00121
00122 static int current_time;
00123 AVIOContext *progress_avio = NULL;
00124
00125 static uint8_t *subtitle_out;
00126
00127 #if HAVE_PTHREADS
00128
00129 static int transcoding_finished;
00130 #endif
00131
00132 #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
00133
00134 InputStream **input_streams = NULL;
00135 int nb_input_streams = 0;
00136 InputFile **input_files = NULL;
00137 int nb_input_files = 0;
00138
00139 OutputStream **output_streams = NULL;
00140 int nb_output_streams = 0;
00141 OutputFile **output_files = NULL;
00142 int nb_output_files = 0;
00143
00144 FilterGraph **filtergraphs;
00145 int nb_filtergraphs;
00146
00147 #if HAVE_TERMIOS_H
00148
00149
00150 static struct termios oldtty;
00151 static int restore_tty;
00152 #endif
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 static void sub2video_copy_rect(uint8_t *dst, int dst_linesize, int w, int h,
00163 AVSubtitleRect *r)
00164 {
00165 uint32_t *pal, *dst2;
00166 uint8_t *src, *src2;
00167 int x, y;
00168
00169 if (r->type != SUBTITLE_BITMAP) {
00170 av_log(NULL, AV_LOG_WARNING, "sub2video: non-bitmap subtitle\n");
00171 return;
00172 }
00173 if (r->x < 0 || r->x + r->w > w || r->y < 0 || r->y + r->h > h) {
00174 av_log(NULL, AV_LOG_WARNING, "sub2video: rectangle overflowing\n");
00175 return;
00176 }
00177
00178 dst += r->y * dst_linesize + r->x * 4;
00179 src = r->pict.data[0];
00180 pal = (uint32_t *)r->pict.data[1];
00181 for (y = 0; y < r->h; y++) {
00182 dst2 = (uint32_t *)dst;
00183 src2 = src;
00184 for (x = 0; x < r->w; x++)
00185 *(dst2++) = pal[*(src2++)];
00186 dst += dst_linesize;
00187 src += r->pict.linesize[0];
00188 }
00189 }
00190
00191 static void sub2video_push_ref(InputStream *ist, int64_t pts)
00192 {
00193 AVFilterBufferRef *ref = ist->sub2video.ref;
00194 int i;
00195
00196 ist->sub2video.last_pts = ref->pts = pts;
00197 for (i = 0; i < ist->nb_filters; i++)
00198 av_buffersrc_add_ref(ist->filters[i]->filter,
00199 avfilter_ref_buffer(ref, ~0),
00200 AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT |
00201 AV_BUFFERSRC_FLAG_NO_COPY |
00202 AV_BUFFERSRC_FLAG_PUSH);
00203 }
00204
00205 static void sub2video_update(InputStream *ist, AVSubtitle *sub)
00206 {
00207 int w = ist->sub2video.w, h = ist->sub2video.h;
00208 AVFilterBufferRef *ref = ist->sub2video.ref;
00209 int8_t *dst;
00210 int dst_linesize;
00211 int i;
00212 int64_t pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ist->st->time_base);
00213
00214 if (!ref)
00215 return;
00216 dst = ref->data [0];
00217 dst_linesize = ref->linesize[0];
00218 memset(dst, 0, h * dst_linesize);
00219 for (i = 0; i < sub->num_rects; i++)
00220 sub2video_copy_rect(dst, dst_linesize, w, h, sub->rects[i]);
00221 sub2video_push_ref(ist, pts);
00222 }
00223
00224 static void sub2video_heartbeat(InputStream *ist, int64_t pts)
00225 {
00226 InputFile *infile = input_files[ist->file_index];
00227 int i, j, nb_reqs;
00228 int64_t pts2;
00229
00230
00231
00232
00233
00234 for (i = 0; i < infile->nb_streams; i++) {
00235 InputStream *ist2 = input_streams[infile->ist_index + i];
00236 if (!ist2->sub2video.ref)
00237 continue;
00238
00239
00240 pts2 = av_rescale_q(pts, ist->st->time_base, ist2->st->time_base) - 1;
00241
00242 if (pts2 <= ist2->sub2video.last_pts)
00243 continue;
00244 for (j = 0, nb_reqs = 0; j < ist2->nb_filters; j++)
00245 nb_reqs += av_buffersrc_get_nb_failed_requests(ist2->filters[j]->filter);
00246 if (nb_reqs)
00247 sub2video_push_ref(ist2, pts2);
00248 }
00249 }
00250
00251 static void sub2video_flush(InputStream *ist)
00252 {
00253 int i;
00254
00255 for (i = 0; i < ist->nb_filters; i++)
00256 av_buffersrc_add_ref(ist->filters[i]->filter, NULL, 0);
00257 }
00258
00259
00260
00261 void term_exit(void)
00262 {
00263 av_log(NULL, AV_LOG_QUIET, "%s", "");
00264 #if HAVE_TERMIOS_H
00265 if(restore_tty)
00266 tcsetattr (0, TCSANOW, &oldtty);
00267 #endif
00268 }
00269
00270 static volatile int received_sigterm = 0;
00271 static volatile int received_nb_signals = 0;
00272
00273 static void
00274 sigterm_handler(int sig)
00275 {
00276 received_sigterm = sig;
00277 received_nb_signals++;
00278 term_exit();
00279 if(received_nb_signals > 3)
00280 exit(123);
00281 }
00282
00283 void term_init(void)
00284 {
00285 #if HAVE_TERMIOS_H
00286 if(!run_as_daemon){
00287 struct termios tty;
00288 int istty = 1;
00289 #if HAVE_ISATTY
00290 istty = isatty(0) && isatty(2);
00291 #endif
00292 if (istty && tcgetattr (0, &tty) == 0) {
00293 oldtty = tty;
00294 restore_tty = 1;
00295 atexit(term_exit);
00296
00297 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
00298 |INLCR|IGNCR|ICRNL|IXON);
00299 tty.c_oflag |= OPOST;
00300 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
00301 tty.c_cflag &= ~(CSIZE|PARENB);
00302 tty.c_cflag |= CS8;
00303 tty.c_cc[VMIN] = 1;
00304 tty.c_cc[VTIME] = 0;
00305
00306 tcsetattr (0, TCSANOW, &tty);
00307 }
00308 signal(SIGQUIT, sigterm_handler);
00309 }
00310 #endif
00311 avformat_network_deinit();
00312
00313 signal(SIGINT , sigterm_handler);
00314 signal(SIGTERM, sigterm_handler);
00315 #ifdef SIGXCPU
00316 signal(SIGXCPU, sigterm_handler);
00317 #endif
00318 }
00319
00320
00321 static int read_key(void)
00322 {
00323 unsigned char ch;
00324 #if HAVE_TERMIOS_H
00325 int n = 1;
00326 struct timeval tv;
00327 fd_set rfds;
00328
00329 FD_ZERO(&rfds);
00330 FD_SET(0, &rfds);
00331 tv.tv_sec = 0;
00332 tv.tv_usec = 0;
00333 n = select(1, &rfds, NULL, NULL, &tv);
00334 if (n > 0) {
00335 n = read(0, &ch, 1);
00336 if (n == 1)
00337 return ch;
00338
00339 return n;
00340 }
00341 #elif HAVE_KBHIT
00342 # if HAVE_PEEKNAMEDPIPE
00343 static int is_pipe;
00344 static HANDLE input_handle;
00345 DWORD dw, nchars;
00346 if(!input_handle){
00347 input_handle = GetStdHandle(STD_INPUT_HANDLE);
00348 is_pipe = !GetConsoleMode(input_handle, &dw);
00349 }
00350
00351 if (stdin->_cnt > 0) {
00352 read(0, &ch, 1);
00353 return ch;
00354 }
00355 if (is_pipe) {
00356
00357 if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL))
00358 return -1;
00359
00360 if(nchars != 0) {
00361 read(0, &ch, 1);
00362 return ch;
00363 }else{
00364 return -1;
00365 }
00366 }
00367 # endif
00368 if(kbhit())
00369 return(getch());
00370 #endif
00371 return -1;
00372 }
00373
00374 static int decode_interrupt_cb(void *ctx)
00375 {
00376 return received_nb_signals > 1;
00377 }
00378
00379 const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
00380
00381 void av_noreturn exit_program(int ret)
00382 {
00383 int i, j;
00384
00385 for (i = 0; i < nb_filtergraphs; i++) {
00386 avfilter_graph_free(&filtergraphs[i]->graph);
00387 for (j = 0; j < filtergraphs[i]->nb_inputs; j++) {
00388 av_freep(&filtergraphs[i]->inputs[j]->name);
00389 av_freep(&filtergraphs[i]->inputs[j]);
00390 }
00391 av_freep(&filtergraphs[i]->inputs);
00392 for (j = 0; j < filtergraphs[i]->nb_outputs; j++) {
00393 av_freep(&filtergraphs[i]->outputs[j]->name);
00394 av_freep(&filtergraphs[i]->outputs[j]);
00395 }
00396 av_freep(&filtergraphs[i]->outputs);
00397 av_freep(&filtergraphs[i]);
00398 }
00399 av_freep(&filtergraphs);
00400
00401 av_freep(&subtitle_out);
00402
00403
00404 for (i = 0; i < nb_output_files; i++) {
00405 AVFormatContext *s = output_files[i]->ctx;
00406 if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
00407 avio_close(s->pb);
00408 avformat_free_context(s);
00409 av_dict_free(&output_files[i]->opts);
00410 av_freep(&output_files[i]);
00411 }
00412 for (i = 0; i < nb_output_streams; i++) {
00413 AVBitStreamFilterContext *bsfc = output_streams[i]->bitstream_filters;
00414 while (bsfc) {
00415 AVBitStreamFilterContext *next = bsfc->next;
00416 av_bitstream_filter_close(bsfc);
00417 bsfc = next;
00418 }
00419 output_streams[i]->bitstream_filters = NULL;
00420 avcodec_free_frame(&output_streams[i]->filtered_frame);
00421
00422 av_freep(&output_streams[i]->forced_keyframes);
00423 av_freep(&output_streams[i]->avfilter);
00424 av_freep(&output_streams[i]->logfile_prefix);
00425 av_freep(&output_streams[i]);
00426 }
00427 for (i = 0; i < nb_input_files; i++) {
00428 avformat_close_input(&input_files[i]->ctx);
00429 av_freep(&input_files[i]);
00430 }
00431 for (i = 0; i < nb_input_streams; i++) {
00432 avcodec_free_frame(&input_streams[i]->decoded_frame);
00433 av_dict_free(&input_streams[i]->opts);
00434 free_buffer_pool(&input_streams[i]->buffer_pool);
00435 avfilter_unref_bufferp(&input_streams[i]->sub2video.ref);
00436 av_freep(&input_streams[i]->filters);
00437 av_freep(&input_streams[i]);
00438 }
00439
00440 if (vstats_file)
00441 fclose(vstats_file);
00442 av_free(vstats_filename);
00443
00444 av_freep(&input_streams);
00445 av_freep(&input_files);
00446 av_freep(&output_streams);
00447 av_freep(&output_files);
00448
00449 uninit_opts();
00450
00451 avfilter_uninit();
00452 avformat_network_deinit();
00453
00454 if (received_sigterm) {
00455 av_log(NULL, AV_LOG_INFO, "Received signal %d: terminating.\n",
00456 (int) received_sigterm);
00457 exit (255);
00458 }
00459
00460 exit(ret);
00461 }
00462
00463 void assert_avoptions(AVDictionary *m)
00464 {
00465 AVDictionaryEntry *t;
00466 if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
00467 av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
00468 exit_program(1);
00469 }
00470 }
00471
00472 static void assert_codec_experimental(AVCodecContext *c, int encoder)
00473 {
00474 const char *codec_string = encoder ? "encoder" : "decoder";
00475 AVCodec *codec;
00476 if (c->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
00477 c->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
00478 av_log(NULL, AV_LOG_FATAL, "%s '%s' is experimental and might produce bad "
00479 "results.\nAdd '-strict experimental' if you want to use it.\n",
00480 codec_string, c->codec->name);
00481 codec = encoder ? avcodec_find_encoder(c->codec->id) : avcodec_find_decoder(c->codec->id);
00482 if (!(codec->capabilities & CODEC_CAP_EXPERIMENTAL))
00483 av_log(NULL, AV_LOG_FATAL, "Or use the non experimental %s '%s'.\n",
00484 codec_string, codec->name);
00485 exit_program(1);
00486 }
00487 }
00488
00489 static void update_benchmark(const char *fmt, ...)
00490 {
00491 if (do_benchmark_all) {
00492 int64_t t = getutime();
00493 va_list va;
00494 char buf[1024];
00495
00496 if (fmt) {
00497 va_start(va, fmt);
00498 vsnprintf(buf, sizeof(buf), fmt, va);
00499 va_end(va);
00500 printf("bench: %8"PRIu64" %s \n", t - current_time, buf);
00501 }
00502 current_time = t;
00503 }
00504 }
00505
00506 static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
00507 {
00508 AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
00509 AVCodecContext *avctx = ost->st->codec;
00510 int ret;
00511
00512 if ((avctx->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) ||
00513 (avctx->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0))
00514 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
00515
00516 if ((avctx->codec_type == AVMEDIA_TYPE_AUDIO || avctx->codec_type == AVMEDIA_TYPE_VIDEO) && pkt->dts != AV_NOPTS_VALUE) {
00517 int64_t max = ost->st->cur_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
00518 if (ost->st->cur_dts && ost->st->cur_dts != AV_NOPTS_VALUE && max > pkt->dts) {
00519 av_log(s, max - pkt->dts > 2 || avctx->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG,
00520 "st:%d PTS: %"PRId64" DTS: %"PRId64" < %"PRId64" invalid, clipping\n", pkt->stream_index, pkt->pts, pkt->dts, max);
00521 if(pkt->pts >= pkt->dts)
00522 pkt->pts = FFMAX(pkt->pts, max);
00523 pkt->dts = max;
00524 }
00525 }
00526
00527
00528
00529
00530
00531
00532
00533
00534 if (!(avctx->codec_type == AVMEDIA_TYPE_VIDEO && avctx->codec)) {
00535 if (ost->frame_number >= ost->max_frames) {
00536 av_free_packet(pkt);
00537 return;
00538 }
00539 ost->frame_number++;
00540 }
00541
00542 while (bsfc) {
00543 AVPacket new_pkt = *pkt;
00544 int a = av_bitstream_filter_filter(bsfc, avctx, NULL,
00545 &new_pkt.data, &new_pkt.size,
00546 pkt->data, pkt->size,
00547 pkt->flags & AV_PKT_FLAG_KEY);
00548 if(a == 0 && new_pkt.data != pkt->data && new_pkt.destruct) {
00549 uint8_t *t = av_malloc(new_pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
00550 if(t) {
00551 memcpy(t, new_pkt.data, new_pkt.size);
00552 memset(t + new_pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00553 new_pkt.data = t;
00554 a = 1;
00555 } else
00556 a = AVERROR(ENOMEM);
00557 }
00558 if (a > 0) {
00559 av_free_packet(pkt);
00560 new_pkt.destruct = av_destruct_packet;
00561 } else if (a < 0) {
00562 av_log(NULL, AV_LOG_ERROR, "Failed to open bitstream filter %s for stream %d with codec %s",
00563 bsfc->filter->name, pkt->stream_index,
00564 avctx->codec ? avctx->codec->name : "copy");
00565 print_error("", a);
00566 if (exit_on_error)
00567 exit_program(1);
00568 }
00569 *pkt = new_pkt;
00570
00571 bsfc = bsfc->next;
00572 }
00573
00574 pkt->stream_index = ost->index;
00575
00576 if (debug_ts) {
00577 av_log(NULL, AV_LOG_INFO, "muxer <- type:%s "
00578 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
00579 av_get_media_type_string(ost->st->codec->codec_type),
00580 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
00581 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base)
00582 );
00583 }
00584
00585 ret = av_interleaved_write_frame(s, pkt);
00586 if (ret < 0) {
00587 print_error("av_interleaved_write_frame()", ret);
00588 exit_program(1);
00589 }
00590 }
00591
00592 static void close_output_stream(OutputStream *ost)
00593 {
00594 OutputFile *of = output_files[ost->file_index];
00595
00596 ost->finished = 1;
00597 if (of->shortest) {
00598 int i;
00599 for (i = 0; i < of->ctx->nb_streams; i++)
00600 output_streams[of->ost_index + i]->finished = 1;
00601 }
00602 }
00603
00604 static int check_recording_time(OutputStream *ost)
00605 {
00606 OutputFile *of = output_files[ost->file_index];
00607
00608 if (of->recording_time != INT64_MAX &&
00609 av_compare_ts(ost->sync_opts - ost->first_pts, ost->st->codec->time_base, of->recording_time,
00610 AV_TIME_BASE_Q) >= 0) {
00611 close_output_stream(ost);
00612 return 0;
00613 }
00614 return 1;
00615 }
00616
00617 static void do_audio_out(AVFormatContext *s, OutputStream *ost,
00618 AVFrame *frame)
00619 {
00620 AVCodecContext *enc = ost->st->codec;
00621 AVPacket pkt;
00622 int got_packet = 0;
00623
00624 av_init_packet(&pkt);
00625 pkt.data = NULL;
00626 pkt.size = 0;
00627
00628 if (!check_recording_time(ost))
00629 return;
00630
00631 if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
00632 frame->pts = ost->sync_opts;
00633 ost->sync_opts = frame->pts + frame->nb_samples;
00634
00635 av_assert0(pkt.size || !pkt.data);
00636 update_benchmark(NULL);
00637 if (avcodec_encode_audio2(enc, &pkt, frame, &got_packet) < 0) {
00638 av_log(NULL, AV_LOG_FATAL, "Audio encoding failed (avcodec_encode_audio2)\n");
00639 exit_program(1);
00640 }
00641 update_benchmark("encode_audio %d.%d", ost->file_index, ost->index);
00642
00643 if (got_packet) {
00644 if (pkt.pts != AV_NOPTS_VALUE)
00645 pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
00646 if (pkt.dts != AV_NOPTS_VALUE)
00647 pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
00648 if (pkt.duration > 0)
00649 pkt.duration = av_rescale_q(pkt.duration, enc->time_base, ost->st->time_base);
00650
00651 if (debug_ts) {
00652 av_log(NULL, AV_LOG_INFO, "encoder -> type:audio "
00653 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
00654 av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->st->time_base),
00655 av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base));
00656 }
00657
00658 audio_size += pkt.size;
00659 write_frame(s, &pkt, ost);
00660
00661 av_free_packet(&pkt);
00662 }
00663 }
00664
00665 static void pre_process_video_frame(InputStream *ist, AVPicture *picture, void **bufp)
00666 {
00667 AVCodecContext *dec;
00668 AVPicture *picture2;
00669 AVPicture picture_tmp;
00670 uint8_t *buf = 0;
00671
00672 dec = ist->st->codec;
00673
00674
00675 if (do_deinterlace) {
00676 int size;
00677
00678
00679 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
00680 buf = av_malloc(size);
00681 if (!buf)
00682 return;
00683
00684 picture2 = &picture_tmp;
00685 avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
00686
00687 if (avpicture_deinterlace(picture2, picture,
00688 dec->pix_fmt, dec->width, dec->height) < 0) {
00689
00690 av_log(NULL, AV_LOG_WARNING, "Deinterlacing failed\n");
00691 av_free(buf);
00692 buf = NULL;
00693 picture2 = picture;
00694 }
00695 } else {
00696 picture2 = picture;
00697 }
00698
00699 if (picture != picture2)
00700 *picture = *picture2;
00701 *bufp = buf;
00702 }
00703
00704 static void do_subtitle_out(AVFormatContext *s,
00705 OutputStream *ost,
00706 InputStream *ist,
00707 AVSubtitle *sub)
00708 {
00709 int subtitle_out_max_size = 1024 * 1024;
00710 int subtitle_out_size, nb, i;
00711 AVCodecContext *enc;
00712 AVPacket pkt;
00713 int64_t pts;
00714
00715 if (sub->pts == AV_NOPTS_VALUE) {
00716 av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
00717 if (exit_on_error)
00718 exit_program(1);
00719 return;
00720 }
00721
00722 enc = ost->st->codec;
00723
00724 if (!subtitle_out) {
00725 subtitle_out = av_malloc(subtitle_out_max_size);
00726 }
00727
00728
00729
00730
00731 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
00732 nb = 2;
00733 else
00734 nb = 1;
00735
00736
00737 pts = sub->pts - output_files[ost->file_index]->start_time;
00738 for (i = 0; i < nb; i++) {
00739 ost->sync_opts = av_rescale_q(pts, AV_TIME_BASE_Q, enc->time_base);
00740 if (!check_recording_time(ost))
00741 return;
00742
00743 sub->pts = pts;
00744
00745 sub->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
00746 sub->end_display_time -= sub->start_display_time;
00747 sub->start_display_time = 0;
00748 if (i == 1)
00749 sub->num_rects = 0;
00750 subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
00751 subtitle_out_max_size, sub);
00752 if (subtitle_out_size < 0) {
00753 av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
00754 exit_program(1);
00755 }
00756
00757 av_init_packet(&pkt);
00758 pkt.data = subtitle_out;
00759 pkt.size = subtitle_out_size;
00760 pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base);
00761 pkt.duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->st->time_base);
00762 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
00763
00764
00765 if (i == 0)
00766 pkt.pts += 90 * sub->start_display_time;
00767 else
00768 pkt.pts += 90 * sub->end_display_time;
00769 }
00770 subtitle_size += pkt.size;
00771 write_frame(s, &pkt, ost);
00772 }
00773 }
00774
00775 static void do_video_out(AVFormatContext *s,
00776 OutputStream *ost,
00777 AVFrame *in_picture,
00778 float quality)
00779 {
00780 int ret, format_video_sync;
00781 AVPacket pkt;
00782 AVCodecContext *enc = ost->st->codec;
00783 int nb_frames, i;
00784 double sync_ipts, delta;
00785 double duration = 0;
00786 int frame_size = 0;
00787 InputStream *ist = NULL;
00788
00789 if (ost->source_index >= 0)
00790 ist = input_streams[ost->source_index];
00791
00792 if(ist && ist->st->start_time != AV_NOPTS_VALUE && ist->st->first_dts != AV_NOPTS_VALUE && ost->frame_rate.num)
00793 duration = 1/(av_q2d(ost->frame_rate) * av_q2d(enc->time_base));
00794
00795 sync_ipts = in_picture->pts;
00796 delta = sync_ipts - ost->sync_opts + duration;
00797
00798
00799 nb_frames = 1;
00800
00801 format_video_sync = video_sync_method;
00802 if (format_video_sync == VSYNC_AUTO)
00803 format_video_sync = (s->oformat->flags & AVFMT_VARIABLE_FPS) ? ((s->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH : VSYNC_VFR) : 1;
00804
00805 switch (format_video_sync) {
00806 case VSYNC_CFR:
00807
00808 if (delta < -1.1)
00809 nb_frames = 0;
00810 else if (delta > 1.1)
00811 nb_frames = lrintf(delta);
00812 break;
00813 case VSYNC_VFR:
00814 if (delta <= -0.6)
00815 nb_frames = 0;
00816 else if (delta > 0.6)
00817 ost->sync_opts = lrint(sync_ipts);
00818 break;
00819 case VSYNC_DROP:
00820 case VSYNC_PASSTHROUGH:
00821 ost->sync_opts = lrint(sync_ipts);
00822 break;
00823 default:
00824 av_assert0(0);
00825 }
00826
00827 nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number);
00828 if (nb_frames == 0) {
00829 nb_frames_drop++;
00830 av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n");
00831 return;
00832 } else if (nb_frames > 1) {
00833 if (nb_frames > dts_error_threshold * 30) {
00834 av_log(NULL, AV_LOG_ERROR, "%d frame duplication too large, skipping\n", nb_frames - 1);
00835 nb_frames_drop++;
00836 return;
00837 }
00838 nb_frames_dup += nb_frames - 1;
00839 av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", nb_frames - 1);
00840 }
00841
00842
00843 for (i = 0; i < nb_frames; i++) {
00844 av_init_packet(&pkt);
00845 pkt.data = NULL;
00846 pkt.size = 0;
00847
00848 in_picture->pts = ost->sync_opts;
00849
00850 if (!check_recording_time(ost))
00851 return;
00852
00853 if (s->oformat->flags & AVFMT_RAWPICTURE &&
00854 enc->codec->id == AV_CODEC_ID_RAWVIDEO) {
00855
00856
00857
00858 enc->coded_frame->interlaced_frame = in_picture->interlaced_frame;
00859 enc->coded_frame->top_field_first = in_picture->top_field_first;
00860 pkt.data = (uint8_t *)in_picture;
00861 pkt.size = sizeof(AVPicture);
00862 pkt.pts = av_rescale_q(in_picture->pts, enc->time_base, ost->st->time_base);
00863 pkt.flags |= AV_PKT_FLAG_KEY;
00864
00865 video_size += pkt.size;
00866 write_frame(s, &pkt, ost);
00867 } else {
00868 int got_packet;
00869 AVFrame big_picture;
00870
00871 big_picture = *in_picture;
00872
00873
00874 big_picture.interlaced_frame = in_picture->interlaced_frame;
00875 if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)) {
00876 if (ost->top_field_first == -1)
00877 big_picture.top_field_first = in_picture->top_field_first;
00878 else
00879 big_picture.top_field_first = !!ost->top_field_first;
00880 }
00881
00882
00883
00884 big_picture.quality = quality;
00885 if (!enc->me_threshold)
00886 big_picture.pict_type = 0;
00887 if (ost->forced_kf_index < ost->forced_kf_count &&
00888 big_picture.pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
00889 big_picture.pict_type = AV_PICTURE_TYPE_I;
00890 ost->forced_kf_index++;
00891 }
00892 update_benchmark(NULL);
00893 ret = avcodec_encode_video2(enc, &pkt, &big_picture, &got_packet);
00894 update_benchmark("encode_video %d.%d", ost->file_index, ost->index);
00895 if (ret < 0) {
00896 av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
00897 exit_program(1);
00898 }
00899
00900 if (got_packet) {
00901 if (pkt.pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & CODEC_CAP_DELAY))
00902 pkt.pts = ost->sync_opts;
00903
00904 if (pkt.pts != AV_NOPTS_VALUE)
00905 pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
00906 if (pkt.dts != AV_NOPTS_VALUE)
00907 pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
00908
00909 if (debug_ts) {
00910 av_log(NULL, AV_LOG_INFO, "encoder -> type:video "
00911 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
00912 av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->st->time_base),
00913 av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base));
00914 }
00915
00916 frame_size = pkt.size;
00917 video_size += pkt.size;
00918 write_frame(s, &pkt, ost);
00919 av_free_packet(&pkt);
00920
00921
00922 if (ost->logfile && enc->stats_out) {
00923 fprintf(ost->logfile, "%s", enc->stats_out);
00924 }
00925 }
00926 }
00927 ost->sync_opts++;
00928
00929
00930
00931
00932
00933 ost->frame_number++;
00934 }
00935
00936 if (vstats_filename && frame_size)
00937 do_video_stats(output_files[ost->file_index]->ctx, ost, frame_size);
00938 }
00939
00940 static double psnr(double d)
00941 {
00942 return -10.0 * log(d) / log(10.0);
00943 }
00944
00945 static void do_video_stats(AVFormatContext *os, OutputStream *ost,
00946 int frame_size)
00947 {
00948 AVCodecContext *enc;
00949 int frame_number;
00950 double ti1, bitrate, avg_bitrate;
00951
00952
00953 if (!vstats_file) {
00954 vstats_file = fopen(vstats_filename, "w");
00955 if (!vstats_file) {
00956 perror("fopen");
00957 exit_program(1);
00958 }
00959 }
00960
00961 enc = ost->st->codec;
00962 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
00963 frame_number = ost->frame_number;
00964 fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality / (float)FF_QP2LAMBDA);
00965 if (enc->flags&CODEC_FLAG_PSNR)
00966 fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
00967
00968 fprintf(vstats_file,"f_size= %6d ", frame_size);
00969
00970 ti1 = ost->sync_opts * av_q2d(enc->time_base);
00971 if (ti1 < 0.01)
00972 ti1 = 0.01;
00973
00974 bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
00975 avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
00976 fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
00977 (double)video_size / 1024, ti1, bitrate, avg_bitrate);
00978 fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type));
00979 }
00980 }
00981
00988 static int reap_filters(void)
00989 {
00990 AVFilterBufferRef *picref;
00991 AVFrame *filtered_frame = NULL;
00992 int i;
00993 int64_t frame_pts;
00994
00995
00996 for (i = 0; i < nb_output_streams; i++) {
00997 OutputStream *ost = output_streams[i];
00998 OutputFile *of = output_files[ost->file_index];
00999 int ret = 0;
01000
01001 if (!ost->filter)
01002 continue;
01003
01004 if (!ost->filtered_frame && !(ost->filtered_frame = avcodec_alloc_frame())) {
01005 return AVERROR(ENOMEM);
01006 } else
01007 avcodec_get_frame_defaults(ost->filtered_frame);
01008 filtered_frame = ost->filtered_frame;
01009
01010 while (1) {
01011 ret = av_buffersink_get_buffer_ref(ost->filter->filter, &picref,
01012 AV_BUFFERSINK_FLAG_NO_REQUEST);
01013 if (ret < 0) {
01014 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
01015 char buf[256];
01016 av_strerror(ret, buf, sizeof(buf));
01017 av_log(NULL, AV_LOG_WARNING,
01018 "Error in av_buffersink_get_buffer_ref(): %s\n", buf);
01019 }
01020 break;
01021 }
01022 frame_pts = AV_NOPTS_VALUE;
01023 if (picref->pts != AV_NOPTS_VALUE) {
01024 filtered_frame->pts = frame_pts = av_rescale_q(picref->pts,
01025 ost->filter->filter->inputs[0]->time_base,
01026 ost->st->codec->time_base) -
01027 av_rescale_q(of->start_time,
01028 AV_TIME_BASE_Q,
01029 ost->st->codec->time_base);
01030
01031 if (of->start_time && filtered_frame->pts < 0) {
01032 avfilter_unref_buffer(picref);
01033 continue;
01034 }
01035 }
01036
01037
01038
01039
01040 switch (ost->filter->filter->inputs[0]->type) {
01041 case AVMEDIA_TYPE_VIDEO:
01042 avfilter_copy_buf_props(filtered_frame, picref);
01043 filtered_frame->pts = frame_pts;
01044 if (!ost->frame_aspect_ratio)
01045 ost->st->codec->sample_aspect_ratio = picref->video->sample_aspect_ratio;
01046
01047 do_video_out(of->ctx, ost, filtered_frame,
01048 same_quant ? ost->last_quality :
01049 ost->st->codec->global_quality);
01050 break;
01051 case AVMEDIA_TYPE_AUDIO:
01052 avfilter_copy_buf_props(filtered_frame, picref);
01053 filtered_frame->pts = frame_pts;
01054 do_audio_out(of->ctx, ost, filtered_frame);
01055 break;
01056 default:
01057
01058 av_assert0(0);
01059 }
01060
01061 avfilter_unref_buffer(picref);
01062 }
01063 }
01064
01065 return 0;
01066 }
01067
01068 static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time)
01069 {
01070 char buf[1024];
01071 AVBPrint buf_script;
01072 OutputStream *ost;
01073 AVFormatContext *oc;
01074 int64_t total_size;
01075 AVCodecContext *enc;
01076 int frame_number, vid, i;
01077 double bitrate;
01078 int64_t pts = INT64_MIN;
01079 static int64_t last_time = -1;
01080 static int qp_histogram[52];
01081 int hours, mins, secs, us;
01082
01083 if (!print_stats && !is_last_report && !progress_avio)
01084 return;
01085
01086 if (!is_last_report) {
01087 if (last_time == -1) {
01088 last_time = cur_time;
01089 return;
01090 }
01091 if ((cur_time - last_time) < 500000)
01092 return;
01093 last_time = cur_time;
01094 }
01095
01096
01097 oc = output_files[0]->ctx;
01098
01099 total_size = avio_size(oc->pb);
01100 if (total_size < 0) {
01101 total_size = avio_tell(oc->pb);
01102 if (total_size < 0)
01103 total_size = 0;
01104 }
01105
01106 buf[0] = '\0';
01107 vid = 0;
01108 av_bprint_init(&buf_script, 0, 1);
01109 for (i = 0; i < nb_output_streams; i++) {
01110 float q = -1;
01111 ost = output_streams[i];
01112 enc = ost->st->codec;
01113 if (!ost->stream_copy && enc->coded_frame)
01114 q = enc->coded_frame->quality / (float)FF_QP2LAMBDA;
01115 if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
01116 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q);
01117 av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
01118 ost->file_index, ost->index, q);
01119 }
01120 if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
01121 float fps, t = (cur_time-timer_start) / 1000000.0;
01122
01123 frame_number = ost->frame_number;
01124 fps = t > 1 ? frame_number / t : 0;
01125 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3.*f q=%3.1f ",
01126 frame_number, fps < 9.95, fps, q);
01127 av_bprintf(&buf_script, "frame=%d\n", frame_number);
01128 av_bprintf(&buf_script, "fps=%.1f\n", fps);
01129 av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
01130 ost->file_index, ost->index, q);
01131 if (is_last_report)
01132 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
01133 if (qp_hist) {
01134 int j;
01135 int qp = lrintf(q);
01136 if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
01137 qp_histogram[qp]++;
01138 for (j = 0; j < 32; j++)
01139 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log2(qp_histogram[j] + 1)));
01140 }
01141 if (enc->flags&CODEC_FLAG_PSNR) {
01142 int j;
01143 double error, error_sum = 0;
01144 double scale, scale_sum = 0;
01145 double p;
01146 char type[3] = { 'Y','U','V' };
01147 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
01148 for (j = 0; j < 3; j++) {
01149 if (is_last_report) {
01150 error = enc->error[j];
01151 scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
01152 } else {
01153 error = enc->coded_frame->error[j];
01154 scale = enc->width * enc->height * 255.0 * 255.0;
01155 }
01156 if (j)
01157 scale /= 4;
01158 error_sum += error;
01159 scale_sum += scale;
01160 p = psnr(error / scale);
01161 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], p);
01162 av_bprintf(&buf_script, "stream_%d_%d_psnr_%c=%2.2f\n",
01163 ost->file_index, ost->index, type[i] | 32, p);
01164 }
01165 p = psnr(error_sum / scale_sum);
01166 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum / scale_sum));
01167 av_bprintf(&buf_script, "stream_%d_%d_psnr_all=%2.2f\n",
01168 ost->file_index, ost->index, p);
01169 }
01170 vid = 1;
01171 }
01172
01173 if ((is_last_report || !ost->finished) && ost->st->pts.val != AV_NOPTS_VALUE)
01174 pts = FFMAX(pts, av_rescale_q(ost->st->pts.val,
01175 ost->st->time_base, AV_TIME_BASE_Q));
01176 }
01177
01178 secs = pts / AV_TIME_BASE;
01179 us = pts % AV_TIME_BASE;
01180 mins = secs / 60;
01181 secs %= 60;
01182 hours = mins / 60;
01183 mins %= 60;
01184
01185 bitrate = pts ? total_size * 8 / (pts / 1000.0) : 0;
01186
01187 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
01188 "size=%8.0fkB time=", total_size / 1024.0);
01189 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
01190 "%02d:%02d:%02d.%02d ", hours, mins, secs,
01191 (100 * us) / AV_TIME_BASE);
01192 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
01193 "bitrate=%6.1fkbits/s", bitrate);
01194 av_bprintf(&buf_script, "total_size=%"PRId64"\n", total_size);
01195 av_bprintf(&buf_script, "out_time_ms=%"PRId64"\n", pts);
01196 av_bprintf(&buf_script, "out_time=%02d:%02d:%02d.%06d\n",
01197 hours, mins, secs, us);
01198
01199 if (nb_frames_dup || nb_frames_drop)
01200 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
01201 nb_frames_dup, nb_frames_drop);
01202 av_bprintf(&buf_script, "dup_frames=%d\n", nb_frames_dup);
01203 av_bprintf(&buf_script, "drop_frames=%d\n", nb_frames_drop);
01204
01205 if (print_stats || is_last_report) {
01206 av_log(NULL, AV_LOG_INFO, "%s \r", buf);
01207
01208 fflush(stderr);
01209 }
01210
01211 if (progress_avio) {
01212 av_bprintf(&buf_script, "progress=%s\n",
01213 is_last_report ? "end" : "continue");
01214 avio_write(progress_avio, buf_script.str,
01215 FFMIN(buf_script.len, buf_script.size - 1));
01216 avio_flush(progress_avio);
01217 av_bprint_finalize(&buf_script, NULL);
01218 if (is_last_report) {
01219 avio_close(progress_avio);
01220 progress_avio = NULL;
01221 }
01222 }
01223
01224 if (is_last_report) {
01225 int64_t raw= audio_size + video_size + subtitle_size + extra_size;
01226 av_log(NULL, AV_LOG_INFO, "\n");
01227 av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB subtitle:%1.0f global headers:%1.0fkB muxing overhead %f%%\n",
01228 video_size / 1024.0,
01229 audio_size / 1024.0,
01230 subtitle_size / 1024.0,
01231 extra_size / 1024.0,
01232 100.0 * (total_size - raw) / raw
01233 );
01234 if(video_size + audio_size + subtitle_size + extra_size == 0){
01235 av_log(NULL, AV_LOG_WARNING, "Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)\n");
01236 }
01237 }
01238 }
01239
01240 static void flush_encoders(void)
01241 {
01242 int i, ret;
01243
01244 for (i = 0; i < nb_output_streams; i++) {
01245 OutputStream *ost = output_streams[i];
01246 AVCodecContext *enc = ost->st->codec;
01247 AVFormatContext *os = output_files[ost->file_index]->ctx;
01248 int stop_encoding = 0;
01249
01250 if (!ost->encoding_needed)
01251 continue;
01252
01253 if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
01254 continue;
01255 if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == AV_CODEC_ID_RAWVIDEO)
01256 continue;
01257
01258 for (;;) {
01259 int (*encode)(AVCodecContext*, AVPacket*, const AVFrame*, int*) = NULL;
01260 const char *desc;
01261 int64_t *size;
01262
01263 switch (ost->st->codec->codec_type) {
01264 case AVMEDIA_TYPE_AUDIO:
01265 encode = avcodec_encode_audio2;
01266 desc = "Audio";
01267 size = &audio_size;
01268 break;
01269 case AVMEDIA_TYPE_VIDEO:
01270 encode = avcodec_encode_video2;
01271 desc = "Video";
01272 size = &video_size;
01273 break;
01274 default:
01275 stop_encoding = 1;
01276 }
01277
01278 if (encode) {
01279 AVPacket pkt;
01280 int got_packet;
01281 av_init_packet(&pkt);
01282 pkt.data = NULL;
01283 pkt.size = 0;
01284
01285 update_benchmark(NULL);
01286 ret = encode(enc, &pkt, NULL, &got_packet);
01287 update_benchmark("flush %s %d.%d", desc, ost->file_index, ost->index);
01288 if (ret < 0) {
01289 av_log(NULL, AV_LOG_FATAL, "%s encoding failed\n", desc);
01290 exit_program(1);
01291 }
01292 *size += pkt.size;
01293 if (ost->logfile && enc->stats_out) {
01294 fprintf(ost->logfile, "%s", enc->stats_out);
01295 }
01296 if (!got_packet) {
01297 stop_encoding = 1;
01298 break;
01299 }
01300 if (pkt.pts != AV_NOPTS_VALUE)
01301 pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
01302 if (pkt.dts != AV_NOPTS_VALUE)
01303 pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
01304 write_frame(os, &pkt, ost);
01305 }
01306
01307 if (stop_encoding)
01308 break;
01309 }
01310 }
01311 }
01312
01313
01314
01315
01316 static int check_output_constraints(InputStream *ist, OutputStream *ost)
01317 {
01318 OutputFile *of = output_files[ost->file_index];
01319 int ist_index = input_files[ist->file_index]->ist_index + ist->st->index;
01320
01321 if (ost->source_index != ist_index)
01322 return 0;
01323
01324 if (of->start_time && ist->pts < of->start_time)
01325 return 0;
01326
01327 return 1;
01328 }
01329
01330 static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
01331 {
01332 OutputFile *of = output_files[ost->file_index];
01333 int64_t ost_tb_start_time = av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base);
01334 AVPicture pict;
01335 AVPacket opkt;
01336
01337 av_init_packet(&opkt);
01338
01339 if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
01340 !ost->copy_initial_nonkeyframes)
01341 return;
01342
01343 if (!ost->frame_number && ist->pts < of->start_time &&
01344 !ost->copy_prior_start)
01345 return;
01346
01347 if (of->recording_time != INT64_MAX &&
01348 ist->pts >= of->recording_time + of->start_time) {
01349 close_output_stream(ost);
01350 return;
01351 }
01352
01353
01354 if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
01355 audio_size += pkt->size;
01356 else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01357 video_size += pkt->size;
01358 ost->sync_opts++;
01359 } else if (ost->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
01360 subtitle_size += pkt->size;
01361 }
01362
01363 if (pkt->pts != AV_NOPTS_VALUE)
01364 opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;
01365 else
01366 opkt.pts = AV_NOPTS_VALUE;
01367
01368 if (pkt->dts == AV_NOPTS_VALUE)
01369 opkt.dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->st->time_base);
01370 else
01371 opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);
01372 opkt.dts -= ost_tb_start_time;
01373
01374 opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base);
01375 opkt.flags = pkt->flags;
01376
01377
01378 if ( ost->st->codec->codec_id != AV_CODEC_ID_H264
01379 && ost->st->codec->codec_id != AV_CODEC_ID_MPEG1VIDEO
01380 && ost->st->codec->codec_id != AV_CODEC_ID_MPEG2VIDEO
01381 && ost->st->codec->codec_id != AV_CODEC_ID_VC1
01382 ) {
01383 if (av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY))
01384 opkt.destruct = av_destruct_packet;
01385 } else {
01386 opkt.data = pkt->data;
01387 opkt.size = pkt->size;
01388 }
01389
01390 if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (of->ctx->oformat->flags & AVFMT_RAWPICTURE)) {
01391
01392 avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height);
01393 opkt.data = (uint8_t *)&pict;
01394 opkt.size = sizeof(AVPicture);
01395 opkt.flags |= AV_PKT_FLAG_KEY;
01396 }
01397
01398 write_frame(of->ctx, &opkt, ost);
01399 ost->st->codec->frame_number++;
01400 av_free_packet(&opkt);
01401 }
01402
01403 static void rate_emu_sleep(InputStream *ist)
01404 {
01405 if (input_files[ist->file_index]->rate_emu) {
01406 int64_t pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE);
01407 int64_t now = av_gettime() - ist->start;
01408 if (pts > now)
01409 av_usleep(pts - now);
01410 }
01411 }
01412
01413 int guess_input_channel_layout(InputStream *ist)
01414 {
01415 AVCodecContext *dec = ist->st->codec;
01416
01417 if (!dec->channel_layout) {
01418 char layout_name[256];
01419
01420 dec->channel_layout = av_get_default_channel_layout(dec->channels);
01421 if (!dec->channel_layout)
01422 return 0;
01423 av_get_channel_layout_string(layout_name, sizeof(layout_name),
01424 dec->channels, dec->channel_layout);
01425 av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for Input Stream "
01426 "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
01427 }
01428 return 1;
01429 }
01430
01431 static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
01432 {
01433 AVFrame *decoded_frame;
01434 AVCodecContext *avctx = ist->st->codec;
01435 int i, ret, resample_changed;
01436 AVRational decoded_frame_tb;
01437
01438 if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
01439 return AVERROR(ENOMEM);
01440 else
01441 avcodec_get_frame_defaults(ist->decoded_frame);
01442 decoded_frame = ist->decoded_frame;
01443
01444 update_benchmark(NULL);
01445 ret = avcodec_decode_audio4(avctx, decoded_frame, got_output, pkt);
01446 update_benchmark("decode_audio %d.%d", ist->file_index, ist->st->index);
01447
01448 if (ret >= 0 && avctx->sample_rate <= 0) {
01449 av_log(avctx, AV_LOG_ERROR, "Sample rate %d invalid\n", avctx->sample_rate);
01450 ret = AVERROR_INVALIDDATA;
01451 }
01452
01453 if (!*got_output || ret < 0) {
01454 if (!pkt->size) {
01455 for (i = 0; i < ist->nb_filters; i++)
01456 av_buffersrc_add_ref(ist->filters[i]->filter, NULL, 0);
01457 }
01458 return ret;
01459 }
01460
01461 #if 1
01462
01463
01464 ist->next_pts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
01465 avctx->sample_rate;
01466 ist->next_dts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
01467 avctx->sample_rate;
01468 #endif
01469
01470 rate_emu_sleep(ist);
01471
01472 resample_changed = ist->resample_sample_fmt != decoded_frame->format ||
01473 ist->resample_channels != avctx->channels ||
01474 ist->resample_channel_layout != decoded_frame->channel_layout ||
01475 ist->resample_sample_rate != decoded_frame->sample_rate;
01476 if (resample_changed) {
01477 char layout1[64], layout2[64];
01478
01479 if (!guess_input_channel_layout(ist)) {
01480 av_log(NULL, AV_LOG_FATAL, "Unable to find default channel "
01481 "layout for Input Stream #%d.%d\n", ist->file_index,
01482 ist->st->index);
01483 exit_program(1);
01484 }
01485 decoded_frame->channel_layout = avctx->channel_layout;
01486
01487 av_get_channel_layout_string(layout1, sizeof(layout1), ist->resample_channels,
01488 ist->resample_channel_layout);
01489 av_get_channel_layout_string(layout2, sizeof(layout2), avctx->channels,
01490 decoded_frame->channel_layout);
01491
01492 av_log(NULL, AV_LOG_INFO,
01493 "Input stream #%d:%d frame changed from rate:%d fmt:%s ch:%d chl:%s to rate:%d fmt:%s ch:%d chl:%s\n",
01494 ist->file_index, ist->st->index,
01495 ist->resample_sample_rate, av_get_sample_fmt_name(ist->resample_sample_fmt),
01496 ist->resample_channels, layout1,
01497 decoded_frame->sample_rate, av_get_sample_fmt_name(decoded_frame->format),
01498 avctx->channels, layout2);
01499
01500 ist->resample_sample_fmt = decoded_frame->format;
01501 ist->resample_sample_rate = decoded_frame->sample_rate;
01502 ist->resample_channel_layout = decoded_frame->channel_layout;
01503 ist->resample_channels = avctx->channels;
01504
01505 for (i = 0; i < nb_filtergraphs; i++)
01506 if (ist_in_filtergraph(filtergraphs[i], ist)) {
01507 FilterGraph *fg = filtergraphs[i];
01508 int j;
01509 if (configure_filtergraph(fg) < 0) {
01510 av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n");
01511 exit_program(1);
01512 }
01513 for (j = 0; j < fg->nb_outputs; j++) {
01514 OutputStream *ost = fg->outputs[j]->ost;
01515 if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
01516 !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
01517 av_buffersink_set_frame_size(ost->filter->filter,
01518 ost->st->codec->frame_size);
01519 }
01520 }
01521 }
01522
01523
01524
01525 if (decoded_frame->pts != AV_NOPTS_VALUE) {
01526 ist->dts = ist->next_dts = ist->pts = ist->next_pts = av_rescale_q(decoded_frame->pts, avctx->time_base, AV_TIME_BASE_Q);
01527 decoded_frame_tb = avctx->time_base;
01528 } else if (decoded_frame->pkt_pts != AV_NOPTS_VALUE) {
01529 decoded_frame->pts = decoded_frame->pkt_pts;
01530 pkt->pts = AV_NOPTS_VALUE;
01531 decoded_frame_tb = ist->st->time_base;
01532 } else if (pkt->pts != AV_NOPTS_VALUE) {
01533 decoded_frame->pts = pkt->pts;
01534 pkt->pts = AV_NOPTS_VALUE;
01535 decoded_frame_tb = ist->st->time_base;
01536 }else {
01537 decoded_frame->pts = ist->dts;
01538 decoded_frame_tb = AV_TIME_BASE_Q;
01539 }
01540 if (decoded_frame->pts != AV_NOPTS_VALUE)
01541 decoded_frame->pts = av_rescale_q(decoded_frame->pts,
01542 decoded_frame_tb,
01543 (AVRational){1, ist->st->codec->sample_rate});
01544 for (i = 0; i < ist->nb_filters; i++)
01545 av_buffersrc_add_frame(ist->filters[i]->filter, decoded_frame,
01546 AV_BUFFERSRC_FLAG_PUSH);
01547
01548 decoded_frame->pts = AV_NOPTS_VALUE;
01549
01550 return ret;
01551 }
01552
01553 static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
01554 {
01555 AVFrame *decoded_frame;
01556 void *buffer_to_free = NULL;
01557 int i, ret = 0, resample_changed;
01558 int64_t best_effort_timestamp;
01559 AVRational *frame_sample_aspect;
01560 float quality;
01561
01562 if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
01563 return AVERROR(ENOMEM);
01564 else
01565 avcodec_get_frame_defaults(ist->decoded_frame);
01566 decoded_frame = ist->decoded_frame;
01567 pkt->dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ist->st->time_base);
01568
01569 update_benchmark(NULL);
01570 ret = avcodec_decode_video2(ist->st->codec,
01571 decoded_frame, got_output, pkt);
01572 update_benchmark("decode_video %d.%d", ist->file_index, ist->st->index);
01573 if (!*got_output || ret < 0) {
01574 if (!pkt->size) {
01575 for (i = 0; i < ist->nb_filters; i++)
01576 av_buffersrc_add_ref(ist->filters[i]->filter, NULL, 0);
01577 }
01578 return ret;
01579 }
01580
01581 quality = same_quant ? decoded_frame->quality : 0;
01582
01583 if(ist->top_field_first>=0)
01584 decoded_frame->top_field_first = ist->top_field_first;
01585
01586 best_effort_timestamp= av_frame_get_best_effort_timestamp(decoded_frame);
01587 if(best_effort_timestamp != AV_NOPTS_VALUE)
01588 ist->next_pts = ist->pts = av_rescale_q(decoded_frame->pts = best_effort_timestamp, ist->st->time_base, AV_TIME_BASE_Q);
01589
01590 if (debug_ts) {
01591 av_log(NULL, AV_LOG_INFO, "decoder -> ist_index:%d type:video "
01592 "frame_pts:%s frame_pts_time:%s best_effort_ts:%"PRId64" best_effort_ts_time:%s keyframe:%d frame_type:%d \n",
01593 ist->st->index, av_ts2str(decoded_frame->pts),
01594 av_ts2timestr(decoded_frame->pts, &ist->st->time_base),
01595 best_effort_timestamp,
01596 av_ts2timestr(best_effort_timestamp, &ist->st->time_base),
01597 decoded_frame->key_frame, decoded_frame->pict_type);
01598 }
01599
01600 pkt->size = 0;
01601 pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
01602
01603 rate_emu_sleep(ist);
01604
01605 if (ist->st->sample_aspect_ratio.num)
01606 decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
01607
01608 resample_changed = ist->resample_width != decoded_frame->width ||
01609 ist->resample_height != decoded_frame->height ||
01610 ist->resample_pix_fmt != decoded_frame->format;
01611 if (resample_changed) {
01612 av_log(NULL, AV_LOG_INFO,
01613 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
01614 ist->file_index, ist->st->index,
01615 ist->resample_width, ist->resample_height, av_get_pix_fmt_name(ist->resample_pix_fmt),
01616 decoded_frame->width, decoded_frame->height, av_get_pix_fmt_name(decoded_frame->format));
01617
01618 ist->resample_width = decoded_frame->width;
01619 ist->resample_height = decoded_frame->height;
01620 ist->resample_pix_fmt = decoded_frame->format;
01621
01622 for (i = 0; i < nb_filtergraphs; i++)
01623 if (ist_in_filtergraph(filtergraphs[i], ist) &&
01624 configure_filtergraph(filtergraphs[i]) < 0) {
01625 av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n");
01626 exit_program(1);
01627 }
01628 }
01629
01630 frame_sample_aspect= av_opt_ptr(avcodec_get_frame_class(), decoded_frame, "sample_aspect_ratio");
01631 for (i = 0; i < ist->nb_filters; i++) {
01632 int changed = ist->st->codec->width != ist->filters[i]->filter->outputs[0]->w
01633 || ist->st->codec->height != ist->filters[i]->filter->outputs[0]->h
01634 || ist->st->codec->pix_fmt != ist->filters[i]->filter->outputs[0]->format;
01635
01636 if (ist->filters[i]->graph->nb_outputs == 1)
01637 ist->filters[i]->graph->outputs[0]->ost->last_quality = quality;
01638
01639 if (!frame_sample_aspect->num)
01640 *frame_sample_aspect = ist->st->sample_aspect_ratio;
01641 if (ist->dr1 && decoded_frame->type==FF_BUFFER_TYPE_USER && !changed) {
01642 FrameBuffer *buf = decoded_frame->opaque;
01643 AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
01644 decoded_frame->data, decoded_frame->linesize,
01645 AV_PERM_READ | AV_PERM_PRESERVE,
01646 ist->st->codec->width, ist->st->codec->height,
01647 ist->st->codec->pix_fmt);
01648
01649 avfilter_copy_frame_props(fb, decoded_frame);
01650 fb->buf->priv = buf;
01651 fb->buf->free = filter_release_buffer;
01652
01653 av_assert0(buf->refcount>0);
01654 buf->refcount++;
01655 av_buffersrc_add_ref(ist->filters[i]->filter, fb,
01656 AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT |
01657 AV_BUFFERSRC_FLAG_NO_COPY |
01658 AV_BUFFERSRC_FLAG_PUSH);
01659 } else
01660 if(av_buffersrc_add_frame(ist->filters[i]->filter, decoded_frame, AV_BUFFERSRC_FLAG_PUSH)<0) {
01661 av_log(NULL, AV_LOG_FATAL, "Failed to inject frame into filter network\n");
01662 exit_program(1);
01663 }
01664
01665 }
01666
01667 av_free(buffer_to_free);
01668 return ret;
01669 }
01670
01671 static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
01672 {
01673 AVSubtitle subtitle;
01674 int i, ret = avcodec_decode_subtitle2(ist->st->codec,
01675 &subtitle, got_output, pkt);
01676 if (ret < 0 || !*got_output) {
01677 if (!pkt->size)
01678 sub2video_flush(ist);
01679 return ret;
01680 }
01681
01682 if (ist->fix_sub_duration) {
01683 if (ist->prev_sub.got_output) {
01684 int end = av_rescale(subtitle.pts - ist->prev_sub.subtitle.pts,
01685 1000, AV_TIME_BASE);
01686 if (end < ist->prev_sub.subtitle.end_display_time) {
01687 av_log(ist->st->codec, AV_LOG_DEBUG,
01688 "Subtitle duration reduced from %d to %d\n",
01689 ist->prev_sub.subtitle.end_display_time, end);
01690 ist->prev_sub.subtitle.end_display_time = end;
01691 }
01692 }
01693 FFSWAP(int, *got_output, ist->prev_sub.got_output);
01694 FFSWAP(int, ret, ist->prev_sub.ret);
01695 FFSWAP(AVSubtitle, subtitle, ist->prev_sub.subtitle);
01696 }
01697
01698 sub2video_update(ist, &subtitle);
01699
01700 if (!*got_output || !subtitle.num_rects)
01701 return ret;
01702
01703 rate_emu_sleep(ist);
01704
01705 for (i = 0; i < nb_output_streams; i++) {
01706 OutputStream *ost = output_streams[i];
01707
01708 if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
01709 continue;
01710
01711 do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle);
01712 }
01713
01714 avsubtitle_free(&subtitle);
01715 return ret;
01716 }
01717
01718
01719 static int output_packet(InputStream *ist, const AVPacket *pkt)
01720 {
01721 int ret = 0, i;
01722 int got_output;
01723
01724 AVPacket avpkt;
01725 if (!ist->saw_first_ts) {
01726 ist->dts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
01727 ist->pts = 0;
01728 if (pkt != NULL && pkt->pts != AV_NOPTS_VALUE && !ist->decoding_needed) {
01729 ist->dts += av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
01730 ist->pts = ist->dts;
01731 }
01732 ist->saw_first_ts = 1;
01733 }
01734
01735 if (ist->next_dts == AV_NOPTS_VALUE)
01736 ist->next_dts = ist->dts;
01737 if (ist->next_pts == AV_NOPTS_VALUE)
01738 ist->next_pts = ist->pts;
01739
01740 if (pkt == NULL) {
01741
01742 av_init_packet(&avpkt);
01743 avpkt.data = NULL;
01744 avpkt.size = 0;
01745 goto handle_eof;
01746 } else {
01747 avpkt = *pkt;
01748 }
01749
01750 if (pkt->dts != AV_NOPTS_VALUE) {
01751 ist->next_dts = ist->dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
01752 if (ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed)
01753 ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
01754 }
01755
01756
01757 while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {
01758 int duration;
01759 handle_eof:
01760
01761 ist->pts = ist->next_pts;
01762 ist->dts = ist->next_dts;
01763
01764 if (avpkt.size && avpkt.size != pkt->size) {
01765 av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,
01766 "Multiple frames in a packet from stream %d\n", pkt->stream_index);
01767 ist->showed_multi_packet_warning = 1;
01768 }
01769
01770 switch (ist->st->codec->codec_type) {
01771 case AVMEDIA_TYPE_AUDIO:
01772 ret = decode_audio (ist, &avpkt, &got_output);
01773 break;
01774 case AVMEDIA_TYPE_VIDEO:
01775 ret = decode_video (ist, &avpkt, &got_output);
01776 if (avpkt.duration) {
01777 duration = av_rescale_q(avpkt.duration, ist->st->time_base, AV_TIME_BASE_Q);
01778 } else if(ist->st->codec->time_base.num != 0 && ist->st->codec->time_base.den != 0) {
01779 int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
01780 duration = ((int64_t)AV_TIME_BASE *
01781 ist->st->codec->time_base.num * ticks) /
01782 ist->st->codec->time_base.den;
01783 } else
01784 duration = 0;
01785
01786 if(ist->dts != AV_NOPTS_VALUE && duration) {
01787 ist->next_dts += duration;
01788 }else
01789 ist->next_dts = AV_NOPTS_VALUE;
01790
01791 if (got_output)
01792 ist->next_pts += duration;
01793 break;
01794 case AVMEDIA_TYPE_SUBTITLE:
01795 ret = transcode_subtitles(ist, &avpkt, &got_output);
01796 break;
01797 default:
01798 return -1;
01799 }
01800
01801 if (ret < 0)
01802 return ret;
01803
01804 avpkt.dts=
01805 avpkt.pts= AV_NOPTS_VALUE;
01806
01807
01808 if (pkt) {
01809 if(ist->st->codec->codec_type != AVMEDIA_TYPE_AUDIO)
01810 ret = avpkt.size;
01811 avpkt.data += ret;
01812 avpkt.size -= ret;
01813 }
01814 if (!got_output) {
01815 continue;
01816 }
01817 }
01818
01819
01820 if (!ist->decoding_needed) {
01821 rate_emu_sleep(ist);
01822 ist->dts = ist->next_dts;
01823 switch (ist->st->codec->codec_type) {
01824 case AVMEDIA_TYPE_AUDIO:
01825 ist->next_dts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
01826 ist->st->codec->sample_rate;
01827 break;
01828 case AVMEDIA_TYPE_VIDEO:
01829 if (pkt->duration) {
01830 ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
01831 } else if(ist->st->codec->time_base.num != 0) {
01832 int ticks= ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->st->codec->ticks_per_frame;
01833 ist->next_dts += ((int64_t)AV_TIME_BASE *
01834 ist->st->codec->time_base.num * ticks) /
01835 ist->st->codec->time_base.den;
01836 }
01837 break;
01838 }
01839 ist->pts = ist->dts;
01840 ist->next_pts = ist->next_dts;
01841 }
01842 for (i = 0; pkt && i < nb_output_streams; i++) {
01843 OutputStream *ost = output_streams[i];
01844
01845 if (!check_output_constraints(ist, ost) || ost->encoding_needed)
01846 continue;
01847
01848 do_streamcopy(ist, ost, pkt);
01849 }
01850
01851 return 0;
01852 }
01853
01854 static void print_sdp(void)
01855 {
01856 char sdp[2048];
01857 int i;
01858 AVFormatContext **avc = av_malloc(sizeof(*avc) * nb_output_files);
01859
01860 if (!avc)
01861 exit_program(1);
01862 for (i = 0; i < nb_output_files; i++)
01863 avc[i] = output_files[i]->ctx;
01864
01865 av_sdp_create(avc, nb_output_files, sdp, sizeof(sdp));
01866 printf("SDP:\n%s\n", sdp);
01867 fflush(stdout);
01868 av_freep(&avc);
01869 }
01870
01871 static int init_input_stream(int ist_index, char *error, int error_len)
01872 {
01873 InputStream *ist = input_streams[ist_index];
01874
01875 if (ist->decoding_needed) {
01876 AVCodec *codec = ist->dec;
01877 if (!codec) {
01878 snprintf(error, error_len, "Decoder (codec %s) not found for input stream #%d:%d",
01879 avcodec_get_name(ist->st->codec->codec_id), ist->file_index, ist->st->index);
01880 return AVERROR(EINVAL);
01881 }
01882
01883 ist->dr1 = (codec->capabilities & CODEC_CAP_DR1) && !do_deinterlace;
01884 if (codec->type == AVMEDIA_TYPE_VIDEO && ist->dr1) {
01885 ist->st->codec->get_buffer = codec_get_buffer;
01886 ist->st->codec->release_buffer = codec_release_buffer;
01887 ist->st->codec->opaque = &ist->buffer_pool;
01888 }
01889
01890 if (!av_dict_get(ist->opts, "threads", NULL, 0))
01891 av_dict_set(&ist->opts, "threads", "auto", 0);
01892 if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) {
01893 snprintf(error, error_len, "Error while opening decoder for input stream #%d:%d",
01894 ist->file_index, ist->st->index);
01895 return AVERROR(EINVAL);
01896 }
01897 assert_codec_experimental(ist->st->codec, 0);
01898 assert_avoptions(ist->opts);
01899 }
01900
01901 ist->next_pts = AV_NOPTS_VALUE;
01902 ist->next_dts = AV_NOPTS_VALUE;
01903 ist->is_start = 1;
01904
01905 return 0;
01906 }
01907
01908 static InputStream *get_input_stream(OutputStream *ost)
01909 {
01910 if (ost->source_index >= 0)
01911 return input_streams[ost->source_index];
01912 return NULL;
01913 }
01914
01915 static void parse_forced_key_frames(char *kf, OutputStream *ost,
01916 AVCodecContext *avctx)
01917 {
01918 char *p;
01919 int n = 1, i;
01920 int64_t t;
01921
01922 for (p = kf; *p; p++)
01923 if (*p == ',')
01924 n++;
01925 ost->forced_kf_count = n;
01926 ost->forced_kf_pts = av_malloc(sizeof(*ost->forced_kf_pts) * n);
01927 if (!ost->forced_kf_pts) {
01928 av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
01929 exit_program(1);
01930 }
01931
01932 p = kf;
01933 for (i = 0; i < n; i++) {
01934 char *next = strchr(p, ',');
01935
01936 if (next)
01937 *next++ = 0;
01938
01939 t = parse_time_or_die("force_key_frames", p, 1);
01940 ost->forced_kf_pts[i] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
01941
01942 p = next;
01943 }
01944 }
01945
01946 static void report_new_stream(int input_index, AVPacket *pkt)
01947 {
01948 InputFile *file = input_files[input_index];
01949 AVStream *st = file->ctx->streams[pkt->stream_index];
01950
01951 if (pkt->stream_index < file->nb_streams_warn)
01952 return;
01953 av_log(file->ctx, AV_LOG_WARNING,
01954 "New %s stream %d:%d at pos:%"PRId64" and DTS:%ss\n",
01955 av_get_media_type_string(st->codec->codec_type),
01956 input_index, pkt->stream_index,
01957 pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
01958 file->nb_streams_warn = pkt->stream_index + 1;
01959 }
01960
01961 static int transcode_init(void)
01962 {
01963 int ret = 0, i, j, k;
01964 AVFormatContext *oc;
01965 AVCodecContext *codec;
01966 OutputStream *ost;
01967 InputStream *ist;
01968 char error[1024];
01969 int want_sdp = 1;
01970
01971
01972 for (i = 0; i < nb_input_files; i++) {
01973 InputFile *ifile = input_files[i];
01974 if (ifile->rate_emu)
01975 for (j = 0; j < ifile->nb_streams; j++)
01976 input_streams[j + ifile->ist_index]->start = av_gettime();
01977 }
01978
01979
01980 for (i = 0; i < nb_output_files; i++) {
01981 oc = output_files[i]->ctx;
01982 if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
01983 av_dump_format(oc, i, oc->filename, 1);
01984 av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", i);
01985 return AVERROR(EINVAL);
01986 }
01987 }
01988
01989
01990 for (i = 0; i < nb_filtergraphs; i++)
01991 if ((ret = avfilter_graph_config(filtergraphs[i]->graph, NULL)) < 0)
01992 return ret;
01993
01994
01995 for (i = 0; i < nb_output_streams; i++) {
01996 AVCodecContext *icodec = NULL;
01997 ost = output_streams[i];
01998 oc = output_files[ost->file_index]->ctx;
01999 ist = get_input_stream(ost);
02000
02001 if (ost->attachment_filename)
02002 continue;
02003
02004 codec = ost->st->codec;
02005
02006 if (ist) {
02007 icodec = ist->st->codec;
02008
02009 ost->st->disposition = ist->st->disposition;
02010 codec->bits_per_raw_sample = icodec->bits_per_raw_sample;
02011 codec->chroma_sample_location = icodec->chroma_sample_location;
02012 }
02013
02014 if (ost->stream_copy) {
02015 uint64_t extra_size;
02016
02017 av_assert0(ist && !ost->filter);
02018
02019 extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
02020
02021 if (extra_size > INT_MAX) {
02022 return AVERROR(EINVAL);
02023 }
02024
02025
02026 codec->codec_id = icodec->codec_id;
02027 codec->codec_type = icodec->codec_type;
02028
02029 if (!codec->codec_tag) {
02030 if (!oc->oformat->codec_tag ||
02031 av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == codec->codec_id ||
02032 av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0)
02033 codec->codec_tag = icodec->codec_tag;
02034 }
02035
02036 codec->bit_rate = icodec->bit_rate;
02037 codec->rc_max_rate = icodec->rc_max_rate;
02038 codec->rc_buffer_size = icodec->rc_buffer_size;
02039 codec->field_order = icodec->field_order;
02040 codec->extradata = av_mallocz(extra_size);
02041 if (!codec->extradata) {
02042 return AVERROR(ENOMEM);
02043 }
02044 memcpy(codec->extradata, icodec->extradata, icodec->extradata_size);
02045 codec->extradata_size= icodec->extradata_size;
02046 codec->bits_per_coded_sample = icodec->bits_per_coded_sample;
02047
02048 codec->time_base = ist->st->time_base;
02049
02050
02051
02052
02053
02054 if(!strcmp(oc->oformat->name, "avi")) {
02055 if ( copy_tb<0 && av_q2d(ist->st->r_frame_rate) >= av_q2d(ist->st->avg_frame_rate)
02056 && 0.5/av_q2d(ist->st->r_frame_rate) > av_q2d(ist->st->time_base)
02057 && 0.5/av_q2d(ist->st->r_frame_rate) > av_q2d(icodec->time_base)
02058 && av_q2d(ist->st->time_base) < 1.0/500 && av_q2d(icodec->time_base) < 1.0/500
02059 || copy_tb==2){
02060 codec->time_base.num = ist->st->r_frame_rate.den;
02061 codec->time_base.den = 2*ist->st->r_frame_rate.num;
02062 codec->ticks_per_frame = 2;
02063 } else if ( copy_tb<0 && av_q2d(icodec->time_base)*icodec->ticks_per_frame > 2*av_q2d(ist->st->time_base)
02064 && av_q2d(ist->st->time_base) < 1.0/500
02065 || copy_tb==0){
02066 codec->time_base = icodec->time_base;
02067 codec->time_base.num *= icodec->ticks_per_frame;
02068 codec->time_base.den *= 2;
02069 codec->ticks_per_frame = 2;
02070 }
02071 } else if(!(oc->oformat->flags & AVFMT_VARIABLE_FPS)
02072 && strcmp(oc->oformat->name, "mov") && strcmp(oc->oformat->name, "mp4") && strcmp(oc->oformat->name, "3gp")
02073 && strcmp(oc->oformat->name, "3g2") && strcmp(oc->oformat->name, "psp") && strcmp(oc->oformat->name, "ipod")
02074 && strcmp(oc->oformat->name, "f4v")
02075 ) {
02076 if( copy_tb<0 && icodec->time_base.den
02077 && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base)
02078 && av_q2d(ist->st->time_base) < 1.0/500
02079 || copy_tb==0){
02080 codec->time_base = icodec->time_base;
02081 codec->time_base.num *= icodec->ticks_per_frame;
02082 }
02083 }
02084
02085 if(ost->frame_rate.num)
02086 codec->time_base = av_inv_q(ost->frame_rate);
02087
02088 av_reduce(&codec->time_base.num, &codec->time_base.den,
02089 codec->time_base.num, codec->time_base.den, INT_MAX);
02090
02091 switch (codec->codec_type) {
02092 case AVMEDIA_TYPE_AUDIO:
02093 if (audio_volume != 256) {
02094 av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
02095 exit_program(1);
02096 }
02097 codec->channel_layout = icodec->channel_layout;
02098 codec->sample_rate = icodec->sample_rate;
02099 codec->channels = icodec->channels;
02100 codec->frame_size = icodec->frame_size;
02101 codec->audio_service_type = icodec->audio_service_type;
02102 codec->block_align = icodec->block_align;
02103 if((codec->block_align == 1 || codec->block_align == 1152) && codec->codec_id == AV_CODEC_ID_MP3)
02104 codec->block_align= 0;
02105 if(codec->codec_id == AV_CODEC_ID_AC3)
02106 codec->block_align= 0;
02107 break;
02108 case AVMEDIA_TYPE_VIDEO:
02109 codec->pix_fmt = icodec->pix_fmt;
02110 codec->width = icodec->width;
02111 codec->height = icodec->height;
02112 codec->has_b_frames = icodec->has_b_frames;
02113 if (!codec->sample_aspect_ratio.num) {
02114 codec->sample_aspect_ratio =
02115 ost->st->sample_aspect_ratio =
02116 ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio :
02117 ist->st->codec->sample_aspect_ratio.num ?
02118 ist->st->codec->sample_aspect_ratio : (AVRational){0, 1};
02119 }
02120 ost->st->avg_frame_rate = ist->st->avg_frame_rate;
02121 break;
02122 case AVMEDIA_TYPE_SUBTITLE:
02123 codec->width = icodec->width;
02124 codec->height = icodec->height;
02125 break;
02126 case AVMEDIA_TYPE_DATA:
02127 case AVMEDIA_TYPE_ATTACHMENT:
02128 break;
02129 default:
02130 abort();
02131 }
02132 } else {
02133 if (!ost->enc)
02134 ost->enc = avcodec_find_encoder(codec->codec_id);
02135 if (!ost->enc) {
02136
02137 snprintf(error, sizeof(error), "Encoder (codec %s) not found for output stream #%d:%d",
02138 avcodec_get_name(ost->st->codec->codec_id), ost->file_index, ost->index);
02139 ret = AVERROR(EINVAL);
02140 goto dump_format;
02141 }
02142
02143 if (ist)
02144 ist->decoding_needed++;
02145 ost->encoding_needed = 1;
02146
02147 if (!ost->filter &&
02148 (codec->codec_type == AVMEDIA_TYPE_VIDEO ||
02149 codec->codec_type == AVMEDIA_TYPE_AUDIO)) {
02150 FilterGraph *fg;
02151 fg = init_simple_filtergraph(ist, ost);
02152 if (configure_filtergraph(fg)) {
02153 av_log(NULL, AV_LOG_FATAL, "Error opening filters!\n");
02154 exit(1);
02155 }
02156 }
02157
02158 if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
02159 if (ost->filter && !ost->frame_rate.num)
02160 ost->frame_rate = av_buffersink_get_frame_rate(ost->filter->filter);
02161 if (ist && !ost->frame_rate.num)
02162 ost->frame_rate = ist->framerate;
02163 if (ist && !ost->frame_rate.num)
02164 ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25, 1};
02165
02166 if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) {
02167 int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
02168 ost->frame_rate = ost->enc->supported_framerates[idx];
02169 }
02170 }
02171
02172 switch (codec->codec_type) {
02173 case AVMEDIA_TYPE_AUDIO:
02174 codec->sample_fmt = ost->filter->filter->inputs[0]->format;
02175 codec->sample_rate = ost->filter->filter->inputs[0]->sample_rate;
02176 codec->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
02177 codec->channels = av_get_channel_layout_nb_channels(codec->channel_layout);
02178 codec->time_base = (AVRational){ 1, codec->sample_rate };
02179 break;
02180 case AVMEDIA_TYPE_VIDEO:
02181 codec->time_base = av_inv_q(ost->frame_rate);
02182 if (ost->filter && !(codec->time_base.num && codec->time_base.den))
02183 codec->time_base = ost->filter->filter->inputs[0]->time_base;
02184 if ( av_q2d(codec->time_base) < 0.001 && video_sync_method != VSYNC_PASSTHROUGH
02185 && (video_sync_method == VSYNC_CFR || (video_sync_method == VSYNC_AUTO && !(oc->oformat->flags & AVFMT_VARIABLE_FPS)))){
02186 av_log(oc, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n"
02187 "Please consider specifying a lower framerate, a different muxer or -vsync 2\n");
02188 }
02189 for (j = 0; j < ost->forced_kf_count; j++)
02190 ost->forced_kf_pts[j] = av_rescale_q(ost->forced_kf_pts[j],
02191 AV_TIME_BASE_Q,
02192 codec->time_base);
02193
02194 codec->width = ost->filter->filter->inputs[0]->w;
02195 codec->height = ost->filter->filter->inputs[0]->h;
02196 codec->sample_aspect_ratio = ost->st->sample_aspect_ratio =
02197 ost->frame_aspect_ratio ?
02198 av_d2q(ost->frame_aspect_ratio * codec->height/codec->width, 255) :
02199 ost->filter->filter->inputs[0]->sample_aspect_ratio;
02200 codec->pix_fmt = ost->filter->filter->inputs[0]->format;
02201
02202 if (!icodec ||
02203 codec->width != icodec->width ||
02204 codec->height != icodec->height ||
02205 codec->pix_fmt != icodec->pix_fmt) {
02206 codec->bits_per_raw_sample = frame_bits_per_raw_sample;
02207 }
02208
02209 if (ost->forced_keyframes)
02210 parse_forced_key_frames(ost->forced_keyframes, ost,
02211 ost->st->codec);
02212 break;
02213 case AVMEDIA_TYPE_SUBTITLE:
02214 codec->time_base = (AVRational){1, 1000};
02215 if (!codec->width) {
02216 codec->width = input_streams[ost->source_index]->st->codec->width;
02217 codec->height = input_streams[ost->source_index]->st->codec->height;
02218 }
02219 break;
02220 default:
02221 abort();
02222 break;
02223 }
02224
02225 if (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2)) {
02226 char logfilename[1024];
02227 FILE *f;
02228
02229 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
02230 ost->logfile_prefix ? ost->logfile_prefix :
02231 DEFAULT_PASS_LOGFILENAME_PREFIX,
02232 i);
02233 if (!strcmp(ost->enc->name, "libx264")) {
02234 av_dict_set(&ost->opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
02235 } else {
02236 if (codec->flags & CODEC_FLAG_PASS2) {
02237 char *logbuffer;
02238 size_t logbuffer_size;
02239 if (cmdutils_read_file(logfilename, &logbuffer, &logbuffer_size) < 0) {
02240 av_log(NULL, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
02241 logfilename);
02242 exit_program(1);
02243 }
02244 codec->stats_in = logbuffer;
02245 }
02246 if (codec->flags & CODEC_FLAG_PASS1) {
02247 f = fopen(logfilename, "wb");
02248 if (!f) {
02249 av_log(NULL, AV_LOG_FATAL, "Cannot write log file '%s' for pass-1 encoding: %s\n",
02250 logfilename, strerror(errno));
02251 exit_program(1);
02252 }
02253 ost->logfile = f;
02254 }
02255 }
02256 }
02257 }
02258 }
02259
02260
02261 for (i = 0; i < nb_output_streams; i++) {
02262 ost = output_streams[i];
02263 if (ost->encoding_needed) {
02264 AVCodec *codec = ost->enc;
02265 AVCodecContext *dec = NULL;
02266
02267 if ((ist = get_input_stream(ost)))
02268 dec = ist->st->codec;
02269 if (dec && dec->subtitle_header) {
02270
02271 ost->st->codec->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
02272 if (!ost->st->codec->subtitle_header) {
02273 ret = AVERROR(ENOMEM);
02274 goto dump_format;
02275 }
02276 memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
02277 ost->st->codec->subtitle_header_size = dec->subtitle_header_size;
02278 }
02279 if (!av_dict_get(ost->opts, "threads", NULL, 0))
02280 av_dict_set(&ost->opts, "threads", "auto", 0);
02281 if (avcodec_open2(ost->st->codec, codec, &ost->opts) < 0) {
02282 snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
02283 ost->file_index, ost->index);
02284 ret = AVERROR(EINVAL);
02285 goto dump_format;
02286 }
02287 if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
02288 !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
02289 av_buffersink_set_frame_size(ost->filter->filter,
02290 ost->st->codec->frame_size);
02291 assert_codec_experimental(ost->st->codec, 1);
02292 assert_avoptions(ost->opts);
02293 if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
02294 av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
02295 " It takes bits/s as argument, not kbits/s\n");
02296 extra_size += ost->st->codec->extradata_size;
02297
02298 if (ost->st->codec->me_threshold)
02299 input_streams[ost->source_index]->st->codec->debug |= FF_DEBUG_MV;
02300 }
02301 }
02302
02303
02304 for (i = 0; i < nb_input_streams; i++)
02305 if ((ret = init_input_stream(i, error, sizeof(error))) < 0)
02306 goto dump_format;
02307
02308
02309 for (i = 0; i < nb_input_files; i++) {
02310 InputFile *ifile = input_files[i];
02311 for (j = 0; j < ifile->ctx->nb_programs; j++) {
02312 AVProgram *p = ifile->ctx->programs[j];
02313 int discard = AVDISCARD_ALL;
02314
02315 for (k = 0; k < p->nb_stream_indexes; k++)
02316 if (!input_streams[ifile->ist_index + p->stream_index[k]]->discard) {
02317 discard = AVDISCARD_DEFAULT;
02318 break;
02319 }
02320 p->discard = discard;
02321 }
02322 }
02323
02324
02325 for (i = 0; i < nb_output_files; i++) {
02326 oc = output_files[i]->ctx;
02327 oc->interrupt_callback = int_cb;
02328 if ((ret = avformat_write_header(oc, &output_files[i]->opts)) < 0) {
02329 char errbuf[128];
02330 const char *errbuf_ptr = errbuf;
02331 if (av_strerror(ret, errbuf, sizeof(errbuf)) < 0)
02332 errbuf_ptr = strerror(AVUNERROR(ret));
02333 snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?): %s", i, errbuf_ptr);
02334 ret = AVERROR(EINVAL);
02335 goto dump_format;
02336 }
02337
02338 if (strcmp(oc->oformat->name, "rtp")) {
02339 want_sdp = 0;
02340 }
02341 }
02342
02343 dump_format:
02344
02345
02346 for (i = 0; i < nb_output_files; i++) {
02347 av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1);
02348 }
02349
02350
02351 av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
02352 for (i = 0; i < nb_input_streams; i++) {
02353 ist = input_streams[i];
02354
02355 for (j = 0; j < ist->nb_filters; j++) {
02356 if (ist->filters[j]->graph->graph_desc) {
02357 av_log(NULL, AV_LOG_INFO, " Stream #%d:%d (%s) -> %s",
02358 ist->file_index, ist->st->index, ist->dec ? ist->dec->name : "?",
02359 ist->filters[j]->name);
02360 if (nb_filtergraphs > 1)
02361 av_log(NULL, AV_LOG_INFO, " (graph %d)", ist->filters[j]->graph->index);
02362 av_log(NULL, AV_LOG_INFO, "\n");
02363 }
02364 }
02365 }
02366
02367 for (i = 0; i < nb_output_streams; i++) {
02368 ost = output_streams[i];
02369
02370 if (ost->attachment_filename) {
02371
02372 av_log(NULL, AV_LOG_INFO, " File %s -> Stream #%d:%d\n",
02373 ost->attachment_filename, ost->file_index, ost->index);
02374 continue;
02375 }
02376
02377 if (ost->filter && ost->filter->graph->graph_desc) {
02378
02379 av_log(NULL, AV_LOG_INFO, " %s", ost->filter->name);
02380 if (nb_filtergraphs > 1)
02381 av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);
02382
02383 av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index,
02384 ost->index, ost->enc ? ost->enc->name : "?");
02385 continue;
02386 }
02387
02388 av_log(NULL, AV_LOG_INFO, " Stream #%d:%d -> #%d:%d",
02389 input_streams[ost->source_index]->file_index,
02390 input_streams[ost->source_index]->st->index,
02391 ost->file_index,
02392 ost->index);
02393 if (ost->sync_ist != input_streams[ost->source_index])
02394 av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]",
02395 ost->sync_ist->file_index,
02396 ost->sync_ist->st->index);
02397 if (ost->stream_copy)
02398 av_log(NULL, AV_LOG_INFO, " (copy)");
02399 else
02400 av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index]->dec ?
02401 input_streams[ost->source_index]->dec->name : "?",
02402 ost->enc ? ost->enc->name : "?");
02403 av_log(NULL, AV_LOG_INFO, "\n");
02404 }
02405
02406 if (ret) {
02407 av_log(NULL, AV_LOG_ERROR, "%s\n", error);
02408 return ret;
02409 }
02410
02411 if (want_sdp) {
02412 print_sdp();
02413 }
02414
02415 return 0;
02416 }
02417
02422 static int need_output(void)
02423 {
02424 int i;
02425
02426 for (i = 0; i < nb_output_streams; i++) {
02427 OutputStream *ost = output_streams[i];
02428 OutputFile *of = output_files[ost->file_index];
02429 AVFormatContext *os = output_files[ost->file_index]->ctx;
02430
02431 if (ost->finished ||
02432 (os->pb && avio_tell(os->pb) >= of->limit_filesize))
02433 continue;
02434 if (ost->frame_number >= ost->max_frames) {
02435 int j;
02436 for (j = 0; j < of->ctx->nb_streams; j++)
02437 close_output_stream(output_streams[of->ost_index + j]);
02438 continue;
02439 }
02440
02441 return 1;
02442 }
02443
02444 return 0;
02445 }
02446
02452 static OutputStream *choose_output(void)
02453 {
02454 int i;
02455 int64_t opts_min = INT64_MAX;
02456 OutputStream *ost_min = NULL;
02457
02458 for (i = 0; i < nb_output_streams; i++) {
02459 OutputStream *ost = output_streams[i];
02460 int64_t opts = av_rescale_q(ost->st->cur_dts, ost->st->time_base,
02461 AV_TIME_BASE_Q);
02462 if (!ost->unavailable && !ost->finished && opts < opts_min) {
02463 opts_min = opts;
02464 ost_min = ost;
02465 }
02466 }
02467 return ost_min;
02468 }
02469
02470 static int check_keyboard_interaction(int64_t cur_time)
02471 {
02472 int i, ret, key;
02473 static int64_t last_time;
02474 if (received_nb_signals)
02475 return AVERROR_EXIT;
02476
02477 if(cur_time - last_time >= 100000 && !run_as_daemon){
02478 key = read_key();
02479 last_time = cur_time;
02480 }else
02481 key = -1;
02482 if (key == 'q')
02483 return AVERROR_EXIT;
02484 if (key == '+') av_log_set_level(av_log_get_level()+10);
02485 if (key == '-') av_log_set_level(av_log_get_level()-10);
02486 if (key == 's') qp_hist ^= 1;
02487 if (key == 'h'){
02488 if (do_hex_dump){
02489 do_hex_dump = do_pkt_dump = 0;
02490 } else if(do_pkt_dump){
02491 do_hex_dump = 1;
02492 } else
02493 do_pkt_dump = 1;
02494 av_log_set_level(AV_LOG_DEBUG);
02495 }
02496 if (key == 'c' || key == 'C'){
02497 char buf[4096], target[64], command[256], arg[256] = {0};
02498 double time;
02499 int k, n = 0;
02500 fprintf(stderr, "\nEnter command: <target> <time> <command>[ <argument>]\n");
02501 i = 0;
02502 while ((k = read_key()) != '\n' && k != '\r' && i < sizeof(buf)-1)
02503 if (k > 0)
02504 buf[i++] = k;
02505 buf[i] = 0;
02506 if (k > 0 &&
02507 (n = sscanf(buf, "%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
02508 av_log(NULL, AV_LOG_DEBUG, "Processing command target:%s time:%f command:%s arg:%s",
02509 target, time, command, arg);
02510 for (i = 0; i < nb_filtergraphs; i++) {
02511 FilterGraph *fg = filtergraphs[i];
02512 if (fg->graph) {
02513 if (time < 0) {
02514 ret = avfilter_graph_send_command(fg->graph, target, command, arg, buf, sizeof(buf),
02515 key == 'c' ? AVFILTER_CMD_FLAG_ONE : 0);
02516 fprintf(stderr, "Command reply for stream %d: ret:%d res:%s\n", i, ret, buf);
02517 } else {
02518 ret = avfilter_graph_queue_command(fg->graph, target, command, arg, 0, time);
02519 }
02520 }
02521 }
02522 } else {
02523 av_log(NULL, AV_LOG_ERROR,
02524 "Parse error, at least 3 arguments were expected, "
02525 "only %d given in string '%s'\n", n, buf);
02526 }
02527 }
02528 if (key == 'd' || key == 'D'){
02529 int debug=0;
02530 if(key == 'D') {
02531 debug = input_streams[0]->st->codec->debug<<1;
02532 if(!debug) debug = 1;
02533 while(debug & (FF_DEBUG_DCT_COEFF|FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE))
02534 debug += debug;
02535 }else
02536 if(scanf("%d", &debug)!=1)
02537 fprintf(stderr,"error parsing debug value\n");
02538 for(i=0;i<nb_input_streams;i++) {
02539 input_streams[i]->st->codec->debug = debug;
02540 }
02541 for(i=0;i<nb_output_streams;i++) {
02542 OutputStream *ost = output_streams[i];
02543 ost->st->codec->debug = debug;
02544 }
02545 if(debug) av_log_set_level(AV_LOG_DEBUG);
02546 fprintf(stderr,"debug=%d\n", debug);
02547 }
02548 if (key == '?'){
02549 fprintf(stderr, "key function\n"
02550 "? show this help\n"
02551 "+ increase verbosity\n"
02552 "- decrease verbosity\n"
02553 "c Send command to filtergraph\n"
02554 "D cycle through available debug modes\n"
02555 "h dump packets/hex press to cycle through the 3 states\n"
02556 "q quit\n"
02557 "s Show QP histogram\n"
02558 );
02559 }
02560 return 0;
02561 }
02562
02563 #if HAVE_PTHREADS
02564 static void *input_thread(void *arg)
02565 {
02566 InputFile *f = arg;
02567 int ret = 0;
02568
02569 while (!transcoding_finished && ret >= 0) {
02570 AVPacket pkt;
02571 ret = av_read_frame(f->ctx, &pkt);
02572
02573 if (ret == AVERROR(EAGAIN)) {
02574 av_usleep(10000);
02575 ret = 0;
02576 continue;
02577 } else if (ret < 0)
02578 break;
02579
02580 pthread_mutex_lock(&f->fifo_lock);
02581 while (!av_fifo_space(f->fifo))
02582 pthread_cond_wait(&f->fifo_cond, &f->fifo_lock);
02583
02584 av_dup_packet(&pkt);
02585 av_fifo_generic_write(f->fifo, &pkt, sizeof(pkt), NULL);
02586
02587 pthread_mutex_unlock(&f->fifo_lock);
02588 }
02589
02590 f->finished = 1;
02591 return NULL;
02592 }
02593
02594 static void free_input_threads(void)
02595 {
02596 int i;
02597
02598 if (nb_input_files == 1)
02599 return;
02600
02601 transcoding_finished = 1;
02602
02603 for (i = 0; i < nb_input_files; i++) {
02604 InputFile *f = input_files[i];
02605 AVPacket pkt;
02606
02607 if (!f->fifo || f->joined)
02608 continue;
02609
02610 pthread_mutex_lock(&f->fifo_lock);
02611 while (av_fifo_size(f->fifo)) {
02612 av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL);
02613 av_free_packet(&pkt);
02614 }
02615 pthread_cond_signal(&f->fifo_cond);
02616 pthread_mutex_unlock(&f->fifo_lock);
02617
02618 pthread_join(f->thread, NULL);
02619 f->joined = 1;
02620
02621 while (av_fifo_size(f->fifo)) {
02622 av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL);
02623 av_free_packet(&pkt);
02624 }
02625 av_fifo_free(f->fifo);
02626 }
02627 }
02628
02629 static int init_input_threads(void)
02630 {
02631 int i, ret;
02632
02633 if (nb_input_files == 1)
02634 return 0;
02635
02636 for (i = 0; i < nb_input_files; i++) {
02637 InputFile *f = input_files[i];
02638
02639 if (!(f->fifo = av_fifo_alloc(8*sizeof(AVPacket))))
02640 return AVERROR(ENOMEM);
02641
02642 pthread_mutex_init(&f->fifo_lock, NULL);
02643 pthread_cond_init (&f->fifo_cond, NULL);
02644
02645 if ((ret = pthread_create(&f->thread, NULL, input_thread, f)))
02646 return AVERROR(ret);
02647 }
02648 return 0;
02649 }
02650
02651 static int get_input_packet_mt(InputFile *f, AVPacket *pkt)
02652 {
02653 int ret = 0;
02654
02655 pthread_mutex_lock(&f->fifo_lock);
02656
02657 if (av_fifo_size(f->fifo)) {
02658 av_fifo_generic_read(f->fifo, pkt, sizeof(*pkt), NULL);
02659 pthread_cond_signal(&f->fifo_cond);
02660 } else {
02661 if (f->finished)
02662 ret = AVERROR_EOF;
02663 else
02664 ret = AVERROR(EAGAIN);
02665 }
02666
02667 pthread_mutex_unlock(&f->fifo_lock);
02668
02669 return ret;
02670 }
02671 #endif
02672
02673 static int get_input_packet(InputFile *f, AVPacket *pkt)
02674 {
02675 #if HAVE_PTHREADS
02676 if (nb_input_files > 1)
02677 return get_input_packet_mt(f, pkt);
02678 #endif
02679 return av_read_frame(f->ctx, pkt);
02680 }
02681
02682 static int got_eagain(void)
02683 {
02684 int i;
02685 for (i = 0; i < nb_output_streams; i++)
02686 if (output_streams[i]->unavailable)
02687 return 1;
02688 return 0;
02689 }
02690
02691 static void reset_eagain(void)
02692 {
02693 int i;
02694 for (i = 0; i < nb_input_files; i++)
02695 input_files[i]->eagain = 0;
02696 for (i = 0; i < nb_output_streams; i++)
02697 output_streams[i]->unavailable = 0;
02698 }
02699
02707 static int process_input(int file_index)
02708 {
02709 InputFile *ifile = input_files[file_index];
02710 AVFormatContext *is;
02711 InputStream *ist;
02712 AVPacket pkt;
02713 int ret, i, j;
02714
02715 is = ifile->ctx;
02716 ret = get_input_packet(ifile, &pkt);
02717
02718 if (ret == AVERROR(EAGAIN)) {
02719 ifile->eagain = 1;
02720 return ret;
02721 }
02722 if (ret < 0) {
02723 if (ret != AVERROR_EOF) {
02724 print_error(is->filename, ret);
02725 if (exit_on_error)
02726 exit_program(1);
02727 }
02728 ifile->eof_reached = 1;
02729
02730 for (i = 0; i < ifile->nb_streams; i++) {
02731 ist = input_streams[ifile->ist_index + i];
02732 if (ist->decoding_needed)
02733 output_packet(ist, NULL);
02734
02735
02736 for (j = 0; j < nb_output_streams; j++) {
02737 OutputStream *ost = output_streams[j];
02738
02739 if (ost->source_index == ifile->ist_index + i &&
02740 (ost->stream_copy || ost->enc->type == AVMEDIA_TYPE_SUBTITLE))
02741 close_output_stream(ost);
02742 }
02743 }
02744
02745 return AVERROR(EAGAIN);
02746 }
02747
02748 reset_eagain();
02749
02750 if (do_pkt_dump) {
02751 av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump,
02752 is->streams[pkt.stream_index]);
02753 }
02754
02755
02756 if (pkt.stream_index >= ifile->nb_streams) {
02757 report_new_stream(file_index, &pkt);
02758 goto discard_packet;
02759 }
02760
02761 ist = input_streams[ifile->ist_index + pkt.stream_index];
02762 if (ist->discard)
02763 goto discard_packet;
02764
02765 if(!ist->wrap_correction_done && input_files[file_index]->ctx->start_time != AV_NOPTS_VALUE && ist->st->pts_wrap_bits < 64){
02766 int64_t stime = av_rescale_q(input_files[file_index]->ctx->start_time, AV_TIME_BASE_Q, ist->st->time_base);
02767 int64_t stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
02768 ist->wrap_correction_done = 1;
02769
02770 if(stime2 > stime && pkt.dts != AV_NOPTS_VALUE && pkt.dts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
02771 pkt.dts -= 1ULL<<ist->st->pts_wrap_bits;
02772 ist->wrap_correction_done = 0;
02773 }
02774 if(stime2 > stime && pkt.pts != AV_NOPTS_VALUE && pkt.pts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
02775 pkt.pts -= 1ULL<<ist->st->pts_wrap_bits;
02776 ist->wrap_correction_done = 0;
02777 }
02778 }
02779
02780 if (pkt.dts != AV_NOPTS_VALUE)
02781 pkt.dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
02782 if (pkt.pts != AV_NOPTS_VALUE)
02783 pkt.pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
02784
02785 if (pkt.pts != AV_NOPTS_VALUE)
02786 pkt.pts *= ist->ts_scale;
02787 if (pkt.dts != AV_NOPTS_VALUE)
02788 pkt.dts *= ist->ts_scale;
02789
02790 if (debug_ts) {
02791 av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s "
02792 "next_dts:%s next_dts_time:%s next_pts:%s next_pts_time:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%"PRId64"\n",
02793 ifile->ist_index + pkt.stream_index, av_get_media_type_string(ist->st->codec->codec_type),
02794 av_ts2str(ist->next_dts), av_ts2timestr(ist->next_dts, &AV_TIME_BASE_Q),
02795 av_ts2str(ist->next_pts), av_ts2timestr(ist->next_pts, &AV_TIME_BASE_Q),
02796 av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ist->st->time_base),
02797 av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ist->st->time_base),
02798 input_files[ist->file_index]->ts_offset);
02799 }
02800
02801 if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE &&
02802 !copy_ts) {
02803 int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
02804 int64_t delta = pkt_dts - ist->next_dts;
02805 if (is->iformat->flags & AVFMT_TS_DISCONT) {
02806 if(delta < -1LL*dts_delta_threshold*AV_TIME_BASE ||
02807 (delta > 1LL*dts_delta_threshold*AV_TIME_BASE &&
02808 ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) ||
02809 pkt_dts+1<ist->pts){
02810 ifile->ts_offset -= delta;
02811 av_log(NULL, AV_LOG_DEBUG,
02812 "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
02813 delta, ifile->ts_offset);
02814 pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
02815 if (pkt.pts != AV_NOPTS_VALUE)
02816 pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
02817 }
02818 } else {
02819 if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
02820 (delta > 1LL*dts_error_threshold*AV_TIME_BASE && ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
02821 ) {
02822 av_log(NULL, AV_LOG_WARNING, "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n", pkt.dts, ist->next_dts, pkt.stream_index);
02823 pkt.dts = AV_NOPTS_VALUE;
02824 }
02825 if (pkt.pts != AV_NOPTS_VALUE){
02826 int64_t pkt_pts = av_rescale_q(pkt.pts, ist->st->time_base, AV_TIME_BASE_Q);
02827 delta = pkt_pts - ist->next_dts;
02828 if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
02829 (delta > 1LL*dts_error_threshold*AV_TIME_BASE && ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
02830 ) {
02831 av_log(NULL, AV_LOG_WARNING, "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n", pkt.pts, ist->next_dts, pkt.stream_index);
02832 pkt.pts = AV_NOPTS_VALUE;
02833 }
02834 }
02835 }
02836 }
02837
02838 sub2video_heartbeat(ist, pkt.pts);
02839
02840 ret = output_packet(ist, &pkt);
02841 if (ret < 0) {
02842 char buf[128];
02843 av_strerror(ret, buf, sizeof(buf));
02844 av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d: %s\n",
02845 ist->file_index, ist->st->index, buf);
02846 if (exit_on_error)
02847 exit_program(1);
02848 }
02849
02850 discard_packet:
02851 av_free_packet(&pkt);
02852
02853 return 0;
02854 }
02855
02863 static int transcode_from_filter(FilterGraph *graph, InputStream **best_ist)
02864 {
02865 int i, ret;
02866 int nb_requests, nb_requests_max = 0;
02867 InputFilter *ifilter;
02868 InputStream *ist;
02869
02870 *best_ist = NULL;
02871 ret = avfilter_graph_request_oldest(graph->graph);
02872 if (ret >= 0)
02873 return reap_filters();
02874
02875 if (ret == AVERROR_EOF) {
02876 ret = reap_filters();
02877 for (i = 0; i < graph->nb_outputs; i++)
02878 close_output_stream(graph->outputs[i]->ost);
02879 return ret;
02880 }
02881 if (ret != AVERROR(EAGAIN))
02882 return ret;
02883
02884 for (i = 0; i < graph->nb_inputs; i++) {
02885 ifilter = graph->inputs[i];
02886 ist = ifilter->ist;
02887 if (input_files[ist->file_index]->eagain ||
02888 input_files[ist->file_index]->eof_reached)
02889 continue;
02890 nb_requests = av_buffersrc_get_nb_failed_requests(ifilter->filter);
02891 if (nb_requests > nb_requests_max) {
02892 nb_requests_max = nb_requests;
02893 *best_ist = ist;
02894 }
02895 }
02896
02897 if (!*best_ist)
02898 for (i = 0; i < graph->nb_outputs; i++)
02899 graph->outputs[i]->ost->unavailable = 1;
02900
02901 return 0;
02902 }
02903
02909 static int transcode_step(void)
02910 {
02911 OutputStream *ost;
02912 InputStream *ist;
02913 int ret;
02914
02915 ost = choose_output();
02916 if (!ost) {
02917 if (got_eagain()) {
02918 reset_eagain();
02919 av_usleep(10000);
02920 return 0;
02921 }
02922 av_log(NULL, AV_LOG_VERBOSE, "No more inputs to read from, finishing.\n");
02923 return AVERROR_EOF;
02924 }
02925
02926 if (ost->filter) {
02927 if ((ret = transcode_from_filter(ost->filter->graph, &ist)) < 0)
02928 return ret;
02929 if (!ist)
02930 return 0;
02931 } else {
02932 av_assert0(ost->source_index >= 0);
02933 ist = input_streams[ost->source_index];
02934 }
02935
02936 ret = process_input(ist->file_index);
02937 if (ret == AVERROR(EAGAIN)) {
02938 if (input_files[ist->file_index]->eagain)
02939 ost->unavailable = 1;
02940 return 0;
02941 }
02942 if (ret < 0)
02943 return ret == AVERROR_EOF ? 0 : ret;
02944
02945 return reap_filters();
02946 }
02947
02948
02949
02950
02951 static int transcode(void)
02952 {
02953 int ret, i;
02954 AVFormatContext *os;
02955 OutputStream *ost;
02956 InputStream *ist;
02957 int64_t timer_start;
02958
02959 ret = transcode_init();
02960 if (ret < 0)
02961 goto fail;
02962
02963 if (stdin_interaction) {
02964 av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");
02965 }
02966
02967 timer_start = av_gettime();
02968
02969 #if HAVE_PTHREADS
02970 if ((ret = init_input_threads()) < 0)
02971 goto fail;
02972 #endif
02973
02974 while (!received_sigterm) {
02975 int64_t cur_time= av_gettime();
02976
02977
02978 if (stdin_interaction)
02979 if (check_keyboard_interaction(cur_time) < 0)
02980 break;
02981
02982
02983 if (!need_output()) {
02984 av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");
02985 break;
02986 }
02987
02988 ret = transcode_step();
02989 if (ret < 0) {
02990 if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
02991 continue;
02992
02993 av_log(NULL, AV_LOG_ERROR, "Error while filtering.\n");
02994 break;
02995 }
02996
02997
02998 print_report(0, timer_start, cur_time);
02999 }
03000 #if HAVE_PTHREADS
03001 free_input_threads();
03002 #endif
03003
03004
03005 for (i = 0; i < nb_input_streams; i++) {
03006 ist = input_streams[i];
03007 if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {
03008 output_packet(ist, NULL);
03009 }
03010 }
03011 flush_encoders();
03012
03013 term_exit();
03014
03015
03016 for (i = 0; i < nb_output_files; i++) {
03017 os = output_files[i]->ctx;
03018 av_write_trailer(os);
03019 }
03020
03021
03022 print_report(1, timer_start, av_gettime());
03023
03024
03025 for (i = 0; i < nb_output_streams; i++) {
03026 ost = output_streams[i];
03027 if (ost->encoding_needed) {
03028 av_freep(&ost->st->codec->stats_in);
03029 avcodec_close(ost->st->codec);
03030 }
03031 }
03032
03033
03034 for (i = 0; i < nb_input_streams; i++) {
03035 ist = input_streams[i];
03036 if (ist->decoding_needed) {
03037 avcodec_close(ist->st->codec);
03038 }
03039 }
03040
03041
03042 ret = 0;
03043
03044 fail:
03045 #if HAVE_PTHREADS
03046 free_input_threads();
03047 #endif
03048
03049 if (output_streams) {
03050 for (i = 0; i < nb_output_streams; i++) {
03051 ost = output_streams[i];
03052 if (ost) {
03053 if (ost->stream_copy)
03054 av_freep(&ost->st->codec->extradata);
03055 if (ost->logfile) {
03056 fclose(ost->logfile);
03057 ost->logfile = NULL;
03058 }
03059 av_freep(&ost->st->codec->subtitle_header);
03060 av_free(ost->forced_kf_pts);
03061 av_dict_free(&ost->opts);
03062 }
03063 }
03064 }
03065 return ret;
03066 }
03067
03068
03069 static int64_t getutime(void)
03070 {
03071 #if HAVE_GETRUSAGE
03072 struct rusage rusage;
03073
03074 getrusage(RUSAGE_SELF, &rusage);
03075 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
03076 #elif HAVE_GETPROCESSTIMES
03077 HANDLE proc;
03078 FILETIME c, e, k, u;
03079 proc = GetCurrentProcess();
03080 GetProcessTimes(proc, &c, &e, &k, &u);
03081 return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
03082 #else
03083 return av_gettime();
03084 #endif
03085 }
03086
03087 static int64_t getmaxrss(void)
03088 {
03089 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
03090 struct rusage rusage;
03091 getrusage(RUSAGE_SELF, &rusage);
03092 return (int64_t)rusage.ru_maxrss * 1024;
03093 #elif HAVE_GETPROCESSMEMORYINFO
03094 HANDLE proc;
03095 PROCESS_MEMORY_COUNTERS memcounters;
03096 proc = GetCurrentProcess();
03097 memcounters.cb = sizeof(memcounters);
03098 GetProcessMemoryInfo(proc, &memcounters, sizeof(memcounters));
03099 return memcounters.PeakPagefileUsage;
03100 #else
03101 return 0;
03102 #endif
03103 }
03104
03105 static void log_callback_null(void *ptr, int level, const char *fmt, va_list vl)
03106 {
03107 }
03108
03109 static void parse_cpuflags(int argc, char **argv, const OptionDef *options)
03110 {
03111 int idx = locate_option(argc, argv, options, "cpuflags");
03112 if (idx && argv[idx + 1])
03113 opt_cpuflags(NULL, "cpuflags", argv[idx + 1]);
03114 }
03115
03116 int main(int argc, char **argv)
03117 {
03118 OptionsContext o = { 0 };
03119 int64_t ti;
03120
03121 reset_options(&o, 0);
03122
03123 av_log_set_flags(AV_LOG_SKIP_REPEATED);
03124 parse_loglevel(argc, argv, options);
03125
03126 if(argc>1 && !strcmp(argv[1], "-d")){
03127 run_as_daemon=1;
03128 av_log_set_callback(log_callback_null);
03129 argc--;
03130 argv++;
03131 }
03132
03133 avcodec_register_all();
03134 #if CONFIG_AVDEVICE
03135 avdevice_register_all();
03136 #endif
03137 avfilter_register_all();
03138 av_register_all();
03139 avformat_network_init();
03140
03141 show_banner(argc, argv, options);
03142
03143 term_init();
03144
03145 parse_cpuflags(argc, argv, options);
03146
03147
03148 parse_options(&o, argc, argv, options, opt_output_file);
03149
03150 if (nb_output_files <= 0 && nb_input_files == 0) {
03151 show_usage();
03152 av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
03153 exit_program(1);
03154 }
03155
03156
03157 if (nb_output_files <= 0) {
03158 av_log(NULL, AV_LOG_FATAL, "At least one output file must be specified\n");
03159 exit_program(1);
03160 }
03161
03162
03163
03164
03165
03166
03167 current_time = ti = getutime();
03168 if (transcode() < 0)
03169 exit_program(1);
03170 ti = getutime() - ti;
03171 if (do_benchmark) {
03172 int maxrss = getmaxrss() / 1024;
03173 printf("bench: utime=%0.3fs maxrss=%ikB\n", ti / 1000000.0, maxrss);
03174 }
03175
03176 exit_program(0);
03177 return 0;
03178 }