Libav
avconv.c
Go to the documentation of this file.
1 /*
2  * avconv main
3  * Copyright (c) 2000-2011 The Libav developers
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "config.h"
23 #include <ctype.h>
24 #include <string.h>
25 #include <math.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <signal.h>
29 #include <limits.h>
30 #include <stdint.h>
31 
32 #include "libavformat/avformat.h"
33 #include "libavdevice/avdevice.h"
34 #include "libswscale/swscale.h"
36 #include "libavutil/opt.h"
38 #include "libavutil/parseutils.h"
39 #include "libavutil/samplefmt.h"
40 #include "libavutil/fifo.h"
41 #include "libavutil/internal.h"
42 #include "libavutil/intreadwrite.h"
43 #include "libavutil/dict.h"
44 #include "libavutil/mathematics.h"
45 #include "libavutil/pixdesc.h"
46 #include "libavutil/avstring.h"
47 #include "libavutil/libm.h"
48 #include "libavutil/imgutils.h"
49 #include "libavutil/time.h"
50 #include "libavformat/os_support.h"
51 
52 # include "libavfilter/avfilter.h"
53 # include "libavfilter/buffersrc.h"
54 # include "libavfilter/buffersink.h"
55 
56 #if HAVE_SYS_RESOURCE_H
57 #include <sys/time.h>
58 #include <sys/types.h>
59 #include <sys/resource.h>
60 #elif HAVE_GETPROCESSTIMES
61 #include <windows.h>
62 #endif
63 #if HAVE_GETPROCESSMEMORYINFO
64 #include <windows.h>
65 #include <psapi.h>
66 #endif
67 
68 #if HAVE_SYS_SELECT_H
69 #include <sys/select.h>
70 #endif
71 
72 #if HAVE_PTHREADS
73 #include <pthread.h>
74 #endif
75 
76 #include <time.h>
77 
78 #include "avconv.h"
79 #include "cmdutils.h"
80 
81 #include "libavutil/avassert.h"
82 
83 const char program_name[] = "avconv";
84 const int program_birth_year = 2000;
85 
86 static FILE *vstats_file;
87 
88 static int nb_frames_drop = 0;
89 
90 static int want_sdp = 1;
91 
92 #if HAVE_PTHREADS
93 /* signal to input threads that they should exit; set by the main thread */
95 #endif
96 
101 
106 
109 
110 static void term_exit(void)
111 {
112  av_log(NULL, AV_LOG_QUIET, "");
113 }
114 
115 static volatile int received_sigterm = 0;
116 static volatile int received_nb_signals = 0;
117 
118 static void
120 {
121  received_sigterm = sig;
123  term_exit();
124 }
125 
126 static void term_init(void)
127 {
128  signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
129  signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
130 #ifdef SIGXCPU
131  signal(SIGXCPU, sigterm_handler);
132 #endif
133 }
134 
135 static int decode_interrupt_cb(void *ctx)
136 {
137  return received_nb_signals > 1;
138 }
139 
141 
142 static void avconv_cleanup(int ret)
143 {
144  int i, j;
145 
146  for (i = 0; i < nb_filtergraphs; i++) {
147  FilterGraph *fg = filtergraphs[i];
149  for (j = 0; j < fg->nb_inputs; j++) {
150  while (av_fifo_size(fg->inputs[j]->frame_queue)) {
151  AVFrame *frame;
152  av_fifo_generic_read(fg->inputs[j]->frame_queue, &frame,
153  sizeof(frame), NULL);
154  av_frame_free(&frame);
155  }
158  av_freep(&fg->inputs[j]->name);
159  av_freep(&fg->inputs[j]);
160  }
161  av_freep(&fg->inputs);
162  for (j = 0; j < fg->nb_outputs; j++) {
163  av_freep(&fg->outputs[j]->name);
164  av_freep(&fg->outputs[j]->formats);
165  av_freep(&fg->outputs[j]->channel_layouts);
166  av_freep(&fg->outputs[j]->sample_rates);
167  av_freep(&fg->outputs[j]);
168  }
169  av_freep(&fg->outputs);
170  av_freep(&fg->graph_desc);
171 
172  av_freep(&filtergraphs[i]);
173  }
174  av_freep(&filtergraphs);
175 
176  /* close files */
177  for (i = 0; i < nb_output_files; i++) {
178  OutputFile *of = output_files[i];
179  AVFormatContext *s = of->ctx;
180  if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE) && s->pb)
181  avio_close(s->pb);
183  av_dict_free(&of->opts);
184 
185  av_freep(&output_files[i]);
186  }
187  for (i = 0; i < nb_output_streams; i++) {
188  OutputStream *ost = output_streams[i];
189 
190  for (j = 0; j < ost->nb_bitstream_filters; j++)
191  av_bsf_free(&ost->bsf_ctx[j]);
192  av_freep(&ost->bsf_ctx);
194 
196 
197  av_parser_close(ost->parser);
199 
200  av_freep(&ost->forced_keyframes);
201  av_freep(&ost->avfilter);
202  av_freep(&ost->logfile_prefix);
203 
205 
206  if (ost->muxing_queue) {
207  while (av_fifo_size(ost->muxing_queue)) {
208  AVPacket pkt;
209  av_log(NULL, AV_LOG_INFO, "after av_fifo_size()\n");
210  av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL);
211  av_packet_unref(&pkt);
212  }
214  }
215  av_freep(&output_streams[i]);
216  }
217  for (i = 0; i < nb_input_files; i++) {
218  avformat_close_input(&input_files[i]->ctx);
219  av_freep(&input_files[i]);
220  }
221  for (i = 0; i < nb_input_streams; i++) {
222  InputStream *ist = input_streams[i];
223 
226  av_dict_free(&ist->decoder_opts);
227  av_freep(&ist->filters);
228  av_freep(&ist->hwaccel_device);
229 
231 
232  av_freep(&input_streams[i]);
233  }
234 
235  if (vstats_file)
236  fclose(vstats_file);
238 
239  av_freep(&input_streams);
240  av_freep(&input_files);
241  av_freep(&output_streams);
242  av_freep(&output_files);
243 
244  uninit_opts();
245 
247 
248  if (received_sigterm) {
249  av_log(NULL, AV_LOG_INFO, "Received signal %d: terminating.\n",
250  (int) received_sigterm);
251  exit (255);
252  }
253 }
254 
256 {
258  if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
259  av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
260  exit_program(1);
261  }
262 }
263 
264 static void abort_codec_experimental(AVCodec *c, int encoder)
265 {
266  const char *codec_string = encoder ? "encoder" : "decoder";
267  AVCodec *codec;
268  av_log(NULL, AV_LOG_FATAL, "%s '%s' is experimental and might produce bad "
269  "results.\nAdd '-strict experimental' if you want to use it.\n",
270  codec_string, c->name);
271  codec = encoder ? avcodec_find_encoder(c->id) : avcodec_find_decoder(c->id);
272  if (!(codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
273  av_log(NULL, AV_LOG_FATAL, "Or use the non experimental %s '%s'.\n",
274  codec_string, codec->name);
275  exit_program(1);
276 }
277 
278 static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
279 {
280  AVFormatContext *s = of->ctx;
281  AVStream *st = ost->st;
282  int ret;
283 
284  if (!of->header_written) {
285  AVPacket tmp_pkt;
286  /* the muxer is not initialized yet, buffer the packet */
287  if (!av_fifo_space(ost->muxing_queue)) {
288  int new_size = FFMIN(2 * av_fifo_size(ost->muxing_queue),
289  ost->max_muxing_queue_size);
290  if (new_size <= av_fifo_size(ost->muxing_queue)) {
292  "Too many packets buffered for output stream %d:%d.\n",
293  ost->file_index, ost->st->index);
294  exit_program(1);
295  }
296  ret = av_fifo_realloc2(ost->muxing_queue, new_size);
297  if (ret < 0)
298  exit_program(1);
299  }
300  av_packet_move_ref(&tmp_pkt, pkt);
301  av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL);
302  return;
303  }
304 
305  /*
306  * Audio encoders may split the packets -- #frames in != #packets out.
307  * But there is no reordering, so we can limit the number of output packets
308  * by simply dropping them here.
309  * Counting encoded video frames needs to be done separately because of
310  * reordering, see do_video_out()
311  */
312  if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) {
313  if (ost->frame_number >= ost->max_frames) {
314  av_packet_unref(pkt);
315  return;
316  }
317  ost->frame_number++;
318  }
319  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
321  NULL);
322  ost->quality = sd ? *(int *)sd : -1;
323 
324  if (ost->frame_rate.num) {
325  pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate),
326  ost->mux_timebase);
327  }
328  }
329 
330  if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
331  ost->last_mux_dts != AV_NOPTS_VALUE &&
332  pkt->dts < ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT)) {
333  av_log(NULL, AV_LOG_WARNING, "Non-monotonous DTS in output stream "
334  "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
335  ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts);
336  if (exit_on_error) {
337  av_log(NULL, AV_LOG_FATAL, "aborting.\n");
338  exit_program(1);
339  }
340  av_log(NULL, AV_LOG_WARNING, "changing to %"PRId64". This may result "
341  "in incorrect timestamps in the output file.\n",
342  ost->last_mux_dts + 1);
343  pkt->dts = ost->last_mux_dts + 1;
344  if (pkt->pts != AV_NOPTS_VALUE)
345  pkt->pts = FFMAX(pkt->pts, pkt->dts);
346  }
347  ost->last_mux_dts = pkt->dts;
348 
349  ost->data_size += pkt->size;
350  ost->packets_written++;
351 
352  pkt->stream_index = ost->index;
353  av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base);
354 
355  ret = av_interleaved_write_frame(s, pkt);
356  if (ret < 0) {
357  print_error("av_interleaved_write_frame()", ret);
358  exit_program(1);
359  }
360 }
361 
362 static void output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
363 {
364  int ret = 0;
365 
366  /* apply the output bitstream filters, if any */
367  if (ost->nb_bitstream_filters) {
368  int idx;
369 
370  ret = av_bsf_send_packet(ost->bsf_ctx[0], pkt);
371  if (ret < 0)
372  goto finish;
373 
374  idx = 1;
375  while (idx) {
376  /* get a packet from the previous filter up the chain */
377  ret = av_bsf_receive_packet(ost->bsf_ctx[idx - 1], pkt);
378  if (ret == AVERROR(EAGAIN)) {
379  ret = 0;
380  idx--;
381  continue;
382  } else if (ret < 0)
383  goto finish;
384 
385  /* send it to the next filter down the chain or to the muxer */
386  if (idx < ost->nb_bitstream_filters) {
387  ret = av_bsf_send_packet(ost->bsf_ctx[idx], pkt);
388  if (ret < 0)
389  goto finish;
390  idx++;
391  } else
392  write_packet(of, pkt, ost);
393  }
394  } else
395  write_packet(of, pkt, ost);
396 
397 finish:
398  if (ret < 0 && ret != AVERROR_EOF) {
399  av_log(NULL, AV_LOG_FATAL, "Error applying bitstream filters to an output "
400  "packet for stream #%d:%d.\n", ost->file_index, ost->index);
401  exit_program(1);
402  }
403 }
404 
406 {
407  OutputFile *of = output_files[ost->file_index];
408 
409  if (of->recording_time != INT64_MAX &&
411  AV_TIME_BASE_Q) >= 0) {
412  ost->finished = 1;
413  return 0;
414  }
415  return 1;
416 }
417 
418 static void do_audio_out(OutputFile *of, OutputStream *ost,
419  AVFrame *frame)
420 {
421  AVCodecContext *enc = ost->enc_ctx;
422  AVPacket pkt;
423  int ret;
424 
425  av_init_packet(&pkt);
426  pkt.data = NULL;
427  pkt.size = 0;
428 
429  if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
430  frame->pts = ost->sync_opts;
431  ost->sync_opts = frame->pts + frame->nb_samples;
432 
433  ost->samples_encoded += frame->nb_samples;
434  ost->frames_encoded++;
435 
436  ret = avcodec_send_frame(enc, frame);
437  if (ret < 0)
438  goto error;
439 
440  while (1) {
441  ret = avcodec_receive_packet(enc, &pkt);
442  if (ret == AVERROR(EAGAIN))
443  break;
444  if (ret < 0)
445  goto error;
446 
447  output_packet(of, &pkt, ost);
448  }
449 
450  return;
451 error:
452  av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
453  exit_program(1);
454 }
455 
456 static void do_subtitle_out(OutputFile *of,
457  OutputStream *ost,
458  InputStream *ist,
459  AVSubtitle *sub,
460  int64_t pts)
461 {
462  static uint8_t *subtitle_out = NULL;
463  int subtitle_out_max_size = 1024 * 1024;
464  int subtitle_out_size, nb, i;
465  AVCodecContext *enc;
466  AVPacket pkt;
467 
468  if (pts == AV_NOPTS_VALUE) {
469  av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
470  if (exit_on_error)
471  exit_program(1);
472  return;
473  }
474 
475  enc = ost->enc_ctx;
476 
477  if (!subtitle_out) {
478  subtitle_out = av_malloc(subtitle_out_max_size);
479  }
480 
481  /* Note: DVB subtitle need one packet to draw them and one other
482  packet to clear them */
483  /* XXX: signal it in the codec context ? */
485  nb = 2;
486  else
487  nb = 1;
488 
489  for (i = 0; i < nb; i++) {
490  ost->sync_opts = av_rescale_q(pts, ist->st->time_base, enc->time_base);
491  if (!check_recording_time(ost))
492  return;
493 
494  sub->pts = av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q);
495  // start_display_time is required to be 0
496  sub->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
498  sub->start_display_time = 0;
499 
500  ost->frames_encoded++;
501 
502  subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
503  subtitle_out_max_size, sub);
504  if (subtitle_out_size < 0) {
505  av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
506  exit_program(1);
507  }
508 
509  av_init_packet(&pkt);
510  pkt.data = subtitle_out;
511  pkt.size = subtitle_out_size;
512  pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->mux_timebase);
513  if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
514  /* XXX: the pts correction is handled here. Maybe handling
515  it in the codec would be better */
516  if (i == 0)
517  pkt.pts += 90 * sub->start_display_time;
518  else
519  pkt.pts += 90 * sub->end_display_time;
520  }
521  output_packet(of, &pkt, ost);
522  }
523 }
524 
525 static void do_video_out(OutputFile *of,
526  OutputStream *ost,
527  AVFrame *in_picture,
528  int *frame_size)
529 {
530  int ret, format_video_sync;
531  AVPacket pkt;
532  AVCodecContext *enc = ost->enc_ctx;
533 
534  *frame_size = 0;
535 
536  format_video_sync = video_sync_method;
537  if (format_video_sync == VSYNC_AUTO)
538  format_video_sync = (of->ctx->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH :
540  if (format_video_sync != VSYNC_PASSTHROUGH &&
541  ost->frame_number &&
542  in_picture->pts != AV_NOPTS_VALUE &&
543  in_picture->pts < ost->sync_opts) {
544  nb_frames_drop++;
546  "*** dropping frame %d from stream %d at ts %"PRId64"\n",
547  ost->frame_number, ost->st->index, in_picture->pts);
548  return;
549  }
550 
551  if (in_picture->pts == AV_NOPTS_VALUE)
552  in_picture->pts = ost->sync_opts;
553  ost->sync_opts = in_picture->pts;
554 
555 
556  if (!ost->frame_number)
557  ost->first_pts = in_picture->pts;
558 
559  av_init_packet(&pkt);
560  pkt.data = NULL;
561  pkt.size = 0;
562 
563  if (ost->frame_number >= ost->max_frames)
564  return;
565 
567  ost->top_field_first >= 0)
568  in_picture->top_field_first = !!ost->top_field_first;
569 
570  in_picture->quality = enc->global_quality;
571  in_picture->pict_type = 0;
572  if (ost->forced_kf_index < ost->forced_kf_count &&
573  in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
574  in_picture->pict_type = AV_PICTURE_TYPE_I;
575  ost->forced_kf_index++;
576  }
577 
578  ost->frames_encoded++;
579 
580  ret = avcodec_send_frame(enc, in_picture);
581  if (ret < 0)
582  goto error;
583 
584  /*
585  * For video, there may be reordering, so we can't throw away frames on
586  * encoder flush, we need to limit them here, before they go into encoder.
587  */
588  ost->frame_number++;
589 
590  while (1) {
591  ret = avcodec_receive_packet(enc, &pkt);
592  if (ret == AVERROR(EAGAIN))
593  break;
594  if (ret < 0)
595  goto error;
596 
597  output_packet(of, &pkt, ost);
598  *frame_size = pkt.size;
599 
600  /* if two pass, output log */
601  if (ost->logfile && enc->stats_out) {
602  fprintf(ost->logfile, "%s", enc->stats_out);
603  }
604 
605  ost->sync_opts++;
606  }
607 
608  return;
609 error:
610  av_assert0(ret != AVERROR(EAGAIN) && ret != AVERROR_EOF);
611  av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
612  exit_program(1);
613 }
614 
615 #if FF_API_CODED_FRAME && FF_API_ERROR_FRAME
616 static double psnr(double d)
617 {
618  return -10.0 * log(d) / log(10.0);
619 }
620 #endif
621 
622 static void do_video_stats(OutputStream *ost, int frame_size)
623 {
624  AVCodecContext *enc;
625  int frame_number;
626  double ti1, bitrate, avg_bitrate;
627 
628  /* this is executed just the first time do_video_stats is called */
629  if (!vstats_file) {
630  vstats_file = fopen(vstats_filename, "w");
631  if (!vstats_file) {
632  perror("fopen");
633  exit_program(1);
634  }
635  }
636 
637  enc = ost->enc_ctx;
638  if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
639  frame_number = ost->frame_number;
640  fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
641  ost->quality / (float)FF_QP2LAMBDA);
642 
643 #if FF_API_CODED_FRAME && FF_API_ERROR_FRAME
645  if (enc->flags & AV_CODEC_FLAG_PSNR)
646  fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
648 #endif
649 
650  fprintf(vstats_file,"f_size= %6d ", frame_size);
651  /* compute pts value */
652  ti1 = ost->sync_opts * av_q2d(enc->time_base);
653  if (ti1 < 0.01)
654  ti1 = 0.01;
655 
656  bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
657  avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
658  fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
659  (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
660 #if FF_API_CODED_FRAME
662  fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type));
664 #endif
665  }
666 }
667 
668 static int init_output_stream(OutputStream *ost, char *error, int error_len);
669 
670 /*
671  * Read one frame for lavfi output for ost and encode it.
672  */
673 static int poll_filter(OutputStream *ost)
674 {
675  OutputFile *of = output_files[ost->file_index];
676  AVFrame *filtered_frame = NULL;
677  int frame_size, ret;
678 
679  if (!ost->filtered_frame && !(ost->filtered_frame = av_frame_alloc())) {
680  return AVERROR(ENOMEM);
681  }
682  filtered_frame = ost->filtered_frame;
683 
684  if (!ost->initialized) {
685  char error[1024];
686  ret = init_output_stream(ost, error, sizeof(error));
687  if (ret < 0) {
688  av_log(NULL, AV_LOG_ERROR, "Error initializing output stream %d:%d -- %s\n",
689  ost->file_index, ost->index, error);
690  exit_program(1);
691  }
692  }
693 
694  if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
696  ret = av_buffersink_get_samples(ost->filter->filter, filtered_frame,
697  ost->enc_ctx->frame_size);
698  else
699  ret = av_buffersink_get_frame(ost->filter->filter, filtered_frame);
700 
701  if (ret < 0)
702  return ret;
703 
704  if (filtered_frame->pts != AV_NOPTS_VALUE) {
705  int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
706  filtered_frame->pts = av_rescale_q(filtered_frame->pts,
707  ost->filter->filter->inputs[0]->time_base,
708  ost->enc_ctx->time_base) -
709  av_rescale_q(start_time,
711  ost->enc_ctx->time_base);
712  }
713 
714  switch (ost->filter->filter->inputs[0]->type) {
715  case AVMEDIA_TYPE_VIDEO:
716  if (!ost->frame_aspect_ratio)
717  ost->enc_ctx->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
718 
719  do_video_out(of, ost, filtered_frame, &frame_size);
720  if (vstats_filename && frame_size)
721  do_video_stats(ost, frame_size);
722  break;
723  case AVMEDIA_TYPE_AUDIO:
724  do_audio_out(of, ost, filtered_frame);
725  break;
726  default:
727  // TODO support subtitle filters
728  av_assert0(0);
729  }
730 
731  av_frame_unref(filtered_frame);
732 
733  return 0;
734 }
735 
737 {
738  OutputFile *of = output_files[ost->file_index];
739  int i;
740 
741  ost->finished = 1;
742 
743  if (of->shortest) {
744  for (i = 0; i < of->ctx->nb_streams; i++)
745  output_streams[of->ost_index + i]->finished = 1;
746  }
747 }
748 
749 /*
750  * Read as many frames from possible from lavfi and encode them.
751  *
752  * Always read from the active stream with the lowest timestamp. If no frames
753  * are available for it then return EAGAIN and wait for more input. This way we
754  * can use lavfi sources that generate unlimited amount of frames without memory
755  * usage exploding.
756  */
757 static int poll_filters(void)
758 {
759  int i, ret = 0;
760 
761  while (ret >= 0 && !received_sigterm) {
762  OutputStream *ost = NULL;
763  int64_t min_pts = INT64_MAX;
764 
765  /* choose output stream with the lowest timestamp */
766  for (i = 0; i < nb_output_streams; i++) {
767  int64_t pts = output_streams[i]->sync_opts;
768 
769  if (output_streams[i]->filter && !output_streams[i]->filter->graph->graph &&
770  !output_streams[i]->filter->graph->nb_inputs) {
771  ret = configure_filtergraph(output_streams[i]->filter->graph);
772  if (ret < 0) {
773  av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
774  return ret;
775  }
776  }
777 
778  if (!output_streams[i]->filter || output_streams[i]->finished ||
779  !output_streams[i]->filter->graph->graph)
780  continue;
781 
782  pts = av_rescale_q(pts, output_streams[i]->enc_ctx->time_base,
784  if (pts < min_pts) {
785  min_pts = pts;
786  ost = output_streams[i];
787  }
788  }
789 
790  if (!ost)
791  break;
792 
793  ret = poll_filter(ost);
794 
795  if (ret == AVERROR_EOF) {
797  ret = 0;
798  } else if (ret == AVERROR(EAGAIN))
799  return 0;
800  }
801 
802  return ret;
803 }
804 
805 static void print_final_stats(int64_t total_size)
806 {
807  uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
808  uint64_t data_size = 0;
809  float percent = -1.0;
810  int i, j;
811 
812  for (i = 0; i < nb_output_streams; i++) {
813  OutputStream *ost = output_streams[i];
814  switch (ost->enc_ctx->codec_type) {
815  case AVMEDIA_TYPE_VIDEO: video_size += ost->data_size; break;
816  case AVMEDIA_TYPE_AUDIO: audio_size += ost->data_size; break;
817  default: other_size += ost->data_size; break;
818  }
819  extra_size += ost->enc_ctx->extradata_size;
820  data_size += ost->data_size;
821  }
822 
823  if (data_size && total_size >= data_size)
824  percent = 100.0 * (total_size - data_size) / data_size;
825 
826  av_log(NULL, AV_LOG_INFO, "\n");
827  av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ",
828  video_size / 1024.0,
829  audio_size / 1024.0,
830  other_size / 1024.0,
831  extra_size / 1024.0);
832  if (percent >= 0.0)
833  av_log(NULL, AV_LOG_INFO, "%f%%", percent);
834  else
835  av_log(NULL, AV_LOG_INFO, "unknown");
836  av_log(NULL, AV_LOG_INFO, "\n");
837 
838  /* print verbose per-stream stats */
839  for (i = 0; i < nb_input_files; i++) {
840  InputFile *f = input_files[i];
841  uint64_t total_packets = 0, total_size = 0;
842 
843  av_log(NULL, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
844  i, f->ctx->filename);
845 
846  for (j = 0; j < f->nb_streams; j++) {
847  InputStream *ist = input_streams[f->ist_index + j];
848  enum AVMediaType type = ist->dec_ctx->codec_type;
849 
850  total_size += ist->data_size;
851  total_packets += ist->nb_packets;
852 
853  av_log(NULL, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
854  i, j, media_type_string(type));
855  av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
856  ist->nb_packets, ist->data_size);
857 
858  if (ist->decoding_needed) {
859  av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames decoded",
860  ist->frames_decoded);
861  if (type == AVMEDIA_TYPE_AUDIO)
862  av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
863  av_log(NULL, AV_LOG_VERBOSE, "; ");
864  }
865 
866  av_log(NULL, AV_LOG_VERBOSE, "\n");
867  }
868 
869  av_log(NULL, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
870  total_packets, total_size);
871  }
872 
873  for (i = 0; i < nb_output_files; i++) {
874  OutputFile *of = output_files[i];
875  uint64_t total_packets = 0, total_size = 0;
876 
877  av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
878  i, of->ctx->filename);
879 
880  for (j = 0; j < of->ctx->nb_streams; j++) {
881  OutputStream *ost = output_streams[of->ost_index + j];
882  enum AVMediaType type = ost->enc_ctx->codec_type;
883 
884  total_size += ost->data_size;
885  total_packets += ost->packets_written;
886 
887  av_log(NULL, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ",
888  i, j, media_type_string(type));
889  if (ost->encoding_needed) {
890  av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
891  ost->frames_encoded);
892  if (type == AVMEDIA_TYPE_AUDIO)
893  av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
894  av_log(NULL, AV_LOG_VERBOSE, "; ");
895  }
896 
897  av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
898  ost->packets_written, ost->data_size);
899 
900  av_log(NULL, AV_LOG_VERBOSE, "\n");
901  }
902 
903  av_log(NULL, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
904  total_packets, total_size);
905  }
906 }
907 
908 static void print_report(int is_last_report, int64_t timer_start)
909 {
910  char buf[1024];
911  OutputStream *ost;
912  AVFormatContext *oc;
913  int64_t total_size;
914  AVCodecContext *enc;
915  int frame_number, vid, i;
916  double bitrate, ti1, pts;
917  static int64_t last_time = -1;
918  static int qp_histogram[52];
919 
920  if (!print_stats && !is_last_report)
921  return;
922 
923  if (!is_last_report) {
924  int64_t cur_time;
925  /* display the report every 0.5 seconds */
926  cur_time = av_gettime_relative();
927  if (last_time == -1) {
928  last_time = cur_time;
929  return;
930  }
931  if ((cur_time - last_time) < 500000)
932  return;
933  last_time = cur_time;
934  }
935 
936 
937  oc = output_files[0]->ctx;
938 
939  total_size = avio_size(oc->pb);
940  if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too
941  total_size = avio_tell(oc->pb);
942  if (total_size < 0) {
943  char errbuf[128];
944  av_strerror(total_size, errbuf, sizeof(errbuf));
945  av_log(NULL, AV_LOG_VERBOSE, "Bitrate not available, "
946  "avio_tell() failed: %s\n", errbuf);
947  total_size = 0;
948  }
949 
950  buf[0] = '\0';
951  ti1 = 1e10;
952  vid = 0;
953  for (i = 0; i < nb_output_streams; i++) {
954  float q = -1;
955  ost = output_streams[i];
956  enc = ost->enc_ctx;
957  if (!ost->stream_copy)
958  q = ost->quality / (float) FF_QP2LAMBDA;
959 
960  if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
961  snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q);
962  }
963  if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
964  float t = (av_gettime_relative() - timer_start) / 1000000.0;
965 
966  frame_number = ost->frame_number;
967  snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3d q=%3.1f ",
968  frame_number, (t > 1) ? (int)(frame_number / t + 0.5) : 0, q);
969  if (is_last_report)
970  snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
971  if (qp_hist) {
972  int j;
973  int qp = lrintf(q);
974  if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
975  qp_histogram[qp]++;
976  for (j = 0; j < 32; j++)
977  snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log2(qp_histogram[j] + 1)));
978  }
979 
980 #if FF_API_CODED_FRAME && FF_API_ERROR_FRAME
982  if (enc->flags & AV_CODEC_FLAG_PSNR) {
983  int j;
984  double error, error_sum = 0;
985  double scale, scale_sum = 0;
986  char type[3] = { 'Y','U','V' };
987  snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
988  for (j = 0; j < 3; j++) {
989  if (is_last_report) {
990  error = enc->error[j];
991  scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
992  } else {
993  error = enc->coded_frame->error[j];
994  scale = enc->width * enc->height * 255.0 * 255.0;
995  }
996  if (j)
997  scale /= 4;
998  error_sum += error;
999  scale_sum += scale;
1000  snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error / scale));
1001  }
1002  snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum / scale_sum));
1003  }
1005 #endif
1006  vid = 1;
1007  }
1008  /* compute min output value */
1009  pts = (double)ost->last_mux_dts * av_q2d(ost->mux_timebase);
1010  if ((pts < ti1) && (pts > 0))
1011  ti1 = pts;
1012  }
1013  if (ti1 < 0.01)
1014  ti1 = 0.01;
1015 
1016  bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1017 
1018  snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1019  "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
1020  (double)total_size / 1024, ti1, bitrate);
1021 
1022  if (nb_frames_drop)
1023  snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " drop=%d",
1024  nb_frames_drop);
1025 
1026  av_log(NULL, AV_LOG_INFO, "%s \r", buf);
1027 
1028  fflush(stderr);
1029 
1030  if (is_last_report)
1031  print_final_stats(total_size);
1032 
1033 }
1034 
1035 static void flush_encoders(void)
1036 {
1037  int i, ret;
1038 
1039  for (i = 0; i < nb_output_streams; i++) {
1040  OutputStream *ost = output_streams[i];
1041  AVCodecContext *enc = ost->enc_ctx;
1042  OutputFile *of = output_files[ost->file_index];
1043  int stop_encoding = 0;
1044 
1045  if (!ost->encoding_needed)
1046  continue;
1047 
1048  if (enc->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
1049  continue;
1050 
1052  continue;
1053 
1054  avcodec_send_frame(enc, NULL);
1055 
1056  for (;;) {
1057  const char *desc = NULL;
1058 
1059  switch (enc->codec_type) {
1060  case AVMEDIA_TYPE_AUDIO:
1061  desc = "Audio";
1062  break;
1063  case AVMEDIA_TYPE_VIDEO:
1064  desc = "Video";
1065  break;
1066  default:
1067  av_assert0(0);
1068  }
1069 
1070  if (1) {
1071  AVPacket pkt;
1072  av_init_packet(&pkt);
1073  pkt.data = NULL;
1074  pkt.size = 0;
1075 
1076  ret = avcodec_receive_packet(enc, &pkt);
1077  if (ret < 0 && ret != AVERROR_EOF) {
1078  av_log(NULL, AV_LOG_FATAL, "%s encoding failed\n", desc);
1079  exit_program(1);
1080  }
1081  if (ost->logfile && enc->stats_out) {
1082  fprintf(ost->logfile, "%s", enc->stats_out);
1083  }
1084  if (ret == AVERROR_EOF) {
1085  stop_encoding = 1;
1086  break;
1087  }
1088  output_packet(of, &pkt, ost);
1089  }
1090 
1091  if (stop_encoding)
1092  break;
1093  }
1094  }
1095 }
1096 
1097 /*
1098  * Check whether a packet from ist should be written into ost at this time
1099  */
1101 {
1102  OutputFile *of = output_files[ost->file_index];
1103  int ist_index = input_files[ist->file_index]->ist_index + ist->st->index;
1104 
1105  if (ost->source_index != ist_index)
1106  return 0;
1107 
1108  if (of->start_time != AV_NOPTS_VALUE && ist->last_dts < of->start_time)
1109  return 0;
1110 
1111  return 1;
1112 }
1113 
1114 static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
1115 {
1116  OutputFile *of = output_files[ost->file_index];
1117  InputFile *f = input_files [ist->file_index];
1118  int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
1119  int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase);
1120  AVPacket opkt;
1121 
1122  av_init_packet(&opkt);
1123 
1124  if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
1126  return;
1127 
1128  if (of->recording_time != INT64_MAX &&
1129  ist->last_dts >= of->recording_time + start_time) {
1130  ost->finished = 1;
1131  return;
1132  }
1133 
1134  if (f->recording_time != INT64_MAX) {
1135  start_time = f->ctx->start_time;
1136  if (f->start_time != AV_NOPTS_VALUE)
1137  start_time += f->start_time;
1138  if (ist->last_dts >= f->recording_time + start_time) {
1139  ost->finished = 1;
1140  return;
1141  }
1142  }
1143 
1144  /* force the input stream PTS */
1145  if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
1146  ost->sync_opts++;
1147 
1148  if (pkt->pts != AV_NOPTS_VALUE)
1149  opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->mux_timebase) - ost_tb_start_time;
1150  else
1151  opkt.pts = AV_NOPTS_VALUE;
1152 
1153  if (pkt->dts == AV_NOPTS_VALUE)
1154  opkt.dts = av_rescale_q(ist->last_dts, AV_TIME_BASE_Q, ost->mux_timebase);
1155  else
1156  opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->mux_timebase);
1157  opkt.dts -= ost_tb_start_time;
1158 
1159  opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->mux_timebase);
1160  opkt.flags = pkt->flags;
1161 
1162  // FIXME remove the following 2 lines they shall be replaced by the bitstream filters
1163  if ( ost->enc_ctx->codec_id != AV_CODEC_ID_H264
1166  && ost->enc_ctx->codec_id != AV_CODEC_ID_VC1
1167  ) {
1168  if (av_parser_change(ost->parser, ost->parser_avctx,
1169  &opkt.data, &opkt.size,
1170  pkt->data, pkt->size,
1171  pkt->flags & AV_PKT_FLAG_KEY)) {
1172  opkt.buf = av_buffer_create(opkt.data, opkt.size, av_buffer_default_free, NULL, 0);
1173  if (!opkt.buf)
1174  exit_program(1);
1175  }
1176  } else {
1177  opkt.data = pkt->data;
1178  opkt.size = pkt->size;
1179  }
1180 
1181  output_packet(of, &opkt, ost);
1182 }
1183 
1184 static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame)
1185 {
1186  FilterGraph *fg = ifilter->graph;
1187  int need_reinit, ret, i;
1188 
1189  /* determine if the parameters for this input changed */
1190  need_reinit = ifilter->format != frame->format;
1191  if (!!ifilter->hw_frames_ctx != !!frame->hw_frames_ctx ||
1192  (ifilter->hw_frames_ctx && ifilter->hw_frames_ctx->data != frame->hw_frames_ctx->data))
1193  need_reinit = 1;
1194 
1195  switch (ifilter->ist->st->codecpar->codec_type) {
1196  case AVMEDIA_TYPE_AUDIO:
1197  need_reinit |= ifilter->sample_rate != frame->sample_rate ||
1198  ifilter->channel_layout != frame->channel_layout;
1199  break;
1200  case AVMEDIA_TYPE_VIDEO:
1201  need_reinit |= ifilter->width != frame->width ||
1202  ifilter->height != frame->height;
1203  break;
1204  }
1205 
1206  if (need_reinit) {
1207  ret = ifilter_parameters_from_frame(ifilter, frame);
1208  if (ret < 0)
1209  return ret;
1210  }
1211 
1212  /* (re)init the graph if possible, otherwise buffer the frame and return */
1213  if (need_reinit || !fg->graph) {
1214  for (i = 0; i < fg->nb_inputs; i++) {
1215  if (fg->inputs[i]->format < 0) {
1216  AVFrame *tmp = av_frame_clone(frame);
1217  if (!tmp)
1218  return AVERROR(ENOMEM);
1219  av_frame_unref(frame);
1220 
1221  if (!av_fifo_space(ifilter->frame_queue)) {
1222  ret = av_fifo_realloc2(ifilter->frame_queue, 2 * av_fifo_size(ifilter->frame_queue));
1223  if (ret < 0)
1224  return ret;
1225  }
1226  av_fifo_generic_write(ifilter->frame_queue, &tmp, sizeof(tmp), NULL);
1227  return 0;
1228  }
1229  }
1230 
1231  ret = poll_filters();
1232  if (ret < 0 && ret != AVERROR_EOF) {
1233  char errbuf[128];
1234  av_strerror(ret, errbuf, sizeof(errbuf));
1235 
1236  av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", errbuf);
1237  return ret;
1238  }
1239 
1240  ret = configure_filtergraph(fg);
1241  if (ret < 0) {
1242  av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
1243  return ret;
1244  }
1245  }
1246 
1247  ret = av_buffersrc_add_frame(ifilter->filter, frame);
1248  if (ret < 0) {
1249  av_log(NULL, AV_LOG_ERROR, "Error while filtering\n");
1250  return ret;
1251  }
1252 
1253  return 0;
1254 }
1255 
1256 static int ifilter_send_eof(InputFilter *ifilter)
1257 {
1258  int i, j, ret;
1259 
1260  ifilter->eof = 1;
1261 
1262  if (ifilter->filter) {
1263  ret = av_buffersrc_add_frame(ifilter->filter, NULL);
1264  if (ret < 0)
1265  return ret;
1266  } else {
1267  // the filtergraph was never configured
1268  FilterGraph *fg = ifilter->graph;
1269  for (i = 0; i < fg->nb_inputs; i++)
1270  if (!fg->inputs[i]->eof)
1271  break;
1272  if (i == fg->nb_inputs) {
1273  // All the input streams have finished without the filtergraph
1274  // ever being configured.
1275  // Mark the output streams as finished.
1276  for (j = 0; j < fg->nb_outputs; j++)
1277  finish_output_stream(fg->outputs[j]->ost);
1278  }
1279  }
1280 
1281  return 0;
1282 }
1283 
1284 // This does not quite work like avcodec_decode_audio4/avcodec_decode_video2.
1285 // There is the following difference: if you got a frame, you must call
1286 // it again with pkt=NULL. pkt==NULL is treated differently from pkt.size==0
1287 // (pkt==NULL means get more output, pkt.size==0 is a flush/drain packet)
1288 static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
1289 {
1290  int ret;
1291 
1292  *got_frame = 0;
1293 
1294  if (pkt) {
1295  ret = avcodec_send_packet(avctx, pkt);
1296  // In particular, we don't expect AVERROR(EAGAIN), because we read all
1297  // decoded frames with avcodec_receive_frame() until done.
1298  if (ret < 0)
1299  return ret == AVERROR_EOF ? 0 : ret;
1300  }
1301 
1302  ret = avcodec_receive_frame(avctx, frame);
1303  if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
1304  return ret;
1305  if (ret >= 0)
1306  *got_frame = 1;
1307 
1308  return 0;
1309 }
1310 
1312 {
1313  AVCodecContext *dec = ist->dec_ctx;
1314 
1315  if (!dec->channel_layout) {
1316  char layout_name[256];
1317 
1319  if (!dec->channel_layout)
1320  return 0;
1321  av_get_channel_layout_string(layout_name, sizeof(layout_name),
1322  dec->channels, dec->channel_layout);
1323  av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for Input Stream "
1324  "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
1325  }
1326  return 1;
1327 }
1328 
1329 static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
1330 {
1331  AVFrame *decoded_frame, *f;
1332  AVCodecContext *avctx = ist->dec_ctx;
1333  int i, ret, err = 0;
1334 
1335  if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
1336  return AVERROR(ENOMEM);
1337  if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
1338  return AVERROR(ENOMEM);
1339  decoded_frame = ist->decoded_frame;
1340 
1341  ret = decode(avctx, decoded_frame, got_output, pkt);
1342  if (!*got_output || ret < 0)
1343  return ret;
1344 
1345  ist->samples_decoded += decoded_frame->nb_samples;
1346  ist->frames_decoded++;
1347 
1348  /* if the decoder provides a pts, use it instead of the last packet pts.
1349  the decoder could be delaying output by a packet or more. */
1350  if (decoded_frame->pts != AV_NOPTS_VALUE)
1351  ist->next_dts = av_rescale_q(decoded_frame->pts, ist->st->time_base, AV_TIME_BASE_Q);
1352  else if (pkt && pkt->pts != AV_NOPTS_VALUE) {
1353  decoded_frame->pts = pkt->pts;
1354  }
1355 
1356  if (decoded_frame->pts != AV_NOPTS_VALUE)
1357  decoded_frame->pts = av_rescale_q(decoded_frame->pts,
1358  ist->st->time_base,
1359  (AVRational){1, avctx->sample_rate});
1360  ist->nb_samples = decoded_frame->nb_samples;
1361  for (i = 0; i < ist->nb_filters; i++) {
1362  if (i < ist->nb_filters - 1) {
1363  f = ist->filter_frame;
1364  err = av_frame_ref(f, decoded_frame);
1365  if (err < 0)
1366  break;
1367  } else
1368  f = decoded_frame;
1369 
1370  err = ifilter_send_frame(ist->filters[i], f);
1371  if (err < 0)
1372  break;
1373  }
1374 
1376  av_frame_unref(decoded_frame);
1377  return err < 0 ? err : ret;
1378 }
1379 
1380 static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
1381 {
1382  AVFrame *decoded_frame, *f;
1383  int i, ret = 0, err = 0;
1384 
1385  if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
1386  return AVERROR(ENOMEM);
1387  if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
1388  return AVERROR(ENOMEM);
1389  decoded_frame = ist->decoded_frame;
1390 
1391  ret = decode(ist->dec_ctx, decoded_frame, got_output, pkt);
1392  if (!*got_output || ret < 0)
1393  return ret;
1394 
1395  ist->frames_decoded++;
1396 
1397  if (ist->hwaccel_retrieve_data && decoded_frame->format == ist->hwaccel_pix_fmt) {
1398  err = ist->hwaccel_retrieve_data(ist->dec_ctx, decoded_frame);
1399  if (err < 0)
1400  goto fail;
1401  }
1402  ist->hwaccel_retrieved_pix_fmt = decoded_frame->format;
1403 
1404  decoded_frame->pts = guess_correct_pts(&ist->pts_ctx, decoded_frame->pts,
1405  decoded_frame->pkt_dts);
1406  if (ist->framerate.num)
1407  decoded_frame->pts = ist->cfr_next_pts++;
1408 
1409  if (ist->st->sample_aspect_ratio.num)
1410  decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
1411 
1412  for (i = 0; i < ist->nb_filters; i++) {
1413  if (i < ist->nb_filters - 1) {
1414  f = ist->filter_frame;
1415  err = av_frame_ref(f, decoded_frame);
1416  if (err < 0)
1417  break;
1418  } else
1419  f = decoded_frame;
1420 
1421  err = ifilter_send_frame(ist->filters[i], f);
1422  if (err < 0)
1423  break;
1424  }
1425 
1426 fail:
1428  av_frame_unref(decoded_frame);
1429  return err < 0 ? err : ret;
1430 }
1431 
1432 static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
1433 {
1434  AVSubtitle subtitle;
1435  int i, ret = avcodec_decode_subtitle2(ist->dec_ctx,
1436  &subtitle, got_output, pkt);
1437  if (ret < 0)
1438  return ret;
1439  if (!*got_output)
1440  return ret;
1441 
1442  ist->frames_decoded++;
1443 
1444  for (i = 0; i < nb_output_streams; i++) {
1445  OutputStream *ost = output_streams[i];
1446 
1447  if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
1448  continue;
1449 
1450  do_subtitle_out(output_files[ost->file_index], ost, ist, &subtitle, pkt->pts);
1451  }
1452 
1453  avsubtitle_free(&subtitle);
1454  return ret;
1455 }
1456 
1458 {
1459  int i, ret;
1460  for (i = 0; i < ist->nb_filters; i++) {
1461  ret = ifilter_send_eof(ist->filters[i]);
1462  if (ret < 0)
1463  return ret;
1464  }
1465  return 0;
1466 }
1467 
1468 /* pkt = NULL means EOF (needed to flush decoder buffers) */
1469 static void process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
1470 {
1471  int i;
1472  int repeating = 0;
1473  AVPacket avpkt;
1474 
1475  if (ist->next_dts == AV_NOPTS_VALUE)
1476  ist->next_dts = ist->last_dts;
1477 
1478  if (!pkt) {
1479  /* EOF handling */
1480  av_init_packet(&avpkt);
1481  avpkt.data = NULL;
1482  avpkt.size = 0;
1483  } else {
1484  avpkt = *pkt;
1485  }
1486 
1487  if (pkt && pkt->dts != AV_NOPTS_VALUE)
1488  ist->next_dts = ist->last_dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
1489 
1490  // while we have more to decode or while the decoder did output something on EOF
1491  while (ist->decoding_needed && (!pkt || avpkt.size > 0)) {
1492  int ret = 0;
1493  int got_output = 0;
1494 
1495  if (!repeating)
1496  ist->last_dts = ist->next_dts;
1497 
1498  switch (ist->dec_ctx->codec_type) {
1499  case AVMEDIA_TYPE_AUDIO:
1500  ret = decode_audio (ist, repeating ? NULL : &avpkt, &got_output);
1501  break;
1502  case AVMEDIA_TYPE_VIDEO:
1503  ret = decode_video (ist, repeating ? NULL : &avpkt, &got_output);
1504  if (repeating && !got_output)
1505  ;
1506  else if (pkt && pkt->duration)
1507  ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
1508  else if (ist->st->avg_frame_rate.num)
1509  ist->next_dts += av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate),
1510  AV_TIME_BASE_Q);
1511  else if (ist->dec_ctx->framerate.num != 0) {
1512  int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 :
1513  ist->dec_ctx->ticks_per_frame;
1514  ist->next_dts += av_rescale_q(ticks, ist->dec_ctx->framerate, AV_TIME_BASE_Q);
1515  }
1516  break;
1517  case AVMEDIA_TYPE_SUBTITLE:
1518  if (repeating)
1519  break;
1520  ret = transcode_subtitles(ist, &avpkt, &got_output);
1521  break;
1522  default:
1523  return;
1524  }
1525 
1526  if (ret < 0) {
1527  av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d\n",
1528  ist->file_index, ist->st->index);
1529  if (exit_on_error)
1530  exit_program(1);
1531  break;
1532  }
1533 
1534  if (!got_output)
1535  break;
1536 
1537  repeating = 1;
1538  }
1539 
1540  /* after flushing, send an EOF on all the filter inputs attached to the stream */
1541  /* except when looping we need to flush but not to send an EOF */
1542  if (!pkt && ist->decoding_needed && !no_eof) {
1543  int ret = send_filter_eof(ist);
1544  if (ret < 0) {
1545  av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n");
1546  exit_program(1);
1547  }
1548  }
1549 
1550  /* handle stream copy */
1551  if (!ist->decoding_needed) {
1552  ist->last_dts = ist->next_dts;
1553  switch (ist->dec_ctx->codec_type) {
1554  case AVMEDIA_TYPE_AUDIO:
1555  ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->frame_size) /
1556  ist->dec_ctx->sample_rate;
1557  break;
1558  case AVMEDIA_TYPE_VIDEO:
1559  if (ist->dec_ctx->framerate.num != 0) {
1560  int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame;
1561  ist->next_dts += ((int64_t)AV_TIME_BASE *
1562  ist->dec_ctx->framerate.den * ticks) /
1563  ist->dec_ctx->framerate.num;
1564  }
1565  break;
1566  }
1567  }
1568  for (i = 0; pkt && i < nb_output_streams; i++) {
1569  OutputStream *ost = output_streams[i];
1570 
1571  if (!check_output_constraints(ist, ost) || ost->encoding_needed)
1572  continue;
1573 
1574  do_streamcopy(ist, ost, pkt);
1575  }
1576 
1577  return;
1578 }
1579 
1580 static void print_sdp(void)
1581 {
1582  char sdp[16384];
1583  int i;
1584  AVFormatContext **avc;
1585 
1586  for (i = 0; i < nb_output_files; i++) {
1587  if (!output_files[i]->header_written)
1588  return;
1589  }
1590 
1591  avc = av_malloc(sizeof(*avc) * nb_output_files);
1592  if (!avc)
1593  exit_program(1);
1594  for (i = 0; i < nb_output_files; i++)
1595  avc[i] = output_files[i]->ctx;
1596 
1597  av_sdp_create(avc, nb_output_files, sdp, sizeof(sdp));
1598  printf("SDP:\n%s\n", sdp);
1599  fflush(stdout);
1600  av_freep(&avc);
1601 }
1602 
1604 {
1605  int i;
1606  for (i = 0; hwaccels[i].name; i++)
1607  if (hwaccels[i].pix_fmt == pix_fmt)
1608  return &hwaccels[i];
1609  return NULL;
1610 }
1611 
1613 {
1614  InputStream *ist = s->opaque;
1615  const enum AVPixelFormat *p;
1616  int ret;
1617 
1618  for (p = pix_fmts; *p != -1; p++) {
1620  const HWAccel *hwaccel;
1621 
1622  if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
1623  break;
1624 
1625  hwaccel = get_hwaccel(*p);
1626  if (!hwaccel ||
1627  (ist->active_hwaccel_id && ist->active_hwaccel_id != hwaccel->id) ||
1628  (ist->hwaccel_id != HWACCEL_AUTO && ist->hwaccel_id != hwaccel->id))
1629  continue;
1630 
1631  ret = hwaccel->init(s);
1632  if (ret < 0) {
1633  if (ist->hwaccel_id == hwaccel->id) {
1635  "%s hwaccel requested for input stream #%d:%d, "
1636  "but cannot be initialized.\n", hwaccel->name,
1637  ist->file_index, ist->st->index);
1638  return AV_PIX_FMT_NONE;
1639  }
1640  continue;
1641  }
1642 
1643  if (ist->hw_frames_ctx) {
1645  if (!s->hw_frames_ctx)
1646  return AV_PIX_FMT_NONE;
1647  }
1648 
1649  ist->active_hwaccel_id = hwaccel->id;
1650  ist->hwaccel_pix_fmt = *p;
1651  break;
1652  }
1653 
1654  return *p;
1655 }
1656 
1657 static int get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
1658 {
1659  InputStream *ist = s->opaque;
1660 
1661  if (ist->hwaccel_get_buffer && frame->format == ist->hwaccel_pix_fmt)
1662  return ist->hwaccel_get_buffer(s, frame, flags);
1663 
1664  return avcodec_default_get_buffer2(s, frame, flags);
1665 }
1666 
1667 static int init_input_stream(int ist_index, char *error, int error_len)
1668 {
1669  int ret;
1670  InputStream *ist = input_streams[ist_index];
1671 
1672  if (ist->decoding_needed) {
1673  AVCodec *codec = ist->dec;
1674  if (!codec) {
1675  snprintf(error, error_len, "Decoder (codec id %d) not found for input stream #%d:%d",
1676  ist->dec_ctx->codec_id, ist->file_index, ist->st->index);
1677  return AVERROR(EINVAL);
1678  }
1679 
1680  ist->dec_ctx->opaque = ist;
1681  ist->dec_ctx->get_format = get_format;
1682  ist->dec_ctx->get_buffer2 = get_buffer;
1683  ist->dec_ctx->thread_safe_callbacks = 1;
1684 
1685  av_opt_set_int(ist->dec_ctx, "refcounted_frames", 1, 0);
1686 
1687  if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
1688  av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
1689  if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
1690  char errbuf[128];
1691  if (ret == AVERROR_EXPERIMENTAL)
1692  abort_codec_experimental(codec, 0);
1693 
1694  av_strerror(ret, errbuf, sizeof(errbuf));
1695 
1696  snprintf(error, error_len,
1697  "Error while opening decoder for input stream "
1698  "#%d:%d : %s",
1699  ist->file_index, ist->st->index, errbuf);
1700  return ret;
1701  }
1703  }
1704 
1705  ist->last_dts = ist->st->avg_frame_rate.num ? - ist->dec_ctx->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
1706  ist->next_dts = AV_NOPTS_VALUE;
1708 
1709  return 0;
1710 }
1711 
1713 {
1714  if (ost->source_index >= 0)
1715  return input_streams[ost->source_index];
1716 
1717  if (ost->filter) {
1718  FilterGraph *fg = ost->filter->graph;
1719  int i;
1720 
1721  for (i = 0; i < fg->nb_inputs; i++)
1722  if (fg->inputs[i]->ist->dec_ctx->codec_type == ost->enc_ctx->codec_type)
1723  return fg->inputs[i]->ist;
1724  }
1725 
1726  return NULL;
1727 }
1728 
1729 /* open the muxer when all the streams are initialized */
1730 static int check_init_output_file(OutputFile *of, int file_index)
1731 {
1732  int ret, i;
1733 
1734  for (i = 0; i < of->ctx->nb_streams; i++) {
1735  OutputStream *ost = output_streams[of->ost_index + i];
1736  if (!ost->initialized)
1737  return 0;
1738  }
1739 
1740  of->ctx->interrupt_callback = int_cb;
1741 
1742  ret = avformat_write_header(of->ctx, &of->opts);
1743  if (ret < 0) {
1744  char errbuf[128];
1745 
1746  av_strerror(ret, errbuf, sizeof(errbuf));
1747 
1749  "Could not write header for output file #%d "
1750  "(incorrect codec parameters ?): %s",
1751  file_index, errbuf);
1752  return ret;
1753  }
1754  assert_avoptions(of->opts);
1755  of->header_written = 1;
1756 
1757  av_dump_format(of->ctx, file_index, of->ctx->filename, 1);
1758 
1759  if (want_sdp)
1760  print_sdp();
1761 
1762  /* flush the muxing queues */
1763  for (i = 0; i < of->ctx->nb_streams; i++) {
1764  OutputStream *ost = output_streams[of->ost_index + i];
1765 
1766  while (av_fifo_size(ost->muxing_queue)) {
1767  AVPacket pkt;
1768  av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL);
1769  write_packet(of, &pkt, ost);
1770  }
1771  }
1772 
1773  return 0;
1774 }
1775 
1777 {
1778  AVBSFContext *ctx;
1779  int i, ret;
1780 
1781  if (!ost->nb_bitstream_filters)
1782  return 0;
1783 
1784  ost->bsf_ctx = av_mallocz_array(ost->nb_bitstream_filters, sizeof(*ost->bsf_ctx));
1785  if (!ost->bsf_ctx)
1786  return AVERROR(ENOMEM);
1787 
1788  for (i = 0; i < ost->nb_bitstream_filters; i++) {
1789  ret = av_bsf_alloc(ost->bitstream_filters[i], &ctx);
1790  if (ret < 0) {
1791  av_log(NULL, AV_LOG_ERROR, "Error allocating a bitstream filter context\n");
1792  return ret;
1793  }
1794  ost->bsf_ctx[i] = ctx;
1795 
1796  ret = avcodec_parameters_copy(ctx->par_in,
1797  i ? ost->bsf_ctx[i - 1]->par_out : ost->st->codecpar);
1798  if (ret < 0)
1799  return ret;
1800 
1801  ctx->time_base_in = i ? ost->bsf_ctx[i - 1]->time_base_out : ost->st->time_base;
1802 
1803  ret = av_bsf_init(ctx);
1804  if (ret < 0) {
1805  av_log(NULL, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
1806  ost->bitstream_filters[i]->name);
1807  return ret;
1808  }
1809  }
1810 
1811  ctx = ost->bsf_ctx[ost->nb_bitstream_filters - 1];
1812  ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
1813  if (ret < 0)
1814  return ret;
1815 
1816  ost->st->time_base = ctx->time_base_out;
1817 
1818  return 0;
1819 }
1820 
1822 {
1823  OutputFile *of = output_files[ost->file_index];
1824  InputStream *ist = get_input_stream(ost);
1825  AVCodecParameters *par_dst = ost->st->codecpar;
1826  AVCodecParameters *par_src = ist->st->codecpar;
1827  AVRational sar;
1828  uint32_t codec_tag = par_dst->codec_tag;
1829  int i, ret;
1830 
1831  if (!codec_tag) {
1832  if (!of->ctx->oformat->codec_tag ||
1833  av_codec_get_id (of->ctx->oformat->codec_tag, par_src->codec_tag) == par_src->codec_id ||
1834  av_codec_get_tag(of->ctx->oformat->codec_tag, par_src->codec_id) <= 0)
1835  codec_tag = par_src->codec_tag;
1836  }
1837 
1838  ret = avcodec_parameters_copy(par_dst, par_src);
1839  if (ret < 0)
1840  return ret;
1841 
1842  par_dst->codec_tag = codec_tag;
1843 
1844  ost->st->disposition = ist->st->disposition;
1845 
1846  ost->st->time_base = ist->st->time_base;
1847 
1848  if (ist->st->nb_side_data) {
1850  sizeof(*ist->st->side_data));
1851  if (!ost->st->side_data)
1852  return AVERROR(ENOMEM);
1853 
1854  for (i = 0; i < ist->st->nb_side_data; i++) {
1855  const AVPacketSideData *sd_src = &ist->st->side_data[i];
1856  AVPacketSideData *sd_dst = &ost->st->side_data[i];
1857 
1858  sd_dst->data = av_malloc(sd_src->size);
1859  if (!sd_dst->data)
1860  return AVERROR(ENOMEM);
1861  memcpy(sd_dst->data, sd_src->data, sd_src->size);
1862  sd_dst->size = sd_src->size;
1863  sd_dst->type = sd_src->type;
1864  ost->st->nb_side_data++;
1865  }
1866  }
1867 
1868  ost->parser = av_parser_init(par_dst->codec_id);
1870  if (!ost->parser_avctx)
1871  return AVERROR(ENOMEM);
1872 
1873  if (par_dst->codec_type == AVMEDIA_TYPE_VIDEO) {
1874  if (ost->frame_aspect_ratio)
1875  sar = av_d2q(ost->frame_aspect_ratio * par_dst->height / par_dst->width, 255);
1876  else if (ist->st->sample_aspect_ratio.num)
1877  sar = ist->st->sample_aspect_ratio;
1878  else
1879  sar = par_src->sample_aspect_ratio;
1880  ost->st->sample_aspect_ratio = par_dst->sample_aspect_ratio = sar;
1881  }
1882 
1883  return 0;
1884 }
1885 
1887 {
1888  AVDictionaryEntry *e;
1889 
1890  uint8_t *encoder_string;
1891  int encoder_string_len;
1892  int format_flags = 0;
1893 
1894  e = av_dict_get(of->opts, "fflags", NULL, 0);
1895  if (e) {
1896  const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0);
1897  if (!o)
1898  return;
1899  av_opt_eval_flags(of->ctx, o, e->value, &format_flags);
1900  }
1901 
1902  encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2;
1903  encoder_string = av_mallocz(encoder_string_len);
1904  if (!encoder_string)
1905  exit_program(1);
1906 
1907  if (!(format_flags & AVFMT_FLAG_BITEXACT))
1908  av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
1909  av_strlcat(encoder_string, ost->enc->name, encoder_string_len);
1910  av_dict_set(&ost->st->metadata, "encoder", encoder_string,
1912 }
1913 
1914 static void parse_forced_key_frames(char *kf, OutputStream *ost,
1915  AVCodecContext *avctx)
1916 {
1917  char *p;
1918  int n = 1, i;
1919  int64_t t;
1920 
1921  for (p = kf; *p; p++)
1922  if (*p == ',')
1923  n++;
1924  ost->forced_kf_count = n;
1925  ost->forced_kf_pts = av_malloc(sizeof(*ost->forced_kf_pts) * n);
1926  if (!ost->forced_kf_pts) {
1927  av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
1928  exit_program(1);
1929  }
1930 
1931  p = kf;
1932  for (i = 0; i < n; i++) {
1933  char *next = strchr(p, ',');
1934 
1935  if (next)
1936  *next++ = 0;
1937 
1938  t = parse_time_or_die("force_key_frames", p, 1);
1939  ost->forced_kf_pts[i] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
1940 
1941  p = next;
1942  }
1943 }
1944 
1946 {
1947  InputStream *ist = get_input_stream(ost);
1948  AVCodecContext *enc_ctx = ost->enc_ctx;
1949  AVCodecContext *dec_ctx = NULL;
1950 
1951  set_encoder_id(output_files[ost->file_index], ost);
1952 
1953  if (ist) {
1954  ost->st->disposition = ist->st->disposition;
1955 
1956  dec_ctx = ist->dec_ctx;
1957 
1958  enc_ctx->bits_per_raw_sample = dec_ctx->bits_per_raw_sample;
1959  enc_ctx->chroma_sample_location = dec_ctx->chroma_sample_location;
1960  }
1961 
1962  switch (enc_ctx->codec_type) {
1963  case AVMEDIA_TYPE_AUDIO:
1964  enc_ctx->sample_fmt = ost->filter->filter->inputs[0]->format;
1965  enc_ctx->sample_rate = ost->filter->filter->inputs[0]->sample_rate;
1966  enc_ctx->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
1968  enc_ctx->time_base = (AVRational){ 1, enc_ctx->sample_rate };
1969  break;
1970  case AVMEDIA_TYPE_VIDEO:
1971  enc_ctx->time_base = ost->filter->filter->inputs[0]->time_base;
1972 
1973  enc_ctx->width = ost->filter->filter->inputs[0]->w;
1974  enc_ctx->height = ost->filter->filter->inputs[0]->h;
1975  enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
1976  ost->frame_aspect_ratio ? // overridden by the -aspect cli option
1977  av_d2q(ost->frame_aspect_ratio * enc_ctx->height/enc_ctx->width, 255) :
1979  enc_ctx->pix_fmt = ost->filter->filter->inputs[0]->format;
1980 
1981  ost->st->avg_frame_rate = ost->frame_rate;
1982 
1983  if (dec_ctx &&
1984  (enc_ctx->width != dec_ctx->width ||
1985  enc_ctx->height != dec_ctx->height ||
1986  enc_ctx->pix_fmt != dec_ctx->pix_fmt)) {
1987  enc_ctx->bits_per_raw_sample = 0;
1988  }
1989 
1990  if (ost->forced_keyframes)
1992  ost->enc_ctx);
1993  break;
1994  case AVMEDIA_TYPE_SUBTITLE:
1995  enc_ctx->time_base = (AVRational){1, 1000};
1996  break;
1997  default:
1998  abort();
1999  break;
2000  }
2001 
2002  return 0;
2003 }
2004 
2005 static int init_output_stream(OutputStream *ost, char *error, int error_len)
2006 {
2007  int ret = 0;
2008 
2009  if (ost->encoding_needed) {
2010  AVCodec *codec = ost->enc;
2011  AVCodecContext *dec = NULL;
2012  InputStream *ist;
2013 
2014  ret = init_output_stream_encode(ost);
2015  if (ret < 0)
2016  return ret;
2017 
2018  if ((ist = get_input_stream(ost)))
2019  dec = ist->dec_ctx;
2020  if (dec && dec->subtitle_header) {
2022  if (!ost->enc_ctx->subtitle_header)
2023  return AVERROR(ENOMEM);
2024  memcpy(ost->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
2026  }
2027  if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
2028  av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
2029 
2030  if (ost->filter && ost->filter->filter->inputs[0]->hw_frames_ctx) {
2032  if (!ost->enc_ctx->hw_frames_ctx)
2033  return AVERROR(ENOMEM);
2034  }
2035 
2036  if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {
2037  if (ret == AVERROR_EXPERIMENTAL)
2038  abort_codec_experimental(codec, 1);
2039  snprintf(error, error_len,
2040  "Error while opening encoder for output stream #%d:%d - "
2041  "maybe incorrect parameters such as bit_rate, rate, width or height",
2042  ost->file_index, ost->index);
2043  return ret;
2044  }
2046  if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000)
2047  av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
2048  "It takes bits/s as argument, not kbits/s\n");
2049 
2051  if (ret < 0) {
2053  "Error initializing the output stream codec context.\n");
2054  exit_program(1);
2055  }
2056 
2057  if (ost->enc_ctx->nb_coded_side_data) {
2058  int i;
2059 
2061  sizeof(*ost->st->side_data));
2062  if (!ost->st->side_data)
2063  return AVERROR(ENOMEM);
2064 
2065  for (i = 0; i < ost->enc_ctx->nb_coded_side_data; i++) {
2066  const AVPacketSideData *sd_src = &ost->enc_ctx->coded_side_data[i];
2067  AVPacketSideData *sd_dst = &ost->st->side_data[i];
2068 
2069  sd_dst->data = av_malloc(sd_src->size);
2070  if (!sd_dst->data)
2071  return AVERROR(ENOMEM);
2072  memcpy(sd_dst->data, sd_src->data, sd_src->size);
2073  sd_dst->size = sd_src->size;
2074  sd_dst->type = sd_src->type;
2075  ost->st->nb_side_data++;
2076  }
2077  }
2078 
2079  ost->st->time_base = ost->enc_ctx->time_base;
2080  } else if (ost->stream_copy) {
2081  ret = init_output_stream_streamcopy(ost);
2082  if (ret < 0)
2083  return ret;
2084 
2085  /*
2086  * FIXME: will the codec context used by the parser during streamcopy
2087  * This should go away with the new parser API.
2088  */
2090  if (ret < 0)
2091  return ret;
2092  }
2093 
2094  /* initialize bitstream filters for the output stream
2095  * needs to be done here, because the codec id for streamcopy is not
2096  * known until now */
2097  ret = init_output_bsfs(ost);
2098  if (ret < 0)
2099  return ret;
2100 
2101  ost->mux_timebase = ost->st->time_base;
2102 
2103  ost->initialized = 1;
2104 
2105  ret = check_init_output_file(output_files[ost->file_index], ost->file_index);
2106  if (ret < 0)
2107  return ret;
2108 
2109  return ret;
2110 }
2111 
2112 static int transcode_init(void)
2113 {
2114  int ret = 0, i, j, k;
2115  OutputStream *ost;
2116  InputStream *ist;
2117  char error[1024];
2118 
2119  /* init framerate emulation */
2120  for (i = 0; i < nb_input_files; i++) {
2121  InputFile *ifile = input_files[i];
2122  if (ifile->rate_emu)
2123  for (j = 0; j < ifile->nb_streams; j++)
2124  input_streams[j + ifile->ist_index]->start = av_gettime_relative();
2125  }
2126 
2127  /* init input streams */
2128  for (i = 0; i < nb_input_streams; i++)
2129  if ((ret = init_input_stream(i, error, sizeof(error))) < 0)
2130  goto dump_format;
2131 
2132  /* open each encoder */
2133  for (i = 0; i < nb_output_streams; i++) {
2134  // skip streams fed from filtergraphs until we have a frame for them
2135  if (output_streams[i]->filter)
2136  continue;
2137 
2138  ret = init_output_stream(output_streams[i], error, sizeof(error));
2139  if (ret < 0)
2140  goto dump_format;
2141  }
2142 
2143 
2144  /* discard unused programs */
2145  for (i = 0; i < nb_input_files; i++) {
2146  InputFile *ifile = input_files[i];
2147  for (j = 0; j < ifile->ctx->nb_programs; j++) {
2148  AVProgram *p = ifile->ctx->programs[j];
2149  int discard = AVDISCARD_ALL;
2150 
2151  for (k = 0; k < p->nb_stream_indexes; k++)
2152  if (!input_streams[ifile->ist_index + p->stream_index[k]]->discard) {
2153  discard = AVDISCARD_DEFAULT;
2154  break;
2155  }
2156  p->discard = discard;
2157  }
2158  }
2159 
2160  dump_format:
2161  /* dump the stream mapping */
2162  av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
2163  for (i = 0; i < nb_input_streams; i++) {
2164  ist = input_streams[i];
2165 
2166  for (j = 0; j < ist->nb_filters; j++) {
2167  if (!filtergraph_is_simple(ist->filters[j]->graph)) {
2168  av_log(NULL, AV_LOG_INFO, " Stream #%d:%d (%s) -> %s",
2169  ist->file_index, ist->st->index, ist->dec ? ist->dec->name : "?",
2170  ist->filters[j]->name);
2171  if (nb_filtergraphs > 1)
2172  av_log(NULL, AV_LOG_INFO, " (graph %d)", ist->filters[j]->graph->index);
2173  av_log(NULL, AV_LOG_INFO, "\n");
2174  }
2175  }
2176  }
2177 
2178  for (i = 0; i < nb_output_streams; i++) {
2179  ost = output_streams[i];
2180 
2181  if (ost->attachment_filename) {
2182  /* an attached file */
2183  av_log(NULL, AV_LOG_INFO, " File %s -> Stream #%d:%d\n",
2184  ost->attachment_filename, ost->file_index, ost->index);
2185  continue;
2186  }
2187 
2188  if (ost->filter && !filtergraph_is_simple(ost->filter->graph)) {
2189  /* output from a complex graph */
2190  av_log(NULL, AV_LOG_INFO, " %s", ost->filter->name);
2191  if (nb_filtergraphs > 1)
2192  av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);
2193 
2194  av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index,
2195  ost->index, ost->enc ? ost->enc->name : "?");
2196  continue;
2197  }
2198 
2199  av_log(NULL, AV_LOG_INFO, " Stream #%d:%d -> #%d:%d",
2200  input_streams[ost->source_index]->file_index,
2201  input_streams[ost->source_index]->st->index,
2202  ost->file_index,
2203  ost->index);
2204  if (ost->sync_ist != input_streams[ost->source_index])
2205  av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]",
2206  ost->sync_ist->file_index,
2207  ost->sync_ist->st->index);
2208  if (ost->stream_copy)
2209  av_log(NULL, AV_LOG_INFO, " (copy)");
2210  else {
2211  const AVCodec *in_codec = input_streams[ost->source_index]->dec;
2212  const AVCodec *out_codec = ost->enc;
2213  const char *decoder_name = "?";
2214  const char *in_codec_name = "?";
2215  const char *encoder_name = "?";
2216  const char *out_codec_name = "?";
2217  const AVCodecDescriptor *desc;
2218 
2219  if (in_codec) {
2220  decoder_name = in_codec->name;
2221  desc = avcodec_descriptor_get(in_codec->id);
2222  if (desc)
2223  in_codec_name = desc->name;
2224  if (!strcmp(decoder_name, in_codec_name))
2225  decoder_name = "native";
2226  }
2227 
2228  if (out_codec) {
2229  encoder_name = out_codec->name;
2230  desc = avcodec_descriptor_get(out_codec->id);
2231  if (desc)
2232  out_codec_name = desc->name;
2233  if (!strcmp(encoder_name, out_codec_name))
2234  encoder_name = "native";
2235  }
2236 
2237  av_log(NULL, AV_LOG_INFO, " (%s (%s) -> %s (%s))",
2238  in_codec_name, decoder_name,
2239  out_codec_name, encoder_name);
2240  }
2241  av_log(NULL, AV_LOG_INFO, "\n");
2242  }
2243 
2244  if (ret) {
2245  av_log(NULL, AV_LOG_ERROR, "%s\n", error);
2246  return ret;
2247  }
2248 
2249  return 0;
2250 }
2251 
2252 /* Return 1 if there remain streams where more output is wanted, 0 otherwise. */
2253 static int need_output(void)
2254 {
2255  int i;
2256 
2257  for (i = 0; i < nb_output_streams; i++) {
2258  OutputStream *ost = output_streams[i];
2259  OutputFile *of = output_files[ost->file_index];
2260  AVFormatContext *os = output_files[ost->file_index]->ctx;
2261 
2262  if (ost->finished ||
2263  (os->pb && avio_tell(os->pb) >= of->limit_filesize))
2264  continue;
2265  if (ost->frame_number >= ost->max_frames) {
2266  int j;
2267  for (j = 0; j < of->ctx->nb_streams; j++)
2268  output_streams[of->ost_index + j]->finished = 1;
2269  continue;
2270  }
2271 
2272  return 1;
2273  }
2274 
2275  return 0;
2276 }
2277 
2279 {
2280  InputFile *ifile = NULL;
2281  int64_t ipts_min = INT64_MAX;
2282  int i;
2283 
2284  for (i = 0; i < nb_input_streams; i++) {
2285  InputStream *ist = input_streams[i];
2286  int64_t ipts = ist->last_dts;
2287 
2288  if (ist->discard || input_files[ist->file_index]->eagain)
2289  continue;
2290  if (!input_files[ist->file_index]->eof_reached) {
2291  if (ipts < ipts_min) {
2292  ipts_min = ipts;
2293  ifile = input_files[ist->file_index];
2294  }
2295  }
2296  }
2297 
2298  return ifile;
2299 }
2300 
2301 #if HAVE_PTHREADS
2302 static void *input_thread(void *arg)
2303 {
2304  InputFile *f = arg;
2305  int ret = 0;
2306 
2307  while (!transcoding_finished && ret >= 0) {
2308  AVPacket pkt;
2309  ret = av_read_frame(f->ctx, &pkt);
2310 
2311  if (ret == AVERROR(EAGAIN)) {
2312  av_usleep(10000);
2313  ret = 0;
2314  continue;
2315  } else if (ret < 0)
2316  break;
2317 
2319  while (!av_fifo_space(f->fifo))
2321 
2322  av_fifo_generic_write(f->fifo, &pkt, sizeof(pkt), NULL);
2323 
2325  }
2326 
2327  f->finished = 1;
2328  return NULL;
2329 }
2330 
2331 static void free_input_threads(void)
2332 {
2333  int i;
2334 
2335  if (nb_input_files == 1)
2336  return;
2337 
2339 
2340  for (i = 0; i < nb_input_files; i++) {
2341  InputFile *f = input_files[i];
2342  AVPacket pkt;
2343 
2344  if (!f->fifo || f->joined)
2345  continue;
2346 
2348  while (av_fifo_size(f->fifo)) {
2349  av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL);
2350  av_packet_unref(&pkt);
2351  }
2354 
2355  pthread_join(f->thread, NULL);
2356  f->joined = 1;
2357 
2358  while (av_fifo_size(f->fifo)) {
2359  av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL);
2360  av_packet_unref(&pkt);
2361  }
2362  av_fifo_free(f->fifo);
2363  }
2364 }
2365 
2366 static int init_input_threads(void)
2367 {
2368  int i, ret;
2369 
2370  if (nb_input_files == 1)
2371  return 0;
2372 
2373  for (i = 0; i < nb_input_files; i++) {
2374  InputFile *f = input_files[i];
2375 
2376  if (!(f->fifo = av_fifo_alloc(8*sizeof(AVPacket))))
2377  return AVERROR(ENOMEM);
2378 
2381 
2382  if ((ret = pthread_create(&f->thread, NULL, input_thread, f)))
2383  return AVERROR(ret);
2384  }
2385  return 0;
2386 }
2387 
2389 {
2390  int ret = 0;
2391 
2393 
2394  if (av_fifo_size(f->fifo)) {
2395  av_fifo_generic_read(f->fifo, pkt, sizeof(*pkt), NULL);
2397  } else {
2398  if (f->finished)
2399  ret = AVERROR_EOF;
2400  else
2401  ret = AVERROR(EAGAIN);
2402  }
2403 
2405 
2406  return ret;
2407 }
2408 #endif
2409 
2411 {
2412  if (f->rate_emu) {
2413  int i;
2414  for (i = 0; i < f->nb_streams; i++) {
2415  InputStream *ist = input_streams[f->ist_index + i];
2416  int64_t pts = av_rescale(ist->last_dts, 1000000, AV_TIME_BASE);
2417  int64_t now = av_gettime_relative() - ist->start;
2418  if (pts > now)
2419  return AVERROR(EAGAIN);
2420  }
2421  }
2422 
2423 #if HAVE_PTHREADS
2424  if (nb_input_files > 1)
2425  return get_input_packet_mt(f, pkt);
2426 #endif
2427  return av_read_frame(f->ctx, pkt);
2428 }
2429 
2430 static int got_eagain(void)
2431 {
2432  int i;
2433  for (i = 0; i < nb_input_files; i++)
2434  if (input_files[i]->eagain)
2435  return 1;
2436  return 0;
2437 }
2438 
2439 static void reset_eagain(void)
2440 {
2441  int i;
2442  for (i = 0; i < nb_input_files; i++)
2443  input_files[i]->eagain = 0;
2444 }
2445 
2446 // set duration to max(tmp, duration) in a proper time base and return duration's time_base
2447 static AVRational duration_max(int64_t tmp, int64_t *duration, AVRational tmp_time_base,
2448  AVRational time_base)
2449 {
2450  int ret;
2451 
2452  if (!*duration) {
2453  *duration = tmp;
2454  return tmp_time_base;
2455  }
2456 
2457  ret = av_compare_ts(*duration, time_base, tmp, tmp_time_base);
2458  if (ret < 0) {
2459  *duration = tmp;
2460  return tmp_time_base;
2461  }
2462 
2463  return time_base;
2464 }
2465 
2466 static int seek_to_start(InputFile *ifile, AVFormatContext *is)
2467 {
2468  InputStream *ist;
2469  AVCodecContext *avctx;
2470  int i, ret, has_audio = 0;
2471  int64_t duration = 0;
2472 
2473  ret = av_seek_frame(is, -1, is->start_time, 0);
2474  if (ret < 0)
2475  return ret;
2476 
2477  for (i = 0; i < ifile->nb_streams; i++) {
2478  ist = input_streams[ifile->ist_index + i];
2479  avctx = ist->dec_ctx;
2480 
2481  // flush decoders
2482  if (ist->decoding_needed) {
2483  process_input_packet(ist, NULL, 1);
2484  avcodec_flush_buffers(avctx);
2485  }
2486 
2487  /* duration is the length of the last frame in a stream
2488  * when audio stream is present we don't care about
2489  * last video frame length because it's not defined exactly */
2490  if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->nb_samples)
2491  has_audio = 1;
2492  }
2493 
2494  for (i = 0; i < ifile->nb_streams; i++) {
2495  ist = input_streams[ifile->ist_index + i];
2496  avctx = ist->dec_ctx;
2497 
2498  if (has_audio) {
2499  if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->nb_samples) {
2500  AVRational sample_rate = {1, avctx->sample_rate};
2501 
2502  duration = av_rescale_q(ist->nb_samples, sample_rate, ist->st->time_base);
2503  } else
2504  continue;
2505  } else {
2506  if (ist->framerate.num) {
2507  duration = av_rescale_q(1, ist->framerate, ist->st->time_base);
2508  } else if (ist->st->avg_frame_rate.num) {
2509  duration = av_rescale_q(1, ist->st->avg_frame_rate, ist->st->time_base);
2510  } else duration = 1;
2511  }
2512  if (!ifile->duration)
2513  ifile->time_base = ist->st->time_base;
2514  /* the total duration of the stream, max_pts - min_pts is
2515  * the duration of the stream without the last frame */
2516  duration += ist->max_pts - ist->min_pts;
2517  ifile->time_base = duration_max(duration, &ifile->duration, ist->st->time_base,
2518  ifile->time_base);
2519  }
2520 
2521  if (ifile->loop > 0)
2522  ifile->loop--;
2523 
2524  return ret;
2525 }
2526 
2527 /*
2528  * Read one packet from an input file and send it for
2529  * - decoding -> lavfi (audio/video)
2530  * - decoding -> encoding -> muxing (subtitles)
2531  * - muxing (streamcopy)
2532  *
2533  * Return
2534  * - 0 -- one packet was read and processed
2535  * - AVERROR(EAGAIN) -- no packets were available for selected file,
2536  * this function should be called again
2537  * - AVERROR_EOF -- this function should not be called again
2538  */
2539 static int process_input(void)
2540 {
2541  InputFile *ifile;
2542  AVFormatContext *is;
2543  InputStream *ist;
2544  AVPacket pkt;
2545  int ret, i, j;
2546  int64_t duration;
2547 
2548  /* select the stream that we must read now */
2549  ifile = select_input_file();
2550  /* if none, if is finished */
2551  if (!ifile) {
2552  if (got_eagain()) {
2553  reset_eagain();
2554  av_usleep(10000);
2555  return AVERROR(EAGAIN);
2556  }
2557  av_log(NULL, AV_LOG_VERBOSE, "No more inputs to read from.\n");
2558  return AVERROR_EOF;
2559  }
2560 
2561  is = ifile->ctx;
2562  ret = get_input_packet(ifile, &pkt);
2563 
2564  if (ret == AVERROR(EAGAIN)) {
2565  ifile->eagain = 1;
2566  return ret;
2567  }
2568  if (ret < 0 && ifile->loop) {
2569  if ((ret = seek_to_start(ifile, is)) < 0)
2570  return ret;
2571  ret = get_input_packet(ifile, &pkt);
2572  }
2573  if (ret < 0) {
2574  if (ret != AVERROR_EOF) {
2575  print_error(is->filename, ret);
2576  if (exit_on_error)
2577  exit_program(1);
2578  }
2579  ifile->eof_reached = 1;
2580 
2581  for (i = 0; i < ifile->nb_streams; i++) {
2582  ist = input_streams[ifile->ist_index + i];
2583  if (ist->decoding_needed)
2584  process_input_packet(ist, NULL, 0);
2585 
2586  /* mark all outputs that don't go through lavfi as finished */
2587  for (j = 0; j < nb_output_streams; j++) {
2588  OutputStream *ost = output_streams[j];
2589 
2590  if (ost->source_index == ifile->ist_index + i &&
2591  (ost->stream_copy || ost->enc->type == AVMEDIA_TYPE_SUBTITLE))
2592  finish_output_stream(ost);
2593  }
2594  }
2595 
2596  return AVERROR(EAGAIN);
2597  }
2598 
2599  reset_eagain();
2600 
2601  if (do_pkt_dump) {
2603  is->streams[pkt.stream_index]);
2604  }
2605  /* the following test is needed in case new streams appear
2606  dynamically in stream : we ignore them */
2607  if (pkt.stream_index >= ifile->nb_streams)
2608  goto discard_packet;
2609 
2610  ist = input_streams[ifile->ist_index + pkt.stream_index];
2611 
2612  ist->data_size += pkt.size;
2613  ist->nb_packets++;
2614 
2615  if (ist->discard)
2616  goto discard_packet;
2617 
2618  /* add the stream-global side data to the first packet */
2619  if (ist->nb_packets == 1)
2620  for (i = 0; i < ist->st->nb_side_data; i++) {
2621  AVPacketSideData *src_sd = &ist->st->side_data[i];
2622  uint8_t *dst_data;
2623 
2624  if (av_packet_get_side_data(&pkt, src_sd->type, NULL))
2625  continue;
2626  if (ist->autorotate && src_sd->type == AV_PKT_DATA_DISPLAYMATRIX)
2627  continue;
2628 
2629  dst_data = av_packet_new_side_data(&pkt, src_sd->type, src_sd->size);
2630  if (!dst_data)
2631  exit_program(1);
2632 
2633  memcpy(dst_data, src_sd->data, src_sd->size);
2634  }
2635 
2636  if (pkt.dts != AV_NOPTS_VALUE)
2637  pkt.dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
2638  if (pkt.pts != AV_NOPTS_VALUE)
2639  pkt.pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
2640 
2641  if (pkt.pts != AV_NOPTS_VALUE)
2642  pkt.pts *= ist->ts_scale;
2643  if (pkt.dts != AV_NOPTS_VALUE)
2644  pkt.dts *= ist->ts_scale;
2645 
2646  if ((ist->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2648  pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE &&
2649  (is->iformat->flags & AVFMT_TS_DISCONT)) {
2650  int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
2651  int64_t delta = pkt_dts - ist->next_dts;
2652 
2653  if ((FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE || pkt_dts + 1 < ist->last_dts) && !copy_ts) {
2654  ifile->ts_offset -= delta;
2656  "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
2657  delta, ifile->ts_offset);
2658  pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
2659  if (pkt.pts != AV_NOPTS_VALUE)
2660  pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
2661  }
2662  }
2663  duration = av_rescale_q(ifile->duration, ifile->time_base, ist->st->time_base);
2664  if (pkt.pts != AV_NOPTS_VALUE) {
2665  pkt.pts += duration;
2666  ist->max_pts = FFMAX(pkt.pts, ist->max_pts);
2667  ist->min_pts = FFMIN(pkt.pts, ist->min_pts);
2668  }
2669 
2670  if (pkt.dts != AV_NOPTS_VALUE)
2671  pkt.dts += duration;
2672 
2673  process_input_packet(ist, &pkt, 0);
2674 
2675 discard_packet:
2676  av_packet_unref(&pkt);
2677 
2678  return 0;
2679 }
2680 
2681 /*
2682  * The following code is the main loop of the file converter
2683  */
2684 static int transcode(void)
2685 {
2686  int ret, i, need_input = 1;
2687  AVFormatContext *os;
2688  OutputStream *ost;
2689  InputStream *ist;
2690  int64_t timer_start;
2691 
2692  ret = transcode_init();
2693  if (ret < 0)
2694  goto fail;
2695 
2696  av_log(NULL, AV_LOG_INFO, "Press ctrl-c to stop encoding\n");
2697  term_init();
2698 
2699  timer_start = av_gettime_relative();
2700 
2701 #if HAVE_PTHREADS
2702  if ((ret = init_input_threads()) < 0)
2703  goto fail;
2704 #endif
2705 
2706  while (!received_sigterm) {
2707  /* check if there's any stream where output is still needed */
2708  if (!need_output()) {
2709  av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");
2710  break;
2711  }
2712 
2713  /* read and process one input packet if needed */
2714  if (need_input) {
2715  ret = process_input();
2716  if (ret == AVERROR_EOF)
2717  need_input = 0;
2718  }
2719 
2720  ret = poll_filters();
2721  if (ret < 0 && ret != AVERROR_EOF) {
2722  char errbuf[128];
2723  av_strerror(ret, errbuf, sizeof(errbuf));
2724 
2725  av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", errbuf);
2726  break;
2727  }
2728 
2729  /* dump report by using the output first video and audio streams */
2730  print_report(0, timer_start);
2731  }
2732 #if HAVE_PTHREADS
2734 #endif
2735 
2736  /* at the end of stream, we must flush the decoder buffers */
2737  for (i = 0; i < nb_input_streams; i++) {
2738  ist = input_streams[i];
2739  if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {
2740  process_input_packet(ist, NULL, 0);
2741  }
2742  }
2743  poll_filters();
2744  flush_encoders();
2745 
2746  term_exit();
2747 
2748  /* write the trailer if needed and close file */
2749  for (i = 0; i < nb_output_files; i++) {
2750  os = output_files[i]->ctx;
2751  if (!output_files[i]->header_written) {
2753  "Nothing was written into output file %d (%s), because "
2754  "at least one of its streams received no packets.\n",
2755  i, os->filename);
2756  continue;
2757  }
2758  av_write_trailer(os);
2759  }
2760 
2761  /* dump report by using the first video and audio streams */
2762  print_report(1, timer_start);
2763 
2764  /* close each encoder */
2765  for (i = 0; i < nb_output_streams; i++) {
2766  ost = output_streams[i];
2767  if (ost->encoding_needed) {
2768  av_freep(&ost->enc_ctx->stats_in);
2769  }
2770  }
2771 
2772  /* close each decoder */
2773  for (i = 0; i < nb_input_streams; i++) {
2774  ist = input_streams[i];
2775  if (ist->decoding_needed) {
2776  avcodec_close(ist->dec_ctx);
2777  if (ist->hwaccel_uninit)
2778  ist->hwaccel_uninit(ist->dec_ctx);
2779  }
2780  }
2781 
2783 
2784  /* finished ! */
2785  ret = 0;
2786 
2787  fail:
2788 #if HAVE_PTHREADS
2790 #endif
2791 
2792  if (output_streams) {
2793  for (i = 0; i < nb_output_streams; i++) {
2794  ost = output_streams[i];
2795  if (ost) {
2796  if (ost->logfile) {
2797  fclose(ost->logfile);
2798  ost->logfile = NULL;
2799  }
2800  av_free(ost->forced_kf_pts);
2801  av_dict_free(&ost->encoder_opts);
2802  av_dict_free(&ost->resample_opts);
2803  }
2804  }
2805  }
2806  return ret;
2807 }
2808 
2809 static int64_t getutime(void)
2810 {
2811 #if HAVE_GETRUSAGE
2812  struct rusage rusage;
2813 
2814  getrusage(RUSAGE_SELF, &rusage);
2815  return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2816 #elif HAVE_GETPROCESSTIMES
2817  HANDLE proc;
2818  FILETIME c, e, k, u;
2819  proc = GetCurrentProcess();
2820  GetProcessTimes(proc, &c, &e, &k, &u);
2821  return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
2822 #else
2823  return av_gettime_relative();
2824 #endif
2825 }
2826 
2827 static int64_t getmaxrss(void)
2828 {
2829 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
2830  struct rusage rusage;
2831  getrusage(RUSAGE_SELF, &rusage);
2832  return (int64_t)rusage.ru_maxrss * 1024;
2833 #elif HAVE_GETPROCESSMEMORYINFO
2834  HANDLE proc;
2835  PROCESS_MEMORY_COUNTERS memcounters;
2836  proc = GetCurrentProcess();
2837  memcounters.cb = sizeof(memcounters);
2838  GetProcessMemoryInfo(proc, &memcounters, sizeof(memcounters));
2839  return memcounters.PeakPagefileUsage;
2840 #else
2841  return 0;
2842 #endif
2843 }
2844 
2845 int main(int argc, char **argv)
2846 {
2847  int i, ret;
2848  int64_t ti;
2849 
2851 
2853  parse_loglevel(argc, argv, options);
2854 
2856 #if CONFIG_AVDEVICE
2858 #endif
2860  av_register_all();
2862 
2863  show_banner();
2864 
2865  /* parse options and open all input/output files */
2866  ret = avconv_parse_options(argc, argv);
2867  if (ret < 0)
2868  exit_program(1);
2869 
2870  if (nb_output_files <= 0 && nb_input_files == 0) {
2871  show_usage();
2872  av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
2873  exit_program(1);
2874  }
2875 
2876  /* file converter / grab */
2877  if (nb_output_files <= 0) {
2878  fprintf(stderr, "At least one output file must be specified\n");
2879  exit_program(1);
2880  }
2881 
2882  for (i = 0; i < nb_output_files; i++) {
2883  if (strcmp(output_files[i]->ctx->oformat->name, "rtp"))
2884  want_sdp = 0;
2885  }
2886 
2887  ti = getutime();
2888  if (transcode() < 0)
2889  exit_program(1);
2890  ti = getutime() - ti;
2891  if (do_benchmark) {
2892  int maxrss = getmaxrss() / 1024;
2893  printf("bench: utime=%0.3fs maxrss=%ikB\n", ti / 1000000.0, maxrss);
2894  }
2895 
2896  exit_program(0);
2897  return 0;
2898 }
int nb_bitstream_filters
Definition: avconv.h:362
static av_unused void pthread_cond_signal(pthread_cond_t *cond)
Definition: w32pthreads.h:354
#define AV_CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
Definition: avcodec.h:797
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
callback to negotiate the pixelFormat
Definition: avcodec.h:1671
uint64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
int nb_input_files
Definition: avconv.c:100
void av_bsf_free(AVBSFContext **ctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition: bsf.c:33
static int check_init_output_file(OutputFile *of, int file_index)
Definition: avconv.c:1730
static int need_output(void)
Definition: avconv.c:2253
int frame_number
Definition: avconv.h:348
AVRational framerate
Definition: avcodec.h:3063
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
static int transcode_init(void)
Definition: avconv.c:2112
enum HWAccelID active_hwaccel_id
Definition: avconv.h:296
static int check_output_constraints(InputStream *ist, OutputStream *ost)
Definition: avconv.c:1100
int64_t recording_time
Definition: avconv.h:433
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:297
AVCodecParameters * par_out
Parameters of the output stream.
Definition: avcodec.h:5021
int nb_input_streams
Definition: avconv.c:98
uint8_t * name
Definition: avconv.h:223
#define VSYNC_AUTO
Definition: avconv.h:46
int nb_outputs
Definition: avconv.h:251
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:106
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1768
static int got_eagain(void)
Definition: avconv.c:2430
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
int stream_copy
Definition: avconv.h:396
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:2010
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
Definition: mux.c:662
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1181
AVOption.
Definition: opt.h:234
AVRational frame_rate
Definition: avconv.h:374
int64_t * forced_kf_pts
Definition: avconv.h:381
uint64_t error[AV_NUM_DATA_POINTERS]
error
Definition: avcodec.h:2715
AVBufferRef * hw_frames_ctx
Definition: avconv.h:303
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:776
static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
Definition: w32pthreads.h:248
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:252
int64_t cfr_next_pts
Definition: avconv.h:275
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
Definition: avconv.c:1329
Main libavfilter public API header.
static void flush_encoders(void)
Definition: avconv.c:1035
#define AV_DICT_DONT_OVERWRITE
Don&#39;t overwrite existing entries.
Definition: dict.h:67
Memory buffer source API.
const char * desc
Definition: nvenc.c:101
static const uint8_t frame_size[4]
Definition: g723_1.h:219
void show_banner(void)
Print the program banner to stderr.
Definition: cmdutils.c:820
AVRational framerate
Definition: avconv.h:281
static void term_exit(void)
Definition: avconv.c:110
AVRational sample_aspect_ratio
Video only.
Definition: avcodec.h:3535
PtsCorrectionContext pts_ctx
Definition: avconv.h:278
static int nb_frames_drop
Definition: avconv.c:88
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Definition: utils.c:1911
int height
Definition: avconv.h:208
AVCodecParserContext * parser
Definition: avconv.h:408
int64_t max_pts
Definition: avconv.h:271
int decoding_needed
Definition: avconv.h:258
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:770
int num
numerator
Definition: rational.h:44
The bitstream filter state.
Definition: avcodec.h:4990
static void set_encoder_id(OutputFile *of, OutputStream *ost)
Definition: avconv.c:1886
int index
stream index in AVFormatContext
Definition: avformat.h:706
int size
Definition: avcodec.h:1347
int max_muxing_queue_size
Definition: avconv.h:423
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1804
int eagain
Definition: avconv.h:318
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
void(* hwaccel_uninit)(AVCodecContext *s)
Definition: avconv.h:298
int quality
Definition: avconv.h:421
AVFrame * filter_frame
Definition: avconv.h:262
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: avcodec.h:885
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:2564
void avdevice_register_all(void)
Initialize libavdevice and register all the input and output devices.
Definition: alldevices.c:41
FilterGraph ** filtergraphs
Definition: avconv.c:107
enum AVMediaType type
Definition: avcodec.h:3133
int copy_ts
Definition: avconv_opt.c:90
#define FF_ARRAY_ELEMS(a)
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:68
int print_stats
Definition: avconv_opt.c:93
discard all
Definition: avcodec.h:689
AVPacketSideData * side_data
An array of side data that applies to the whole stream (i.e.
Definition: avformat.h:808
const AVBitStreamFilter ** bitstream_filters
Definition: avconv.h:363
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2776
void avcodec_register_all(void)
Register all the codecs, parsers and bitstream filters which were enabled at configuration time...
Definition: allcodecs.c:61
const char * name
Definition: avconv.h:62
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, AVPacket *avpkt)
Decode a subtitle message.
Definition: utils.c:1673
static volatile int received_sigterm
Definition: avconv.c:115
uint64_t packets_written
Definition: avconv.h:415
AVCodec.
Definition: avcodec.h:3120
static void finish_output_stream(OutputStream *ost)
Definition: avconv.c:736
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:430
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3475
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:132
int64_t start_time
Definition: avconv.h:434
void init_pts_correction(PtsCorrectionContext *ctx)
Reset the state of the PtsCorrectionContext.
Definition: cmdutils.c:1431
int index
Definition: avconv.h:243
uint64_t data_size
Definition: avconv.h:413
AVBSFContext ** bsf_ctx
Definition: avconv.h:364
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
Definition: fifo.c:84
int64_t last_dts
Definition: avconv.h:269
int video_sync_method
Definition: avconv_opt.c:86
#define log2(x)
Definition: libm.h:111
int filtergraph_is_simple(FilterGraph *fg)
struct FilterGraph * graph
Definition: avconv.h:200
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1535
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
float dts_delta_threshold
Definition: avconv_opt.c:82
int encoding_needed
Definition: avconv.h:347
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame...
Definition: frame.h:365
Format I/O context.
Definition: avformat.h:940
uint64_t samples_decoded
Definition: avconv.h:312
memory buffer sink API
struct InputStream * ist
Definition: avconv.h:199
unsigned int nb_stream_indexes
Definition: avformat.h:915
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:79
#define AV_LOG_QUIET
Print no output.
Definition: log.h:106
enum HWAccelID id
Definition: avconv.h:64
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:270
uint64_t frames_decoded
Definition: avconv.h:311
int header_written
Definition: avconv.h:439
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVFilterGraph * graph
Definition: avconv.h:246
Public dictionary API.
char * logfile_prefix
Definition: avconv.h:386
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:194
int copy_initial_nonkeyframes
Definition: avconv.h:404
static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
Definition: avconv.c:1114
void register_exit(void(*cb)(int ret))
Register a program-specific cleanup routine.
Definition: cmdutils.c:89
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2160
int flags
can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS, AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS, AVFMT_ALLOW_FLUSH, AVFMT_TS_NONSTRICT, AVFMT_TS_NEGATIVE
Definition: avformat.h:469
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:68
int width
Video only.
Definition: avcodec.h:3525
static void do_video_out(OutputFile *of, OutputStream *ost, AVFrame *in_picture, int *frame_size)
Definition: avconv.c:525
int av_parser_change(AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
Definition: parser.c:182
static int want_sdp
Definition: avconv.c:90
float delta
AVOptions.
int subtitle_header_size
Definition: avcodec.h:3000
int flags
Can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_SHOW_IDS, AVFMT_GENERIC_INDEX, AVFMT_TS_DISCONT, AVFMT_NOBINSEARCH, AVFMT_NOGENSEARCH, AVFMT_NO_BYTE_SEEK.
Definition: avformat.h:543
enum AVCodecID av_codec_get_id(const struct AVCodecTag *const *tags, unsigned int tag)
Get the AVCodecID for the given codec tag tag.
miscellaneous OS support macros and functions.
static int transcoding_finished
Definition: avconv.c:94
InputFile ** input_files
Definition: avconv.c:99
FILE * logfile
Definition: avconv.h:387
AVDictionary * opts
Definition: avconv.h:431
#define VSYNC_PASSTHROUGH
Definition: avconv.h:47
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1364
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: avcodec.h:1254
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:199
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:211
int nb_side_data
The number of elements in the AVStream.side_data array.
Definition: avformat.h:812
int shortest
Definition: avconv.h:437
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
int64_t duration
Definition: movenc.c:63
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
Definition: utils.c:2885
void avfilter_register_all(void)
Initialize the filter system.
Definition: allfilters.c:39
static av_unused int pthread_create(pthread_t *thread, const void *unused_attr, void *(*start_routine)(void *), void *arg)
Definition: w32pthreads.h:84
const char * name
Definition: avcodec.h:5037
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:38
static void finish(void)
Definition: movenc.c:338
int nb_streams
Definition: avconv.h:327
pthread_t thread
Definition: avconv.h:333
uint8_t * data
Definition: avcodec.h:1346
static void term_init(void)
Definition: avconv.c:126
AVDictionary * resample_opts
Definition: avconv.h:394
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:401
static int flags
Definition: log.c:50
AVFilterContext * filter
Definition: avconv.h:220
int avformat_network_init(void)
Do global initialization of network components.
Definition: utils.c:3100
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
Definition: utils.c:50
int * formats
Definition: avconv.h:237
#define AVERROR_EOF
End of file.
Definition: error.h:51
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
Definition: fifo.c:38
int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
Generate an SDP for an RTP session.
Definition: sdp.c:805
uint8_t * data
Definition: avcodec.h:1290
void parse_loglevel(int argc, char **argv, const OptionDef *options)
Find the &#39;-loglevel&#39; option in the command line args and apply it.
Definition: cmdutils.c:429
external api for the swscale stuff
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
AVCodec * dec
Definition: avconv.h:260
enum AVDiscard discard
selects which program to discard and which to feed to the caller
Definition: avformat.h:913
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:2556
static FILE * vstats_file
Definition: avconv.c:86
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1068
int file_index
Definition: avconv.h:255
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2134
attribute_deprecated uint64_t error[AV_NUM_DATA_POINTERS]
Definition: frame.h:251
unsigned int * stream_index
Definition: avformat.h:914
static int decode_interrupt_cb(void *ctx)
Definition: avconv.c:135
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:959
int64_t next_dts
Definition: avconv.h:267
static int send_filter_eof(InputStream *ist)
Definition: avconv.c:1457
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
int configure_filtergraph(FilterGraph *fg)
const OptionDef options[]
Definition: avconv_opt.c:2447
void av_buffer_default_free(void *opaque, uint8_t *data)
Default free callback, which calls av_free() on the buffer data.
Definition: buffer.c:61
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:99
static void abort_codec_experimental(AVCodec *c, int encoder)
Definition: avconv.c:264
void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output)
Print detailed information about the input or output format, such as duration, bitrate, streams, container, programs, metadata, side data, codec and time base.
Definition: dump.c:453
static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
Definition: avconv.c:1612
Main libavdevice API header.
Callback for checking whether to abort blocking functions.
Definition: avio.h:51
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
Definition: utils.c:1943
static void do_audio_out(OutputFile *of, OutputStream *ost, AVFrame *frame)
Definition: avconv.c:418
enum AVCodecID id
Definition: avcodec.h:3134
int(* hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame)
Definition: avconv.h:300
static int init_output_stream(OutputStream *ost, char *error, int error_len)
Definition: avconv.c:2005
int rate_emu
Definition: avconv.h:329
static int64_t start_time
Definition: avplay.c:245
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2819
int width
width and height of the video frame
Definition: frame.h:179
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1715
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
int sample_rate
Definition: avconv.h:211
enum AVPixelFormat hwaccel_pix_fmt
Definition: avconv.h:301
void av_packet_rescale_ts(AVPacket *pkt, AVRational tb_src, AVRational tb_dst)
Convert valid timing fields (timestamps / durations) in a packet from one timebase to another...
Definition: avpacket.c:409
AVFilterContext * filter
Definition: avconv.h:198
static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame)
Definition: avconv.c:1184
#define AVFMT_TS_DISCONT
Format allows timestamp discontinuities.
Definition: avformat.h:422
int64_t start
Definition: avconv.h:264
int loop
Definition: avconv.h:320
uint64_t nb_packets
Definition: avconv.h:309
#define AVERROR(e)
Definition: error.h:43
int64_t last_mux_dts
Definition: avconv.h:358
int format
Definition: avconv.h:206
int av_opt_eval_flags(void *obj, const AVOption *o, const char *val, int *flags_out)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:80
static int get_input_packet_mt(InputFile *f, AVPacket *pkt)
Definition: avconv.c:2388
static int pthread_mutex_init(pthread_mutex_t *m, void *attr)
Definition: w32pthreads.h:104
static int pthread_mutex_unlock(pthread_mutex_t *m)
Definition: w32pthreads.h:119
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:983
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder.
Definition: utils.c:1807
int capabilities
Codec capabilities.
Definition: avcodec.h:3139
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
Definition: fifo.c:107
unsigned int nb_programs
Definition: avformat.h:1087
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:175
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1329
static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
Definition: avconv.c:1432
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1503
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:307
#define AV_LOG_SKIP_REPEATED
Skip repeated messages, this requires the user app to use av_log() instead of (f)printf as the 2 woul...
Definition: log.h:274
int finished
Definition: avconv.h:395
AVRational time_base_in
The timebase used for the timestamps of the input packets.
Definition: avcodec.h:5027
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:142
simple assert() macros that are a bit more flexible than ISO C assert().
static int init_output_stream_streamcopy(OutputStream *ost)
Definition: avconv.c:1821
enum AVPacketSideDataType type
Definition: avcodec.h:1292
int main(int argc, char **argv)
Definition: avconv.c:2845
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
int eof
Definition: avconv.h:216
#define VSYNC_VFR
Definition: avconv.h:49
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:781
New fields can be added to the end with minor version bumps.
Definition: avformat.h:910
#define FFMAX(a, b)
Definition: common.h:64
static int seek_to_start(InputFile *ifile, AVFormatContext *is)
Definition: avconv.c:2466
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
#define fail()
Definition: checkasm.h:80
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
Definition: avcodec.h:909
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:2462
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2203
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
Definition: mathematics.c:104
uint32_t end_display_time
Definition: avcodec.h:3462
int nb_output_streams
Definition: avconv.c:103
int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts, int64_t dts)
Attempt to guess proper monotonic timestamps for decoded video frames which might have incorrect time...
Definition: cmdutils.c:1437
int64_t pts
Same as packet pts, in AV_TIME_BASE.
Definition: avcodec.h:3465
OutputFilter * filter
Definition: avconv.h:389
static av_unused void pthread_join(pthread_t thread, void **value_ptr)
Definition: w32pthreads.h:94
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:294
const AVOption * av_opt_find(void *obj, const char *name, const char *unit, int opt_flags, int search_flags)
Look for an option in an object.
Definition: opt.c:756
common internal API header
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
Definition: mpegaudioenc.c:307
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:105
AVRational mux_timebase
Definition: avconv.h:360
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
int nb_filtergraphs
Definition: avconv.c:108
AVCodecContext * parser_avctx
Definition: avconv.h:409
int bit_rate
the average bitrate
Definition: avcodec.h:1473
audio channel layout utility functions
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:105
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
const AVIOInterruptCB int_cb
Definition: avconv.c:140
char filename[1024]
input or output filename
Definition: avformat.h:1016
const char * media_type_string(enum AVMediaType media_type)
Get a string describing a media type.
Definition: cmdutils.c:1692
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:86
static void do_subtitle_out(OutputFile *of, OutputStream *ost, InputStream *ist, AVSubtitle *sub, int64_t pts)
Definition: avconv.c:456
static InputStream * get_input_stream(OutputStream *ost)
Definition: avconv.c:1712
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:241
external API header
#define FFMIN(a, b)
Definition: common.h:66
AVPacketSideData * coded_side_data
Additional data associated with the entire coded stream.
Definition: avcodec.h:3078
uint64_t * channel_layouts
Definition: avconv.h:238
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:136
static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: w32pthreads.h:322
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() and children.
Definition: dict.h:64
struct OutputStream * ost
Definition: avconv.h:221
int width
picture width / height.
Definition: avcodec.h:1580
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
AVFifoBuffer * fifo
Definition: avconv.h:338
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
Definition: avcodec.h:3102
int64_t nb_samples
Definition: avconv.h:277
AVRational time_base_out
The timebase used for the timestamps of the output packets.
Definition: avcodec.h:5033
static void do_video_stats(OutputStream *ost, int frame_size)
Definition: avconv.c:622
int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx, AVFrame *frame, int nb_samples)
Same as av_buffersink_get_frame(), but with the ability to specify the number of samples read...
Definition: buffersink.c:102
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:173
int64_t duration
Definition: avconv.h:321
const char * name
Definition: avformat.h:450
int width
Definition: avconv.h:208
AVFormatContext * ctx
Definition: movenc.c:48
void av_parser_close(AVCodecParserContext *s)
Definition: parser.c:217
#define AV_CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
Definition: avcodec.h:767
static av_always_inline av_const long int lrintf(float x)
Definition: libm.h:144
#define FFABS(a)
Definition: common.h:61
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:239
static int get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
Definition: avconv.c:1657
int64_t max_frames
Definition: avconv.h:368
static volatile int received_nb_signals
Definition: avconv.c:116
AVDictionary * metadata
Definition: avformat.h:772
static void process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
Definition: avconv.c:1469
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1544
enum AVPixelFormat pix_fmt
Definition: movenc.c:853
#define VSYNC_CFR
Definition: avconv.h:48
int do_benchmark
Definition: avconv_opt.c:87
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:198
int finished
Definition: avconv.h:334
AVBufferRef * hw_device_ctx
Definition: avconv_opt.c:77
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:289
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it...
Definition: error.h:62
void exit_program(int ret)
Wraps exit with a program-specific cleanup routine.
Definition: cmdutils.c:94
static void print_sdp(void)
Definition: avconv.c:1580
Stream structure.
Definition: avformat.h:705
void avcodec_flush_buffers(AVCodecContext *avctx)
Reset the internal decoder state / flush internal buffers.
Definition: utils.c:2285
int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:420
int audio_sync_method
Definition: avconv_opt.c:85
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:191
InputFilter ** filters
Definition: avconv.h:287
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: utils.c:1767
int64_t recording_time
Definition: avconv.h:326
int avformat_network_deinit(void)
Undo the initialization done by avformat_network_init.
Definition: utils.c:3112
Definition: avconv.h:61
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2172
int(* init)(AVCodecContext *s)
Definition: avconv.h:63
static void parse_forced_key_frames(char *kf, OutputStream *ost, AVCodecContext *avctx)
Definition: avconv.c:1914
NULL
Definition: eval.c:55
static int pthread_mutex_lock(pthread_mutex_t *m)
Definition: w32pthreads.h:114
AVStream * st
Definition: avconv.h:256
int av_fifo_space(AVFifoBuffer *f)
Return the amount of space in bytes in the AVFifoBuffer, that is the amount of data you can write int...
Definition: fifo.c:57
static int64_t getmaxrss(void)
Definition: avconv.c:2827
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
pthread_mutex_t fifo_lock
Definition: avconv.h:336
static int ifilter_send_eof(InputFilter *ifilter)
Definition: avconv.c:1256
AVCodecParserContext * av_parser_init(int codec_id)
Definition: parser.c:48
int ost_index
Definition: avconv.h:432
struct InputStream * sync_ist
Definition: avconv.h:352
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition: utils.c:514
OutputFile ** output_files
Definition: avconv.c:104
int exit_on_error
Definition: avconv_opt.c:92
enum AVMediaType codec_type
Definition: avcodec.h:1417
double ts_scale
Definition: avconv.h:279
static int init_input_threads(void)
Definition: avconv.c:2366
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:151
enum AVCodecID codec_id
Definition: avcodec.h:1426
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:247
int sample_rate
samples per second
Definition: avcodec.h:2152
static void avconv_cleanup(int ret)
Definition: avconv.c:142
uint64_t frames_encoded
Definition: avconv.h:417
AVIOContext * pb
I/O context.
Definition: avformat.h:982
OutputStream ** output_streams
Definition: avconv.c:102
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
Definition: fifo.c:62
AVFifoBuffer * muxing_queue
Definition: avconv.h:426
static int init_output_bsfs(OutputStream *ost)
Definition: avconv.c:1776
int ist_index
Definition: avconv.h:319
const struct AVCodecTag *const * codec_tag
List of supported codec_id-codec_tag pairs, ordered by "better choice first".
Definition: avformat.h:475
const char * graph_desc
Definition: avconv.h:244
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:80
int64_t start_time
Definition: avconv.h:325
main external API structure.
Definition: avcodec.h:1409
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: avconv.c:278
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:2029
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:347
static int check_recording_time(OutputStream *ost)
Definition: avconv.c:405
uint8_t * data
The data buffer.
Definition: buffer.h:89
static double psnr(double d)
Definition: avconv.c:616
int * sample_rates
Definition: avconv.h:239
void avsubtitle_free(AVSubtitle *sub)
Free all allocated data in the given subtitle struct.
Definition: utils.c:1687
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:206
const int program_birth_year
program birth year, defined by the program for show_banner()
Definition: avconv.c:84
const char * attachment_filename
Definition: avconv.h:403
a very simple circular buffer FIFO implementation
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
Definition: utils.c:1881
AVRational time_base
Definition: avconv.h:323
AVCodecContext * enc_ctx
Definition: avconv.h:366
AVFrame * decoded_frame
Definition: avconv.h:261
int extradata_size
Definition: avcodec.h:1524
AVBufferRef * hw_frames_ctx
Definition: avconv.h:214
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:68
Replacements for frequently missing libm functions.
int avconv_parse_options(int argc, char **argv)
Definition: avconv_opt.c:2393
int nb_coded_side_data
Definition: avcodec.h:3079
int sample_rate
Sample rate of the audio data.
Definition: frame.h:289
int(* get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags)
This callback is called at the beginning of each frame to get data buffer(s) for it.
Definition: avcodec.h:2304
rational number numerator/denominator
Definition: rational.h:43
const char program_name[]
program name, defined by the program for show_version().
Definition: avconv.c:83
int file_index
Definition: avconv.h:343
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: utils.c:2837
int64_t sync_opts
Definition: avconv.h:353
int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
Parse a string specifying a time and return its corresponding value as a number of microseconds...
Definition: cmdutils.c:123
AVCodecContext * dec_ctx
Definition: avconv.h:259
static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
Definition: avconv.c:1380
static int64_t getutime(void)
Definition: avconv.c:2809
AVMediaType
Definition: avutil.h:192
discard useless packets like 0 size packets in avi
Definition: avcodec.h:685
AVDictionary * decoder_opts
Definition: avconv.h:280
static int poll_filter(OutputStream *ost)
Definition: avconv.c:673
int autorotate
Definition: avconv.h:283
const char * name
Name of the codec described by this descriptor.
Definition: avcodec.h:588
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:850
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:2594
#define u(width,...)
int64_t ts_offset
Definition: avconv.h:324
misc parsing utilities
int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
Add a frame to the buffer source.
Definition: buffersrc.c:152
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1021
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:91
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:257
This struct describes the properties of a single codec described by an AVCodecID. ...
Definition: avcodec.h:580
AVFrame * filtered_frame
Definition: avconv.h:369
int source_index
Definition: avconv.h:345
This side data contains an integer value representing the quality factor of the compressed frame...
Definition: avcodec.h:1273
int(* hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags)
Definition: avconv.h:299
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:302
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1489
static int64_t pts
Global timestamp for the audio frames.
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:57
static int init_output_stream_encode(OutputStream *ost)
Definition: avconv.c:1945
int nb_filters
Definition: avconv.h:288
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:122
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
Definition: avformat.h:1025
int qp_hist
Definition: avconv_opt.c:94
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Seek to the keyframe at timestamp.
Definition: utils.c:1564
static int process_input(void)
Definition: avconv.c:2539
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
Definition: error.c:23
int forced_kf_count
Definition: avconv.h:382
char * forced_keyframes
Definition: avconv.h:384
uint64_t data_size
Definition: avconv.h:307
static int loop
Definition: avplay.c:260
static int poll_filters(void)
Definition: avconv.c:757
static const HWAccel * get_hwaccel(enum AVPixelFormat pix_fmt)
Definition: avconv.c:1603
static int get_input_packet(InputFile *f, AVPacket *pkt)
Definition: avconv.c:2410
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:225
int guess_input_channel_layout(InputStream *ist)
Definition: avconv.c:1311
Main libavformat public API header.
void print_error(const char *filename, int err)
Print an error message to stderr, indicating filename and a human readable description of the error c...
Definition: cmdutils.c:765
int av_fifo_size(AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
Definition: fifo.c:52
struct FilterGraph * graph
Definition: avconv.h:222
uint64_t limit_filesize
Definition: avconv.h:435
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:412
static void reset_eagain(void)
Definition: avconv.c:2439
InputStream ** input_streams
Definition: avconv.c:97
int do_pkt_dump
Definition: avconv_opt.c:89
AVStream * st
Definition: avconv.h:346
const HWAccel hwaccels[]
Definition: avconv_opt.c:58
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:761
uint32_t start_display_time
Definition: avcodec.h:3461
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2797
uint64_t samples_encoded
Definition: avconv.h:418
static InputFile * select_input_file(void)
Definition: avconv.c:2278
char * vstats_filename
Definition: avconv_opt.c:79
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:161
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:31
int thread_safe_callbacks
Set by the client if its custom get_buffer() callback can be called synchronously from another thread...
Definition: avcodec.h:2835
char * key
Definition: dict.h:73
int den
denominator
Definition: rational.h:45
void uninit_opts(void)
Uninitialize the cmdutils option system, in particular free the *_opts contexts and their contents...
Definition: cmdutils.c:71
uint64_t channel_layout
Definition: avconv.h:212
void show_usage(void)
Definition: avconv_opt.c:2343
struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:952
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:2626
AVFormatContext * ctx
Definition: avconv.h:316
AVCodec * enc
Definition: avconv.h:367
int eof_reached
Definition: avconv.h:317
int forced_kf_index
Definition: avconv.h:383
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:423
char * avfilter
Definition: avconv.h:390
uint8_t * name
Definition: avconv.h:201
char * value
Definition: dict.h:74
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:25
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:268
static void free_input_threads(void)
Definition: avconv.c:2331
int channels
number of audio channels
Definition: avcodec.h:2153
int top_field_first
Definition: avconv.h:376
static uint8_t tmp[8]
Definition: des.c:38
OutputFilter ** outputs
Definition: avconv.h:250
struct AVCodecParserContext * parser
Definition: avformat.h:880
void av_log_set_flags(int arg)
Definition: log.c:210
static int transcode(void)
Definition: avconv.c:2684
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:214
AVFormatContext * ctx
Definition: avconv.h:430
pthread_cond_t fifo_cond
Definition: avconv.h:337
int nb_output_files
Definition: avconv.c:105
#define LIBAVCODEC_IDENT
Definition: version.h:42
char * hwaccel_device
Definition: avconv.h:292
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
AVDictionary * encoder_opts
Definition: avconv.h:393
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:205
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:714
int repeat_pict
This field is used for proper frame duration computation in lavf.
Definition: avcodec.h:4380
int do_hex_dump
Definition: avconv_opt.c:88
int height
Definition: frame.h:179
static AVRational duration_max(int64_t tmp, int64_t *duration, AVRational tmp_time_base, AVRational time_base)
Definition: avconv.c:2447
static void print_final_stats(int64_t total_size)
Definition: avconv.c:805
InputFilter ** inputs
Definition: avconv.h:248
float frame_aspect_ratio
Definition: avconv.h:378
enum AVPixelFormat hwaccel_retrieved_pix_fmt
Definition: avconv.h:302
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:60
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:284
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:118
AVCodecParameters * codecpar
Definition: avformat.h:831
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3487
int64_t min_pts
Definition: avconv.h:270
static void sigterm_handler(int sig)
Definition: avconv.c:119
int initialized
Definition: avconv.h:401
int discard
Definition: avconv.h:257
static int init_input_stream(int ist_index, char *error, int error_len)
Definition: avconv.c:1667
void av_pkt_dump_log2(void *avcl, int level, AVPacket *pkt, int dump_payload, AVStream *st)
Send a nice dump of a packet to the log.
Definition: dump.c:108
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:263
int stream_index
Definition: avcodec.h:1348
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:742
enum HWAccelID hwaccel_id
Definition: avconv.h:291
static void output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: avconv.c:362
int64_t first_pts
Definition: avconv.h:356
unsigned int av_codec_get_tag(const struct AVCodecTag *const *tags, enum AVCodecID id)
Get the codec tag for the given codec id id.
int nb_inputs
Definition: avconv.h:249
int index
Definition: avconv.h:344
void assert_avoptions(AVDictionary *m)
Definition: avconv.c:255
int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
Get a frame with filtered data from sink and put it in frame.
Definition: buffersink.c:62
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:44
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5016
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub)
Definition: utils.c:1434
AVFifoBuffer * frame_queue
Definition: avconv.h:203
static void print_report(int is_last_report, int64_t timer_start)
Definition: avconv.c:908
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:184
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:1466
static void * input_thread(void *arg)
Definition: avconv.c:2302
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235
AVProgram ** programs
Definition: avformat.h:1088
int joined
Definition: avconv.h:335
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:2999