]> git.sesse.net Git - ffmpeg/blob - ffmpeg.c
avcodec/qsvenc: Added PicTiming SEI
[ffmpeg] / ffmpeg.c
1 /*
2  * Copyright (c) 2000-2003 Fabrice Bellard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * multimedia converter based on the FFmpeg libraries
24  */
25
26 #include "config.h"
27 #include <ctype.h>
28 #include <string.h>
29 #include <math.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdint.h>
34
35 #if HAVE_ISATTY
36 #if HAVE_IO_H
37 #include <io.h>
38 #endif
39 #if HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #endif
43
44 #include "libavformat/avformat.h"
45 #include "libavdevice/avdevice.h"
46 #include "libswresample/swresample.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/channel_layout.h"
49 #include "libavutil/parseutils.h"
50 #include "libavutil/samplefmt.h"
51 #include "libavutil/fifo.h"
52 #include "libavutil/internal.h"
53 #include "libavutil/intreadwrite.h"
54 #include "libavutil/dict.h"
55 #include "libavutil/mathematics.h"
56 #include "libavutil/pixdesc.h"
57 #include "libavutil/avstring.h"
58 #include "libavutil/libm.h"
59 #include "libavutil/imgutils.h"
60 #include "libavutil/timestamp.h"
61 #include "libavutil/bprint.h"
62 #include "libavutil/time.h"
63 #include "libavutil/threadmessage.h"
64 #include "libavcodec/mathops.h"
65 #include "libavformat/os_support.h"
66
67 # include "libavfilter/avcodec.h"
68 # include "libavfilter/avfilter.h"
69 # include "libavfilter/buffersrc.h"
70 # include "libavfilter/buffersink.h"
71
72 #if HAVE_SYS_RESOURCE_H
73 #include <sys/time.h>
74 #include <sys/types.h>
75 #include <sys/resource.h>
76 #elif HAVE_GETPROCESSTIMES
77 #include <windows.h>
78 #endif
79 #if HAVE_GETPROCESSMEMORYINFO
80 #include <windows.h>
81 #include <psapi.h>
82 #endif
83 #if HAVE_SETCONSOLECTRLHANDLER
84 #include <windows.h>
85 #endif
86
87
88 #if HAVE_SYS_SELECT_H
89 #include <sys/select.h>
90 #endif
91
92 #if HAVE_TERMIOS_H
93 #include <fcntl.h>
94 #include <sys/ioctl.h>
95 #include <sys/time.h>
96 #include <termios.h>
97 #elif HAVE_KBHIT
98 #include <conio.h>
99 #endif
100
101 #if HAVE_PTHREADS
102 #include <pthread.h>
103 #endif
104
105 #include <time.h>
106
107 #include "ffmpeg.h"
108 #include "cmdutils.h"
109
110 #include "libavutil/avassert.h"
111
112 const char program_name[] = "ffmpeg";
113 const int program_birth_year = 2000;
114
115 static FILE *vstats_file;
116
117 const char *const forced_keyframes_const_names[] = {
118     "n",
119     "n_forced",
120     "prev_forced_n",
121     "prev_forced_t",
122     "t",
123     NULL
124 };
125
126 static void do_video_stats(OutputStream *ost, int frame_size);
127 static int64_t getutime(void);
128 static int64_t getmaxrss(void);
129
130 static int run_as_daemon  = 0;
131 static int nb_frames_dup = 0;
132 static int nb_frames_drop = 0;
133 static int64_t decode_error_stat[2];
134
135 static int current_time;
136 AVIOContext *progress_avio = NULL;
137
138 static uint8_t *subtitle_out;
139
140 InputStream **input_streams = NULL;
141 int        nb_input_streams = 0;
142 InputFile   **input_files   = NULL;
143 int        nb_input_files   = 0;
144
145 OutputStream **output_streams = NULL;
146 int         nb_output_streams = 0;
147 OutputFile   **output_files   = NULL;
148 int         nb_output_files   = 0;
149
150 FilterGraph **filtergraphs;
151 int        nb_filtergraphs;
152
153 #if HAVE_TERMIOS_H
154
155 /* init terminal so that we can grab keys */
156 static struct termios oldtty;
157 static int restore_tty;
158 #endif
159
160 #if HAVE_PTHREADS
161 static void free_input_threads(void);
162 #endif
163
164 /* sub2video hack:
165    Convert subtitles to video with alpha to insert them in filter graphs.
166    This is a temporary solution until libavfilter gets real subtitles support.
167  */
168
169 static int sub2video_get_blank_frame(InputStream *ist)
170 {
171     int ret;
172     AVFrame *frame = ist->sub2video.frame;
173
174     av_frame_unref(frame);
175     ist->sub2video.frame->width  = ist->dec_ctx->width  ? ist->dec_ctx->width  : ist->sub2video.w;
176     ist->sub2video.frame->height = ist->dec_ctx->height ? ist->dec_ctx->height : ist->sub2video.h;
177     ist->sub2video.frame->format = AV_PIX_FMT_RGB32;
178     if ((ret = av_frame_get_buffer(frame, 32)) < 0)
179         return ret;
180     memset(frame->data[0], 0, frame->height * frame->linesize[0]);
181     return 0;
182 }
183
184 static void sub2video_copy_rect(uint8_t *dst, int dst_linesize, int w, int h,
185                                 AVSubtitleRect *r)
186 {
187     uint32_t *pal, *dst2;
188     uint8_t *src, *src2;
189     int x, y;
190
191     if (r->type != SUBTITLE_BITMAP) {
192         av_log(NULL, AV_LOG_WARNING, "sub2video: non-bitmap subtitle\n");
193         return;
194     }
195     if (r->x < 0 || r->x + r->w > w || r->y < 0 || r->y + r->h > h) {
196         av_log(NULL, AV_LOG_WARNING, "sub2video: rectangle (%d %d %d %d) overflowing %d %d\n",
197             r->x, r->y, r->w, r->h, w, h
198         );
199         return;
200     }
201
202     dst += r->y * dst_linesize + r->x * 4;
203     src = r->pict.data[0];
204     pal = (uint32_t *)r->pict.data[1];
205     for (y = 0; y < r->h; y++) {
206         dst2 = (uint32_t *)dst;
207         src2 = src;
208         for (x = 0; x < r->w; x++)
209             *(dst2++) = pal[*(src2++)];
210         dst += dst_linesize;
211         src += r->pict.linesize[0];
212     }
213 }
214
215 static void sub2video_push_ref(InputStream *ist, int64_t pts)
216 {
217     AVFrame *frame = ist->sub2video.frame;
218     int i;
219
220     av_assert1(frame->data[0]);
221     ist->sub2video.last_pts = frame->pts = pts;
222     for (i = 0; i < ist->nb_filters; i++)
223         av_buffersrc_add_frame_flags(ist->filters[i]->filter, frame,
224                                      AV_BUFFERSRC_FLAG_KEEP_REF |
225                                      AV_BUFFERSRC_FLAG_PUSH);
226 }
227
228 static void sub2video_update(InputStream *ist, AVSubtitle *sub)
229 {
230     AVFrame *frame = ist->sub2video.frame;
231     int8_t *dst;
232     int     dst_linesize;
233     int num_rects, i;
234     int64_t pts, end_pts;
235
236     if (!frame)
237         return;
238     if (sub) {
239         pts       = av_rescale_q(sub->pts + sub->start_display_time * 1000LL,
240                                  AV_TIME_BASE_Q, ist->st->time_base);
241         end_pts   = av_rescale_q(sub->pts + sub->end_display_time   * 1000LL,
242                                  AV_TIME_BASE_Q, ist->st->time_base);
243         num_rects = sub->num_rects;
244     } else {
245         pts       = ist->sub2video.end_pts;
246         end_pts   = INT64_MAX;
247         num_rects = 0;
248     }
249     if (sub2video_get_blank_frame(ist) < 0) {
250         av_log(ist->dec_ctx, AV_LOG_ERROR,
251                "Impossible to get a blank canvas.\n");
252         return;
253     }
254     dst          = frame->data    [0];
255     dst_linesize = frame->linesize[0];
256     for (i = 0; i < num_rects; i++)
257         sub2video_copy_rect(dst, dst_linesize, frame->width, frame->height, sub->rects[i]);
258     sub2video_push_ref(ist, pts);
259     ist->sub2video.end_pts = end_pts;
260 }
261
262 static void sub2video_heartbeat(InputStream *ist, int64_t pts)
263 {
264     InputFile *infile = input_files[ist->file_index];
265     int i, j, nb_reqs;
266     int64_t pts2;
267
268     /* When a frame is read from a file, examine all sub2video streams in
269        the same file and send the sub2video frame again. Otherwise, decoded
270        video frames could be accumulating in the filter graph while a filter
271        (possibly overlay) is desperately waiting for a subtitle frame. */
272     for (i = 0; i < infile->nb_streams; i++) {
273         InputStream *ist2 = input_streams[infile->ist_index + i];
274         if (!ist2->sub2video.frame)
275             continue;
276         /* subtitles seem to be usually muxed ahead of other streams;
277            if not, subtracting a larger time here is necessary */
278         pts2 = av_rescale_q(pts, ist->st->time_base, ist2->st->time_base) - 1;
279         /* do not send the heartbeat frame if the subtitle is already ahead */
280         if (pts2 <= ist2->sub2video.last_pts)
281             continue;
282         if (pts2 >= ist2->sub2video.end_pts || !ist2->sub2video.frame->data[0])
283             sub2video_update(ist2, NULL);
284         for (j = 0, nb_reqs = 0; j < ist2->nb_filters; j++)
285             nb_reqs += av_buffersrc_get_nb_failed_requests(ist2->filters[j]->filter);
286         if (nb_reqs)
287             sub2video_push_ref(ist2, pts2);
288     }
289 }
290
291 static void sub2video_flush(InputStream *ist)
292 {
293     int i;
294
295     if (ist->sub2video.end_pts < INT64_MAX)
296         sub2video_update(ist, NULL);
297     for (i = 0; i < ist->nb_filters; i++)
298         av_buffersrc_add_frame(ist->filters[i]->filter, NULL);
299 }
300
301 /* end of sub2video hack */
302
303 static void term_exit_sigsafe(void)
304 {
305 #if HAVE_TERMIOS_H
306     if(restore_tty)
307         tcsetattr (0, TCSANOW, &oldtty);
308 #endif
309 }
310
311 void term_exit(void)
312 {
313     av_log(NULL, AV_LOG_QUIET, "%s", "");
314     term_exit_sigsafe();
315 }
316
317 static volatile int received_sigterm = 0;
318 static volatile int received_nb_signals = 0;
319 static volatile int transcode_init_done = 0;
320 static volatile int ffmpeg_exited = 0;
321 static int main_return_code = 0;
322
323 static void
324 sigterm_handler(int sig)
325 {
326     received_sigterm = sig;
327     received_nb_signals++;
328     term_exit_sigsafe();
329     if(received_nb_signals > 3) {
330         write(2/*STDERR_FILENO*/, "Received > 3 system signals, hard exiting\n",
331                            strlen("Received > 3 system signals, hard exiting\n"));
332
333         exit(123);
334     }
335 }
336
337 #if HAVE_SETCONSOLECTRLHANDLER
338 static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
339 {
340     av_log(NULL, AV_LOG_DEBUG, "\nReceived windows signal %ld\n", fdwCtrlType);
341
342     switch (fdwCtrlType)
343     {
344     case CTRL_C_EVENT:
345     case CTRL_BREAK_EVENT:
346         sigterm_handler(SIGINT);
347         return TRUE;
348
349     case CTRL_CLOSE_EVENT:
350     case CTRL_LOGOFF_EVENT:
351     case CTRL_SHUTDOWN_EVENT:
352         sigterm_handler(SIGTERM);
353         /* Basically, with these 3 events, when we return from this method the
354            process is hard terminated, so stall as long as we need to
355            to try and let the main thread(s) clean up and gracefully terminate
356            (we have at most 5 seconds, but should be done far before that). */
357         while (!ffmpeg_exited) {
358             Sleep(0);
359         }
360         return TRUE;
361
362     default:
363         av_log(NULL, AV_LOG_ERROR, "Received unknown windows signal %ld\n", fdwCtrlType);
364         return FALSE;
365     }
366 }
367 #endif
368
369 void term_init(void)
370 {
371 #if HAVE_TERMIOS_H
372     if(!run_as_daemon){
373         struct termios tty;
374         int istty = 1;
375 #if HAVE_ISATTY
376         istty = isatty(0) && isatty(2);
377 #endif
378         if (istty && tcgetattr (0, &tty) == 0) {
379             oldtty = tty;
380             restore_tty = 1;
381
382             tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
383                              |INLCR|IGNCR|ICRNL|IXON);
384             tty.c_oflag |= OPOST;
385             tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
386             tty.c_cflag &= ~(CSIZE|PARENB);
387             tty.c_cflag |= CS8;
388             tty.c_cc[VMIN] = 1;
389             tty.c_cc[VTIME] = 0;
390
391             tcsetattr (0, TCSANOW, &tty);
392         }
393         signal(SIGQUIT, sigterm_handler); /* Quit (POSIX).  */
394     }
395 #endif
396
397     signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).    */
398     signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
399 #ifdef SIGXCPU
400     signal(SIGXCPU, sigterm_handler);
401 #endif
402 #if HAVE_SETCONSOLECTRLHANDLER
403     SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE);
404 #endif
405 }
406
407 /* read a key without blocking */
408 static int read_key(void)
409 {
410     unsigned char ch;
411 #if HAVE_TERMIOS_H
412     int n = 1;
413     struct timeval tv;
414     fd_set rfds;
415
416     FD_ZERO(&rfds);
417     FD_SET(0, &rfds);
418     tv.tv_sec = 0;
419     tv.tv_usec = 0;
420     n = select(1, &rfds, NULL, NULL, &tv);
421     if (n > 0) {
422         n = read(0, &ch, 1);
423         if (n == 1)
424             return ch;
425
426         return n;
427     }
428 #elif HAVE_KBHIT
429 #    if HAVE_PEEKNAMEDPIPE
430     static int is_pipe;
431     static HANDLE input_handle;
432     DWORD dw, nchars;
433     if(!input_handle){
434         input_handle = GetStdHandle(STD_INPUT_HANDLE);
435         is_pipe = !GetConsoleMode(input_handle, &dw);
436     }
437
438     if (is_pipe) {
439         /* When running under a GUI, you will end here. */
440         if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL)) {
441             // input pipe may have been closed by the program that ran ffmpeg
442             return -1;
443         }
444         //Read it
445         if(nchars != 0) {
446             read(0, &ch, 1);
447             return ch;
448         }else{
449             return -1;
450         }
451     }
452 #    endif
453     if(kbhit())
454         return(getch());
455 #endif
456     return -1;
457 }
458
459 static int decode_interrupt_cb(void *ctx)
460 {
461     return received_nb_signals > transcode_init_done;
462 }
463
464 const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
465
466 static void ffmpeg_cleanup(int ret)
467 {
468     int i, j;
469
470     if (do_benchmark) {
471         int maxrss = getmaxrss() / 1024;
472         av_log(NULL, AV_LOG_INFO, "bench: maxrss=%ikB\n", maxrss);
473     }
474
475     for (i = 0; i < nb_filtergraphs; i++) {
476         FilterGraph *fg = filtergraphs[i];
477         avfilter_graph_free(&fg->graph);
478         for (j = 0; j < fg->nb_inputs; j++) {
479             av_freep(&fg->inputs[j]->name);
480             av_freep(&fg->inputs[j]);
481         }
482         av_freep(&fg->inputs);
483         for (j = 0; j < fg->nb_outputs; j++) {
484             av_freep(&fg->outputs[j]->name);
485             av_freep(&fg->outputs[j]);
486         }
487         av_freep(&fg->outputs);
488         av_freep(&fg->graph_desc);
489
490         av_freep(&filtergraphs[i]);
491     }
492     av_freep(&filtergraphs);
493
494     av_freep(&subtitle_out);
495
496     /* close files */
497     for (i = 0; i < nb_output_files; i++) {
498         OutputFile *of = output_files[i];
499         AVFormatContext *s;
500         if (!of)
501             continue;
502         s = of->ctx;
503         if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE))
504             avio_closep(&s->pb);
505         avformat_free_context(s);
506         av_dict_free(&of->opts);
507
508         av_freep(&output_files[i]);
509     }
510     for (i = 0; i < nb_output_streams; i++) {
511         OutputStream *ost = output_streams[i];
512         AVBitStreamFilterContext *bsfc;
513
514         if (!ost)
515             continue;
516
517         bsfc = ost->bitstream_filters;
518         while (bsfc) {
519             AVBitStreamFilterContext *next = bsfc->next;
520             av_bitstream_filter_close(bsfc);
521             bsfc = next;
522         }
523         ost->bitstream_filters = NULL;
524         av_frame_free(&ost->filtered_frame);
525         av_frame_free(&ost->last_frame);
526
527         av_parser_close(ost->parser);
528
529         av_freep(&ost->forced_keyframes);
530         av_expr_free(ost->forced_keyframes_pexpr);
531         av_freep(&ost->avfilter);
532         av_freep(&ost->logfile_prefix);
533
534         av_freep(&ost->audio_channels_map);
535         ost->audio_channels_mapped = 0;
536
537         avcodec_free_context(&ost->enc_ctx);
538
539         av_freep(&output_streams[i]);
540     }
541 #if HAVE_PTHREADS
542     free_input_threads();
543 #endif
544     for (i = 0; i < nb_input_files; i++) {
545         avformat_close_input(&input_files[i]->ctx);
546         av_freep(&input_files[i]);
547     }
548     for (i = 0; i < nb_input_streams; i++) {
549         InputStream *ist = input_streams[i];
550
551         av_frame_free(&ist->decoded_frame);
552         av_frame_free(&ist->filter_frame);
553         av_dict_free(&ist->decoder_opts);
554         avsubtitle_free(&ist->prev_sub.subtitle);
555         av_frame_free(&ist->sub2video.frame);
556         av_freep(&ist->filters);
557         av_freep(&ist->hwaccel_device);
558
559         avcodec_free_context(&ist->dec_ctx);
560
561         av_freep(&input_streams[i]);
562     }
563
564     if (vstats_file)
565         fclose(vstats_file);
566     av_freep(&vstats_filename);
567
568     av_freep(&input_streams);
569     av_freep(&input_files);
570     av_freep(&output_streams);
571     av_freep(&output_files);
572
573     uninit_opts();
574
575     avformat_network_deinit();
576
577     if (received_sigterm) {
578         av_log(NULL, AV_LOG_INFO, "Exiting normally, received signal %d.\n",
579                (int) received_sigterm);
580     } else if (ret && transcode_init_done) {
581         av_log(NULL, AV_LOG_INFO, "Conversion failed!\n");
582     }
583     term_exit();
584     ffmpeg_exited = 1;
585 }
586
587 void remove_avoptions(AVDictionary **a, AVDictionary *b)
588 {
589     AVDictionaryEntry *t = NULL;
590
591     while ((t = av_dict_get(b, "", t, AV_DICT_IGNORE_SUFFIX))) {
592         av_dict_set(a, t->key, NULL, AV_DICT_MATCH_CASE);
593     }
594 }
595
596 void assert_avoptions(AVDictionary *m)
597 {
598     AVDictionaryEntry *t;
599     if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
600         av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
601         exit_program(1);
602     }
603 }
604
605 static void abort_codec_experimental(AVCodec *c, int encoder)
606 {
607     exit_program(1);
608 }
609
610 static void update_benchmark(const char *fmt, ...)
611 {
612     if (do_benchmark_all) {
613         int64_t t = getutime();
614         va_list va;
615         char buf[1024];
616
617         if (fmt) {
618             va_start(va, fmt);
619             vsnprintf(buf, sizeof(buf), fmt, va);
620             va_end(va);
621             av_log(NULL, AV_LOG_INFO, "bench: %8"PRIu64" %s \n", t - current_time, buf);
622         }
623         current_time = t;
624     }
625 }
626
627 static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others)
628 {
629     int i;
630     for (i = 0; i < nb_output_streams; i++) {
631         OutputStream *ost2 = output_streams[i];
632         ost2->finished |= ost == ost2 ? this_stream : others;
633     }
634 }
635
636 static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
637 {
638     AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
639     AVCodecContext          *avctx = ost->encoding_needed ? ost->enc_ctx : ost->st->codec;
640     int ret;
641
642     if (!ost->st->codec->extradata_size && ost->enc_ctx->extradata_size) {
643         ost->st->codec->extradata = av_mallocz(ost->enc_ctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
644         if (ost->st->codec->extradata) {
645             memcpy(ost->st->codec->extradata, ost->enc_ctx->extradata, ost->enc_ctx->extradata_size);
646             ost->st->codec->extradata_size = ost->enc_ctx->extradata_size;
647         }
648     }
649
650     if ((avctx->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) ||
651         (avctx->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0))
652         pkt->pts = pkt->dts = AV_NOPTS_VALUE;
653
654     /*
655      * Audio encoders may split the packets --  #frames in != #packets out.
656      * But there is no reordering, so we can limit the number of output packets
657      * by simply dropping them here.
658      * Counting encoded video frames needs to be done separately because of
659      * reordering, see do_video_out()
660      */
661     if (!(avctx->codec_type == AVMEDIA_TYPE_VIDEO && avctx->codec)) {
662         if (ost->frame_number >= ost->max_frames) {
663             av_free_packet(pkt);
664             return;
665         }
666         ost->frame_number++;
667     }
668     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
669         int i;
670         uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
671                                               NULL);
672         ost->quality = sd ? AV_RL32(sd) : -1;
673         ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
674
675         for (i = 0; i<FF_ARRAY_ELEMS(ost->error); i++) {
676             if (sd && i < sd[5])
677                 ost->error[i] = AV_RL64(sd + 8 + 8*i);
678             else
679                 ost->error[i] = -1;
680         }
681     }
682
683     if (bsfc)
684         av_packet_split_side_data(pkt);
685
686     while (bsfc) {
687         AVPacket new_pkt = *pkt;
688         AVDictionaryEntry *bsf_arg = av_dict_get(ost->bsf_args,
689                                                  bsfc->filter->name,
690                                                  NULL, 0);
691         int a = av_bitstream_filter_filter(bsfc, avctx,
692                                            bsf_arg ? bsf_arg->value : NULL,
693                                            &new_pkt.data, &new_pkt.size,
694                                            pkt->data, pkt->size,
695                                            pkt->flags & AV_PKT_FLAG_KEY);
696         if(a == 0 && new_pkt.data != pkt->data && new_pkt.destruct) {
697             uint8_t *t = av_malloc(new_pkt.size + AV_INPUT_BUFFER_PADDING_SIZE); //the new should be a subset of the old so cannot overflow
698             if(t) {
699                 memcpy(t, new_pkt.data, new_pkt.size);
700                 memset(t + new_pkt.size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
701                 new_pkt.data = t;
702                 new_pkt.buf = NULL;
703                 a = 1;
704             } else
705                 a = AVERROR(ENOMEM);
706         }
707         if (a > 0) {
708             pkt->side_data = NULL;
709             pkt->side_data_elems = 0;
710             av_free_packet(pkt);
711             new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size,
712                                            av_buffer_default_free, NULL, 0);
713             if (!new_pkt.buf)
714                 exit_program(1);
715         } else if (a < 0) {
716             new_pkt = *pkt;
717             av_log(NULL, AV_LOG_ERROR, "Failed to open bitstream filter %s for stream %d with codec %s",
718                    bsfc->filter->name, pkt->stream_index,
719                    avctx->codec ? avctx->codec->name : "copy");
720             print_error("", a);
721             if (exit_on_error)
722                 exit_program(1);
723         }
724         *pkt = new_pkt;
725
726         bsfc = bsfc->next;
727     }
728
729     if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
730         if (pkt->dts != AV_NOPTS_VALUE &&
731             pkt->pts != AV_NOPTS_VALUE &&
732             pkt->dts > pkt->pts) {
733             av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d, replacing by guess\n",
734                    pkt->dts, pkt->pts,
735                    ost->file_index, ost->st->index);
736             pkt->pts =
737             pkt->dts = pkt->pts + pkt->dts + ost->last_mux_dts + 1
738                      - FFMIN3(pkt->pts, pkt->dts, ost->last_mux_dts + 1)
739                      - FFMAX3(pkt->pts, pkt->dts, ost->last_mux_dts + 1);
740         }
741      if(
742         (avctx->codec_type == AVMEDIA_TYPE_AUDIO || avctx->codec_type == AVMEDIA_TYPE_VIDEO) &&
743         pkt->dts != AV_NOPTS_VALUE &&
744         ost->last_mux_dts != AV_NOPTS_VALUE) {
745       int64_t max = ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
746       if (pkt->dts < max) {
747         int loglevel = max - pkt->dts > 2 || avctx->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
748         av_log(s, loglevel, "Non-monotonous DTS in output stream "
749                "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
750                ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts);
751         if (exit_on_error) {
752             av_log(NULL, AV_LOG_FATAL, "aborting.\n");
753             exit_program(1);
754         }
755         av_log(s, loglevel, "changing to %"PRId64". This may result "
756                "in incorrect timestamps in the output file.\n",
757                max);
758         if(pkt->pts >= pkt->dts)
759             pkt->pts = FFMAX(pkt->pts, max);
760         pkt->dts = max;
761       }
762      }
763     }
764     ost->last_mux_dts = pkt->dts;
765
766     ost->data_size += pkt->size;
767     ost->packets_written++;
768
769     pkt->stream_index = ost->index;
770
771     if (debug_ts) {
772         av_log(NULL, AV_LOG_INFO, "muxer <- type:%s "
773                 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n",
774                 av_get_media_type_string(ost->enc_ctx->codec_type),
775                 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
776                 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
777                 pkt->size
778               );
779     }
780
781     ret = av_interleaved_write_frame(s, pkt);
782     if (ret < 0) {
783         print_error("av_interleaved_write_frame()", ret);
784         main_return_code = 1;
785         close_all_output_streams(ost, MUXER_FINISHED | ENCODER_FINISHED, ENCODER_FINISHED);
786     }
787     av_free_packet(pkt);
788 }
789
790 static void close_output_stream(OutputStream *ost)
791 {
792     OutputFile *of = output_files[ost->file_index];
793
794     ost->finished |= ENCODER_FINISHED;
795     if (of->shortest) {
796         int64_t end = av_rescale_q(ost->sync_opts - ost->first_pts, ost->enc_ctx->time_base, AV_TIME_BASE_Q);
797         of->recording_time = FFMIN(of->recording_time, end);
798     }
799 }
800
801 static int check_recording_time(OutputStream *ost)
802 {
803     OutputFile *of = output_files[ost->file_index];
804
805     if (of->recording_time != INT64_MAX &&
806         av_compare_ts(ost->sync_opts - ost->first_pts, ost->enc_ctx->time_base, of->recording_time,
807                       AV_TIME_BASE_Q) >= 0) {
808         close_output_stream(ost);
809         return 0;
810     }
811     return 1;
812 }
813
814 static void do_audio_out(AVFormatContext *s, OutputStream *ost,
815                          AVFrame *frame)
816 {
817     AVCodecContext *enc = ost->enc_ctx;
818     AVPacket pkt;
819     int got_packet = 0;
820
821     av_init_packet(&pkt);
822     pkt.data = NULL;
823     pkt.size = 0;
824
825     if (!check_recording_time(ost))
826         return;
827
828     if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
829         frame->pts = ost->sync_opts;
830     ost->sync_opts = frame->pts + frame->nb_samples;
831     ost->samples_encoded += frame->nb_samples;
832     ost->frames_encoded++;
833
834     av_assert0(pkt.size || !pkt.data);
835     update_benchmark(NULL);
836     if (debug_ts) {
837         av_log(NULL, AV_LOG_INFO, "encoder <- type:audio "
838                "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
839                av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
840                enc->time_base.num, enc->time_base.den);
841     }
842
843     if (avcodec_encode_audio2(enc, &pkt, frame, &got_packet) < 0) {
844         av_log(NULL, AV_LOG_FATAL, "Audio encoding failed (avcodec_encode_audio2)\n");
845         exit_program(1);
846     }
847     update_benchmark("encode_audio %d.%d", ost->file_index, ost->index);
848
849     if (got_packet) {
850         av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base);
851
852         if (debug_ts) {
853             av_log(NULL, AV_LOG_INFO, "encoder -> type:audio "
854                    "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
855                    av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->st->time_base),
856                    av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base));
857         }
858
859         write_frame(s, &pkt, ost);
860     }
861 }
862
863 static void do_subtitle_out(AVFormatContext *s,
864                             OutputStream *ost,
865                             InputStream *ist,
866                             AVSubtitle *sub)
867 {
868     int subtitle_out_max_size = 1024 * 1024;
869     int subtitle_out_size, nb, i;
870     AVCodecContext *enc;
871     AVPacket pkt;
872     int64_t pts;
873
874     if (sub->pts == AV_NOPTS_VALUE) {
875         av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
876         if (exit_on_error)
877             exit_program(1);
878         return;
879     }
880
881     enc = ost->enc_ctx;
882
883     if (!subtitle_out) {
884         subtitle_out = av_malloc(subtitle_out_max_size);
885         if (!subtitle_out) {
886             av_log(NULL, AV_LOG_FATAL, "Failed to allocate subtitle_out\n");
887             exit_program(1);
888         }
889     }
890
891     /* Note: DVB subtitle need one packet to draw them and one other
892        packet to clear them */
893     /* XXX: signal it in the codec context ? */
894     if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
895         nb = 2;
896     else
897         nb = 1;
898
899     /* shift timestamp to honor -ss and make check_recording_time() work with -t */
900     pts = sub->pts;
901     if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
902         pts -= output_files[ost->file_index]->start_time;
903     for (i = 0; i < nb; i++) {
904         unsigned save_num_rects = sub->num_rects;
905
906         ost->sync_opts = av_rescale_q(pts, AV_TIME_BASE_Q, enc->time_base);
907         if (!check_recording_time(ost))
908             return;
909
910         sub->pts = pts;
911         // start_display_time is required to be 0
912         sub->pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
913         sub->end_display_time  -= sub->start_display_time;
914         sub->start_display_time = 0;
915         if (i == 1)
916             sub->num_rects = 0;
917
918         ost->frames_encoded++;
919
920         subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
921                                                     subtitle_out_max_size, sub);
922         if (i == 1)
923             sub->num_rects = save_num_rects;
924         if (subtitle_out_size < 0) {
925             av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
926             exit_program(1);
927         }
928
929         av_init_packet(&pkt);
930         pkt.data = subtitle_out;
931         pkt.size = subtitle_out_size;
932         pkt.pts  = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base);
933         pkt.duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->st->time_base);
934         if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
935             /* XXX: the pts correction is handled here. Maybe handling
936                it in the codec would be better */
937             if (i == 0)
938                 pkt.pts += 90 * sub->start_display_time;
939             else
940                 pkt.pts += 90 * sub->end_display_time;
941         }
942         pkt.dts = pkt.pts;
943         write_frame(s, &pkt, ost);
944     }
945 }
946
947 static void do_video_out(AVFormatContext *s,
948                          OutputStream *ost,
949                          AVFrame *next_picture,
950                          double sync_ipts)
951 {
952     int ret, format_video_sync;
953     AVPacket pkt;
954     AVCodecContext *enc = ost->enc_ctx;
955     AVCodecContext *mux_enc = ost->st->codec;
956     int nb_frames, nb0_frames, i;
957     double delta, delta0;
958     double duration = 0;
959     int frame_size = 0;
960     InputStream *ist = NULL;
961     AVFilterContext *filter = ost->filter->filter;
962
963     if (ost->source_index >= 0)
964         ist = input_streams[ost->source_index];
965
966     if (filter->inputs[0]->frame_rate.num > 0 &&
967         filter->inputs[0]->frame_rate.den > 0)
968         duration = 1/(av_q2d(filter->inputs[0]->frame_rate) * av_q2d(enc->time_base));
969
970     if(ist && ist->st->start_time != AV_NOPTS_VALUE && ist->st->first_dts != AV_NOPTS_VALUE && ost->frame_rate.num)
971         duration = FFMIN(duration, 1/(av_q2d(ost->frame_rate) * av_q2d(enc->time_base)));
972
973     if (!ost->filters_script &&
974         !ost->filters &&
975         next_picture &&
976         ist &&
977         lrintf(av_frame_get_pkt_duration(next_picture) * av_q2d(ist->st->time_base) / av_q2d(enc->time_base)) > 0) {
978         duration = lrintf(av_frame_get_pkt_duration(next_picture) * av_q2d(ist->st->time_base) / av_q2d(enc->time_base));
979     }
980
981     if (!next_picture) {
982         //end, flushing
983         nb0_frames = nb_frames = mid_pred(ost->last_nb0_frames[0],
984                                           ost->last_nb0_frames[1],
985                                           ost->last_nb0_frames[2]);
986     } else {
987         delta0 = sync_ipts - ost->sync_opts;
988         delta  = delta0 + duration;
989
990         /* by default, we output a single frame */
991         nb0_frames = 0;
992         nb_frames = 1;
993
994         format_video_sync = video_sync_method;
995         if (format_video_sync == VSYNC_AUTO) {
996             if(!strcmp(s->oformat->name, "avi")) {
997                 format_video_sync = VSYNC_VFR;
998             } else
999                 format_video_sync = (s->oformat->flags & AVFMT_VARIABLE_FPS) ? ((s->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH : VSYNC_VFR) : VSYNC_CFR;
1000             if (   ist
1001                 && format_video_sync == VSYNC_CFR
1002                 && input_files[ist->file_index]->ctx->nb_streams == 1
1003                 && input_files[ist->file_index]->input_ts_offset == 0) {
1004                 format_video_sync = VSYNC_VSCFR;
1005             }
1006             if (format_video_sync == VSYNC_CFR && copy_ts) {
1007                 format_video_sync = VSYNC_VSCFR;
1008             }
1009         }
1010
1011         if (delta0 < 0 &&
1012             delta > 0 &&
1013             format_video_sync != VSYNC_PASSTHROUGH &&
1014             format_video_sync != VSYNC_DROP) {
1015             double cor = FFMIN(-delta0, duration);
1016             if (delta0 < -0.6) {
1017                 av_log(NULL, AV_LOG_WARNING, "Past duration %f too large\n", -delta0);
1018             } else
1019                 av_log(NULL, AV_LOG_DEBUG, "Cliping frame in rate conversion by %f\n", -delta0);
1020             sync_ipts += cor;
1021             duration -= cor;
1022             delta0 += cor;
1023         }
1024
1025         switch (format_video_sync) {
1026         case VSYNC_VSCFR:
1027             if (ost->frame_number == 0 && delta - duration >= 0.5) {
1028                 av_log(NULL, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta - duration));
1029                 delta = duration;
1030                 delta0 = 0;
1031                 ost->sync_opts = lrint(sync_ipts);
1032             }
1033         case VSYNC_CFR:
1034             // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
1035             if (frame_drop_threshold && delta < frame_drop_threshold && ost->frame_number) {
1036                 nb_frames = 0;
1037             } else if (delta < -1.1)
1038                 nb_frames = 0;
1039             else if (delta > 1.1) {
1040                 nb_frames = lrintf(delta);
1041                 if (delta0 > 1.1)
1042                     nb0_frames = lrintf(delta0 - 0.6);
1043             }
1044             break;
1045         case VSYNC_VFR:
1046             if (delta <= -0.6)
1047                 nb_frames = 0;
1048             else if (delta > 0.6)
1049                 ost->sync_opts = lrint(sync_ipts);
1050             break;
1051         case VSYNC_DROP:
1052         case VSYNC_PASSTHROUGH:
1053             ost->sync_opts = lrint(sync_ipts);
1054             break;
1055         default:
1056             av_assert0(0);
1057         }
1058     }
1059
1060     nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number);
1061     nb0_frames = FFMIN(nb0_frames, nb_frames);
1062
1063     memmove(ost->last_nb0_frames + 1,
1064             ost->last_nb0_frames,
1065             sizeof(ost->last_nb0_frames[0]) * (FF_ARRAY_ELEMS(ost->last_nb0_frames) - 1));
1066     ost->last_nb0_frames[0] = nb0_frames;
1067
1068     if (nb0_frames == 0 && ost->last_droped) {
1069         nb_frames_drop++;
1070         av_log(NULL, AV_LOG_VERBOSE,
1071                "*** dropping frame %d from stream %d at ts %"PRId64"\n",
1072                ost->frame_number, ost->st->index, ost->last_frame->pts);
1073     }
1074     if (nb_frames > (nb0_frames && ost->last_droped) + (nb_frames > nb0_frames)) {
1075         if (nb_frames > dts_error_threshold * 30) {
1076             av_log(NULL, AV_LOG_ERROR, "%d frame duplication too large, skipping\n", nb_frames - 1);
1077             nb_frames_drop++;
1078             return;
1079         }
1080         nb_frames_dup += nb_frames - (nb0_frames && ost->last_droped) - (nb_frames > nb0_frames);
1081         av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", nb_frames - 1);
1082     }
1083     ost->last_droped = nb_frames == nb0_frames && next_picture;
1084
1085   /* duplicates frame if needed */
1086   for (i = 0; i < nb_frames; i++) {
1087     AVFrame *in_picture;
1088     av_init_packet(&pkt);
1089     pkt.data = NULL;
1090     pkt.size = 0;
1091
1092     if (i < nb0_frames && ost->last_frame) {
1093         in_picture = ost->last_frame;
1094     } else
1095         in_picture = next_picture;
1096
1097     if (!in_picture)
1098         return;
1099
1100     in_picture->pts = ost->sync_opts;
1101
1102 #if 1
1103     if (!check_recording_time(ost))
1104 #else
1105     if (ost->frame_number >= ost->max_frames)
1106 #endif
1107         return;
1108
1109     if (s->oformat->flags & AVFMT_RAWPICTURE &&
1110         enc->codec->id == AV_CODEC_ID_RAWVIDEO) {
1111         /* raw pictures are written as AVPicture structure to
1112            avoid any copies. We support temporarily the older
1113            method. */
1114         if (in_picture->interlaced_frame)
1115             mux_enc->field_order = in_picture->top_field_first ? AV_FIELD_TB:AV_FIELD_BT;
1116         else
1117             mux_enc->field_order = AV_FIELD_PROGRESSIVE;
1118         pkt.data   = (uint8_t *)in_picture;
1119         pkt.size   =  sizeof(AVPicture);
1120         pkt.pts    = av_rescale_q(in_picture->pts, enc->time_base, ost->st->time_base);
1121         pkt.flags |= AV_PKT_FLAG_KEY;
1122
1123         write_frame(s, &pkt, ost);
1124     } else {
1125         int got_packet, forced_keyframe = 0;
1126         double pts_time;
1127
1128         if (enc->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) &&
1129             ost->top_field_first >= 0)
1130             in_picture->top_field_first = !!ost->top_field_first;
1131
1132         if (in_picture->interlaced_frame) {
1133             if (enc->codec->id == AV_CODEC_ID_MJPEG)
1134                 mux_enc->field_order = in_picture->top_field_first ? AV_FIELD_TT:AV_FIELD_BB;
1135             else
1136                 mux_enc->field_order = in_picture->top_field_first ? AV_FIELD_TB:AV_FIELD_BT;
1137         } else
1138             mux_enc->field_order = AV_FIELD_PROGRESSIVE;
1139
1140         in_picture->quality = enc->global_quality;
1141         in_picture->pict_type = 0;
1142
1143         pts_time = in_picture->pts != AV_NOPTS_VALUE ?
1144             in_picture->pts * av_q2d(enc->time_base) : NAN;
1145         if (ost->forced_kf_index < ost->forced_kf_count &&
1146             in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
1147             ost->forced_kf_index++;
1148             forced_keyframe = 1;
1149         } else if (ost->forced_keyframes_pexpr) {
1150             double res;
1151             ost->forced_keyframes_expr_const_values[FKF_T] = pts_time;
1152             res = av_expr_eval(ost->forced_keyframes_pexpr,
1153                                ost->forced_keyframes_expr_const_values, NULL);
1154             ff_dlog(NULL, "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
1155                     ost->forced_keyframes_expr_const_values[FKF_N],
1156                     ost->forced_keyframes_expr_const_values[FKF_N_FORCED],
1157                     ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N],
1158                     ost->forced_keyframes_expr_const_values[FKF_T],
1159                     ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T],
1160                     res);
1161             if (res) {
1162                 forced_keyframe = 1;
1163                 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] =
1164                     ost->forced_keyframes_expr_const_values[FKF_N];
1165                 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] =
1166                     ost->forced_keyframes_expr_const_values[FKF_T];
1167                 ost->forced_keyframes_expr_const_values[FKF_N_FORCED] += 1;
1168             }
1169
1170             ost->forced_keyframes_expr_const_values[FKF_N] += 1;
1171         } else if (   ost->forced_keyframes
1172                    && !strncmp(ost->forced_keyframes, "source", 6)
1173                    && in_picture->key_frame==1) {
1174             forced_keyframe = 1;
1175         }
1176
1177         if (forced_keyframe) {
1178             in_picture->pict_type = AV_PICTURE_TYPE_I;
1179             av_log(NULL, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
1180         }
1181
1182         update_benchmark(NULL);
1183         if (debug_ts) {
1184             av_log(NULL, AV_LOG_INFO, "encoder <- type:video "
1185                    "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
1186                    av_ts2str(in_picture->pts), av_ts2timestr(in_picture->pts, &enc->time_base),
1187                    enc->time_base.num, enc->time_base.den);
1188         }
1189
1190         ost->frames_encoded++;
1191
1192         ret = avcodec_encode_video2(enc, &pkt, in_picture, &got_packet);
1193         update_benchmark("encode_video %d.%d", ost->file_index, ost->index);
1194         if (ret < 0) {
1195             av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
1196             exit_program(1);
1197         }
1198
1199         if (got_packet) {
1200             if (debug_ts) {
1201                 av_log(NULL, AV_LOG_INFO, "encoder -> type:video "
1202                        "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1203                        av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &enc->time_base),
1204                        av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &enc->time_base));
1205             }
1206
1207             if (pkt.pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & AV_CODEC_CAP_DELAY))
1208                 pkt.pts = ost->sync_opts;
1209
1210             av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base);
1211
1212             if (debug_ts) {
1213                 av_log(NULL, AV_LOG_INFO, "encoder -> type:video "
1214                     "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1215                     av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->st->time_base),
1216                     av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base));
1217             }
1218
1219             frame_size = pkt.size;
1220             write_frame(s, &pkt, ost);
1221
1222             /* if two pass, output log */
1223             if (ost->logfile && enc->stats_out) {
1224                 fprintf(ost->logfile, "%s", enc->stats_out);
1225             }
1226         }
1227     }
1228     ost->sync_opts++;
1229     /*
1230      * For video, number of frames in == number of packets out.
1231      * But there may be reordering, so we can't throw away frames on encoder
1232      * flush, we need to limit them here, before they go into encoder.
1233      */
1234     ost->frame_number++;
1235
1236     if (vstats_filename && frame_size)
1237         do_video_stats(ost, frame_size);
1238   }
1239
1240     if (!ost->last_frame)
1241         ost->last_frame = av_frame_alloc();
1242     av_frame_unref(ost->last_frame);
1243     if (next_picture && ost->last_frame)
1244         av_frame_ref(ost->last_frame, next_picture);
1245     else
1246         av_frame_free(&ost->last_frame);
1247 }
1248
1249 static double psnr(double d)
1250 {
1251     return -10.0 * log(d) / log(10.0);
1252 }
1253
1254 static void do_video_stats(OutputStream *ost, int frame_size)
1255 {
1256     AVCodecContext *enc;
1257     int frame_number;
1258     double ti1, bitrate, avg_bitrate;
1259
1260     /* this is executed just the first time do_video_stats is called */
1261     if (!vstats_file) {
1262         vstats_file = fopen(vstats_filename, "w");
1263         if (!vstats_file) {
1264             perror("fopen");
1265             exit_program(1);
1266         }
1267     }
1268
1269     enc = ost->enc_ctx;
1270     if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1271         frame_number = ost->st->nb_frames;
1272         fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
1273                 ost->quality / (float)FF_QP2LAMBDA);
1274
1275         if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
1276             fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
1277
1278         fprintf(vstats_file,"f_size= %6d ", frame_size);
1279         /* compute pts value */
1280         ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base);
1281         if (ti1 < 0.01)
1282             ti1 = 0.01;
1283
1284         bitrate     = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
1285         avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
1286         fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1287                (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
1288         fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type));
1289     }
1290 }
1291
1292 static void finish_output_stream(OutputStream *ost)
1293 {
1294     OutputFile *of = output_files[ost->file_index];
1295     int i;
1296
1297     ost->finished = ENCODER_FINISHED | MUXER_FINISHED;
1298
1299     if (of->shortest) {
1300         for (i = 0; i < of->ctx->nb_streams; i++)
1301             output_streams[of->ost_index + i]->finished = ENCODER_FINISHED | MUXER_FINISHED;
1302     }
1303 }
1304
1305 /**
1306  * Get and encode new output from any of the filtergraphs, without causing
1307  * activity.
1308  *
1309  * @return  0 for success, <0 for severe errors
1310  */
1311 static int reap_filters(int flush)
1312 {
1313     AVFrame *filtered_frame = NULL;
1314     int i;
1315
1316     /* Reap all buffers present in the buffer sinks */
1317     for (i = 0; i < nb_output_streams; i++) {
1318         OutputStream *ost = output_streams[i];
1319         OutputFile    *of = output_files[ost->file_index];
1320         AVFilterContext *filter;
1321         AVCodecContext *enc = ost->enc_ctx;
1322         int ret = 0;
1323
1324         if (!ost->filter)
1325             continue;
1326         filter = ost->filter->filter;
1327
1328         if (!ost->filtered_frame && !(ost->filtered_frame = av_frame_alloc())) {
1329             return AVERROR(ENOMEM);
1330         }
1331         filtered_frame = ost->filtered_frame;
1332
1333         while (1) {
1334             double float_pts = AV_NOPTS_VALUE; // this is identical to filtered_frame.pts but with higher precision
1335             ret = av_buffersink_get_frame_flags(filter, filtered_frame,
1336                                                AV_BUFFERSINK_FLAG_NO_REQUEST);
1337             if (ret < 0) {
1338                 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
1339                     av_log(NULL, AV_LOG_WARNING,
1340                            "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret));
1341                 } else if (flush && ret == AVERROR_EOF) {
1342                     if (filter->inputs[0]->type == AVMEDIA_TYPE_VIDEO)
1343                         do_video_out(of->ctx, ost, NULL, AV_NOPTS_VALUE);
1344                 }
1345                 break;
1346             }
1347             if (ost->finished) {
1348                 av_frame_unref(filtered_frame);
1349                 continue;
1350             }
1351             if (filtered_frame->pts != AV_NOPTS_VALUE) {
1352                 int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
1353                 AVRational tb = enc->time_base;
1354                 int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
1355
1356                 tb.den <<= extra_bits;
1357                 float_pts =
1358                     av_rescale_q(filtered_frame->pts, filter->inputs[0]->time_base, tb) -
1359                     av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
1360                 float_pts /= 1 << extra_bits;
1361                 // avoid exact midoints to reduce the chance of rounding differences, this can be removed in case the fps code is changed to work with integers
1362                 float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
1363
1364                 filtered_frame->pts =
1365                     av_rescale_q(filtered_frame->pts, filter->inputs[0]->time_base, enc->time_base) -
1366                     av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
1367             }
1368             //if (ost->source_index >= 0)
1369             //    *filtered_frame= *input_streams[ost->source_index]->decoded_frame; //for me_threshold
1370
1371             switch (filter->inputs[0]->type) {
1372             case AVMEDIA_TYPE_VIDEO:
1373                 if (!ost->frame_aspect_ratio.num)
1374                     enc->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
1375
1376                 if (debug_ts) {
1377                     av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
1378                             av_ts2str(filtered_frame->pts), av_ts2timestr(filtered_frame->pts, &enc->time_base),
1379                             float_pts,
1380                             enc->time_base.num, enc->time_base.den);
1381                 }
1382
1383                 do_video_out(of->ctx, ost, filtered_frame, float_pts);
1384                 break;
1385             case AVMEDIA_TYPE_AUDIO:
1386                 if (!(enc->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
1387                     enc->channels != av_frame_get_channels(filtered_frame)) {
1388                     av_log(NULL, AV_LOG_ERROR,
1389                            "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
1390                     break;
1391                 }
1392                 do_audio_out(of->ctx, ost, filtered_frame);
1393                 break;
1394             default:
1395                 // TODO support subtitle filters
1396                 av_assert0(0);
1397             }
1398
1399             av_frame_unref(filtered_frame);
1400         }
1401     }
1402
1403     return 0;
1404 }
1405
1406 static void print_final_stats(int64_t total_size)
1407 {
1408     uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
1409     uint64_t subtitle_size = 0;
1410     uint64_t data_size = 0;
1411     float percent = -1.0;
1412     int i, j;
1413     int pass1_used = 1;
1414
1415     for (i = 0; i < nb_output_streams; i++) {
1416         OutputStream *ost = output_streams[i];
1417         switch (ost->enc_ctx->codec_type) {
1418             case AVMEDIA_TYPE_VIDEO: video_size += ost->data_size; break;
1419             case AVMEDIA_TYPE_AUDIO: audio_size += ost->data_size; break;
1420             case AVMEDIA_TYPE_SUBTITLE: subtitle_size += ost->data_size; break;
1421             default:                 other_size += ost->data_size; break;
1422         }
1423         extra_size += ost->enc_ctx->extradata_size;
1424         data_size  += ost->data_size;
1425         if (   (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))
1426             != AV_CODEC_FLAG_PASS1)
1427             pass1_used = 0;
1428     }
1429
1430     if (data_size && total_size>0 && total_size >= data_size)
1431         percent = 100.0 * (total_size - data_size) / data_size;
1432
1433     av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB subtitle:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ",
1434            video_size / 1024.0,
1435            audio_size / 1024.0,
1436            subtitle_size / 1024.0,
1437            other_size / 1024.0,
1438            extra_size / 1024.0);
1439     if (percent >= 0.0)
1440         av_log(NULL, AV_LOG_INFO, "%f%%", percent);
1441     else
1442         av_log(NULL, AV_LOG_INFO, "unknown");
1443     av_log(NULL, AV_LOG_INFO, "\n");
1444
1445     /* print verbose per-stream stats */
1446     for (i = 0; i < nb_input_files; i++) {
1447         InputFile *f = input_files[i];
1448         uint64_t total_packets = 0, total_size = 0;
1449
1450         av_log(NULL, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
1451                i, f->ctx->filename);
1452
1453         for (j = 0; j < f->nb_streams; j++) {
1454             InputStream *ist = input_streams[f->ist_index + j];
1455             enum AVMediaType type = ist->dec_ctx->codec_type;
1456
1457             total_size    += ist->data_size;
1458             total_packets += ist->nb_packets;
1459
1460             av_log(NULL, AV_LOG_VERBOSE, "  Input stream #%d:%d (%s): ",
1461                    i, j, media_type_string(type));
1462             av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
1463                    ist->nb_packets, ist->data_size);
1464
1465             if (ist->decoding_needed) {
1466                 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames decoded",
1467                        ist->frames_decoded);
1468                 if (type == AVMEDIA_TYPE_AUDIO)
1469                     av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
1470                 av_log(NULL, AV_LOG_VERBOSE, "; ");
1471             }
1472
1473             av_log(NULL, AV_LOG_VERBOSE, "\n");
1474         }
1475
1476         av_log(NULL, AV_LOG_VERBOSE, "  Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
1477                total_packets, total_size);
1478     }
1479
1480     for (i = 0; i < nb_output_files; i++) {
1481         OutputFile *of = output_files[i];
1482         uint64_t total_packets = 0, total_size = 0;
1483
1484         av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
1485                i, of->ctx->filename);
1486
1487         for (j = 0; j < of->ctx->nb_streams; j++) {
1488             OutputStream *ost = output_streams[of->ost_index + j];
1489             enum AVMediaType type = ost->enc_ctx->codec_type;
1490
1491             total_size    += ost->data_size;
1492             total_packets += ost->packets_written;
1493
1494             av_log(NULL, AV_LOG_VERBOSE, "  Output stream #%d:%d (%s): ",
1495                    i, j, media_type_string(type));
1496             if (ost->encoding_needed) {
1497                 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
1498                        ost->frames_encoded);
1499                 if (type == AVMEDIA_TYPE_AUDIO)
1500                     av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
1501                 av_log(NULL, AV_LOG_VERBOSE, "; ");
1502             }
1503
1504             av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
1505                    ost->packets_written, ost->data_size);
1506
1507             av_log(NULL, AV_LOG_VERBOSE, "\n");
1508         }
1509
1510         av_log(NULL, AV_LOG_VERBOSE, "  Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
1511                total_packets, total_size);
1512     }
1513     if(video_size + data_size + audio_size + subtitle_size + extra_size == 0){
1514         av_log(NULL, AV_LOG_WARNING, "Output file is empty, nothing was encoded ");
1515         if (pass1_used) {
1516             av_log(NULL, AV_LOG_WARNING, "\n");
1517         } else {
1518             av_log(NULL, AV_LOG_WARNING, "(check -ss / -t / -frames parameters if used)\n");
1519         }
1520     }
1521 }
1522
1523 static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time)
1524 {
1525     char buf[1024];
1526     AVBPrint buf_script;
1527     OutputStream *ost;
1528     AVFormatContext *oc;
1529     int64_t total_size;
1530     AVCodecContext *enc;
1531     int frame_number, vid, i;
1532     double bitrate;
1533     int64_t pts = INT64_MIN;
1534     static int64_t last_time = -1;
1535     static int qp_histogram[52];
1536     int hours, mins, secs, us;
1537
1538     if (!print_stats && !is_last_report && !progress_avio)
1539         return;
1540
1541     if (!is_last_report) {
1542         if (last_time == -1) {
1543             last_time = cur_time;
1544             return;
1545         }
1546         if ((cur_time - last_time) < 500000)
1547             return;
1548         last_time = cur_time;
1549     }
1550
1551
1552     oc = output_files[0]->ctx;
1553
1554     total_size = avio_size(oc->pb);
1555     if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too
1556         total_size = avio_tell(oc->pb);
1557
1558     buf[0] = '\0';
1559     vid = 0;
1560     av_bprint_init(&buf_script, 0, 1);
1561     for (i = 0; i < nb_output_streams; i++) {
1562         float q = -1;
1563         ost = output_streams[i];
1564         enc = ost->enc_ctx;
1565         if (!ost->stream_copy)
1566             q = ost->quality / (float) FF_QP2LAMBDA;
1567
1568         if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1569             snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q);
1570             av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
1571                        ost->file_index, ost->index, q);
1572         }
1573         if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1574             float fps, t = (cur_time-timer_start) / 1000000.0;
1575
1576             frame_number = ost->frame_number;
1577             fps = t > 1 ? frame_number / t : 0;
1578             snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3.*f q=%3.1f ",
1579                      frame_number, fps < 9.95, fps, q);
1580             av_bprintf(&buf_script, "frame=%d\n", frame_number);
1581             av_bprintf(&buf_script, "fps=%.1f\n", fps);
1582             av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
1583                        ost->file_index, ost->index, q);
1584             if (is_last_report)
1585                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
1586             if (qp_hist) {
1587                 int j;
1588                 int qp = lrintf(q);
1589                 if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
1590                     qp_histogram[qp]++;
1591                 for (j = 0; j < 32; j++)
1592                     snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log2(qp_histogram[j] + 1)));
1593             }
1594
1595             if ((enc->flags & AV_CODEC_FLAG_PSNR) && (ost->pict_type != AV_PICTURE_TYPE_NONE || is_last_report)) {
1596                 int j;
1597                 double error, error_sum = 0;
1598                 double scale, scale_sum = 0;
1599                 double p;
1600                 char type[3] = { 'Y','U','V' };
1601                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
1602                 for (j = 0; j < 3; j++) {
1603                     if (is_last_report) {
1604                         error = enc->error[j];
1605                         scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
1606                     } else {
1607                         error = ost->error[j];
1608                         scale = enc->width * enc->height * 255.0 * 255.0;
1609                     }
1610                     if (j)
1611                         scale /= 4;
1612                     error_sum += error;
1613                     scale_sum += scale;
1614                     p = psnr(error / scale);
1615                     snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], p);
1616                     av_bprintf(&buf_script, "stream_%d_%d_psnr_%c=%2.2f\n",
1617                                ost->file_index, ost->index, type[j] | 32, p);
1618                 }
1619                 p = psnr(error_sum / scale_sum);
1620                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum / scale_sum));
1621                 av_bprintf(&buf_script, "stream_%d_%d_psnr_all=%2.2f\n",
1622                            ost->file_index, ost->index, p);
1623             }
1624             vid = 1;
1625         }
1626         /* compute min output value */
1627         if (av_stream_get_end_pts(ost->st) != AV_NOPTS_VALUE)
1628             pts = FFMAX(pts, av_rescale_q(av_stream_get_end_pts(ost->st),
1629                                           ost->st->time_base, AV_TIME_BASE_Q));
1630         if (is_last_report)
1631             nb_frames_drop += ost->last_droped;
1632     }
1633
1634     secs = FFABS(pts) / AV_TIME_BASE;
1635     us = FFABS(pts) % AV_TIME_BASE;
1636     mins = secs / 60;
1637     secs %= 60;
1638     hours = mins / 60;
1639     mins %= 60;
1640
1641     bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
1642
1643     if (total_size < 0) snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1644                                  "size=N/A time=");
1645     else                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1646                                  "size=%8.0fkB time=", total_size / 1024.0);
1647     if (pts < 0)
1648         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "-");
1649     snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1650              "%02d:%02d:%02d.%02d ", hours, mins, secs,
1651              (100 * us) / AV_TIME_BASE);
1652
1653     if (bitrate < 0) {
1654         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),"bitrate=N/A");
1655         av_bprintf(&buf_script, "bitrate=N/A\n");
1656     }else{
1657         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),"bitrate=%6.1fkbits/s", bitrate);
1658         av_bprintf(&buf_script, "bitrate=%6.1fkbits/s\n", bitrate);
1659     }
1660
1661     if (total_size < 0) av_bprintf(&buf_script, "total_size=N/A\n");
1662     else                av_bprintf(&buf_script, "total_size=%"PRId64"\n", total_size);
1663     av_bprintf(&buf_script, "out_time_ms=%"PRId64"\n", pts);
1664     av_bprintf(&buf_script, "out_time=%02d:%02d:%02d.%06d\n",
1665                hours, mins, secs, us);
1666
1667     if (nb_frames_dup || nb_frames_drop)
1668         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
1669                 nb_frames_dup, nb_frames_drop);
1670     av_bprintf(&buf_script, "dup_frames=%d\n", nb_frames_dup);
1671     av_bprintf(&buf_script, "drop_frames=%d\n", nb_frames_drop);
1672
1673     if (print_stats || is_last_report) {
1674         const char end = is_last_report ? '\n' : '\r';
1675         if (print_stats==1 && AV_LOG_INFO > av_log_get_level()) {
1676             fprintf(stderr, "%s    %c", buf, end);
1677         } else
1678             av_log(NULL, AV_LOG_INFO, "%s    %c", buf, end);
1679
1680     fflush(stderr);
1681     }
1682
1683     if (progress_avio) {
1684         av_bprintf(&buf_script, "progress=%s\n",
1685                    is_last_report ? "end" : "continue");
1686         avio_write(progress_avio, buf_script.str,
1687                    FFMIN(buf_script.len, buf_script.size - 1));
1688         avio_flush(progress_avio);
1689         av_bprint_finalize(&buf_script, NULL);
1690         if (is_last_report) {
1691             avio_closep(&progress_avio);
1692         }
1693     }
1694
1695     if (is_last_report)
1696         print_final_stats(total_size);
1697 }
1698
1699 static void flush_encoders(void)
1700 {
1701     int i, ret;
1702
1703     for (i = 0; i < nb_output_streams; i++) {
1704         OutputStream   *ost = output_streams[i];
1705         AVCodecContext *enc = ost->enc_ctx;
1706         AVFormatContext *os = output_files[ost->file_index]->ctx;
1707         int stop_encoding = 0;
1708
1709         if (!ost->encoding_needed)
1710             continue;
1711
1712         if (enc->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
1713             continue;
1714         if (enc->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == AV_CODEC_ID_RAWVIDEO)
1715             continue;
1716
1717         for (;;) {
1718             int (*encode)(AVCodecContext*, AVPacket*, const AVFrame*, int*) = NULL;
1719             const char *desc;
1720
1721             switch (enc->codec_type) {
1722             case AVMEDIA_TYPE_AUDIO:
1723                 encode = avcodec_encode_audio2;
1724                 desc   = "Audio";
1725                 break;
1726             case AVMEDIA_TYPE_VIDEO:
1727                 encode = avcodec_encode_video2;
1728                 desc   = "Video";
1729                 break;
1730             default:
1731                 stop_encoding = 1;
1732             }
1733
1734             if (encode) {
1735                 AVPacket pkt;
1736                 int pkt_size;
1737                 int got_packet;
1738                 av_init_packet(&pkt);
1739                 pkt.data = NULL;
1740                 pkt.size = 0;
1741
1742                 update_benchmark(NULL);
1743                 ret = encode(enc, &pkt, NULL, &got_packet);
1744                 update_benchmark("flush %s %d.%d", desc, ost->file_index, ost->index);
1745                 if (ret < 0) {
1746                     av_log(NULL, AV_LOG_FATAL, "%s encoding failed\n", desc);
1747                     exit_program(1);
1748                 }
1749                 if (ost->logfile && enc->stats_out) {
1750                     fprintf(ost->logfile, "%s", enc->stats_out);
1751                 }
1752                 if (!got_packet) {
1753                     stop_encoding = 1;
1754                     break;
1755                 }
1756                 if (ost->finished & MUXER_FINISHED) {
1757                     av_free_packet(&pkt);
1758                     continue;
1759                 }
1760                 av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base);
1761                 pkt_size = pkt.size;
1762                 write_frame(os, &pkt, ost);
1763                 if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename) {
1764                     do_video_stats(ost, pkt_size);
1765                 }
1766             }
1767
1768             if (stop_encoding)
1769                 break;
1770         }
1771     }
1772 }
1773
1774 /*
1775  * Check whether a packet from ist should be written into ost at this time
1776  */
1777 static int check_output_constraints(InputStream *ist, OutputStream *ost)
1778 {
1779     OutputFile *of = output_files[ost->file_index];
1780     int ist_index  = input_files[ist->file_index]->ist_index + ist->st->index;
1781
1782     if (ost->source_index != ist_index)
1783         return 0;
1784
1785     if (ost->finished)
1786         return 0;
1787
1788     if (of->start_time != AV_NOPTS_VALUE && ist->pts < of->start_time)
1789         return 0;
1790
1791     return 1;
1792 }
1793
1794 static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
1795 {
1796     OutputFile *of = output_files[ost->file_index];
1797     InputFile   *f = input_files [ist->file_index];
1798     int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
1799     int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->st->time_base);
1800     int64_t ist_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ist->st->time_base);
1801     AVPicture pict;
1802     AVPacket opkt;
1803
1804     av_init_packet(&opkt);
1805
1806     if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
1807         !ost->copy_initial_nonkeyframes)
1808         return;
1809
1810     if (pkt->pts == AV_NOPTS_VALUE) {
1811         if (!ost->frame_number && ist->pts < start_time &&
1812             !ost->copy_prior_start)
1813             return;
1814     } else {
1815         if (!ost->frame_number && pkt->pts < ist_tb_start_time &&
1816             !ost->copy_prior_start)
1817             return;
1818     }
1819
1820     if (of->recording_time != INT64_MAX &&
1821         ist->pts >= of->recording_time + start_time) {
1822         close_output_stream(ost);
1823         return;
1824     }
1825
1826     if (f->recording_time != INT64_MAX) {
1827         start_time = f->ctx->start_time;
1828         if (f->start_time != AV_NOPTS_VALUE)
1829             start_time += f->start_time;
1830         if (ist->pts >= f->recording_time + start_time) {
1831             close_output_stream(ost);
1832             return;
1833         }
1834     }
1835
1836     /* force the input stream PTS */
1837     if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
1838         ost->sync_opts++;
1839
1840     if (pkt->pts != AV_NOPTS_VALUE)
1841         opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;
1842     else
1843         opkt.pts = AV_NOPTS_VALUE;
1844
1845     if (pkt->dts == AV_NOPTS_VALUE)
1846         opkt.dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->st->time_base);
1847     else
1848         opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);
1849     opkt.dts -= ost_tb_start_time;
1850
1851     if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->dts != AV_NOPTS_VALUE) {
1852         int duration = av_get_audio_frame_duration(ist->dec_ctx, pkt->size);
1853         if(!duration)
1854             duration = ist->dec_ctx->frame_size;
1855         opkt.dts = opkt.pts = av_rescale_delta(ist->st->time_base, pkt->dts,
1856                                                (AVRational){1, ist->dec_ctx->sample_rate}, duration, &ist->filter_in_rescale_delta_last,
1857                                                ost->st->time_base) - ost_tb_start_time;
1858     }
1859
1860     opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base);
1861     opkt.flags    = pkt->flags;
1862
1863     // FIXME remove the following 2 lines they shall be replaced by the bitstream filters
1864     if (  ost->enc_ctx->codec_id != AV_CODEC_ID_H264
1865        && ost->enc_ctx->codec_id != AV_CODEC_ID_MPEG1VIDEO
1866        && ost->enc_ctx->codec_id != AV_CODEC_ID_MPEG2VIDEO
1867        && ost->enc_ctx->codec_id != AV_CODEC_ID_VC1
1868        ) {
1869         if (av_parser_change(ost->parser, ost->st->codec,
1870                              &opkt.data, &opkt.size,
1871                              pkt->data, pkt->size,
1872                              pkt->flags & AV_PKT_FLAG_KEY)) {
1873             opkt.buf = av_buffer_create(opkt.data, opkt.size, av_buffer_default_free, NULL, 0);
1874             if (!opkt.buf)
1875                 exit_program(1);
1876         }
1877     } else {
1878         opkt.data = pkt->data;
1879         opkt.size = pkt->size;
1880     }
1881     av_copy_packet_side_data(&opkt, pkt);
1882
1883     if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (of->ctx->oformat->flags & AVFMT_RAWPICTURE)) {
1884         /* store AVPicture in AVPacket, as expected by the output format */
1885         avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height);
1886         opkt.data = (uint8_t *)&pict;
1887         opkt.size = sizeof(AVPicture);
1888         opkt.flags |= AV_PKT_FLAG_KEY;
1889     }
1890
1891     write_frame(of->ctx, &opkt, ost);
1892 }
1893
1894 int guess_input_channel_layout(InputStream *ist)
1895 {
1896     AVCodecContext *dec = ist->dec_ctx;
1897
1898     if (!dec->channel_layout) {
1899         char layout_name[256];
1900
1901         if (dec->channels > ist->guess_layout_max)
1902             return 0;
1903         dec->channel_layout = av_get_default_channel_layout(dec->channels);
1904         if (!dec->channel_layout)
1905             return 0;
1906         av_get_channel_layout_string(layout_name, sizeof(layout_name),
1907                                      dec->channels, dec->channel_layout);
1908         av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for  Input Stream "
1909                "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
1910     }
1911     return 1;
1912 }
1913
1914 static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
1915 {
1916     AVFrame *decoded_frame, *f;
1917     AVCodecContext *avctx = ist->dec_ctx;
1918     int i, ret, err = 0, resample_changed;
1919     AVRational decoded_frame_tb;
1920
1921     if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
1922         return AVERROR(ENOMEM);
1923     if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
1924         return AVERROR(ENOMEM);
1925     decoded_frame = ist->decoded_frame;
1926
1927     update_benchmark(NULL);
1928     ret = avcodec_decode_audio4(avctx, decoded_frame, got_output, pkt);
1929     update_benchmark("decode_audio %d.%d", ist->file_index, ist->st->index);
1930
1931     if (ret >= 0 && avctx->sample_rate <= 0) {
1932         av_log(avctx, AV_LOG_ERROR, "Sample rate %d invalid\n", avctx->sample_rate);
1933         ret = AVERROR_INVALIDDATA;
1934     }
1935
1936     if (*got_output || ret<0)
1937         decode_error_stat[ret<0] ++;
1938
1939     if (ret < 0 && exit_on_error)
1940         exit_program(1);
1941
1942     if (!*got_output || ret < 0)
1943         return ret;
1944
1945     ist->samples_decoded += decoded_frame->nb_samples;
1946     ist->frames_decoded++;
1947
1948 #if 1
1949     /* increment next_dts to use for the case where the input stream does not
1950        have timestamps or there are multiple frames in the packet */
1951     ist->next_pts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
1952                      avctx->sample_rate;
1953     ist->next_dts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
1954                      avctx->sample_rate;
1955 #endif
1956
1957     resample_changed = ist->resample_sample_fmt     != decoded_frame->format         ||
1958                        ist->resample_channels       != avctx->channels               ||
1959                        ist->resample_channel_layout != decoded_frame->channel_layout ||
1960                        ist->resample_sample_rate    != decoded_frame->sample_rate;
1961     if (resample_changed) {
1962         char layout1[64], layout2[64];
1963
1964         if (!guess_input_channel_layout(ist)) {
1965             av_log(NULL, AV_LOG_FATAL, "Unable to find default channel "
1966                    "layout for Input Stream #%d.%d\n", ist->file_index,
1967                    ist->st->index);
1968             exit_program(1);
1969         }
1970         decoded_frame->channel_layout = avctx->channel_layout;
1971
1972         av_get_channel_layout_string(layout1, sizeof(layout1), ist->resample_channels,
1973                                      ist->resample_channel_layout);
1974         av_get_channel_layout_string(layout2, sizeof(layout2), avctx->channels,
1975                                      decoded_frame->channel_layout);
1976
1977         av_log(NULL, AV_LOG_INFO,
1978                "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",
1979                ist->file_index, ist->st->index,
1980                ist->resample_sample_rate,  av_get_sample_fmt_name(ist->resample_sample_fmt),
1981                ist->resample_channels, layout1,
1982                decoded_frame->sample_rate, av_get_sample_fmt_name(decoded_frame->format),
1983                avctx->channels, layout2);
1984
1985         ist->resample_sample_fmt     = decoded_frame->format;
1986         ist->resample_sample_rate    = decoded_frame->sample_rate;
1987         ist->resample_channel_layout = decoded_frame->channel_layout;
1988         ist->resample_channels       = avctx->channels;
1989
1990         for (i = 0; i < nb_filtergraphs; i++)
1991             if (ist_in_filtergraph(filtergraphs[i], ist)) {
1992                 FilterGraph *fg = filtergraphs[i];
1993                 if (configure_filtergraph(fg) < 0) {
1994                     av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n");
1995                     exit_program(1);
1996                 }
1997             }
1998     }
1999
2000     /* if the decoder provides a pts, use it instead of the last packet pts.
2001        the decoder could be delaying output by a packet or more. */
2002     if (decoded_frame->pts != AV_NOPTS_VALUE) {
2003         ist->dts = ist->next_dts = ist->pts = ist->next_pts = av_rescale_q(decoded_frame->pts, avctx->time_base, AV_TIME_BASE_Q);
2004         decoded_frame_tb   = avctx->time_base;
2005     } else if (decoded_frame->pkt_pts != AV_NOPTS_VALUE) {
2006         decoded_frame->pts = decoded_frame->pkt_pts;
2007         decoded_frame_tb   = ist->st->time_base;
2008     } else if (pkt->pts != AV_NOPTS_VALUE) {
2009         decoded_frame->pts = pkt->pts;
2010         decoded_frame_tb   = ist->st->time_base;
2011     }else {
2012         decoded_frame->pts = ist->dts;
2013         decoded_frame_tb   = AV_TIME_BASE_Q;
2014     }
2015     pkt->pts           = AV_NOPTS_VALUE;
2016     if (decoded_frame->pts != AV_NOPTS_VALUE)
2017         decoded_frame->pts = av_rescale_delta(decoded_frame_tb, decoded_frame->pts,
2018                                               (AVRational){1, avctx->sample_rate}, decoded_frame->nb_samples, &ist->filter_in_rescale_delta_last,
2019                                               (AVRational){1, avctx->sample_rate});
2020     for (i = 0; i < ist->nb_filters; i++) {
2021         if (i < ist->nb_filters - 1) {
2022             f = ist->filter_frame;
2023             err = av_frame_ref(f, decoded_frame);
2024             if (err < 0)
2025                 break;
2026         } else
2027             f = decoded_frame;
2028         err = av_buffersrc_add_frame_flags(ist->filters[i]->filter, f,
2029                                      AV_BUFFERSRC_FLAG_PUSH);
2030         if (err == AVERROR_EOF)
2031             err = 0; /* ignore */
2032         if (err < 0)
2033             break;
2034     }
2035     decoded_frame->pts = AV_NOPTS_VALUE;
2036
2037     av_frame_unref(ist->filter_frame);
2038     av_frame_unref(decoded_frame);
2039     return err < 0 ? err : ret;
2040 }
2041
2042 static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
2043 {
2044     AVFrame *decoded_frame, *f;
2045     int i, ret = 0, err = 0, resample_changed;
2046     int64_t best_effort_timestamp;
2047     AVRational *frame_sample_aspect;
2048
2049     if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
2050         return AVERROR(ENOMEM);
2051     if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
2052         return AVERROR(ENOMEM);
2053     decoded_frame = ist->decoded_frame;
2054     pkt->dts  = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ist->st->time_base);
2055
2056     update_benchmark(NULL);
2057     ret = avcodec_decode_video2(ist->dec_ctx,
2058                                 decoded_frame, got_output, pkt);
2059     update_benchmark("decode_video %d.%d", ist->file_index, ist->st->index);
2060
2061     // The following line may be required in some cases where there is no parser
2062     // or the parser does not has_b_frames correctly
2063     if (ist->st->codec->has_b_frames < ist->dec_ctx->has_b_frames) {
2064         if (ist->dec_ctx->codec_id == AV_CODEC_ID_H264) {
2065             ist->st->codec->has_b_frames = ist->dec_ctx->has_b_frames;
2066         } else
2067             av_log(ist->dec_ctx, AV_LOG_WARNING,
2068                    "has_b_frames is larger in decoder than demuxer %d > %d.\n"
2069                    "If you want to help, upload a sample "
2070                    "of this file to ftp://upload.ffmpeg.org/incoming/ "
2071                    "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)",
2072                    ist->dec_ctx->has_b_frames,
2073                    ist->st->codec->has_b_frames);
2074     }
2075
2076     if (*got_output || ret<0)
2077         decode_error_stat[ret<0] ++;
2078
2079     if (ret < 0 && exit_on_error)
2080         exit_program(1);
2081
2082     if (*got_output && ret >= 0) {
2083         if (ist->dec_ctx->width  != decoded_frame->width ||
2084             ist->dec_ctx->height != decoded_frame->height ||
2085             ist->dec_ctx->pix_fmt != decoded_frame->format) {
2086             av_log(NULL, AV_LOG_DEBUG, "Frame parameters mismatch context %d,%d,%d != %d,%d,%d\n",
2087                 decoded_frame->width,
2088                 decoded_frame->height,
2089                 decoded_frame->format,
2090                 ist->dec_ctx->width,
2091                 ist->dec_ctx->height,
2092                 ist->dec_ctx->pix_fmt);
2093         }
2094     }
2095
2096     if (!*got_output || ret < 0)
2097         return ret;
2098
2099     if(ist->top_field_first>=0)
2100         decoded_frame->top_field_first = ist->top_field_first;
2101
2102     ist->frames_decoded++;
2103
2104     if (ist->hwaccel_retrieve_data && decoded_frame->format == ist->hwaccel_pix_fmt) {
2105         err = ist->hwaccel_retrieve_data(ist->dec_ctx, decoded_frame);
2106         if (err < 0)
2107             goto fail;
2108     }
2109     ist->hwaccel_retrieved_pix_fmt = decoded_frame->format;
2110
2111     best_effort_timestamp= av_frame_get_best_effort_timestamp(decoded_frame);
2112     if(best_effort_timestamp != AV_NOPTS_VALUE)
2113         ist->next_pts = ist->pts = av_rescale_q(decoded_frame->pts = best_effort_timestamp, ist->st->time_base, AV_TIME_BASE_Q);
2114
2115     if (debug_ts) {
2116         av_log(NULL, AV_LOG_INFO, "decoder -> ist_index:%d type:video "
2117                "frame_pts:%s frame_pts_time:%s best_effort_ts:%"PRId64" best_effort_ts_time:%s keyframe:%d frame_type:%d time_base:%d/%d\n",
2118                ist->st->index, av_ts2str(decoded_frame->pts),
2119                av_ts2timestr(decoded_frame->pts, &ist->st->time_base),
2120                best_effort_timestamp,
2121                av_ts2timestr(best_effort_timestamp, &ist->st->time_base),
2122                decoded_frame->key_frame, decoded_frame->pict_type,
2123                ist->st->time_base.num, ist->st->time_base.den);
2124     }
2125
2126     pkt->size = 0;
2127
2128     if (ist->st->sample_aspect_ratio.num)
2129         decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
2130
2131     resample_changed = ist->resample_width   != decoded_frame->width  ||
2132                        ist->resample_height  != decoded_frame->height ||
2133                        ist->resample_pix_fmt != decoded_frame->format;
2134     if (resample_changed) {
2135         av_log(NULL, AV_LOG_INFO,
2136                "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
2137                ist->file_index, ist->st->index,
2138                ist->resample_width,  ist->resample_height,  av_get_pix_fmt_name(ist->resample_pix_fmt),
2139                decoded_frame->width, decoded_frame->height, av_get_pix_fmt_name(decoded_frame->format));
2140
2141         ist->resample_width   = decoded_frame->width;
2142         ist->resample_height  = decoded_frame->height;
2143         ist->resample_pix_fmt = decoded_frame->format;
2144
2145         for (i = 0; i < nb_filtergraphs; i++) {
2146             if (ist_in_filtergraph(filtergraphs[i], ist) && ist->reinit_filters &&
2147                 configure_filtergraph(filtergraphs[i]) < 0) {
2148                 av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n");
2149                 exit_program(1);
2150             }
2151         }
2152     }
2153
2154     frame_sample_aspect= av_opt_ptr(avcodec_get_frame_class(), decoded_frame, "sample_aspect_ratio");
2155     for (i = 0; i < ist->nb_filters; i++) {
2156         if (!frame_sample_aspect->num)
2157             *frame_sample_aspect = ist->st->sample_aspect_ratio;
2158
2159         if (i < ist->nb_filters - 1) {
2160             f = ist->filter_frame;
2161             err = av_frame_ref(f, decoded_frame);
2162             if (err < 0)
2163                 break;
2164         } else
2165             f = decoded_frame;
2166         ret = av_buffersrc_add_frame_flags(ist->filters[i]->filter, f, AV_BUFFERSRC_FLAG_PUSH);
2167         if (ret == AVERROR_EOF) {
2168             ret = 0; /* ignore */
2169         } else if (ret < 0) {
2170             av_log(NULL, AV_LOG_FATAL,
2171                    "Failed to inject frame into filter network: %s\n", av_err2str(ret));
2172             exit_program(1);
2173         }
2174     }
2175
2176 fail:
2177     av_frame_unref(ist->filter_frame);
2178     av_frame_unref(decoded_frame);
2179     return err < 0 ? err : ret;
2180 }
2181
2182 static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
2183 {
2184     AVSubtitle subtitle;
2185     int i, ret = avcodec_decode_subtitle2(ist->dec_ctx,
2186                                           &subtitle, got_output, pkt);
2187
2188     if (*got_output || ret<0)
2189         decode_error_stat[ret<0] ++;
2190
2191     if (ret < 0 && exit_on_error)
2192         exit_program(1);
2193
2194     if (ret < 0 || !*got_output) {
2195         if (!pkt->size)
2196             sub2video_flush(ist);
2197         return ret;
2198     }
2199
2200     if (ist->fix_sub_duration) {
2201         int end = 1;
2202         if (ist->prev_sub.got_output) {
2203             end = av_rescale(subtitle.pts - ist->prev_sub.subtitle.pts,
2204                              1000, AV_TIME_BASE);
2205             if (end < ist->prev_sub.subtitle.end_display_time) {
2206                 av_log(ist->dec_ctx, AV_LOG_DEBUG,
2207                        "Subtitle duration reduced from %d to %d%s\n",
2208                        ist->prev_sub.subtitle.end_display_time, end,
2209                        end <= 0 ? ", dropping it" : "");
2210                 ist->prev_sub.subtitle.end_display_time = end;
2211             }
2212         }
2213         FFSWAP(int,        *got_output, ist->prev_sub.got_output);
2214         FFSWAP(int,        ret,         ist->prev_sub.ret);
2215         FFSWAP(AVSubtitle, subtitle,    ist->prev_sub.subtitle);
2216         if (end <= 0)
2217             goto out;
2218     }
2219
2220     if (!*got_output)
2221         return ret;
2222
2223     sub2video_update(ist, &subtitle);
2224
2225     if (!subtitle.num_rects)
2226         goto out;
2227
2228     ist->frames_decoded++;
2229
2230     for (i = 0; i < nb_output_streams; i++) {
2231         OutputStream *ost = output_streams[i];
2232
2233         if (!check_output_constraints(ist, ost) || !ost->encoding_needed
2234             || ost->enc->type != AVMEDIA_TYPE_SUBTITLE)
2235             continue;
2236
2237         do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle);
2238     }
2239
2240 out:
2241     avsubtitle_free(&subtitle);
2242     return ret;
2243 }
2244
2245 static int send_filter_eof(InputStream *ist)
2246 {
2247     int i, ret;
2248     for (i = 0; i < ist->nb_filters; i++) {
2249         ret = av_buffersrc_add_frame(ist->filters[i]->filter, NULL);
2250         if (ret < 0)
2251             return ret;
2252     }
2253     return 0;
2254 }
2255
2256 /* pkt = NULL means EOF (needed to flush decoder buffers) */
2257 static int process_input_packet(InputStream *ist, const AVPacket *pkt)
2258 {
2259     int ret = 0, i;
2260     int got_output = 0;
2261
2262     AVPacket avpkt;
2263     if (!ist->saw_first_ts) {
2264         ist->dts = ist->st->avg_frame_rate.num ? - ist->dec_ctx->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
2265         ist->pts = 0;
2266         if (pkt && pkt->pts != AV_NOPTS_VALUE && !ist->decoding_needed) {
2267             ist->dts += av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
2268             ist->pts = ist->dts; //unused but better to set it to a value thats not totally wrong
2269         }
2270         ist->saw_first_ts = 1;
2271     }
2272
2273     if (ist->next_dts == AV_NOPTS_VALUE)
2274         ist->next_dts = ist->dts;
2275     if (ist->next_pts == AV_NOPTS_VALUE)
2276         ist->next_pts = ist->pts;
2277
2278     if (!pkt) {
2279         /* EOF handling */
2280         av_init_packet(&avpkt);
2281         avpkt.data = NULL;
2282         avpkt.size = 0;
2283         goto handle_eof;
2284     } else {
2285         avpkt = *pkt;
2286     }
2287
2288     if (pkt->dts != AV_NOPTS_VALUE) {
2289         ist->next_dts = ist->dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
2290         if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed)
2291             ist->next_pts = ist->pts = ist->dts;
2292     }
2293
2294     // while we have more to decode or while the decoder did output something on EOF
2295     while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {
2296         int duration;
2297     handle_eof:
2298
2299         ist->pts = ist->next_pts;
2300         ist->dts = ist->next_dts;
2301
2302         if (avpkt.size && avpkt.size != pkt->size &&
2303             !(ist->dec->capabilities & AV_CODEC_CAP_SUBFRAMES)) {
2304             av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,
2305                    "Multiple frames in a packet from stream %d\n", pkt->stream_index);
2306             ist->showed_multi_packet_warning = 1;
2307         }
2308
2309         switch (ist->dec_ctx->codec_type) {
2310         case AVMEDIA_TYPE_AUDIO:
2311             ret = decode_audio    (ist, &avpkt, &got_output);
2312             break;
2313         case AVMEDIA_TYPE_VIDEO:
2314             ret = decode_video    (ist, &avpkt, &got_output);
2315             if (avpkt.duration) {
2316                 duration = av_rescale_q(avpkt.duration, ist->st->time_base, AV_TIME_BASE_Q);
2317             } else if(ist->dec_ctx->framerate.num != 0 && ist->dec_ctx->framerate.den != 0) {
2318                 int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict+1 : ist->dec_ctx->ticks_per_frame;
2319                 duration = ((int64_t)AV_TIME_BASE *
2320                                 ist->dec_ctx->framerate.den * ticks) /
2321                                 ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame;
2322             } else
2323                 duration = 0;
2324
2325             if(ist->dts != AV_NOPTS_VALUE && duration) {
2326                 ist->next_dts += duration;
2327             }else
2328                 ist->next_dts = AV_NOPTS_VALUE;
2329
2330             if (got_output)
2331                 ist->next_pts += duration; //FIXME the duration is not correct in some cases
2332             break;
2333         case AVMEDIA_TYPE_SUBTITLE:
2334             ret = transcode_subtitles(ist, &avpkt, &got_output);
2335             break;
2336         default:
2337             return -1;
2338         }
2339
2340         if (ret < 0) {
2341             av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d: %s\n",
2342                    ist->file_index, ist->st->index, av_err2str(ret));
2343             if (exit_on_error)
2344                 exit_program(1);
2345             break;
2346         }
2347
2348         avpkt.dts=
2349         avpkt.pts= AV_NOPTS_VALUE;
2350
2351         // touch data and size only if not EOF
2352         if (pkt) {
2353             if(ist->dec_ctx->codec_type != AVMEDIA_TYPE_AUDIO)
2354                 ret = avpkt.size;
2355             avpkt.data += ret;
2356             avpkt.size -= ret;
2357         }
2358         if (!got_output) {
2359             continue;
2360         }
2361         if (got_output && !pkt)
2362             break;
2363     }
2364
2365     /* after flushing, send an EOF on all the filter inputs attached to the stream */
2366     if (!pkt && ist->decoding_needed && !got_output) {
2367         int ret = send_filter_eof(ist);
2368         if (ret < 0) {
2369             av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n");
2370             exit_program(1);
2371         }
2372     }
2373
2374     /* handle stream copy */
2375     if (!ist->decoding_needed) {
2376         ist->dts = ist->next_dts;
2377         switch (ist->dec_ctx->codec_type) {
2378         case AVMEDIA_TYPE_AUDIO:
2379             ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->frame_size) /
2380                              ist->dec_ctx->sample_rate;
2381             break;
2382         case AVMEDIA_TYPE_VIDEO:
2383             if (ist->framerate.num) {
2384                 // TODO: Remove work-around for c99-to-c89 issue 7
2385                 AVRational time_base_q = AV_TIME_BASE_Q;
2386                 int64_t next_dts = av_rescale_q(ist->next_dts, time_base_q, av_inv_q(ist->framerate));
2387                 ist->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
2388             } else if (pkt->duration) {
2389                 ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
2390             } else if(ist->dec_ctx->framerate.num != 0) {
2391                 int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame;
2392                 ist->next_dts += ((int64_t)AV_TIME_BASE *
2393                                   ist->dec_ctx->framerate.den * ticks) /
2394                                   ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame;
2395             }
2396             break;
2397         }
2398         ist->pts = ist->dts;
2399         ist->next_pts = ist->next_dts;
2400     }
2401     for (i = 0; pkt && i < nb_output_streams; i++) {
2402         OutputStream *ost = output_streams[i];
2403
2404         if (!check_output_constraints(ist, ost) || ost->encoding_needed)
2405             continue;
2406
2407         do_streamcopy(ist, ost, pkt);
2408     }
2409
2410     return got_output;
2411 }
2412
2413 static void print_sdp(void)
2414 {
2415     char sdp[16384];
2416     int i;
2417     int j;
2418     AVIOContext *sdp_pb;
2419     AVFormatContext **avc = av_malloc_array(nb_output_files, sizeof(*avc));
2420
2421     if (!avc)
2422         exit_program(1);
2423     for (i = 0, j = 0; i < nb_output_files; i++) {
2424         if (!strcmp(output_files[i]->ctx->oformat->name, "rtp")) {
2425             avc[j] = output_files[i]->ctx;
2426             j++;
2427         }
2428     }
2429
2430     av_sdp_create(avc, j, sdp, sizeof(sdp));
2431
2432     if (!sdp_filename) {
2433         printf("SDP:\n%s\n", sdp);
2434         fflush(stdout);
2435     } else {
2436         if (avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL) < 0) {
2437             av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename);
2438         } else {
2439             avio_printf(sdp_pb, "SDP:\n%s", sdp);
2440             avio_closep(&sdp_pb);
2441             av_freep(&sdp_filename);
2442         }
2443     }
2444
2445     av_freep(&avc);
2446 }
2447
2448 static const HWAccel *get_hwaccel(enum AVPixelFormat pix_fmt)
2449 {
2450     int i;
2451     for (i = 0; hwaccels[i].name; i++)
2452         if (hwaccels[i].pix_fmt == pix_fmt)
2453             return &hwaccels[i];
2454     return NULL;
2455 }
2456
2457 static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
2458 {
2459     InputStream *ist = s->opaque;
2460     const enum AVPixelFormat *p;
2461     int ret;
2462
2463     for (p = pix_fmts; *p != -1; p++) {
2464         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
2465         const HWAccel *hwaccel;
2466
2467         if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
2468             break;
2469
2470         hwaccel = get_hwaccel(*p);
2471         if (!hwaccel ||
2472             (ist->active_hwaccel_id && ist->active_hwaccel_id != hwaccel->id) ||
2473             (ist->hwaccel_id != HWACCEL_AUTO && ist->hwaccel_id != hwaccel->id))
2474             continue;
2475
2476         ret = hwaccel->init(s);
2477         if (ret < 0) {
2478             if (ist->hwaccel_id == hwaccel->id) {
2479                 av_log(NULL, AV_LOG_FATAL,
2480                        "%s hwaccel requested for input stream #%d:%d, "
2481                        "but cannot be initialized.\n", hwaccel->name,
2482                        ist->file_index, ist->st->index);
2483                 return AV_PIX_FMT_NONE;
2484             }
2485             continue;
2486         }
2487         ist->active_hwaccel_id = hwaccel->id;
2488         ist->hwaccel_pix_fmt   = *p;
2489         break;
2490     }
2491
2492     return *p;
2493 }
2494
2495 static int get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
2496 {
2497     InputStream *ist = s->opaque;
2498
2499     if (ist->hwaccel_get_buffer && frame->format == ist->hwaccel_pix_fmt)
2500         return ist->hwaccel_get_buffer(s, frame, flags);
2501
2502     return avcodec_default_get_buffer2(s, frame, flags);
2503 }
2504
2505 static int init_input_stream(int ist_index, char *error, int error_len)
2506 {
2507     int ret;
2508     InputStream *ist = input_streams[ist_index];
2509
2510     if (ist->decoding_needed) {
2511         AVCodec *codec = ist->dec;
2512         if (!codec) {
2513             snprintf(error, error_len, "Decoder (codec %s) not found for input stream #%d:%d",
2514                     avcodec_get_name(ist->dec_ctx->codec_id), ist->file_index, ist->st->index);
2515             return AVERROR(EINVAL);
2516         }
2517
2518         ist->dec_ctx->opaque                = ist;
2519         ist->dec_ctx->get_format            = get_format;
2520         ist->dec_ctx->get_buffer2           = get_buffer;
2521         ist->dec_ctx->thread_safe_callbacks = 1;
2522
2523         av_opt_set_int(ist->dec_ctx, "refcounted_frames", 1, 0);
2524         if (ist->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
2525            (ist->decoding_needed & DECODING_FOR_OST)) {
2526             av_dict_set(&ist->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
2527             if (ist->decoding_needed & DECODING_FOR_FILTER)
2528                 av_log(NULL, AV_LOG_WARNING, "Warning using DVB subtitles for filtering and output at the same time is not fully supported, also see -compute_edt [0|1]\n");
2529         }
2530
2531         if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
2532             av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
2533         if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
2534             if (ret == AVERROR_EXPERIMENTAL)
2535                 abort_codec_experimental(codec, 0);
2536
2537             snprintf(error, error_len,
2538                      "Error while opening decoder for input stream "
2539                      "#%d:%d : %s",
2540                      ist->file_index, ist->st->index, av_err2str(ret));
2541             return ret;
2542         }
2543         assert_avoptions(ist->decoder_opts);
2544     }
2545
2546     ist->next_pts = AV_NOPTS_VALUE;
2547     ist->next_dts = AV_NOPTS_VALUE;
2548
2549     return 0;
2550 }
2551
2552 static InputStream *get_input_stream(OutputStream *ost)
2553 {
2554     if (ost->source_index >= 0)
2555         return input_streams[ost->source_index];
2556     return NULL;
2557 }
2558
2559 static int compare_int64(const void *a, const void *b)
2560 {
2561     int64_t va = *(int64_t *)a, vb = *(int64_t *)b;
2562     return va < vb ? -1 : va > vb ? +1 : 0;
2563 }
2564
2565 static int init_output_stream(OutputStream *ost, char *error, int error_len)
2566 {
2567     int ret = 0;
2568
2569     if (ost->encoding_needed) {
2570         AVCodec      *codec = ost->enc;
2571         AVCodecContext *dec = NULL;
2572         InputStream *ist;
2573
2574         if ((ist = get_input_stream(ost)))
2575             dec = ist->dec_ctx;
2576         if (dec && dec->subtitle_header) {
2577             /* ASS code assumes this buffer is null terminated so add extra byte. */
2578             ost->enc_ctx->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
2579             if (!ost->enc_ctx->subtitle_header)
2580                 return AVERROR(ENOMEM);
2581             memcpy(ost->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
2582             ost->enc_ctx->subtitle_header_size = dec->subtitle_header_size;
2583         }
2584         if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
2585             av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
2586         av_dict_set(&ost->encoder_opts, "side_data_only_packets", "1", 0);
2587
2588         if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {
2589             if (ret == AVERROR_EXPERIMENTAL)
2590                 abort_codec_experimental(codec, 1);
2591             snprintf(error, error_len,
2592                      "Error while opening encoder for output stream #%d:%d - "
2593                      "maybe incorrect parameters such as bit_rate, rate, width or height",
2594                     ost->file_index, ost->index);
2595             return ret;
2596         }
2597         if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
2598             !(ost->enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
2599             av_buffersink_set_frame_size(ost->filter->filter,
2600                                             ost->enc_ctx->frame_size);
2601         assert_avoptions(ost->encoder_opts);
2602         if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000)
2603             av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
2604                                          " It takes bits/s as argument, not kbits/s\n");
2605
2606         ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
2607         if (ret < 0) {
2608             av_log(NULL, AV_LOG_FATAL,
2609                    "Error initializing the output stream codec context.\n");
2610             exit_program(1);
2611         }
2612
2613         // copy timebase while removing common factors
2614         ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
2615         ost->st->codec->codec= ost->enc_ctx->codec;
2616     } else {
2617         ret = av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
2618         if (ret < 0) {
2619            av_log(NULL, AV_LOG_FATAL,
2620                   "Error setting up codec context options.\n");
2621            return ret;
2622         }
2623         // copy timebase while removing common factors
2624         ost->st->time_base = av_add_q(ost->st->codec->time_base, (AVRational){0, 1});
2625     }
2626
2627     return ret;
2628 }
2629
2630 static void parse_forced_key_frames(char *kf, OutputStream *ost,
2631                                     AVCodecContext *avctx)
2632 {
2633     char *p;
2634     int n = 1, i, size, index = 0;
2635     int64_t t, *pts;
2636
2637     for (p = kf; *p; p++)
2638         if (*p == ',')
2639             n++;
2640     size = n;
2641     pts = av_malloc_array(size, sizeof(*pts));
2642     if (!pts) {
2643         av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
2644         exit_program(1);
2645     }
2646
2647     p = kf;
2648     for (i = 0; i < n; i++) {
2649         char *next = strchr(p, ',');
2650
2651         if (next)
2652             *next++ = 0;
2653
2654         if (!memcmp(p, "chapters", 8)) {
2655
2656             AVFormatContext *avf = output_files[ost->file_index]->ctx;
2657             int j;
2658
2659             if (avf->nb_chapters > INT_MAX - size ||
2660                 !(pts = av_realloc_f(pts, size += avf->nb_chapters - 1,
2661                                      sizeof(*pts)))) {
2662                 av_log(NULL, AV_LOG_FATAL,
2663                        "Could not allocate forced key frames array.\n");
2664                 exit_program(1);
2665             }
2666             t = p[8] ? parse_time_or_die("force_key_frames", p + 8, 1) : 0;
2667             t = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
2668
2669             for (j = 0; j < avf->nb_chapters; j++) {
2670                 AVChapter *c = avf->chapters[j];
2671                 av_assert1(index < size);
2672                 pts[index++] = av_rescale_q(c->start, c->time_base,
2673                                             avctx->time_base) + t;
2674             }
2675
2676         } else {
2677
2678             t = parse_time_or_die("force_key_frames", p, 1);
2679             av_assert1(index < size);
2680             pts[index++] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
2681
2682         }
2683
2684         p = next;
2685     }
2686
2687     av_assert0(index == size);
2688     qsort(pts, size, sizeof(*pts), compare_int64);
2689     ost->forced_kf_count = size;
2690     ost->forced_kf_pts   = pts;
2691 }
2692
2693 static void report_new_stream(int input_index, AVPacket *pkt)
2694 {
2695     InputFile *file = input_files[input_index];
2696     AVStream *st = file->ctx->streams[pkt->stream_index];
2697
2698     if (pkt->stream_index < file->nb_streams_warn)
2699         return;
2700     av_log(file->ctx, AV_LOG_WARNING,
2701            "New %s stream %d:%d at pos:%"PRId64" and DTS:%ss\n",
2702            av_get_media_type_string(st->codec->codec_type),
2703            input_index, pkt->stream_index,
2704            pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
2705     file->nb_streams_warn = pkt->stream_index + 1;
2706 }
2707
2708 static void set_encoder_id(OutputFile *of, OutputStream *ost)
2709 {
2710     AVDictionaryEntry *e;
2711
2712     uint8_t *encoder_string;
2713     int encoder_string_len;
2714     int format_flags = 0;
2715     int codec_flags = 0;
2716
2717     if (av_dict_get(ost->st->metadata, "encoder",  NULL, 0))
2718         return;
2719
2720     e = av_dict_get(of->opts, "fflags", NULL, 0);
2721     if (e) {
2722         const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0);
2723         if (!o)
2724             return;
2725         av_opt_eval_flags(of->ctx, o, e->value, &format_flags);
2726     }
2727     e = av_dict_get(ost->encoder_opts, "flags", NULL, 0);
2728     if (e) {
2729         const AVOption *o = av_opt_find(ost->enc_ctx, "flags", NULL, 0, 0);
2730         if (!o)
2731             return;
2732         av_opt_eval_flags(ost->enc_ctx, o, e->value, &codec_flags);
2733     }
2734
2735     encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2;
2736     encoder_string     = av_mallocz(encoder_string_len);
2737     if (!encoder_string)
2738         exit_program(1);
2739
2740     if (!(format_flags & AVFMT_FLAG_BITEXACT) && !(codec_flags & AV_CODEC_FLAG_BITEXACT))
2741         av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
2742     else
2743         av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
2744     av_strlcat(encoder_string, ost->enc->name, encoder_string_len);
2745     av_dict_set(&ost->st->metadata, "encoder",  encoder_string,
2746                 AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
2747 }
2748
2749 static int transcode_init(void)
2750 {
2751     int ret = 0, i, j, k;
2752     AVFormatContext *oc;
2753     OutputStream *ost;
2754     InputStream *ist;
2755     char error[1024] = {0};
2756     int want_sdp = 1;
2757
2758     for (i = 0; i < nb_filtergraphs; i++) {
2759         FilterGraph *fg = filtergraphs[i];
2760         for (j = 0; j < fg->nb_outputs; j++) {
2761             OutputFilter *ofilter = fg->outputs[j];
2762             if (!ofilter->ost || ofilter->ost->source_index >= 0)
2763                 continue;
2764             if (fg->nb_inputs != 1)
2765                 continue;
2766             for (k = nb_input_streams-1; k >= 0 ; k--)
2767                 if (fg->inputs[0]->ist == input_streams[k])
2768                     break;
2769             ofilter->ost->source_index = k;
2770         }
2771     }
2772
2773     /* init framerate emulation */
2774     for (i = 0; i < nb_input_files; i++) {
2775         InputFile *ifile = input_files[i];
2776         if (ifile->rate_emu)
2777             for (j = 0; j < ifile->nb_streams; j++)
2778                 input_streams[j + ifile->ist_index]->start = av_gettime_relative();
2779     }
2780
2781     /* for each output stream, we compute the right encoding parameters */
2782     for (i = 0; i < nb_output_streams; i++) {
2783         AVCodecContext *enc_ctx;
2784         AVCodecContext *dec_ctx = NULL;
2785         ost = output_streams[i];
2786         oc  = output_files[ost->file_index]->ctx;
2787         ist = get_input_stream(ost);
2788
2789         if (ost->attachment_filename)
2790             continue;
2791
2792         enc_ctx = ost->stream_copy ? ost->st->codec : ost->enc_ctx;
2793
2794         if (ist) {
2795             dec_ctx = ist->dec_ctx;
2796
2797             ost->st->disposition          = ist->st->disposition;
2798             enc_ctx->bits_per_raw_sample    = dec_ctx->bits_per_raw_sample;
2799             enc_ctx->chroma_sample_location = dec_ctx->chroma_sample_location;
2800         } else {
2801             for (j=0; j<oc->nb_streams; j++) {
2802                 AVStream *st = oc->streams[j];
2803                 if (st != ost->st && st->codec->codec_type == enc_ctx->codec_type)
2804                     break;
2805             }
2806             if (j == oc->nb_streams)
2807                 if (enc_ctx->codec_type == AVMEDIA_TYPE_AUDIO || enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
2808                     ost->st->disposition = AV_DISPOSITION_DEFAULT;
2809         }
2810
2811         if (ost->stream_copy) {
2812             AVRational sar;
2813             uint64_t extra_size;
2814
2815             av_assert0(ist && !ost->filter);
2816
2817             extra_size = (uint64_t)dec_ctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE;
2818
2819             if (extra_size > INT_MAX) {
2820                 return AVERROR(EINVAL);
2821             }
2822
2823             /* if stream_copy is selected, no need to decode or encode */
2824             enc_ctx->codec_id   = dec_ctx->codec_id;
2825             enc_ctx->codec_type = dec_ctx->codec_type;
2826
2827             if (!enc_ctx->codec_tag) {
2828                 unsigned int codec_tag;
2829                 if (!oc->oformat->codec_tag ||
2830                      av_codec_get_id (oc->oformat->codec_tag, dec_ctx->codec_tag) == enc_ctx->codec_id ||
2831                      !av_codec_get_tag2(oc->oformat->codec_tag, dec_ctx->codec_id, &codec_tag))
2832                     enc_ctx->codec_tag = dec_ctx->codec_tag;
2833             }
2834
2835             enc_ctx->bit_rate       = dec_ctx->bit_rate;
2836             enc_ctx->rc_max_rate    = dec_ctx->rc_max_rate;
2837             enc_ctx->rc_buffer_size = dec_ctx->rc_buffer_size;
2838             enc_ctx->field_order    = dec_ctx->field_order;
2839             if (dec_ctx->extradata_size) {
2840                 enc_ctx->extradata      = av_mallocz(extra_size);
2841                 if (!enc_ctx->extradata) {
2842                     return AVERROR(ENOMEM);
2843                 }
2844                 memcpy(enc_ctx->extradata, dec_ctx->extradata, dec_ctx->extradata_size);
2845             }
2846             enc_ctx->extradata_size= dec_ctx->extradata_size;
2847             enc_ctx->bits_per_coded_sample  = dec_ctx->bits_per_coded_sample;
2848
2849             enc_ctx->time_base = ist->st->time_base;
2850             /*
2851              * Avi is a special case here because it supports variable fps but
2852              * having the fps and timebase differe significantly adds quite some
2853              * overhead
2854              */
2855             if(!strcmp(oc->oformat->name, "avi")) {
2856                 if ( copy_tb<0 && av_q2d(ist->st->r_frame_rate) >= av_q2d(ist->st->avg_frame_rate)
2857                                && 0.5/av_q2d(ist->st->r_frame_rate) > av_q2d(ist->st->time_base)
2858                                && 0.5/av_q2d(ist->st->r_frame_rate) > av_q2d(dec_ctx->time_base)
2859                                && av_q2d(ist->st->time_base) < 1.0/500 && av_q2d(dec_ctx->time_base) < 1.0/500
2860                      || copy_tb==2){
2861                     enc_ctx->time_base.num = ist->st->r_frame_rate.den;
2862                     enc_ctx->time_base.den = 2*ist->st->r_frame_rate.num;
2863                     enc_ctx->ticks_per_frame = 2;
2864                 } else if (   copy_tb<0 && av_q2d(dec_ctx->time_base)*dec_ctx->ticks_per_frame > 2*av_q2d(ist->st->time_base)
2865                                  && av_q2d(ist->st->time_base) < 1.0/500
2866                     || copy_tb==0){
2867                     enc_ctx->time_base = dec_ctx->time_base;
2868                     enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
2869                     enc_ctx->time_base.den *= 2;
2870                     enc_ctx->ticks_per_frame = 2;
2871                 }
2872             } else if(!(oc->oformat->flags & AVFMT_VARIABLE_FPS)
2873                       && strcmp(oc->oformat->name, "mov") && strcmp(oc->oformat->name, "mp4") && strcmp(oc->oformat->name, "3gp")
2874                       && strcmp(oc->oformat->name, "3g2") && strcmp(oc->oformat->name, "psp") && strcmp(oc->oformat->name, "ipod")
2875                       && strcmp(oc->oformat->name, "f4v")
2876             ) {
2877                 if(   copy_tb<0 && dec_ctx->time_base.den
2878                                 && av_q2d(dec_ctx->time_base)*dec_ctx->ticks_per_frame > av_q2d(ist->st->time_base)
2879                                 && av_q2d(ist->st->time_base) < 1.0/500
2880                    || copy_tb==0){
2881                     enc_ctx->time_base = dec_ctx->time_base;
2882                     enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
2883                 }
2884             }
2885             if (   enc_ctx->codec_tag == AV_RL32("tmcd")
2886                 && dec_ctx->time_base.num < dec_ctx->time_base.den
2887                 && dec_ctx->time_base.num > 0
2888                 && 121LL*dec_ctx->time_base.num > dec_ctx->time_base.den) {
2889                 enc_ctx->time_base = dec_ctx->time_base;
2890             }
2891
2892             if (ist && !ost->frame_rate.num)
2893                 ost->frame_rate = ist->framerate;
2894             if(ost->frame_rate.num)
2895                 enc_ctx->time_base = av_inv_q(ost->frame_rate);
2896
2897             av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den,
2898                         enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX);
2899
2900             if (ist->st->nb_side_data) {
2901                 ost->st->side_data = av_realloc_array(NULL, ist->st->nb_side_data,
2902                                                       sizeof(*ist->st->side_data));
2903                 if (!ost->st->side_data)
2904                     return AVERROR(ENOMEM);
2905
2906                 ost->st->nb_side_data = 0;
2907                 for (j = 0; j < ist->st->nb_side_data; j++) {
2908                     const AVPacketSideData *sd_src = &ist->st->side_data[j];
2909                     AVPacketSideData *sd_dst = &ost->st->side_data[ost->st->nb_side_data];
2910
2911                     if (ost->rotate_overridden && sd_src->type == AV_PKT_DATA_DISPLAYMATRIX)
2912                         continue;
2913
2914                     sd_dst->data = av_malloc(sd_src->size);
2915                     if (!sd_dst->data)
2916                         return AVERROR(ENOMEM);
2917                     memcpy(sd_dst->data, sd_src->data, sd_src->size);
2918                     sd_dst->size = sd_src->size;
2919                     sd_dst->type = sd_src->type;
2920                     ost->st->nb_side_data++;
2921                 }
2922             }
2923
2924             ost->parser = av_parser_init(enc_ctx->codec_id);
2925
2926             switch (enc_ctx->codec_type) {
2927             case AVMEDIA_TYPE_AUDIO:
2928                 if (audio_volume != 256) {
2929                     av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
2930                     exit_program(1);
2931                 }
2932                 enc_ctx->channel_layout     = dec_ctx->channel_layout;
2933                 enc_ctx->sample_rate        = dec_ctx->sample_rate;
2934                 enc_ctx->channels           = dec_ctx->channels;
2935                 enc_ctx->frame_size         = dec_ctx->frame_size;
2936                 enc_ctx->audio_service_type = dec_ctx->audio_service_type;
2937                 enc_ctx->block_align        = dec_ctx->block_align;
2938                 enc_ctx->initial_padding    = dec_ctx->delay;
2939 #if FF_API_AUDIOENC_DELAY
2940                 enc_ctx->delay              = dec_ctx->delay;
2941 #endif
2942                 if((enc_ctx->block_align == 1 || enc_ctx->block_align == 1152 || enc_ctx->block_align == 576) && enc_ctx->codec_id == AV_CODEC_ID_MP3)
2943                     enc_ctx->block_align= 0;
2944                 if(enc_ctx->codec_id == AV_CODEC_ID_AC3)
2945                     enc_ctx->block_align= 0;
2946                 break;
2947             case AVMEDIA_TYPE_VIDEO:
2948                 enc_ctx->pix_fmt            = dec_ctx->pix_fmt;
2949                 enc_ctx->width              = dec_ctx->width;
2950                 enc_ctx->height             = dec_ctx->height;
2951                 enc_ctx->has_b_frames       = dec_ctx->has_b_frames;
2952                 if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
2953                     sar =
2954                         av_mul_q(ost->frame_aspect_ratio,
2955                                  (AVRational){ enc_ctx->height, enc_ctx->width });
2956                     av_log(NULL, AV_LOG_WARNING, "Overriding aspect ratio "
2957                            "with stream copy may produce invalid files\n");
2958                 }
2959                 else if (ist->st->sample_aspect_ratio.num)
2960                     sar = ist->st->sample_aspect_ratio;
2961                 else
2962                     sar = dec_ctx->sample_aspect_ratio;
2963                 ost->st->sample_aspect_ratio = enc_ctx->sample_aspect_ratio = sar;
2964                 ost->st->avg_frame_rate = ist->st->avg_frame_rate;
2965                 ost->st->r_frame_rate = ist->st->r_frame_rate;
2966                 break;
2967             case AVMEDIA_TYPE_SUBTITLE:
2968                 enc_ctx->width  = dec_ctx->width;
2969                 enc_ctx->height = dec_ctx->height;
2970                 break;
2971             case AVMEDIA_TYPE_UNKNOWN:
2972             case AVMEDIA_TYPE_DATA:
2973             case AVMEDIA_TYPE_ATTACHMENT:
2974                 break;
2975             default:
2976                 abort();
2977             }
2978         } else {
2979             if (!ost->enc)
2980                 ost->enc = avcodec_find_encoder(enc_ctx->codec_id);
2981             if (!ost->enc) {
2982                 /* should only happen when a default codec is not present. */
2983                 snprintf(error, sizeof(error), "Encoder (codec %s) not found for output stream #%d:%d",
2984                          avcodec_get_name(ost->st->codec->codec_id), ost->file_index, ost->index);
2985                 ret = AVERROR(EINVAL);
2986                 goto dump_format;
2987             }
2988
2989             set_encoder_id(output_files[ost->file_index], ost);
2990
2991             if (!ost->filter &&
2992                 (enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2993                  enc_ctx->codec_type == AVMEDIA_TYPE_AUDIO)) {
2994                     FilterGraph *fg;
2995                     fg = init_simple_filtergraph(ist, ost);
2996                     if (configure_filtergraph(fg)) {
2997                         av_log(NULL, AV_LOG_FATAL, "Error opening filters!\n");
2998                         exit_program(1);
2999                     }
3000             }
3001
3002             if (enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
3003                 if (!ost->frame_rate.num)
3004                     ost->frame_rate = av_buffersink_get_frame_rate(ost->filter->filter);
3005                 if (ist && !ost->frame_rate.num)
3006                     ost->frame_rate = ist->framerate;
3007                 if (ist && !ost->frame_rate.num)
3008                     ost->frame_rate = ist->st->r_frame_rate;
3009                 if (ist && !ost->frame_rate.num) {
3010                     ost->frame_rate = (AVRational){25, 1};
3011                     av_log(NULL, AV_LOG_WARNING,
3012                            "No information "
3013                            "about the input framerate is available. Falling "
3014                            "back to a default value of 25fps for output stream #%d:%d. Use the -r option "
3015                            "if you want a different framerate.\n",
3016                            ost->file_index, ost->index);
3017                 }
3018 //                    ost->frame_rate = ist->st->avg_frame_rate.num ? ist->st->avg_frame_rate : (AVRational){25, 1};
3019                 if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) {
3020                     int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
3021                     ost->frame_rate = ost->enc->supported_framerates[idx];
3022                 }
3023                 // reduce frame rate for mpeg4 to be within the spec limits
3024                 if (enc_ctx->codec_id == AV_CODEC_ID_MPEG4) {
3025                     av_reduce(&ost->frame_rate.num, &ost->frame_rate.den,
3026                               ost->frame_rate.num, ost->frame_rate.den, 65535);
3027                 }
3028             }
3029
3030             switch (enc_ctx->codec_type) {
3031             case AVMEDIA_TYPE_AUDIO:
3032                 enc_ctx->sample_fmt     = ost->filter->filter->inputs[0]->format;
3033                 enc_ctx->sample_rate    = ost->filter->filter->inputs[0]->sample_rate;
3034                 enc_ctx->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
3035                 enc_ctx->channels       = avfilter_link_get_channels(ost->filter->filter->inputs[0]);
3036                 enc_ctx->time_base      = (AVRational){ 1, enc_ctx->sample_rate };
3037                 break;
3038             case AVMEDIA_TYPE_VIDEO:
3039                 enc_ctx->time_base = av_inv_q(ost->frame_rate);
3040                 if (!(enc_ctx->time_base.num && enc_ctx->time_base.den))
3041                     enc_ctx->time_base = ost->filter->filter->inputs[0]->time_base;
3042                 if (   av_q2d(enc_ctx->time_base) < 0.001 && video_sync_method != VSYNC_PASSTHROUGH
3043                    && (video_sync_method == VSYNC_CFR || video_sync_method == VSYNC_VSCFR || (video_sync_method == VSYNC_AUTO && !(oc->oformat->flags & AVFMT_VARIABLE_FPS)))){
3044                     av_log(oc, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n"
3045                                                "Please consider specifying a lower framerate, a different muxer or -vsync 2\n");
3046                 }
3047                 for (j = 0; j < ost->forced_kf_count; j++)
3048                     ost->forced_kf_pts[j] = av_rescale_q(ost->forced_kf_pts[j],
3049                                                          AV_TIME_BASE_Q,
3050                                                          enc_ctx->time_base);
3051
3052                 enc_ctx->width  = ost->filter->filter->inputs[0]->w;
3053                 enc_ctx->height = ost->filter->filter->inputs[0]->h;
3054                 enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
3055                     ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
3056                     av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) :
3057                     ost->filter->filter->inputs[0]->sample_aspect_ratio;
3058                 if (!strncmp(ost->enc->name, "libx264", 7) &&
3059                     enc_ctx->pix_fmt == AV_PIX_FMT_NONE &&
3060                     ost->filter->filter->inputs[0]->format != AV_PIX_FMT_YUV420P)
3061                     av_log(NULL, AV_LOG_WARNING,
3062                            "No pixel format specified, %s for H.264 encoding chosen.\n"
3063                            "Use -pix_fmt yuv420p for compatibility with outdated media players.\n",
3064                            av_get_pix_fmt_name(ost->filter->filter->inputs[0]->format));
3065                 if (!strncmp(ost->enc->name, "mpeg2video", 10) &&
3066                     enc_ctx->pix_fmt == AV_PIX_FMT_NONE &&
3067                     ost->filter->filter->inputs[0]->format != AV_PIX_FMT_YUV420P)
3068                     av_log(NULL, AV_LOG_WARNING,
3069                            "No pixel format specified, %s for MPEG-2 encoding chosen.\n"
3070                            "Use -pix_fmt yuv420p for compatibility with outdated media players.\n",
3071                            av_get_pix_fmt_name(ost->filter->filter->inputs[0]->format));
3072                 enc_ctx->pix_fmt = ost->filter->filter->inputs[0]->format;
3073
3074                 ost->st->avg_frame_rate = ost->frame_rate;
3075
3076                 if (!dec_ctx ||
3077                     enc_ctx->width   != dec_ctx->width  ||
3078                     enc_ctx->height  != dec_ctx->height ||
3079                     enc_ctx->pix_fmt != dec_ctx->pix_fmt) {
3080                     enc_ctx->bits_per_raw_sample = frame_bits_per_raw_sample;
3081                 }
3082
3083                 if (ost->forced_keyframes) {
3084                     if (!strncmp(ost->forced_keyframes, "expr:", 5)) {
3085                         ret = av_expr_parse(&ost->forced_keyframes_pexpr, ost->forced_keyframes+5,
3086                                             forced_keyframes_const_names, NULL, NULL, NULL, NULL, 0, NULL);
3087                         if (ret < 0) {
3088                             av_log(NULL, AV_LOG_ERROR,
3089                                    "Invalid force_key_frames expression '%s'\n", ost->forced_keyframes+5);
3090                             return ret;
3091                         }
3092                         ost->forced_keyframes_expr_const_values[FKF_N] = 0;
3093                         ost->forced_keyframes_expr_const_values[FKF_N_FORCED] = 0;
3094                         ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] = NAN;
3095                         ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] = NAN;
3096
3097                         // Don't parse the 'forced_keyframes' in case of 'keep-source-keyframes',
3098                         // parse it only for static kf timings
3099                     } else if(strncmp(ost->forced_keyframes, "source", 6)) {
3100                         parse_forced_key_frames(ost->forced_keyframes, ost, ost->enc_ctx);
3101                     }
3102                 }
3103                 break;
3104             case AVMEDIA_TYPE_SUBTITLE:
3105                 enc_ctx->time_base = (AVRational){1, 1000};
3106                 if (!enc_ctx->width) {
3107                     enc_ctx->width     = input_streams[ost->source_index]->st->codec->width;
3108                     enc_ctx->height    = input_streams[ost->source_index]->st->codec->height;
3109                 }
3110                 break;
3111             case AVMEDIA_TYPE_DATA:
3112                 break;
3113             default:
3114                 abort();
3115                 break;
3116             }
3117         }
3118
3119         if (ost->disposition) {
3120             static const AVOption opts[] = {
3121                 { "disposition"         , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
3122                 { "default"             , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEFAULT           },    .unit = "flags" },
3123                 { "dub"                 , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DUB               },    .unit = "flags" },
3124                 { "original"            , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ORIGINAL          },    .unit = "flags" },
3125                 { "comment"             , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_COMMENT           },    .unit = "flags" },
3126                 { "lyrics"              , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_LYRICS            },    .unit = "flags" },
3127                 { "karaoke"             , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_KARAOKE           },    .unit = "flags" },
3128                 { "forced"              , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_FORCED            },    .unit = "flags" },
3129                 { "hearing_impaired"    , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_HEARING_IMPAIRED  },    .unit = "flags" },
3130                 { "visual_impaired"     , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_VISUAL_IMPAIRED   },    .unit = "flags" },
3131                 { "clean_effects"       , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CLEAN_EFFECTS     },    .unit = "flags" },
3132                 { "captions"            , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CAPTIONS          },    .unit = "flags" },
3133                 { "descriptions"        , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DESCRIPTIONS      },    .unit = "flags" },
3134                 { "metadata"            , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_METADATA          },    .unit = "flags" },
3135                 { NULL },
3136             };
3137             static const AVClass class = {
3138                 .class_name = "",
3139                 .item_name  = av_default_item_name,
3140                 .option     = opts,
3141                 .version    = LIBAVUTIL_VERSION_INT,
3142             };
3143             const AVClass *pclass = &class;
3144
3145             ret = av_opt_eval_flags(&pclass, &opts[0], ost->disposition, &ost->st->disposition);
3146             if (ret < 0)
3147                 goto dump_format;
3148         }
3149     }
3150
3151     /* open each encoder */
3152     for (i = 0; i < nb_output_streams; i++) {
3153         ret = init_output_stream(output_streams[i], error, sizeof(error));
3154         if (ret < 0)
3155             goto dump_format;
3156     }
3157
3158     /* init input streams */
3159     for (i = 0; i < nb_input_streams; i++)
3160         if ((ret = init_input_stream(i, error, sizeof(error))) < 0) {
3161             for (i = 0; i < nb_output_streams; i++) {
3162                 ost = output_streams[i];
3163                 avcodec_close(ost->enc_ctx);
3164             }
3165             goto dump_format;
3166         }
3167
3168     /* discard unused programs */
3169     for (i = 0; i < nb_input_files; i++) {
3170         InputFile *ifile = input_files[i];
3171         for (j = 0; j < ifile->ctx->nb_programs; j++) {
3172             AVProgram *p = ifile->ctx->programs[j];
3173             int discard  = AVDISCARD_ALL;
3174
3175             for (k = 0; k < p->nb_stream_indexes; k++)
3176                 if (!input_streams[ifile->ist_index + p->stream_index[k]]->discard) {
3177                     discard = AVDISCARD_DEFAULT;
3178                     break;
3179                 }
3180             p->discard = discard;
3181         }
3182     }
3183
3184     /* open files and write file headers */
3185     for (i = 0; i < nb_output_files; i++) {
3186         oc = output_files[i]->ctx;
3187         oc->interrupt_callback = int_cb;
3188         if ((ret = avformat_write_header(oc, &output_files[i]->opts)) < 0) {
3189             snprintf(error, sizeof(error),
3190                      "Could not write header for output file #%d "
3191                      "(incorrect codec parameters ?): %s",
3192                      i, av_err2str(ret));
3193             ret = AVERROR(EINVAL);
3194             goto dump_format;
3195         }
3196 //         assert_avoptions(output_files[i]->opts);
3197         if (strcmp(oc->oformat->name, "rtp")) {
3198             want_sdp = 0;
3199         }
3200     }
3201
3202  dump_format:
3203     /* dump the file output parameters - cannot be done before in case
3204        of stream copy */
3205     for (i = 0; i < nb_output_files; i++) {
3206         av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1);
3207     }
3208
3209     /* dump the stream mapping */
3210     av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
3211     for (i = 0; i < nb_input_streams; i++) {
3212         ist = input_streams[i];
3213
3214         for (j = 0; j < ist->nb_filters; j++) {
3215             if (ist->filters[j]->graph->graph_desc) {
3216                 av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d (%s) -> %s",
3217                        ist->file_index, ist->st->index, ist->dec ? ist->dec->name : "?",
3218                        ist->filters[j]->name);
3219                 if (nb_filtergraphs > 1)
3220                     av_log(NULL, AV_LOG_INFO, " (graph %d)", ist->filters[j]->graph->index);
3221                 av_log(NULL, AV_LOG_INFO, "\n");
3222             }
3223         }
3224     }
3225
3226     for (i = 0; i < nb_output_streams; i++) {
3227         ost = output_streams[i];
3228
3229         if (ost->attachment_filename) {
3230             /* an attached file */
3231             av_log(NULL, AV_LOG_INFO, "  File %s -> Stream #%d:%d\n",
3232                    ost->attachment_filename, ost->file_index, ost->index);
3233             continue;
3234         }
3235
3236         if (ost->filter && ost->filter->graph->graph_desc) {
3237             /* output from a complex graph */
3238             av_log(NULL, AV_LOG_INFO, "  %s", ost->filter->name);
3239             if (nb_filtergraphs > 1)
3240                 av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);
3241
3242             av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index,
3243                    ost->index, ost->enc ? ost->enc->name : "?");
3244             continue;
3245         }
3246
3247         av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d -> #%d:%d",
3248                input_streams[ost->source_index]->file_index,
3249                input_streams[ost->source_index]->st->index,
3250                ost->file_index,
3251                ost->index);
3252         if (ost->sync_ist != input_streams[ost->source_index])
3253             av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]",
3254                    ost->sync_ist->file_index,
3255                    ost->sync_ist->st->index);
3256         if (ost->stream_copy)
3257             av_log(NULL, AV_LOG_INFO, " (copy)");
3258         else {
3259             const AVCodec *in_codec    = input_streams[ost->source_index]->dec;
3260             const AVCodec *out_codec   = ost->enc;
3261             const char *decoder_name   = "?";
3262             const char *in_codec_name  = "?";
3263             const char *encoder_name   = "?";
3264             const char *out_codec_name = "?";
3265             const AVCodecDescriptor *desc;
3266
3267             if (in_codec) {
3268                 decoder_name  = in_codec->name;
3269                 desc = avcodec_descriptor_get(in_codec->id);
3270                 if (desc)
3271                     in_codec_name = desc->name;
3272                 if (!strcmp(decoder_name, in_codec_name))
3273                     decoder_name = "native";
3274             }
3275
3276             if (out_codec) {
3277                 encoder_name   = out_codec->name;
3278                 desc = avcodec_descriptor_get(out_codec->id);
3279                 if (desc)
3280                     out_codec_name = desc->name;
3281                 if (!strcmp(encoder_name, out_codec_name))
3282                     encoder_name = "native";
3283             }
3284
3285             av_log(NULL, AV_LOG_INFO, " (%s (%s) -> %s (%s))",
3286                    in_codec_name, decoder_name,
3287                    out_codec_name, encoder_name);
3288         }
3289         av_log(NULL, AV_LOG_INFO, "\n");
3290     }
3291
3292     if (ret) {
3293         av_log(NULL, AV_LOG_ERROR, "%s\n", error);
3294         return ret;
3295     }
3296
3297     if (sdp_filename || want_sdp) {
3298         print_sdp();
3299     }
3300
3301     transcode_init_done = 1;
3302
3303     return 0;
3304 }
3305
3306 /* Return 1 if there remain streams where more output is wanted, 0 otherwise. */
3307 static int need_output(void)
3308 {
3309     int i;
3310
3311     for (i = 0; i < nb_output_streams; i++) {
3312         OutputStream *ost    = output_streams[i];
3313         OutputFile *of       = output_files[ost->file_index];
3314         AVFormatContext *os  = output_files[ost->file_index]->ctx;
3315
3316         if (ost->finished ||
3317             (os->pb && avio_tell(os->pb) >= of->limit_filesize))
3318             continue;
3319         if (ost->frame_number >= ost->max_frames) {
3320             int j;
3321             for (j = 0; j < of->ctx->nb_streams; j++)
3322                 close_output_stream(output_streams[of->ost_index + j]);
3323             continue;
3324         }
3325
3326         return 1;
3327     }
3328
3329     return 0;
3330 }
3331
3332 /**
3333  * Select the output stream to process.
3334  *
3335  * @return  selected output stream, or NULL if none available
3336  */
3337 static OutputStream *choose_output(void)
3338 {
3339     int i;
3340     int64_t opts_min = INT64_MAX;
3341     OutputStream *ost_min = NULL;
3342
3343     for (i = 0; i < nb_output_streams; i++) {
3344         OutputStream *ost = output_streams[i];
3345         int64_t opts = av_rescale_q(ost->st->cur_dts, ost->st->time_base,
3346                                     AV_TIME_BASE_Q);
3347         if (!ost->finished && opts < opts_min) {
3348             opts_min = opts;
3349             ost_min  = ost->unavailable ? NULL : ost;
3350         }
3351     }
3352     return ost_min;
3353 }
3354
3355 static int check_keyboard_interaction(int64_t cur_time)
3356 {
3357     int i, ret, key;
3358     static int64_t last_time;
3359     if (received_nb_signals)
3360         return AVERROR_EXIT;
3361     /* read_key() returns 0 on EOF */
3362     if(cur_time - last_time >= 100000 && !run_as_daemon){
3363         key =  read_key();
3364         last_time = cur_time;
3365     }else
3366         key = -1;
3367     if (key == 'q')
3368         return AVERROR_EXIT;
3369     if (key == '+') av_log_set_level(av_log_get_level()+10);
3370     if (key == '-') av_log_set_level(av_log_get_level()-10);
3371     if (key == 's') qp_hist     ^= 1;
3372     if (key == 'h'){
3373         if (do_hex_dump){
3374             do_hex_dump = do_pkt_dump = 0;
3375         } else if(do_pkt_dump){
3376             do_hex_dump = 1;
3377         } else
3378             do_pkt_dump = 1;
3379         av_log_set_level(AV_LOG_DEBUG);
3380     }
3381     if (key == 'c' || key == 'C'){
3382         char buf[4096], target[64], command[256], arg[256] = {0};
3383         double time;
3384         int k, n = 0;
3385         fprintf(stderr, "\nEnter command: <target>|all <time>|-1 <command>[ <argument>]\n");
3386         i = 0;
3387         while ((k = read_key()) != '\n' && k != '\r' && i < sizeof(buf)-1)
3388             if (k > 0)
3389                 buf[i++] = k;
3390         buf[i] = 0;
3391         if (k > 0 &&
3392             (n = sscanf(buf, "%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
3393             av_log(NULL, AV_LOG_DEBUG, "Processing command target:%s time:%f command:%s arg:%s",
3394                    target, time, command, arg);
3395             for (i = 0; i < nb_filtergraphs; i++) {
3396                 FilterGraph *fg = filtergraphs[i];
3397                 if (fg->graph) {
3398                     if (time < 0) {
3399                         ret = avfilter_graph_send_command(fg->graph, target, command, arg, buf, sizeof(buf),
3400                                                           key == 'c' ? AVFILTER_CMD_FLAG_ONE : 0);
3401                         fprintf(stderr, "Command reply for stream %d: ret:%d res:\n%s", i, ret, buf);
3402                     } else if (key == 'c') {
3403                         fprintf(stderr, "Queing commands only on filters supporting the specific command is unsupported\n");
3404                         ret = AVERROR_PATCHWELCOME;
3405                     } else {
3406                         ret = avfilter_graph_queue_command(fg->graph, target, command, arg, 0, time);
3407                         if (ret < 0)
3408                             fprintf(stderr, "Queing command failed with error %s\n", av_err2str(ret));
3409                     }
3410                 }
3411             }
3412         } else {
3413             av_log(NULL, AV_LOG_ERROR,
3414                    "Parse error, at least 3 arguments were expected, "
3415                    "only %d given in string '%s'\n", n, buf);
3416         }
3417     }
3418     if (key == 'd' || key == 'D'){
3419         int debug=0;
3420         if(key == 'D') {
3421             debug = input_streams[0]->st->codec->debug<<1;
3422             if(!debug) debug = 1;
3423             while(debug & (FF_DEBUG_DCT_COEFF|FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) //unsupported, would just crash
3424                 debug += debug;
3425         }else{
3426             char buf[32];
3427             int k = 0;
3428             i = 0;
3429             while ((k = read_key()) != '\n' && k != '\r' && i < sizeof(buf)-1)
3430                 if (k > 0)
3431                     buf[i++] = k;
3432             buf[i] = 0;
3433             if (k <= 0 || sscanf(buf, "%d", &debug)!=1)
3434                 fprintf(stderr,"error parsing debug value\n");
3435         }
3436         for(i=0;i<nb_input_streams;i++) {
3437             input_streams[i]->st->codec->debug = debug;
3438         }
3439         for(i=0;i<nb_output_streams;i++) {
3440             OutputStream *ost = output_streams[i];
3441             ost->enc_ctx->debug = debug;
3442         }
3443         if(debug) av_log_set_level(AV_LOG_DEBUG);
3444         fprintf(stderr,"debug=%d\n", debug);
3445     }
3446     if (key == '?'){
3447         fprintf(stderr, "key    function\n"
3448                         "?      show this help\n"
3449                         "+      increase verbosity\n"
3450                         "-      decrease verbosity\n"
3451                         "c      Send command to first matching filter supporting it\n"
3452                         "C      Send/Que command to all matching filters\n"
3453                         "D      cycle through available debug modes\n"
3454                         "h      dump packets/hex press to cycle through the 3 states\n"
3455                         "q      quit\n"
3456                         "s      Show QP histogram\n"
3457         );
3458     }
3459     return 0;
3460 }
3461
3462 #if HAVE_PTHREADS
3463 static void *input_thread(void *arg)
3464 {
3465     InputFile *f = arg;
3466     unsigned flags = f->non_blocking ? AV_THREAD_MESSAGE_NONBLOCK : 0;
3467     int ret = 0;
3468
3469     while (1) {
3470         AVPacket pkt;
3471         ret = av_read_frame(f->ctx, &pkt);
3472
3473         if (ret == AVERROR(EAGAIN)) {
3474             av_usleep(10000);
3475             continue;
3476         }
3477         if (ret < 0) {
3478             av_thread_message_queue_set_err_recv(f->in_thread_queue, ret);
3479             break;
3480         }
3481         av_dup_packet(&pkt);
3482         ret = av_thread_message_queue_send(f->in_thread_queue, &pkt, flags);
3483         if (flags && ret == AVERROR(EAGAIN)) {
3484             flags = 0;
3485             ret = av_thread_message_queue_send(f->in_thread_queue, &pkt, flags);
3486             av_log(f->ctx, AV_LOG_WARNING,
3487                    "Thread message queue blocking; consider raising the "
3488                    "thread_queue_size option (current value: %d)\n",
3489                    f->thread_queue_size);
3490         }
3491         if (ret < 0) {
3492             if (ret != AVERROR_EOF)
3493                 av_log(f->ctx, AV_LOG_ERROR,
3494                        "Unable to send packet to main thread: %s\n",
3495                        av_err2str(ret));
3496             av_free_packet(&pkt);
3497             av_thread_message_queue_set_err_recv(f->in_thread_queue, ret);
3498             break;
3499         }
3500     }
3501
3502     return NULL;
3503 }
3504
3505 static void free_input_threads(void)
3506 {
3507     int i;
3508
3509     for (i = 0; i < nb_input_files; i++) {
3510         InputFile *f = input_files[i];
3511         AVPacket pkt;
3512
3513         if (!f || !f->in_thread_queue)
3514             continue;
3515         av_thread_message_queue_set_err_send(f->in_thread_queue, AVERROR_EOF);
3516         while (av_thread_message_queue_recv(f->in_thread_queue, &pkt, 0) >= 0)
3517             av_free_packet(&pkt);
3518
3519         pthread_join(f->thread, NULL);
3520         f->joined = 1;
3521         av_thread_message_queue_free(&f->in_thread_queue);
3522     }
3523 }
3524
3525 static int init_input_threads(void)
3526 {
3527     int i, ret;
3528
3529     if (nb_input_files == 1)
3530         return 0;
3531
3532     for (i = 0; i < nb_input_files; i++) {
3533         InputFile *f = input_files[i];
3534
3535         if (f->ctx->pb ? !f->ctx->pb->seekable :
3536             strcmp(f->ctx->iformat->name, "lavfi"))
3537             f->non_blocking = 1;
3538         ret = av_thread_message_queue_alloc(&f->in_thread_queue,
3539                                             f->thread_queue_size, sizeof(AVPacket));
3540         if (ret < 0)
3541             return ret;
3542
3543         if ((ret = pthread_create(&f->thread, NULL, input_thread, f))) {
3544             av_log(NULL, AV_LOG_ERROR, "pthread_create failed: %s. Try to increase `ulimit -v` or decrease `ulimit -s`.\n", strerror(ret));
3545             av_thread_message_queue_free(&f->in_thread_queue);
3546             return AVERROR(ret);
3547         }
3548     }
3549     return 0;
3550 }
3551
3552 static int get_input_packet_mt(InputFile *f, AVPacket *pkt)
3553 {
3554     return av_thread_message_queue_recv(f->in_thread_queue, pkt,
3555                                         f->non_blocking ?
3556                                         AV_THREAD_MESSAGE_NONBLOCK : 0);
3557 }
3558 #endif
3559
3560 static int get_input_packet(InputFile *f, AVPacket *pkt)
3561 {
3562     if (f->rate_emu) {
3563         int i;
3564         for (i = 0; i < f->nb_streams; i++) {
3565             InputStream *ist = input_streams[f->ist_index + i];
3566             int64_t pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE);
3567             int64_t now = av_gettime_relative() - ist->start;
3568             if (pts > now)
3569                 return AVERROR(EAGAIN);
3570         }
3571     }
3572
3573 #if HAVE_PTHREADS
3574     if (nb_input_files > 1)
3575         return get_input_packet_mt(f, pkt);
3576 #endif
3577     return av_read_frame(f->ctx, pkt);
3578 }
3579
3580 static int got_eagain(void)
3581 {
3582     int i;
3583     for (i = 0; i < nb_output_streams; i++)
3584         if (output_streams[i]->unavailable)
3585             return 1;
3586     return 0;
3587 }
3588
3589 static void reset_eagain(void)
3590 {
3591     int i;
3592     for (i = 0; i < nb_input_files; i++)
3593         input_files[i]->eagain = 0;
3594     for (i = 0; i < nb_output_streams; i++)
3595         output_streams[i]->unavailable = 0;
3596 }
3597
3598 /*
3599  * Return
3600  * - 0 -- one packet was read and processed
3601  * - AVERROR(EAGAIN) -- no packets were available for selected file,
3602  *   this function should be called again
3603  * - AVERROR_EOF -- this function should not be called again
3604  */
3605 static int process_input(int file_index)
3606 {
3607     InputFile *ifile = input_files[file_index];
3608     AVFormatContext *is;
3609     InputStream *ist;
3610     AVPacket pkt;
3611     int ret, i, j;
3612
3613     is  = ifile->ctx;
3614     ret = get_input_packet(ifile, &pkt);
3615
3616     if (ret == AVERROR(EAGAIN)) {
3617         ifile->eagain = 1;
3618         return ret;
3619     }
3620     if (ret < 0) {
3621         if (ret != AVERROR_EOF) {
3622             print_error(is->filename, ret);
3623             if (exit_on_error)
3624                 exit_program(1);
3625         }
3626
3627         for (i = 0; i < ifile->nb_streams; i++) {
3628             ist = input_streams[ifile->ist_index + i];
3629             if (ist->decoding_needed) {
3630                 ret = process_input_packet(ist, NULL);
3631                 if (ret>0)
3632                     return 0;
3633             }
3634
3635             /* mark all outputs that don't go through lavfi as finished */
3636             for (j = 0; j < nb_output_streams; j++) {
3637                 OutputStream *ost = output_streams[j];
3638
3639                 if (ost->source_index == ifile->ist_index + i &&
3640                     (ost->stream_copy || ost->enc->type == AVMEDIA_TYPE_SUBTITLE))
3641                     finish_output_stream(ost);
3642             }
3643         }
3644
3645         ifile->eof_reached = 1;
3646         return AVERROR(EAGAIN);
3647     }
3648
3649     reset_eagain();
3650
3651     if (do_pkt_dump) {
3652         av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump,
3653                          is->streams[pkt.stream_index]);
3654     }
3655     /* the following test is needed in case new streams appear
3656        dynamically in stream : we ignore them */
3657     if (pkt.stream_index >= ifile->nb_streams) {
3658         report_new_stream(file_index, &pkt);
3659         goto discard_packet;
3660     }
3661
3662     ist = input_streams[ifile->ist_index + pkt.stream_index];
3663
3664     ist->data_size += pkt.size;
3665     ist->nb_packets++;
3666
3667     if (ist->discard)
3668         goto discard_packet;
3669
3670     if (debug_ts) {
3671         av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s "
3672                "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:%s off_time:%s\n",
3673                ifile->ist_index + pkt.stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
3674                av_ts2str(ist->next_dts), av_ts2timestr(ist->next_dts, &AV_TIME_BASE_Q),
3675                av_ts2str(ist->next_pts), av_ts2timestr(ist->next_pts, &AV_TIME_BASE_Q),
3676                av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ist->st->time_base),
3677                av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ist->st->time_base),
3678                av_ts2str(input_files[ist->file_index]->ts_offset),
3679                av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q));
3680     }
3681
3682     if(!ist->wrap_correction_done && is->start_time != AV_NOPTS_VALUE && ist->st->pts_wrap_bits < 64){
3683         int64_t stime, stime2;
3684         // Correcting starttime based on the enabled streams
3685         // FIXME this ideally should be done before the first use of starttime but we do not know which are the enabled streams at that point.
3686         //       so we instead do it here as part of discontinuity handling
3687         if (   ist->next_dts == AV_NOPTS_VALUE
3688             && ifile->ts_offset == -is->start_time
3689             && (is->iformat->flags & AVFMT_TS_DISCONT)) {
3690             int64_t new_start_time = INT64_MAX;
3691             for (i=0; i<is->nb_streams; i++) {
3692                 AVStream *st = is->streams[i];
3693                 if(st->discard == AVDISCARD_ALL || st->start_time == AV_NOPTS_VALUE)
3694                     continue;
3695                 new_start_time = FFMIN(new_start_time, av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q));
3696             }
3697             if (new_start_time > is->start_time) {
3698                 av_log(is, AV_LOG_VERBOSE, "Correcting start time by %"PRId64"\n", new_start_time - is->start_time);
3699                 ifile->ts_offset = -new_start_time;
3700             }
3701         }
3702
3703         stime = av_rescale_q(is->start_time, AV_TIME_BASE_Q, ist->st->time_base);
3704         stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
3705         ist->wrap_correction_done = 1;
3706
3707         if(stime2 > stime && pkt.dts != AV_NOPTS_VALUE && pkt.dts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
3708             pkt.dts -= 1ULL<<ist->st->pts_wrap_bits;
3709             ist->wrap_correction_done = 0;
3710         }
3711         if(stime2 > stime && pkt.pts != AV_NOPTS_VALUE && pkt.pts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
3712             pkt.pts -= 1ULL<<ist->st->pts_wrap_bits;
3713             ist->wrap_correction_done = 0;
3714         }
3715     }
3716
3717     /* add the stream-global side data to the first packet */
3718     if (ist->nb_packets == 1) {
3719         if (ist->st->nb_side_data)
3720             av_packet_split_side_data(&pkt);
3721         for (i = 0; i < ist->st->nb_side_data; i++) {
3722             AVPacketSideData *src_sd = &ist->st->side_data[i];
3723             uint8_t *dst_data;
3724
3725             if (av_packet_get_side_data(&pkt, src_sd->type, NULL))
3726                 continue;
3727             if (ist->autorotate && src_sd->type == AV_PKT_DATA_DISPLAYMATRIX)
3728                 continue;
3729
3730             dst_data = av_packet_new_side_data(&pkt, src_sd->type, src_sd->size);
3731             if (!dst_data)
3732                 exit_program(1);
3733
3734             memcpy(dst_data, src_sd->data, src_sd->size);
3735         }
3736     }
3737
3738     if (pkt.dts != AV_NOPTS_VALUE)
3739         pkt.dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
3740     if (pkt.pts != AV_NOPTS_VALUE)
3741         pkt.pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
3742
3743     if (pkt.pts != AV_NOPTS_VALUE)
3744         pkt.pts *= ist->ts_scale;
3745     if (pkt.dts != AV_NOPTS_VALUE)
3746         pkt.dts *= ist->ts_scale;
3747
3748     if ((ist->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
3749          ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) &&
3750         pkt.dts != AV_NOPTS_VALUE && ist->next_dts == AV_NOPTS_VALUE && !copy_ts
3751         && (is->iformat->flags & AVFMT_TS_DISCONT) && ifile->last_ts != AV_NOPTS_VALUE) {
3752         int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
3753         int64_t delta   = pkt_dts - ifile->last_ts;
3754         if (delta < -1LL*dts_delta_threshold*AV_TIME_BASE ||
3755             delta >  1LL*dts_delta_threshold*AV_TIME_BASE){
3756             ifile->ts_offset -= delta;
3757             av_log(NULL, AV_LOG_DEBUG,
3758                    "Inter stream timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
3759                    delta, ifile->ts_offset);
3760             pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
3761             if (pkt.pts != AV_NOPTS_VALUE)
3762                 pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
3763         }
3764     }
3765
3766     if ((ist->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
3767          ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) &&
3768          pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE &&
3769         !copy_ts) {
3770         int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
3771         int64_t delta   = pkt_dts - ist->next_dts;
3772         if (is->iformat->flags & AVFMT_TS_DISCONT) {
3773             if (delta < -1LL*dts_delta_threshold*AV_TIME_BASE ||
3774                 delta >  1LL*dts_delta_threshold*AV_TIME_BASE ||
3775                 pkt_dts + AV_TIME_BASE/10 < FFMAX(ist->pts, ist->dts)) {
3776                 ifile->ts_offset -= delta;
3777                 av_log(NULL, AV_LOG_DEBUG,
3778                        "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
3779                        delta, ifile->ts_offset);
3780                 pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
3781                 if (pkt.pts != AV_NOPTS_VALUE)
3782                     pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
3783             }
3784         } else {
3785             if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
3786                  delta >  1LL*dts_error_threshold*AV_TIME_BASE) {
3787                 av_log(NULL, AV_LOG_WARNING, "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n", pkt.dts, ist->next_dts, pkt.stream_index);
3788                 pkt.dts = AV_NOPTS_VALUE;
3789             }
3790             if (pkt.pts != AV_NOPTS_VALUE){
3791                 int64_t pkt_pts = av_rescale_q(pkt.pts, ist->st->time_base, AV_TIME_BASE_Q);
3792                 delta   = pkt_pts - ist->next_dts;
3793                 if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
3794                      delta >  1LL*dts_error_threshold*AV_TIME_BASE) {
3795                     av_log(NULL, AV_LOG_WARNING, "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n", pkt.pts, ist->next_dts, pkt.stream_index);
3796                     pkt.pts = AV_NOPTS_VALUE;
3797                 }
3798             }
3799         }
3800     }
3801
3802     if (pkt.dts != AV_NOPTS_VALUE)
3803         ifile->last_ts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
3804
3805     if (debug_ts) {
3806         av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
3807                ifile->ist_index + pkt.stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
3808                av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ist->st->time_base),
3809                av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ist->st->time_base),
3810                av_ts2str(input_files[ist->file_index]->ts_offset),
3811                av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q));
3812     }
3813
3814     sub2video_heartbeat(ist, pkt.pts);
3815
3816     process_input_packet(ist, &pkt);
3817
3818 discard_packet:
3819     av_free_packet(&pkt);
3820
3821     return 0;
3822 }
3823
3824 /**
3825  * Perform a step of transcoding for the specified filter graph.
3826  *
3827  * @param[in]  graph     filter graph to consider
3828  * @param[out] best_ist  input stream where a frame would allow to continue
3829  * @return  0 for success, <0 for error
3830  */
3831 static int transcode_from_filter(FilterGraph *graph, InputStream **best_ist)
3832 {
3833     int i, ret;
3834     int nb_requests, nb_requests_max = 0;
3835     InputFilter *ifilter;
3836     InputStream *ist;
3837
3838     *best_ist = NULL;
3839     ret = avfilter_graph_request_oldest(graph->graph);
3840     if (ret >= 0)
3841         return reap_filters(0);
3842
3843     if (ret == AVERROR_EOF) {
3844         ret = reap_filters(1);
3845         for (i = 0; i < graph->nb_outputs; i++)
3846             close_output_stream(graph->outputs[i]->ost);
3847         return ret;
3848     }
3849     if (ret != AVERROR(EAGAIN))
3850         return ret;
3851
3852     for (i = 0; i < graph->nb_inputs; i++) {
3853         ifilter = graph->inputs[i];
3854         ist = ifilter->ist;
3855         if (input_files[ist->file_index]->eagain ||
3856             input_files[ist->file_index]->eof_reached)
3857             continue;
3858         nb_requests = av_buffersrc_get_nb_failed_requests(ifilter->filter);
3859         if (nb_requests > nb_requests_max) {
3860             nb_requests_max = nb_requests;
3861             *best_ist = ist;
3862         }
3863     }
3864
3865     if (!*best_ist)
3866         for (i = 0; i < graph->nb_outputs; i++)
3867             graph->outputs[i]->ost->unavailable = 1;
3868
3869     return 0;
3870 }
3871
3872 /**
3873  * Run a single step of transcoding.
3874  *
3875  * @return  0 for success, <0 for error
3876  */
3877 static int transcode_step(void)
3878 {
3879     OutputStream *ost;
3880     InputStream  *ist;
3881     int ret;
3882
3883     ost = choose_output();
3884     if (!ost) {
3885         if (got_eagain()) {
3886             reset_eagain();
3887             av_usleep(10000);
3888             return 0;
3889         }
3890         av_log(NULL, AV_LOG_VERBOSE, "No more inputs to read from, finishing.\n");
3891         return AVERROR_EOF;
3892     }
3893
3894     if (ost->filter) {
3895         if ((ret = transcode_from_filter(ost->filter->graph, &ist)) < 0)
3896             return ret;
3897         if (!ist)
3898             return 0;
3899     } else {
3900         av_assert0(ost->source_index >= 0);
3901         ist = input_streams[ost->source_index];
3902     }
3903
3904     ret = process_input(ist->file_index);
3905     if (ret == AVERROR(EAGAIN)) {
3906         if (input_files[ist->file_index]->eagain)
3907             ost->unavailable = 1;
3908         return 0;
3909     }
3910
3911     if (ret < 0)
3912         return ret == AVERROR_EOF ? 0 : ret;
3913
3914     return reap_filters(0);
3915 }
3916
3917 /*
3918  * The following code is the main loop of the file converter
3919  */
3920 static int transcode(void)
3921 {
3922     int ret, i;
3923     AVFormatContext *os;
3924     OutputStream *ost;
3925     InputStream *ist;
3926     int64_t timer_start;
3927
3928     ret = transcode_init();
3929     if (ret < 0)
3930         goto fail;
3931
3932     if (stdin_interaction) {
3933         av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");
3934     }
3935
3936     timer_start = av_gettime_relative();
3937
3938 #if HAVE_PTHREADS
3939     if ((ret = init_input_threads()) < 0)
3940         goto fail;
3941 #endif
3942
3943     while (!received_sigterm) {
3944         int64_t cur_time= av_gettime_relative();
3945
3946         /* if 'q' pressed, exits */
3947         if (stdin_interaction)
3948             if (check_keyboard_interaction(cur_time) < 0)
3949                 break;
3950
3951         /* check if there's any stream where output is still needed */
3952         if (!need_output()) {
3953             av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");
3954             break;
3955         }
3956
3957         ret = transcode_step();
3958         if (ret < 0) {
3959             if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) {
3960                 continue;
3961             } else {
3962                 char errbuf[128];
3963                 av_strerror(ret, errbuf, sizeof(errbuf));
3964
3965                 av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", errbuf);
3966                 break;
3967             }
3968         }
3969
3970         /* dump report by using the output first video and audio streams */
3971         print_report(0, timer_start, cur_time);
3972     }
3973 #if HAVE_PTHREADS
3974     free_input_threads();
3975 #endif
3976
3977     /* at the end of stream, we must flush the decoder buffers */
3978     for (i = 0; i < nb_input_streams; i++) {
3979         ist = input_streams[i];
3980         if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {
3981             process_input_packet(ist, NULL);
3982         }
3983     }
3984     flush_encoders();
3985
3986     term_exit();
3987
3988     /* write the trailer if needed and close file */
3989     for (i = 0; i < nb_output_files; i++) {
3990         os = output_files[i]->ctx;
3991         av_write_trailer(os);
3992     }
3993
3994     /* dump report by using the first video and audio streams */
3995     print_report(1, timer_start, av_gettime_relative());
3996
3997     /* close each encoder */
3998     for (i = 0; i < nb_output_streams; i++) {
3999         ost = output_streams[i];
4000         if (ost->encoding_needed) {
4001             av_freep(&ost->enc_ctx->stats_in);
4002         }
4003     }
4004
4005     /* close each decoder */
4006     for (i = 0; i < nb_input_streams; i++) {
4007         ist = input_streams[i];
4008         if (ist->decoding_needed) {
4009             avcodec_close(ist->dec_ctx);
4010             if (ist->hwaccel_uninit)
4011                 ist->hwaccel_uninit(ist->dec_ctx);
4012         }
4013     }
4014
4015     /* finished ! */
4016     ret = 0;
4017
4018  fail:
4019 #if HAVE_PTHREADS
4020     free_input_threads();
4021 #endif
4022
4023     if (output_streams) {
4024         for (i = 0; i < nb_output_streams; i++) {
4025             ost = output_streams[i];
4026             if (ost) {
4027                 if (ost->logfile) {
4028                     fclose(ost->logfile);
4029                     ost->logfile = NULL;
4030                 }
4031                 av_freep(&ost->forced_kf_pts);
4032                 av_freep(&ost->apad);
4033                 av_freep(&ost->disposition);
4034                 av_dict_free(&ost->encoder_opts);
4035                 av_dict_free(&ost->sws_dict);
4036                 av_dict_free(&ost->swr_opts);
4037                 av_dict_free(&ost->resample_opts);
4038                 av_dict_free(&ost->bsf_args);
4039             }
4040         }
4041     }
4042     return ret;
4043 }
4044
4045
4046 static int64_t getutime(void)
4047 {
4048 #if HAVE_GETRUSAGE
4049     struct rusage rusage;
4050
4051     getrusage(RUSAGE_SELF, &rusage);
4052     return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
4053 #elif HAVE_GETPROCESSTIMES
4054     HANDLE proc;
4055     FILETIME c, e, k, u;
4056     proc = GetCurrentProcess();
4057     GetProcessTimes(proc, &c, &e, &k, &u);
4058     return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
4059 #else
4060     return av_gettime_relative();
4061 #endif
4062 }
4063
4064 static int64_t getmaxrss(void)
4065 {
4066 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
4067     struct rusage rusage;
4068     getrusage(RUSAGE_SELF, &rusage);
4069     return (int64_t)rusage.ru_maxrss * 1024;
4070 #elif HAVE_GETPROCESSMEMORYINFO
4071     HANDLE proc;
4072     PROCESS_MEMORY_COUNTERS memcounters;
4073     proc = GetCurrentProcess();
4074     memcounters.cb = sizeof(memcounters);
4075     GetProcessMemoryInfo(proc, &memcounters, sizeof(memcounters));
4076     return memcounters.PeakPagefileUsage;
4077 #else
4078     return 0;
4079 #endif
4080 }
4081
4082 static void log_callback_null(void *ptr, int level, const char *fmt, va_list vl)
4083 {
4084 }
4085
4086 int main(int argc, char **argv)
4087 {
4088     int ret;
4089     int64_t ti;
4090
4091     register_exit(ffmpeg_cleanup);
4092
4093     setvbuf(stderr,NULL,_IONBF,0); /* win32 runtime needs this */
4094
4095     av_log_set_flags(AV_LOG_SKIP_REPEATED);
4096     parse_loglevel(argc, argv, options);
4097
4098     if(argc>1 && !strcmp(argv[1], "-d")){
4099         run_as_daemon=1;
4100         av_log_set_callback(log_callback_null);
4101         argc--;
4102         argv++;
4103     }
4104
4105     avcodec_register_all();
4106 #if CONFIG_AVDEVICE
4107     avdevice_register_all();
4108 #endif
4109     avfilter_register_all();
4110     av_register_all();
4111     avformat_network_init();
4112
4113     show_banner(argc, argv, options);
4114
4115     term_init();
4116
4117     /* parse options and open all input/output files */
4118     ret = ffmpeg_parse_options(argc, argv);
4119     if (ret < 0)
4120         exit_program(1);
4121
4122     if (nb_output_files <= 0 && nb_input_files == 0) {
4123         show_usage();
4124         av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
4125         exit_program(1);
4126     }
4127
4128     /* file converter / grab */
4129     if (nb_output_files <= 0) {
4130         av_log(NULL, AV_LOG_FATAL, "At least one output file must be specified\n");
4131         exit_program(1);
4132     }
4133
4134 //     if (nb_input_files == 0) {
4135 //         av_log(NULL, AV_LOG_FATAL, "At least one input file must be specified\n");
4136 //         exit_program(1);
4137 //     }
4138
4139     current_time = ti = getutime();
4140     if (transcode() < 0)
4141         exit_program(1);
4142     ti = getutime() - ti;
4143     if (do_benchmark) {
4144         av_log(NULL, AV_LOG_INFO, "bench: utime=%0.3fs\n", ti / 1000000.0);
4145     }
4146     av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" frames successfully decoded, %"PRIu64" decoding errors\n",
4147            decode_error_stat[0], decode_error_stat[1]);
4148     if ((decode_error_stat[0] + decode_error_stat[1]) * max_error_rate < decode_error_stat[1])
4149         exit_program(69);
4150
4151     exit_program(received_nb_signals ? 255 : main_return_code);
4152     return main_return_code;
4153 }