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