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