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