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