3 * Copyright (c) 2000-2011 The Libav developers
5 * This file is part of Libav.
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.
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.
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
32 #include "libavformat/avformat.h"
33 #include "libavdevice/avdevice.h"
34 #include "libswscale/swscale.h"
35 #include "libavresample/avresample.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/channel_layout.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"
52 # include "libavfilter/avfilter.h"
53 # include "libavfilter/buffersrc.h"
54 # include "libavfilter/buffersink.h"
56 #if HAVE_SYS_RESOURCE_H
58 #include <sys/types.h>
59 #include <sys/resource.h>
60 #elif HAVE_GETPROCESSTIMES
63 #if HAVE_GETPROCESSMEMORYINFO
69 #include <sys/select.h>
81 #include "libavutil/avassert.h"
83 const char program_name
[] = "avconv";
84 const int program_birth_year
= 2000;
86 static FILE *vstats_file
;
88 static int nb_frames_drop
= 0;
90 static int want_sdp
= 1;
93 /* signal to input threads that they should exit; set by the main thread */
94 static int transcoding_finished
;
97 InputStream
**input_streams
= NULL
;
98 int nb_input_streams
= 0;
99 InputFile
**input_files
= NULL
;
100 int nb_input_files
= 0;
102 OutputStream
**output_streams
= NULL
;
103 int nb_output_streams
= 0;
104 OutputFile
**output_files
= NULL
;
105 int nb_output_files
= 0;
107 FilterGraph
**filtergraphs
;
110 static void term_exit(void)
112 av_log(NULL
, AV_LOG_QUIET
, "");
115 static volatile int received_sigterm
= 0;
116 static volatile int received_nb_signals
= 0;
119 sigterm_handler(int sig
)
121 received_sigterm
= sig
;
122 received_nb_signals
++;
126 static void term_init(void)
128 signal(SIGINT
, sigterm_handler
); /* Interrupt (ANSI). */
129 signal(SIGTERM
, sigterm_handler
); /* Termination (ANSI). */
131 signal(SIGXCPU
, sigterm_handler
);
135 static int decode_interrupt_cb(void *ctx
)
137 return received_nb_signals
> 1;
140 const AVIOInterruptCB int_cb
= { decode_interrupt_cb
, NULL
};
142 static void avconv_cleanup(int ret
)
146 for (i
= 0; i
< nb_filtergraphs
; i
++) {
147 FilterGraph
*fg
= filtergraphs
[i
];
148 avfilter_graph_free(&fg
->graph
);
149 for (j
= 0; j
< fg
->nb_inputs
; j
++) {
150 av_buffer_unref(&fg
->inputs
[j
]->hw_frames_ctx
);
151 av_freep(&fg
->inputs
[j
]->name
);
152 av_freep(&fg
->inputs
[j
]);
154 av_freep(&fg
->inputs
);
155 for (j
= 0; j
< fg
->nb_outputs
; j
++) {
156 av_freep(&fg
->outputs
[j
]->name
);
157 av_freep(&fg
->outputs
[j
]);
159 av_freep(&fg
->outputs
);
160 av_freep(&fg
->graph_desc
);
162 av_freep(&filtergraphs
[i
]);
164 av_freep(&filtergraphs
);
167 for (i
= 0; i
< nb_output_files
; i
++) {
168 OutputFile
*of
= output_files
[i
];
169 AVFormatContext
*s
= of
->ctx
;
170 if (s
&& s
->oformat
&& !(s
->oformat
->flags
& AVFMT_NOFILE
) && s
->pb
)
172 avformat_free_context(s
);
173 av_dict_free(&of
->opts
);
175 av_freep(&output_files
[i
]);
177 for (i
= 0; i
< nb_output_streams
; i
++) {
178 OutputStream
*ost
= output_streams
[i
];
180 for (j
= 0; j
< ost
->nb_bitstream_filters
; j
++)
181 av_bsf_free(&ost
->bsf_ctx
[j
]);
182 av_freep(&ost
->bsf_ctx
);
183 av_freep(&ost
->bitstream_filters
);
185 av_frame_free(&ost
->filtered_frame
);
187 av_parser_close(ost
->parser
);
188 avcodec_free_context(&ost
->parser_avctx
);
190 av_freep(&ost
->forced_keyframes
);
191 av_freep(&ost
->avfilter
);
192 av_freep(&ost
->logfile_prefix
);
194 avcodec_free_context(&ost
->enc_ctx
);
196 while (av_fifo_size(ost
->muxing_queue
)) {
198 av_fifo_generic_read(ost
->muxing_queue
, &pkt
, sizeof(pkt
), NULL
);
199 av_packet_unref(&pkt
);
201 av_fifo_free(ost
->muxing_queue
);
203 av_freep(&output_streams
[i
]);
205 for (i
= 0; i
< nb_input_files
; i
++) {
206 avformat_close_input(&input_files
[i
]->ctx
);
207 av_freep(&input_files
[i
]);
209 for (i
= 0; i
< nb_input_streams
; i
++) {
210 InputStream
*ist
= input_streams
[i
];
212 av_frame_free(&ist
->decoded_frame
);
213 av_frame_free(&ist
->filter_frame
);
214 av_dict_free(&ist
->decoder_opts
);
215 av_freep(&ist
->filters
);
216 av_freep(&ist
->hwaccel_device
);
218 avcodec_free_context(&ist
->dec_ctx
);
220 av_freep(&input_streams
[i
]);
225 av_free(vstats_filename
);
227 av_freep(&input_streams
);
228 av_freep(&input_files
);
229 av_freep(&output_streams
);
230 av_freep(&output_files
);
234 avformat_network_deinit();
236 if (received_sigterm
) {
237 av_log(NULL
, AV_LOG_INFO
, "Received signal %d: terminating.\n",
238 (int) received_sigterm
);
243 void assert_avoptions(AVDictionary
*m
)
245 AVDictionaryEntry
*t
;
246 if ((t
= av_dict_get(m
, "", NULL
, AV_DICT_IGNORE_SUFFIX
))) {
247 av_log(NULL
, AV_LOG_FATAL
, "Option %s not found.\n", t
->key
);
252 static void abort_codec_experimental(AVCodec
*c
, int encoder
)
254 const char *codec_string
= encoder ?
"encoder" : "decoder";
256 av_log(NULL
, AV_LOG_FATAL
, "%s '%s' is experimental and might produce bad "
257 "results.\nAdd '-strict experimental' if you want to use it.\n",
258 codec_string
, c
->name
);
259 codec
= encoder ?
avcodec_find_encoder(c
->id
) : avcodec_find_decoder(c
->id
);
260 if (!(codec
->capabilities
& AV_CODEC_CAP_EXPERIMENTAL
))
261 av_log(NULL
, AV_LOG_FATAL
, "Or use the non experimental %s '%s'.\n",
262 codec_string
, codec
->name
);
266 static void write_packet(OutputFile
*of
, AVPacket
*pkt
, OutputStream
*ost
)
268 AVFormatContext
*s
= of
->ctx
;
269 AVStream
*st
= ost
->st
;
272 if (!of
->header_written
) {
274 /* the muxer is not initialized yet, buffer the packet */
275 if (!av_fifo_space(ost
->muxing_queue
)) {
276 int new_size
= FFMIN(2 * av_fifo_size(ost
->muxing_queue
),
277 ost
->max_muxing_queue_size
);
278 if (new_size
<= av_fifo_size(ost
->muxing_queue
)) {
279 av_log(NULL
, AV_LOG_ERROR
,
280 "Too many packets buffered for output stream %d:%d.\n",
281 ost
->file_index
, ost
->st
->index
);
284 ret
= av_fifo_realloc2(ost
->muxing_queue
, new_size
);
288 av_packet_move_ref(&tmp_pkt
, pkt
);
289 av_fifo_generic_write(ost
->muxing_queue
, &tmp_pkt
, sizeof(tmp_pkt
), NULL
);
294 * Audio encoders may split the packets -- #frames in != #packets out.
295 * But there is no reordering, so we can limit the number of output packets
296 * by simply dropping them here.
297 * Counting encoded video frames needs to be done separately because of
298 * reordering, see do_video_out()
300 if (!(st
->codecpar
->codec_type
== AVMEDIA_TYPE_VIDEO
&& ost
->encoding_needed
)) {
301 if (ost
->frame_number
>= ost
->max_frames
) {
302 av_packet_unref(pkt
);
307 if (st
->codecpar
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
308 uint8_t *sd
= av_packet_get_side_data(pkt
, AV_PKT_DATA_QUALITY_FACTOR
,
310 ost
->quality
= sd ?
*(int *)sd
: -1;
312 if (ost
->frame_rate
.num
) {
313 pkt
->duration
= av_rescale_q(1, av_inv_q(ost
->frame_rate
),
318 if (!(s
->oformat
->flags
& AVFMT_NOTIMESTAMPS
) &&
319 ost
->last_mux_dts
!= AV_NOPTS_VALUE
&&
320 pkt
->dts
< ost
->last_mux_dts
+ !(s
->oformat
->flags
& AVFMT_TS_NONSTRICT
)) {
321 av_log(NULL
, AV_LOG_WARNING
, "Non-monotonous DTS in output stream "
322 "%d:%d; previous: %"PRId64
", current: %"PRId64
"; ",
323 ost
->file_index
, ost
->st
->index
, ost
->last_mux_dts
, pkt
->dts
);
325 av_log(NULL
, AV_LOG_FATAL
, "aborting.\n");
328 av_log(NULL
, AV_LOG_WARNING
, "changing to %"PRId64
". This may result "
329 "in incorrect timestamps in the output file.\n",
330 ost
->last_mux_dts
+ 1);
331 pkt
->dts
= ost
->last_mux_dts
+ 1;
332 if (pkt
->pts
!= AV_NOPTS_VALUE
)
333 pkt
->pts
= FFMAX(pkt
->pts
, pkt
->dts
);
335 ost
->last_mux_dts
= pkt
->dts
;
337 ost
->data_size
+= pkt
->size
;
338 ost
->packets_written
++;
340 pkt
->stream_index
= ost
->index
;
341 ret
= av_interleaved_write_frame(s
, pkt
);
343 print_error("av_interleaved_write_frame()", ret
);
348 static void output_packet(OutputFile
*of
, AVPacket
*pkt
, OutputStream
*ost
)
352 /* apply the output bitstream filters, if any */
353 if (ost
->nb_bitstream_filters
) {
356 ret
= av_bsf_send_packet(ost
->bsf_ctx
[0], pkt
);
362 /* get a packet from the previous filter up the chain */
363 ret
= av_bsf_receive_packet(ost
->bsf_ctx
[idx
- 1], pkt
);
364 if (ret
== AVERROR(EAGAIN
)) {
371 /* send it to the next filter down the chain or to the muxer */
372 if (idx
< ost
->nb_bitstream_filters
) {
373 ret
= av_bsf_send_packet(ost
->bsf_ctx
[idx
], pkt
);
378 write_packet(of
, pkt
, ost
);
381 write_packet(of
, pkt
, ost
);
384 if (ret
< 0 && ret
!= AVERROR_EOF
) {
385 av_log(NULL
, AV_LOG_FATAL
, "Error applying bitstream filters to an output "
386 "packet for stream #%d:%d.\n", ost
->file_index
, ost
->index
);
391 static int check_recording_time(OutputStream
*ost
)
393 OutputFile
*of
= output_files
[ost
->file_index
];
395 if (of
->recording_time
!= INT64_MAX
&&
396 av_compare_ts(ost
->sync_opts
- ost
->first_pts
, ost
->enc_ctx
->time_base
, of
->recording_time
,
397 AV_TIME_BASE_Q
) >= 0) {
404 static void do_audio_out(OutputFile
*of
, OutputStream
*ost
,
407 AVCodecContext
*enc
= ost
->enc_ctx
;
411 av_init_packet(&pkt
);
415 if (frame
->pts
== AV_NOPTS_VALUE
|| audio_sync_method
< 0)
416 frame
->pts
= ost
->sync_opts
;
417 ost
->sync_opts
= frame
->pts
+ frame
->nb_samples
;
419 ost
->samples_encoded
+= frame
->nb_samples
;
420 ost
->frames_encoded
++;
422 ret
= avcodec_send_frame(enc
, frame
);
427 ret
= avcodec_receive_packet(enc
, &pkt
);
428 if (ret
== AVERROR(EAGAIN
))
433 av_packet_rescale_ts(&pkt
, enc
->time_base
, ost
->st
->time_base
);
434 output_packet(of
, &pkt
, ost
);
439 av_log(NULL
, AV_LOG_FATAL
, "Audio encoding failed\n");
443 static void do_subtitle_out(OutputFile
*of
,
449 static uint8_t *subtitle_out
= NULL
;
450 int subtitle_out_max_size
= 1024 * 1024;
451 int subtitle_out_size
, nb
, i
;
455 if (pts
== AV_NOPTS_VALUE
) {
456 av_log(NULL
, AV_LOG_ERROR
, "Subtitle packets must have a pts\n");
465 subtitle_out
= av_malloc(subtitle_out_max_size
);
468 /* Note: DVB subtitle need one packet to draw them and one other
469 packet to clear them */
470 /* XXX: signal it in the codec context ? */
471 if (enc
->codec_id
== AV_CODEC_ID_DVB_SUBTITLE
)
476 for (i
= 0; i
< nb
; i
++) {
477 ost
->sync_opts
= av_rescale_q(pts
, ist
->st
->time_base
, enc
->time_base
);
478 if (!check_recording_time(ost
))
481 sub
->pts
= av_rescale_q(pts
, ist
->st
->time_base
, AV_TIME_BASE_Q
);
482 // start_display_time is required to be 0
483 sub
->pts
+= av_rescale_q(sub
->start_display_time
, (AVRational
){ 1, 1000 }, AV_TIME_BASE_Q
);
484 sub
->end_display_time
-= sub
->start_display_time
;
485 sub
->start_display_time
= 0;
487 ost
->frames_encoded
++;
489 subtitle_out_size
= avcodec_encode_subtitle(enc
, subtitle_out
,
490 subtitle_out_max_size
, sub
);
491 if (subtitle_out_size
< 0) {
492 av_log(NULL
, AV_LOG_FATAL
, "Subtitle encoding failed\n");
496 av_init_packet(&pkt
);
497 pkt
.data
= subtitle_out
;
498 pkt
.size
= subtitle_out_size
;
499 pkt
.pts
= av_rescale_q(sub
->pts
, AV_TIME_BASE_Q
, ost
->st
->time_base
);
500 if (enc
->codec_id
== AV_CODEC_ID_DVB_SUBTITLE
) {
501 /* XXX: the pts correction is handled here. Maybe handling
502 it in the codec would be better */
504 pkt
.pts
+= 90 * sub
->start_display_time
;
506 pkt
.pts
+= 90 * sub
->end_display_time
;
508 output_packet(of
, &pkt
, ost
);
512 static void do_video_out(OutputFile
*of
,
517 int ret
, format_video_sync
;
519 AVCodecContext
*enc
= ost
->enc_ctx
;
523 format_video_sync
= video_sync_method
;
524 if (format_video_sync
== VSYNC_AUTO
)
525 format_video_sync
= (of
->ctx
->oformat
->flags
& AVFMT_NOTIMESTAMPS
) ? VSYNC_PASSTHROUGH
:
526 (of
->ctx
->oformat
->flags
& AVFMT_VARIABLE_FPS
) ? VSYNC_VFR
: VSYNC_CFR
;
527 if (format_video_sync
!= VSYNC_PASSTHROUGH
&&
529 in_picture
->pts
!= AV_NOPTS_VALUE
&&
530 in_picture
->pts
< ost
->sync_opts
) {
532 av_log(NULL
, AV_LOG_WARNING
,
533 "*** dropping frame %d from stream %d at ts %"PRId64
"\n",
534 ost
->frame_number
, ost
->st
->index
, in_picture
->pts
);
538 if (in_picture
->pts
== AV_NOPTS_VALUE
)
539 in_picture
->pts
= ost
->sync_opts
;
540 ost
->sync_opts
= in_picture
->pts
;
543 if (!ost
->frame_number
)
544 ost
->first_pts
= in_picture
->pts
;
546 av_init_packet(&pkt
);
550 if (ost
->frame_number
>= ost
->max_frames
)
553 if (enc
->flags
& (AV_CODEC_FLAG_INTERLACED_DCT
| AV_CODEC_FLAG_INTERLACED_ME
) &&
554 ost
->top_field_first
>= 0)
555 in_picture
->top_field_first
= !!ost
->top_field_first
;
557 in_picture
->quality
= enc
->global_quality
;
558 in_picture
->pict_type
= 0;
559 if (ost
->forced_kf_index
< ost
->forced_kf_count
&&
560 in_picture
->pts
>= ost
->forced_kf_pts
[ost
->forced_kf_index
]) {
561 in_picture
->pict_type
= AV_PICTURE_TYPE_I
;
562 ost
->forced_kf_index
++;
565 ost
->frames_encoded
++;
567 ret
= avcodec_send_frame(enc
, in_picture
);
572 * For video, there may be reordering, so we can't throw away frames on
573 * encoder flush, we need to limit them here, before they go into encoder.
578 ret
= avcodec_receive_packet(enc
, &pkt
);
579 if (ret
== AVERROR(EAGAIN
))
584 av_packet_rescale_ts(&pkt
, enc
->time_base
, ost
->st
->time_base
);
585 output_packet(of
, &pkt
, ost
);
586 *frame_size
= pkt
.size
;
588 /* if two pass, output log */
589 if (ost
->logfile
&& enc
->stats_out
) {
590 fprintf(ost
->logfile
, "%s", enc
->stats_out
);
598 av_assert0(ret
!= AVERROR(EAGAIN
) && ret
!= AVERROR_EOF
);
599 av_log(NULL
, AV_LOG_FATAL
, "Video encoding failed\n");
603 #if FF_API_CODED_FRAME && FF_API_ERROR_FRAME
604 static double psnr(double d
)
606 return -10.0 * log(d
) / log(10.0);
610 static void do_video_stats(OutputStream
*ost
, int frame_size
)
614 double ti1
, bitrate
, avg_bitrate
;
616 /* this is executed just the first time do_video_stats is called */
618 vstats_file
= fopen(vstats_filename
, "w");
626 if (enc
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
627 frame_number
= ost
->frame_number
;
628 fprintf(vstats_file
, "frame= %5d q= %2.1f ", frame_number
,
629 ost
->quality
/ (float)FF_QP2LAMBDA
);
631 #if FF_API_CODED_FRAME && FF_API_ERROR_FRAME
632 FF_DISABLE_DEPRECATION_WARNINGS
633 if (enc
->flags
& AV_CODEC_FLAG_PSNR
)
634 fprintf(vstats_file
, "PSNR= %6.2f ", psnr(enc
->coded_frame
->error
[0] / (enc
->width
* enc
->height
* 255.0 * 255.0)));
635 FF_ENABLE_DEPRECATION_WARNINGS
638 fprintf(vstats_file
,"f_size= %6d ", frame_size
);
639 /* compute pts value */
640 ti1
= ost
->sync_opts
* av_q2d(enc
->time_base
);
644 bitrate
= (frame_size
* 8) / av_q2d(enc
->time_base
) / 1000.0;
645 avg_bitrate
= (double)(ost
->data_size
* 8) / ti1
/ 1000.0;
646 fprintf(vstats_file
, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
647 (double)ost
->data_size
/ 1024, ti1
, bitrate
, avg_bitrate
);
648 #if FF_API_CODED_FRAME
649 FF_DISABLE_DEPRECATION_WARNINGS
650 fprintf(vstats_file
, "type= %c\n", av_get_picture_type_char(enc
->coded_frame
->pict_type
));
651 FF_ENABLE_DEPRECATION_WARNINGS
657 * Read one frame for lavfi output for ost and encode it.
659 static int poll_filter(OutputStream
*ost
)
661 OutputFile
*of
= output_files
[ost
->file_index
];
662 AVFrame
*filtered_frame
= NULL
;
665 if (!ost
->filtered_frame
&& !(ost
->filtered_frame
= av_frame_alloc())) {
666 return AVERROR(ENOMEM
);
668 filtered_frame
= ost
->filtered_frame
;
670 if (ost
->enc
->type
== AVMEDIA_TYPE_AUDIO
&&
671 !(ost
->enc
->capabilities
& AV_CODEC_CAP_VARIABLE_FRAME_SIZE
))
672 ret
= av_buffersink_get_samples(ost
->filter
->filter
, filtered_frame
,
673 ost
->enc_ctx
->frame_size
);
675 ret
= av_buffersink_get_frame(ost
->filter
->filter
, filtered_frame
);
680 if (filtered_frame
->pts
!= AV_NOPTS_VALUE
) {
681 int64_t start_time
= (of
->start_time
== AV_NOPTS_VALUE
) ?
0 : of
->start_time
;
682 filtered_frame
->pts
= av_rescale_q(filtered_frame
->pts
,
683 ost
->filter
->filter
->inputs
[0]->time_base
,
684 ost
->enc_ctx
->time_base
) -
685 av_rescale_q(start_time
,
687 ost
->enc_ctx
->time_base
);
690 switch (ost
->filter
->filter
->inputs
[0]->type
) {
691 case AVMEDIA_TYPE_VIDEO
:
692 if (!ost
->frame_aspect_ratio
)
693 ost
->enc_ctx
->sample_aspect_ratio
= filtered_frame
->sample_aspect_ratio
;
695 do_video_out(of
, ost
, filtered_frame
, &frame_size
);
696 if (vstats_filename
&& frame_size
)
697 do_video_stats(ost
, frame_size
);
699 case AVMEDIA_TYPE_AUDIO
:
700 do_audio_out(of
, ost
, filtered_frame
);
703 // TODO support subtitle filters
707 av_frame_unref(filtered_frame
);
712 static void finish_output_stream(OutputStream
*ost
)
714 OutputFile
*of
= output_files
[ost
->file_index
];
720 for (i
= 0; i
< of
->ctx
->nb_streams
; i
++)
721 output_streams
[of
->ost_index
+ i
]->finished
= 1;
726 * Read as many frames from possible from lavfi and encode them.
728 * Always read from the active stream with the lowest timestamp. If no frames
729 * are available for it then return EAGAIN and wait for more input. This way we
730 * can use lavfi sources that generate unlimited amount of frames without memory
733 static int poll_filters(void)
737 while (ret
>= 0 && !received_sigterm
) {
738 OutputStream
*ost
= NULL
;
739 int64_t min_pts
= INT64_MAX
;
741 /* choose output stream with the lowest timestamp */
742 for (i
= 0; i
< nb_output_streams
; i
++) {
743 int64_t pts
= output_streams
[i
]->sync_opts
;
745 if (!output_streams
[i
]->filter
|| output_streams
[i
]->finished
)
748 pts
= av_rescale_q(pts
, output_streams
[i
]->enc_ctx
->time_base
,
752 ost
= output_streams
[i
];
759 ret
= poll_filter(ost
);
761 if (ret
== AVERROR_EOF
) {
762 finish_output_stream(ost
);
764 } else if (ret
== AVERROR(EAGAIN
))
771 static void print_final_stats(int64_t total_size
)
773 uint64_t video_size
= 0, audio_size
= 0, extra_size
= 0, other_size
= 0;
774 uint64_t data_size
= 0;
775 float percent
= -1.0;
778 for (i
= 0; i
< nb_output_streams
; i
++) {
779 OutputStream
*ost
= output_streams
[i
];
780 switch (ost
->enc_ctx
->codec_type
) {
781 case AVMEDIA_TYPE_VIDEO
: video_size
+= ost
->data_size
; break;
782 case AVMEDIA_TYPE_AUDIO
: audio_size
+= ost
->data_size
; break;
783 default: other_size
+= ost
->data_size
; break;
785 extra_size
+= ost
->enc_ctx
->extradata_size
;
786 data_size
+= ost
->data_size
;
789 if (data_size
&& total_size
>= data_size
)
790 percent
= 100.0 * (total_size
- data_size
) / data_size
;
792 av_log(NULL
, AV_LOG_INFO
, "\n");
793 av_log(NULL
, AV_LOG_INFO
, "video:%1.0fkB audio:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ",
797 extra_size
/ 1024.0);
799 av_log(NULL
, AV_LOG_INFO
, "%f%%", percent
);
801 av_log(NULL
, AV_LOG_INFO
, "unknown");
802 av_log(NULL
, AV_LOG_INFO
, "\n");
804 /* print verbose per-stream stats */
805 for (i
= 0; i
< nb_input_files
; i
++) {
806 InputFile
*f
= input_files
[i
];
807 uint64_t total_packets
= 0, total_size
= 0;
809 av_log(NULL
, AV_LOG_VERBOSE
, "Input file #%d (%s):\n",
810 i
, f
->ctx
->filename
);
812 for (j
= 0; j
< f
->nb_streams
; j
++) {
813 InputStream
*ist
= input_streams
[f
->ist_index
+ j
];
814 enum AVMediaType type
= ist
->dec_ctx
->codec_type
;
816 total_size
+= ist
->data_size
;
817 total_packets
+= ist
->nb_packets
;
819 av_log(NULL
, AV_LOG_VERBOSE
, " Input stream #%d:%d (%s): ",
820 i
, j
, media_type_string(type
));
821 av_log(NULL
, AV_LOG_VERBOSE
, "%"PRIu64
" packets read (%"PRIu64
" bytes); ",
822 ist
->nb_packets
, ist
->data_size
);
824 if (ist
->decoding_needed
) {
825 av_log(NULL
, AV_LOG_VERBOSE
, "%"PRIu64
" frames decoded",
826 ist
->frames_decoded
);
827 if (type
== AVMEDIA_TYPE_AUDIO
)
828 av_log(NULL
, AV_LOG_VERBOSE
, " (%"PRIu64
" samples)", ist
->samples_decoded
);
829 av_log(NULL
, AV_LOG_VERBOSE
, "; ");
832 av_log(NULL
, AV_LOG_VERBOSE
, "\n");
835 av_log(NULL
, AV_LOG_VERBOSE
, " Total: %"PRIu64
" packets (%"PRIu64
" bytes) demuxed\n",
836 total_packets
, total_size
);
839 for (i
= 0; i
< nb_output_files
; i
++) {
840 OutputFile
*of
= output_files
[i
];
841 uint64_t total_packets
= 0, total_size
= 0;
843 av_log(NULL
, AV_LOG_VERBOSE
, "Output file #%d (%s):\n",
844 i
, of
->ctx
->filename
);
846 for (j
= 0; j
< of
->ctx
->nb_streams
; j
++) {
847 OutputStream
*ost
= output_streams
[of
->ost_index
+ j
];
848 enum AVMediaType type
= ost
->enc_ctx
->codec_type
;
850 total_size
+= ost
->data_size
;
851 total_packets
+= ost
->packets_written
;
853 av_log(NULL
, AV_LOG_VERBOSE
, " Output stream #%d:%d (%s): ",
854 i
, j
, media_type_string(type
));
855 if (ost
->encoding_needed
) {
856 av_log(NULL
, AV_LOG_VERBOSE
, "%"PRIu64
" frames encoded",
857 ost
->frames_encoded
);
858 if (type
== AVMEDIA_TYPE_AUDIO
)
859 av_log(NULL
, AV_LOG_VERBOSE
, " (%"PRIu64
" samples)", ost
->samples_encoded
);
860 av_log(NULL
, AV_LOG_VERBOSE
, "; ");
863 av_log(NULL
, AV_LOG_VERBOSE
, "%"PRIu64
" packets muxed (%"PRIu64
" bytes); ",
864 ost
->packets_written
, ost
->data_size
);
866 av_log(NULL
, AV_LOG_VERBOSE
, "\n");
869 av_log(NULL
, AV_LOG_VERBOSE
, " Total: %"PRIu64
" packets (%"PRIu64
" bytes) muxed\n",
870 total_packets
, total_size
);
874 static void print_report(int is_last_report
, int64_t timer_start
)
881 int frame_number
, vid
, i
;
882 double bitrate
, ti1
, pts
;
883 static int64_t last_time
= -1;
884 static int qp_histogram
[52];
886 if (!print_stats
&& !is_last_report
)
889 if (!is_last_report
) {
891 /* display the report every 0.5 seconds */
892 cur_time
= av_gettime_relative();
893 if (last_time
== -1) {
894 last_time
= cur_time
;
897 if ((cur_time
- last_time
) < 500000)
899 last_time
= cur_time
;
903 oc
= output_files
[0]->ctx
;
905 total_size
= avio_size(oc
->pb
);
906 if (total_size
<= 0) // FIXME improve avio_size() so it works with non seekable output too
907 total_size
= avio_tell(oc
->pb
);
908 if (total_size
< 0) {
910 av_strerror(total_size
, errbuf
, sizeof(errbuf
));
911 av_log(NULL
, AV_LOG_VERBOSE
, "Bitrate not available, "
912 "avio_tell() failed: %s\n", errbuf
);
919 for (i
= 0; i
< nb_output_streams
; i
++) {
921 ost
= output_streams
[i
];
923 if (!ost
->stream_copy
)
924 q
= ost
->quality
/ (float) FF_QP2LAMBDA
;
926 if (vid
&& enc
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
927 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
), "q=%2.1f ", q
);
929 if (!vid
&& enc
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
930 float t
= (av_gettime_relative() - timer_start
) / 1000000.0;
932 frame_number
= ost
->frame_number
;
933 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
), "frame=%5d fps=%3d q=%3.1f ",
934 frame_number
, (t
> 1) ?
(int)(frame_number
/ t
+ 0.5) : 0, q
);
936 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
), "L");
940 if (qp
>= 0 && qp
< FF_ARRAY_ELEMS(qp_histogram
))
942 for (j
= 0; j
< 32; j
++)
943 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
), "%X", (int)lrintf(log2(qp_histogram
[j
] + 1)));
946 #if FF_API_CODED_FRAME && FF_API_ERROR_FRAME
947 FF_DISABLE_DEPRECATION_WARNINGS
948 if (enc
->flags
& AV_CODEC_FLAG_PSNR
) {
950 double error
, error_sum
= 0;
951 double scale
, scale_sum
= 0;
952 char type
[3] = { 'Y','U','V' };
953 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
), "PSNR=");
954 for (j
= 0; j
< 3; j
++) {
955 if (is_last_report
) {
956 error
= enc
->error
[j
];
957 scale
= enc
->width
* enc
->height
* 255.0 * 255.0 * frame_number
;
959 error
= enc
->coded_frame
->error
[j
];
960 scale
= enc
->width
* enc
->height
* 255.0 * 255.0;
966 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
), "%c:%2.2f ", type
[j
], psnr(error
/ scale
));
968 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
), "*:%2.2f ", psnr(error_sum
/ scale_sum
));
970 FF_ENABLE_DEPRECATION_WARNINGS
974 /* compute min output value */
975 pts
= (double)ost
->last_mux_dts
* av_q2d(ost
->st
->time_base
);
976 if ((pts
< ti1
) && (pts
> 0))
982 bitrate
= (double)(total_size
* 8) / ti1
/ 1000.0;
984 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
),
985 "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
986 (double)total_size
/ 1024, ti1
, bitrate
);
989 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
), " drop=%d",
992 av_log(NULL
, AV_LOG_INFO
, "%s \r", buf
);
997 print_final_stats(total_size
);
1001 static void flush_encoders(void)
1005 for (i
= 0; i
< nb_output_streams
; i
++) {
1006 OutputStream
*ost
= output_streams
[i
];
1007 AVCodecContext
*enc
= ost
->enc_ctx
;
1008 OutputFile
*of
= output_files
[ost
->file_index
];
1009 int stop_encoding
= 0;
1011 if (!ost
->encoding_needed
)
1014 if (enc
->codec_type
== AVMEDIA_TYPE_AUDIO
&& enc
->frame_size
<= 1)
1017 if (enc
->codec_type
!= AVMEDIA_TYPE_VIDEO
&& enc
->codec_type
!= AVMEDIA_TYPE_AUDIO
)
1020 avcodec_send_frame(enc
, NULL
);
1023 const char *desc
= NULL
;
1025 switch (enc
->codec_type
) {
1026 case AVMEDIA_TYPE_AUDIO
:
1029 case AVMEDIA_TYPE_VIDEO
:
1038 av_init_packet(&pkt
);
1042 ret
= avcodec_receive_packet(enc
, &pkt
);
1043 if (ret
< 0 && ret
!= AVERROR_EOF
) {
1044 av_log(NULL
, AV_LOG_FATAL
, "%s encoding failed\n", desc
);
1047 if (ost
->logfile
&& enc
->stats_out
) {
1048 fprintf(ost
->logfile
, "%s", enc
->stats_out
);
1050 if (ret
== AVERROR_EOF
) {
1054 av_packet_rescale_ts(&pkt
, enc
->time_base
, ost
->st
->time_base
);
1055 output_packet(of
, &pkt
, ost
);
1065 * Check whether a packet from ist should be written into ost at this time
1067 static int check_output_constraints(InputStream
*ist
, OutputStream
*ost
)
1069 OutputFile
*of
= output_files
[ost
->file_index
];
1070 int ist_index
= input_files
[ist
->file_index
]->ist_index
+ ist
->st
->index
;
1072 if (ost
->source_index
!= ist_index
)
1075 if (of
->start_time
!= AV_NOPTS_VALUE
&& ist
->last_dts
< of
->start_time
)
1081 static void do_streamcopy(InputStream
*ist
, OutputStream
*ost
, const AVPacket
*pkt
)
1083 OutputFile
*of
= output_files
[ost
->file_index
];
1084 InputFile
*f
= input_files
[ist
->file_index
];
1085 int64_t start_time
= (of
->start_time
== AV_NOPTS_VALUE
) ?
0 : of
->start_time
;
1086 int64_t ost_tb_start_time
= av_rescale_q(start_time
, AV_TIME_BASE_Q
, ost
->st
->time_base
);
1089 av_init_packet(&opkt
);
1091 if ((!ost
->frame_number
&& !(pkt
->flags
& AV_PKT_FLAG_KEY
)) &&
1092 !ost
->copy_initial_nonkeyframes
)
1095 if (of
->recording_time
!= INT64_MAX
&&
1096 ist
->last_dts
>= of
->recording_time
+ start_time
) {
1101 if (f
->recording_time
!= INT64_MAX
) {
1102 start_time
= f
->ctx
->start_time
;
1103 if (f
->start_time
!= AV_NOPTS_VALUE
)
1104 start_time
+= f
->start_time
;
1105 if (ist
->last_dts
>= f
->recording_time
+ start_time
) {
1111 /* force the input stream PTS */
1112 if (ost
->enc_ctx
->codec_type
== AVMEDIA_TYPE_VIDEO
)
1115 if (pkt
->pts
!= AV_NOPTS_VALUE
)
1116 opkt
.pts
= av_rescale_q(pkt
->pts
, ist
->st
->time_base
, ost
->st
->time_base
) - ost_tb_start_time
;
1118 opkt
.pts
= AV_NOPTS_VALUE
;
1120 if (pkt
->dts
== AV_NOPTS_VALUE
)
1121 opkt
.dts
= av_rescale_q(ist
->last_dts
, AV_TIME_BASE_Q
, ost
->st
->time_base
);
1123 opkt
.dts
= av_rescale_q(pkt
->dts
, ist
->st
->time_base
, ost
->st
->time_base
);
1124 opkt
.dts
-= ost_tb_start_time
;
1126 opkt
.duration
= av_rescale_q(pkt
->duration
, ist
->st
->time_base
, ost
->st
->time_base
);
1127 opkt
.flags
= pkt
->flags
;
1129 // FIXME remove the following 2 lines they shall be replaced by the bitstream filters
1130 if ( ost
->enc_ctx
->codec_id
!= AV_CODEC_ID_H264
1131 && ost
->enc_ctx
->codec_id
!= AV_CODEC_ID_MPEG1VIDEO
1132 && ost
->enc_ctx
->codec_id
!= AV_CODEC_ID_MPEG2VIDEO
1133 && ost
->enc_ctx
->codec_id
!= AV_CODEC_ID_VC1
1135 if (av_parser_change(ost
->parser
, ost
->parser_avctx
,
1136 &opkt
.data
, &opkt
.size
,
1137 pkt
->data
, pkt
->size
,
1138 pkt
->flags
& AV_PKT_FLAG_KEY
)) {
1139 opkt
.buf
= av_buffer_create(opkt
.data
, opkt
.size
, av_buffer_default_free
, NULL
, 0);
1144 opkt
.data
= pkt
->data
;
1145 opkt
.size
= pkt
->size
;
1148 output_packet(of
, &opkt
, ost
);
1151 // This does not quite work like avcodec_decode_audio4/avcodec_decode_video2.
1152 // There is the following difference: if you got a frame, you must call
1153 // it again with pkt=NULL. pkt==NULL is treated differently from pkt.size==0
1154 // (pkt==NULL means get more output, pkt.size==0 is a flush/drain packet)
1155 static int decode(AVCodecContext
*avctx
, AVFrame
*frame
, int *got_frame
, AVPacket
*pkt
)
1162 ret
= avcodec_send_packet(avctx
, pkt
);
1163 // In particular, we don't expect AVERROR(EAGAIN), because we read all
1164 // decoded frames with avcodec_receive_frame() until done.
1166 return ret
== AVERROR_EOF ?
0 : ret
;
1169 ret
= avcodec_receive_frame(avctx
, frame
);
1170 if (ret
< 0 && ret
!= AVERROR(EAGAIN
) && ret
!= AVERROR_EOF
)
1178 int guess_input_channel_layout(InputStream
*ist
)
1180 AVCodecContext
*dec
= ist
->dec_ctx
;
1182 if (!dec
->channel_layout
) {
1183 char layout_name
[256];
1185 dec
->channel_layout
= av_get_default_channel_layout(dec
->channels
);
1186 if (!dec
->channel_layout
)
1188 av_get_channel_layout_string(layout_name
, sizeof(layout_name
),
1189 dec
->channels
, dec
->channel_layout
);
1190 av_log(NULL
, AV_LOG_WARNING
, "Guessed Channel Layout for Input Stream "
1191 "#%d.%d : %s\n", ist
->file_index
, ist
->st
->index
, layout_name
);
1196 static int decode_audio(InputStream
*ist
, AVPacket
*pkt
, int *got_output
)
1198 AVFrame
*decoded_frame
, *f
;
1199 AVCodecContext
*avctx
= ist
->dec_ctx
;
1200 int i
, ret
, err
= 0, resample_changed
;
1202 if (!ist
->decoded_frame
&& !(ist
->decoded_frame
= av_frame_alloc()))
1203 return AVERROR(ENOMEM
);
1204 if (!ist
->filter_frame
&& !(ist
->filter_frame
= av_frame_alloc()))
1205 return AVERROR(ENOMEM
);
1206 decoded_frame
= ist
->decoded_frame
;
1208 ret
= decode(avctx
, decoded_frame
, got_output
, pkt
);
1209 if (!*got_output
|| ret
< 0)
1212 ist
->samples_decoded
+= decoded_frame
->nb_samples
;
1213 ist
->frames_decoded
++;
1215 /* if the decoder provides a pts, use it instead of the last packet pts.
1216 the decoder could be delaying output by a packet or more. */
1217 if (decoded_frame
->pts
!= AV_NOPTS_VALUE
)
1218 ist
->next_dts
= decoded_frame
->pts
;
1219 else if (pkt
&& pkt
->pts
!= AV_NOPTS_VALUE
) {
1220 decoded_frame
->pts
= pkt
->pts
;
1223 resample_changed
= ist
->resample_sample_fmt
!= decoded_frame
->format
||
1224 ist
->resample_channels
!= avctx
->channels
||
1225 ist
->resample_channel_layout
!= decoded_frame
->channel_layout
||
1226 ist
->resample_sample_rate
!= decoded_frame
->sample_rate
;
1227 if (resample_changed
) {
1228 char layout1
[64], layout2
[64];
1230 if (!guess_input_channel_layout(ist
)) {
1231 av_log(NULL
, AV_LOG_FATAL
, "Unable to find default channel "
1232 "layout for Input Stream #%d.%d\n", ist
->file_index
,
1236 decoded_frame
->channel_layout
= avctx
->channel_layout
;
1238 av_get_channel_layout_string(layout1
, sizeof(layout1
), ist
->resample_channels
,
1239 ist
->resample_channel_layout
);
1240 av_get_channel_layout_string(layout2
, sizeof(layout2
), avctx
->channels
,
1241 decoded_frame
->channel_layout
);
1243 av_log(NULL
, AV_LOG_INFO
,
1244 "Input stream #%d:%d frame changed from rate:%d fmt:%s ch:%d chl:%s to rate:%d fmt:%s ch:%d chl:%s\n",
1245 ist
->file_index
, ist
->st
->index
,
1246 ist
->resample_sample_rate
, av_get_sample_fmt_name(ist
->resample_sample_fmt
),
1247 ist
->resample_channels
, layout1
,
1248 decoded_frame
->sample_rate
, av_get_sample_fmt_name(decoded_frame
->format
),
1249 avctx
->channels
, layout2
);
1251 ist
->resample_sample_fmt
= decoded_frame
->format
;
1252 ist
->resample_sample_rate
= decoded_frame
->sample_rate
;
1253 ist
->resample_channel_layout
= decoded_frame
->channel_layout
;
1254 ist
->resample_channels
= avctx
->channels
;
1256 for (i
= 0; i
< ist
->nb_filters
; i
++) {
1257 err
= ifilter_parameters_from_frame(ist
->filters
[i
], decoded_frame
);
1259 av_log(NULL
, AV_LOG_ERROR
,
1260 "Error reconfiguring input stream %d:%d filter %d\n",
1261 ist
->file_index
, ist
->st
->index
, i
);
1266 for (i
= 0; i
< nb_filtergraphs
; i
++)
1267 if (ist_in_filtergraph(filtergraphs
[i
], ist
) &&
1268 configure_filtergraph(filtergraphs
[i
]) < 0) {
1269 av_log(NULL
, AV_LOG_FATAL
, "Error reinitializing filters!\n");
1274 if (decoded_frame
->pts
!= AV_NOPTS_VALUE
)
1275 decoded_frame
->pts
= av_rescale_q(decoded_frame
->pts
,
1277 (AVRational
){1, avctx
->sample_rate
});
1278 ist
->nb_samples
= decoded_frame
->nb_samples
;
1279 for (i
= 0; i
< ist
->nb_filters
; i
++) {
1280 if (i
< ist
->nb_filters
- 1) {
1281 f
= ist
->filter_frame
;
1282 err
= av_frame_ref(f
, decoded_frame
);
1288 err
= av_buffersrc_add_frame(ist
->filters
[i
]->filter
, f
);
1294 av_frame_unref(ist
->filter_frame
);
1295 av_frame_unref(decoded_frame
);
1296 return err
< 0 ? err
: ret
;
1299 static int decode_video(InputStream
*ist
, AVPacket
*pkt
, int *got_output
)
1301 AVFrame
*decoded_frame
, *f
;
1302 int i
, ret
= 0, err
= 0, resample_changed
;
1304 if (!ist
->decoded_frame
&& !(ist
->decoded_frame
= av_frame_alloc()))
1305 return AVERROR(ENOMEM
);
1306 if (!ist
->filter_frame
&& !(ist
->filter_frame
= av_frame_alloc()))
1307 return AVERROR(ENOMEM
);
1308 decoded_frame
= ist
->decoded_frame
;
1310 ret
= decode(ist
->dec_ctx
, decoded_frame
, got_output
, pkt
);
1311 if (!*got_output
|| ret
< 0)
1314 ist
->frames_decoded
++;
1316 if (ist
->hwaccel_retrieve_data
&& decoded_frame
->format
== ist
->hwaccel_pix_fmt
) {
1317 err
= ist
->hwaccel_retrieve_data(ist
->dec_ctx
, decoded_frame
);
1321 ist
->hwaccel_retrieved_pix_fmt
= decoded_frame
->format
;
1323 decoded_frame
->pts
= guess_correct_pts(&ist
->pts_ctx
, decoded_frame
->pts
,
1324 decoded_frame
->pkt_dts
);
1326 if (ist
->st
->sample_aspect_ratio
.num
)
1327 decoded_frame
->sample_aspect_ratio
= ist
->st
->sample_aspect_ratio
;
1329 resample_changed
= ist
->resample_width
!= decoded_frame
->width
||
1330 ist
->resample_height
!= decoded_frame
->height
||
1331 ist
->resample_pix_fmt
!= decoded_frame
->format
;
1332 if (resample_changed
) {
1333 av_log(NULL
, AV_LOG_INFO
,
1334 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
1335 ist
->file_index
, ist
->st
->index
,
1336 ist
->resample_width
, ist
->resample_height
, av_get_pix_fmt_name(ist
->resample_pix_fmt
),
1337 decoded_frame
->width
, decoded_frame
->height
, av_get_pix_fmt_name(decoded_frame
->format
));
1339 ret
= poll_filters();
1340 if (ret
< 0 && ret
!= AVERROR_EOF
) {
1342 av_strerror(ret
, errbuf
, sizeof(errbuf
));
1344 av_log(NULL
, AV_LOG_ERROR
, "Error while filtering: %s\n", errbuf
);
1347 ist
->resample_width
= decoded_frame
->width
;
1348 ist
->resample_height
= decoded_frame
->height
;
1349 ist
->resample_pix_fmt
= decoded_frame
->format
;
1351 for (i
= 0; i
< ist
->nb_filters
; i
++) {
1352 err
= ifilter_parameters_from_frame(ist
->filters
[i
], decoded_frame
);
1354 av_log(NULL
, AV_LOG_ERROR
,
1355 "Error reconfiguring input stream %d:%d filter %d\n",
1356 ist
->file_index
, ist
->st
->index
, i
);
1361 for (i
= 0; i
< nb_filtergraphs
; i
++)
1362 if (ist_in_filtergraph(filtergraphs
[i
], ist
) &&
1363 configure_filtergraph(filtergraphs
[i
]) < 0) {
1364 av_log(NULL
, AV_LOG_FATAL
, "Error reinitializing filters!\n");
1369 for (i
= 0; i
< ist
->nb_filters
; i
++) {
1370 if (i
< ist
->nb_filters
- 1) {
1371 f
= ist
->filter_frame
;
1372 err
= av_frame_ref(f
, decoded_frame
);
1378 err
= av_buffersrc_add_frame(ist
->filters
[i
]->filter
, f
);
1384 av_frame_unref(ist
->filter_frame
);
1385 av_frame_unref(decoded_frame
);
1386 return err
< 0 ? err
: ret
;
1389 static int transcode_subtitles(InputStream
*ist
, AVPacket
*pkt
, int *got_output
)
1391 AVSubtitle subtitle
;
1392 int i
, ret
= avcodec_decode_subtitle2(ist
->dec_ctx
,
1393 &subtitle
, got_output
, pkt
);
1399 ist
->frames_decoded
++;
1401 for (i
= 0; i
< nb_output_streams
; i
++) {
1402 OutputStream
*ost
= output_streams
[i
];
1404 if (!check_output_constraints(ist
, ost
) || !ost
->encoding_needed
)
1407 do_subtitle_out(output_files
[ost
->file_index
], ost
, ist
, &subtitle
, pkt
->pts
);
1410 avsubtitle_free(&subtitle
);
1414 static int send_filter_eof(InputStream
*ist
)
1417 for (i
= 0; i
< ist
->nb_filters
; i
++) {
1418 ret
= av_buffersrc_add_frame(ist
->filters
[i
]->filter
, NULL
);
1425 /* pkt = NULL means EOF (needed to flush decoder buffers) */
1426 static void process_input_packet(InputStream
*ist
, const AVPacket
*pkt
, int no_eof
)
1432 if (ist
->next_dts
== AV_NOPTS_VALUE
)
1433 ist
->next_dts
= ist
->last_dts
;
1437 av_init_packet(&avpkt
);
1444 if (pkt
&& pkt
->dts
!= AV_NOPTS_VALUE
)
1445 ist
->next_dts
= ist
->last_dts
= av_rescale_q(pkt
->dts
, ist
->st
->time_base
, AV_TIME_BASE_Q
);
1447 // while we have more to decode or while the decoder did output something on EOF
1448 while (ist
->decoding_needed
&& (!pkt
|| avpkt
.size
> 0)) {
1453 ist
->last_dts
= ist
->next_dts
;
1455 switch (ist
->dec_ctx
->codec_type
) {
1456 case AVMEDIA_TYPE_AUDIO
:
1457 ret
= decode_audio (ist
, repeating ? NULL
: &avpkt
, &got_output
);
1459 case AVMEDIA_TYPE_VIDEO
:
1460 ret
= decode_video (ist
, repeating ? NULL
: &avpkt
, &got_output
);
1461 if (repeating
&& !got_output
)
1463 else if (pkt
&& pkt
->duration
)
1464 ist
->next_dts
+= av_rescale_q(pkt
->duration
, ist
->st
->time_base
, AV_TIME_BASE_Q
);
1465 else if (ist
->st
->avg_frame_rate
.num
)
1466 ist
->next_dts
+= av_rescale_q(1, av_inv_q(ist
->st
->avg_frame_rate
),
1468 else if (ist
->dec_ctx
->framerate
.num
!= 0) {
1469 int ticks
= ist
->st
->parser ? ist
->st
->parser
->repeat_pict
+ 1 :
1470 ist
->dec_ctx
->ticks_per_frame
;
1471 ist
->next_dts
+= av_rescale_q(ticks
, ist
->dec_ctx
->framerate
, AV_TIME_BASE_Q
);
1474 case AVMEDIA_TYPE_SUBTITLE
:
1477 ret
= transcode_subtitles(ist
, &avpkt
, &got_output
);
1484 av_log(NULL
, AV_LOG_ERROR
, "Error while decoding stream #%d:%d\n",
1485 ist
->file_index
, ist
->st
->index
);
1497 /* after flushing, send an EOF on all the filter inputs attached to the stream */
1498 /* except when looping we need to flush but not to send an EOF */
1499 if (!pkt
&& ist
->decoding_needed
&& !no_eof
) {
1500 int ret
= send_filter_eof(ist
);
1502 av_log(NULL
, AV_LOG_FATAL
, "Error marking filters as finished\n");
1507 /* handle stream copy */
1508 if (!ist
->decoding_needed
) {
1509 ist
->last_dts
= ist
->next_dts
;
1510 switch (ist
->dec_ctx
->codec_type
) {
1511 case AVMEDIA_TYPE_AUDIO
:
1512 ist
->next_dts
+= ((int64_t)AV_TIME_BASE
* ist
->dec_ctx
->frame_size
) /
1513 ist
->dec_ctx
->sample_rate
;
1515 case AVMEDIA_TYPE_VIDEO
:
1516 if (ist
->dec_ctx
->framerate
.num
!= 0) {
1517 int ticks
= ist
->st
->parser ? ist
->st
->parser
->repeat_pict
+ 1 : ist
->dec_ctx
->ticks_per_frame
;
1518 ist
->next_dts
+= ((int64_t)AV_TIME_BASE
*
1519 ist
->dec_ctx
->framerate
.den
* ticks
) /
1520 ist
->dec_ctx
->framerate
.num
;
1525 for (i
= 0; pkt
&& i
< nb_output_streams
; i
++) {
1526 OutputStream
*ost
= output_streams
[i
];
1528 if (!check_output_constraints(ist
, ost
) || ost
->encoding_needed
)
1531 do_streamcopy(ist
, ost
, pkt
);
1537 static void print_sdp(void)
1541 AVFormatContext
**avc
;
1543 for (i
= 0; i
< nb_output_files
; i
++) {
1544 if (!output_files
[i
]->header_written
)
1548 avc
= av_malloc(sizeof(*avc
) * nb_output_files
);
1551 for (i
= 0; i
< nb_output_files
; i
++)
1552 avc
[i
] = output_files
[i
]->ctx
;
1554 av_sdp_create(avc
, nb_output_files
, sdp
, sizeof(sdp
));
1555 printf("SDP:\n%s\n", sdp
);
1560 static const HWAccel
*get_hwaccel(enum AVPixelFormat pix_fmt
)
1563 for (i
= 0; hwaccels
[i
].name
; i
++)
1564 if (hwaccels
[i
].pix_fmt
== pix_fmt
)
1565 return &hwaccels
[i
];
1569 static enum AVPixelFormat
get_format(AVCodecContext
*s
, const enum AVPixelFormat
*pix_fmts
)
1571 InputStream
*ist
= s
->opaque
;
1572 const enum AVPixelFormat
*p
;
1575 for (p
= pix_fmts
; *p
!= -1; p
++) {
1576 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(*p
);
1577 const HWAccel
*hwaccel
;
1579 if (!(desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
))
1582 hwaccel
= get_hwaccel(*p
);
1584 (ist
->active_hwaccel_id
&& ist
->active_hwaccel_id
!= hwaccel
->id
) ||
1585 (ist
->hwaccel_id
!= HWACCEL_AUTO
&& ist
->hwaccel_id
!= hwaccel
->id
))
1588 ret
= hwaccel
->init(s
);
1590 if (ist
->hwaccel_id
== hwaccel
->id
) {
1591 av_log(NULL
, AV_LOG_FATAL
,
1592 "%s hwaccel requested for input stream #%d:%d, "
1593 "but cannot be initialized.\n", hwaccel
->name
,
1594 ist
->file_index
, ist
->st
->index
);
1595 return AV_PIX_FMT_NONE
;
1599 ist
->active_hwaccel_id
= hwaccel
->id
;
1600 ist
->hwaccel_pix_fmt
= *p
;
1607 static int get_buffer(AVCodecContext
*s
, AVFrame
*frame
, int flags
)
1609 InputStream
*ist
= s
->opaque
;
1611 if (ist
->hwaccel_get_buffer
&& frame
->format
== ist
->hwaccel_pix_fmt
)
1612 return ist
->hwaccel_get_buffer(s
, frame
, flags
);
1614 return avcodec_default_get_buffer2(s
, frame
, flags
);
1617 static int init_input_stream(int ist_index
, char *error
, int error_len
)
1620 InputStream
*ist
= input_streams
[ist_index
];
1622 for (i
= 0; i
< ist
->nb_filters
; i
++) {
1623 ret
= ifilter_parameters_from_decoder(ist
->filters
[i
], ist
->dec_ctx
);
1625 av_log(NULL
, AV_LOG_FATAL
, "Error initializing filter input\n");
1630 if (ist
->decoding_needed
) {
1631 AVCodec
*codec
= ist
->dec
;
1633 snprintf(error
, error_len
, "Decoder (codec id %d) not found for input stream #%d:%d",
1634 ist
->dec_ctx
->codec_id
, ist
->file_index
, ist
->st
->index
);
1635 return AVERROR(EINVAL
);
1638 ist
->dec_ctx
->opaque
= ist
;
1639 ist
->dec_ctx
->get_format
= get_format
;
1640 ist
->dec_ctx
->get_buffer2
= get_buffer
;
1641 ist
->dec_ctx
->thread_safe_callbacks
= 1;
1643 av_opt_set_int(ist
->dec_ctx
, "refcounted_frames", 1, 0);
1645 if (!av_dict_get(ist
->decoder_opts
, "threads", NULL
, 0))
1646 av_dict_set(&ist
->decoder_opts
, "threads", "auto", 0);
1647 if ((ret
= avcodec_open2(ist
->dec_ctx
, codec
, &ist
->decoder_opts
)) < 0) {
1649 if (ret
== AVERROR_EXPERIMENTAL
)
1650 abort_codec_experimental(codec
, 0);
1652 av_strerror(ret
, errbuf
, sizeof(errbuf
));
1654 snprintf(error
, error_len
,
1655 "Error while opening decoder for input stream "
1657 ist
->file_index
, ist
->st
->index
, errbuf
);
1660 assert_avoptions(ist
->decoder_opts
);
1663 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;
1664 ist
->next_dts
= AV_NOPTS_VALUE
;
1665 init_pts_correction(&ist
->pts_ctx
);
1670 static InputStream
*get_input_stream(OutputStream
*ost
)
1672 if (ost
->source_index
>= 0)
1673 return input_streams
[ost
->source_index
];
1676 FilterGraph
*fg
= ost
->filter
->graph
;
1679 for (i
= 0; i
< fg
->nb_inputs
; i
++)
1680 if (fg
->inputs
[i
]->ist
->dec_ctx
->codec_type
== ost
->enc_ctx
->codec_type
)
1681 return fg
->inputs
[i
]->ist
;
1687 /* open the muxer when all the streams are initialized */
1688 static int check_init_output_file(OutputFile
*of
, int file_index
)
1692 for (i
= 0; i
< of
->ctx
->nb_streams
; i
++) {
1693 OutputStream
*ost
= output_streams
[of
->ost_index
+ i
];
1694 if (!ost
->initialized
)
1698 of
->ctx
->interrupt_callback
= int_cb
;
1700 ret
= avformat_write_header(of
->ctx
, &of
->opts
);
1704 av_strerror(ret
, errbuf
, sizeof(errbuf
));
1706 av_log(NULL
, AV_LOG_ERROR
,
1707 "Could not write header for output file #%d "
1708 "(incorrect codec parameters ?): %s",
1709 file_index
, errbuf
);
1712 assert_avoptions(of
->opts
);
1713 of
->header_written
= 1;
1715 av_dump_format(of
->ctx
, file_index
, of
->ctx
->filename
, 1);
1720 /* flush the muxing queues */
1721 for (i
= 0; i
< of
->ctx
->nb_streams
; i
++) {
1722 OutputStream
*ost
= output_streams
[of
->ost_index
+ i
];
1724 while (av_fifo_size(ost
->muxing_queue
)) {
1726 av_fifo_generic_read(ost
->muxing_queue
, &pkt
, sizeof(pkt
), NULL
);
1727 write_packet(of
, &pkt
, ost
);
1734 static int init_output_bsfs(OutputStream
*ost
)
1739 if (!ost
->nb_bitstream_filters
)
1742 ost
->bsf_ctx
= av_mallocz_array(ost
->nb_bitstream_filters
, sizeof(*ost
->bsf_ctx
));
1744 return AVERROR(ENOMEM
);
1746 for (i
= 0; i
< ost
->nb_bitstream_filters
; i
++) {
1747 ret
= av_bsf_alloc(ost
->bitstream_filters
[i
], &ctx
);
1749 av_log(NULL
, AV_LOG_ERROR
, "Error allocating a bitstream filter context\n");
1752 ost
->bsf_ctx
[i
] = ctx
;
1754 ret
= avcodec_parameters_copy(ctx
->par_in
,
1755 i ? ost
->bsf_ctx
[i
- 1]->par_out
: ost
->st
->codecpar
);
1759 ctx
->time_base_in
= i ? ost
->bsf_ctx
[i
- 1]->time_base_out
: ost
->st
->time_base
;
1761 ret
= av_bsf_init(ctx
);
1763 av_log(NULL
, AV_LOG_ERROR
, "Error initializing bitstream filter: %s\n",
1764 ost
->bitstream_filters
[i
]->name
);
1769 ctx
= ost
->bsf_ctx
[ost
->nb_bitstream_filters
- 1];
1770 ret
= avcodec_parameters_copy(ost
->st
->codecpar
, ctx
->par_out
);
1774 ost
->st
->time_base
= ctx
->time_base_out
;
1779 static int init_output_stream_streamcopy(OutputStream
*ost
)
1781 OutputFile
*of
= output_files
[ost
->file_index
];
1782 InputStream
*ist
= get_input_stream(ost
);
1783 AVCodecParameters
*par_dst
= ost
->st
->codecpar
;
1784 AVCodecParameters
*par_src
= ist
->st
->codecpar
;
1787 uint64_t extra_size
;
1789 extra_size
= (uint64_t)par_src
->extradata_size
+ AV_INPUT_BUFFER_PADDING_SIZE
;
1790 if (extra_size
> INT_MAX
) {
1791 return AVERROR(EINVAL
);
1794 ost
->st
->disposition
= ist
->st
->disposition
;
1796 /* if stream_copy is selected, no need to decode or encode */
1797 par_dst
->codec_id
= par_src
->codec_id
;
1798 par_dst
->codec_type
= par_src
->codec_type
;
1800 if (!par_dst
->codec_tag
) {
1801 if (!of
->ctx
->oformat
->codec_tag
||
1802 av_codec_get_id (of
->ctx
->oformat
->codec_tag
, par_src
->codec_tag
) == par_dst
->codec_id
||
1803 av_codec_get_tag(of
->ctx
->oformat
->codec_tag
, par_src
->codec_id
) <= 0)
1804 par_dst
->codec_tag
= par_src
->codec_tag
;
1807 par_dst
->bit_rate
= par_src
->bit_rate
;
1808 par_dst
->field_order
= par_src
->field_order
;
1809 par_dst
->chroma_location
= par_src
->chroma_location
;
1811 if (par_src
->extradata
) {
1812 par_dst
->extradata
= av_mallocz(extra_size
);
1813 if (!par_dst
->extradata
) {
1814 return AVERROR(ENOMEM
);
1816 memcpy(par_dst
->extradata
, par_src
->extradata
, par_src
->extradata_size
);
1817 par_dst
->extradata_size
= par_src
->extradata_size
;
1820 ost
->st
->time_base
= ist
->st
->time_base
;
1822 if (ist
->st
->nb_side_data
) {
1823 ost
->st
->side_data
= av_realloc_array(NULL
, ist
->st
->nb_side_data
,
1824 sizeof(*ist
->st
->side_data
));
1825 if (!ost
->st
->side_data
)
1826 return AVERROR(ENOMEM
);
1828 for (i
= 0; i
< ist
->st
->nb_side_data
; i
++) {
1829 const AVPacketSideData
*sd_src
= &ist
->st
->side_data
[i
];
1830 AVPacketSideData
*sd_dst
= &ost
->st
->side_data
[i
];
1832 sd_dst
->data
= av_malloc(sd_src
->size
);
1834 return AVERROR(ENOMEM
);
1835 memcpy(sd_dst
->data
, sd_src
->data
, sd_src
->size
);
1836 sd_dst
->size
= sd_src
->size
;
1837 sd_dst
->type
= sd_src
->type
;
1838 ost
->st
->nb_side_data
++;
1842 ost
->parser
= av_parser_init(par_dst
->codec_id
);
1843 ost
->parser_avctx
= avcodec_alloc_context3(NULL
);
1844 if (!ost
->parser_avctx
)
1845 return AVERROR(ENOMEM
);
1847 switch (par_dst
->codec_type
) {
1848 case AVMEDIA_TYPE_AUDIO
:
1849 if (audio_volume
!= 256) {
1850 av_log(NULL
, AV_LOG_FATAL
, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
1853 par_dst
->channel_layout
= par_src
->channel_layout
;
1854 par_dst
->sample_rate
= par_src
->sample_rate
;
1855 par_dst
->channels
= par_src
->channels
;
1856 par_dst
->block_align
= par_src
->block_align
;
1858 case AVMEDIA_TYPE_VIDEO
:
1859 par_dst
->format
= par_src
->format
;
1860 par_dst
->width
= par_src
->width
;
1861 par_dst
->height
= par_src
->height
;
1862 if (ost
->frame_aspect_ratio
)
1863 sar
= av_d2q(ost
->frame_aspect_ratio
* par_dst
->height
/ par_dst
->width
, 255);
1864 else if (ist
->st
->sample_aspect_ratio
.num
)
1865 sar
= ist
->st
->sample_aspect_ratio
;
1867 sar
= par_src
->sample_aspect_ratio
;
1868 ost
->st
->sample_aspect_ratio
= par_dst
->sample_aspect_ratio
= sar
;
1870 case AVMEDIA_TYPE_SUBTITLE
:
1871 par_dst
->width
= par_src
->width
;
1872 par_dst
->height
= par_src
->height
;
1874 case AVMEDIA_TYPE_DATA
:
1875 case AVMEDIA_TYPE_ATTACHMENT
:
1884 static void set_encoder_id(OutputFile
*of
, OutputStream
*ost
)
1886 AVDictionaryEntry
*e
;
1888 uint8_t *encoder_string
;
1889 int encoder_string_len
;
1890 int format_flags
= 0;
1892 e
= av_dict_get(of
->opts
, "fflags", NULL
, 0);
1894 const AVOption
*o
= av_opt_find(of
->ctx
, "fflags", NULL
, 0, 0);
1897 av_opt_eval_flags(of
->ctx
, o
, e
->value
, &format_flags
);
1900 encoder_string_len
= sizeof(LIBAVCODEC_IDENT
) + strlen(ost
->enc
->name
) + 2;
1901 encoder_string
= av_mallocz(encoder_string_len
);
1902 if (!encoder_string
)
1905 if (!(format_flags
& AVFMT_FLAG_BITEXACT
))
1906 av_strlcpy(encoder_string
, LIBAVCODEC_IDENT
" ", encoder_string_len
);
1907 av_strlcat(encoder_string
, ost
->enc
->name
, encoder_string_len
);
1908 av_dict_set(&ost
->st
->metadata
, "encoder", encoder_string
,
1909 AV_DICT_DONT_STRDUP_VAL
| AV_DICT_DONT_OVERWRITE
);
1912 static void parse_forced_key_frames(char *kf
, OutputStream
*ost
,
1913 AVCodecContext
*avctx
)
1919 for (p
= kf
; *p
; p
++)
1922 ost
->forced_kf_count
= n
;
1923 ost
->forced_kf_pts
= av_malloc(sizeof(*ost
->forced_kf_pts
) * n
);
1924 if (!ost
->forced_kf_pts
) {
1925 av_log(NULL
, AV_LOG_FATAL
, "Could not allocate forced key frames array.\n");
1930 for (i
= 0; i
< n
; i
++) {
1931 char *next
= strchr(p
, ',');
1936 t
= parse_time_or_die("force_key_frames", p
, 1);
1937 ost
->forced_kf_pts
[i
] = av_rescale_q(t
, AV_TIME_BASE_Q
, avctx
->time_base
);
1943 static int init_output_stream_encode(OutputStream
*ost
)
1945 InputStream
*ist
= get_input_stream(ost
);
1946 AVCodecContext
*enc_ctx
= ost
->enc_ctx
;
1947 AVCodecContext
*dec_ctx
= NULL
;
1949 set_encoder_id(output_files
[ost
->file_index
], ost
);
1952 ost
->st
->disposition
= ist
->st
->disposition
;
1954 dec_ctx
= ist
->dec_ctx
;
1956 enc_ctx
->bits_per_raw_sample
= dec_ctx
->bits_per_raw_sample
;
1957 enc_ctx
->chroma_sample_location
= dec_ctx
->chroma_sample_location
;
1960 if ((enc_ctx
->codec_type
== AVMEDIA_TYPE_VIDEO
||
1961 enc_ctx
->codec_type
== AVMEDIA_TYPE_AUDIO
) &&
1962 filtergraph_is_simple(ost
->filter
->graph
)) {
1963 FilterGraph
*fg
= ost
->filter
->graph
;
1965 if (configure_filtergraph(fg
)) {
1966 av_log(NULL
, AV_LOG_FATAL
, "Error opening filters!\n");
1971 switch (enc_ctx
->codec_type
) {
1972 case AVMEDIA_TYPE_AUDIO
:
1973 enc_ctx
->sample_fmt
= ost
->filter
->filter
->inputs
[0]->format
;
1974 enc_ctx
->sample_rate
= ost
->filter
->filter
->inputs
[0]->sample_rate
;
1975 enc_ctx
->channel_layout
= ost
->filter
->filter
->inputs
[0]->channel_layout
;
1976 enc_ctx
->channels
= av_get_channel_layout_nb_channels(enc_ctx
->channel_layout
);
1977 enc_ctx
->time_base
= (AVRational
){ 1, enc_ctx
->sample_rate
};
1979 case AVMEDIA_TYPE_VIDEO
:
1980 enc_ctx
->time_base
= ost
->filter
->filter
->inputs
[0]->time_base
;
1982 enc_ctx
->width
= ost
->filter
->filter
->inputs
[0]->w
;
1983 enc_ctx
->height
= ost
->filter
->filter
->inputs
[0]->h
;
1984 enc_ctx
->sample_aspect_ratio
= ost
->st
->sample_aspect_ratio
=
1985 ost
->frame_aspect_ratio ?
// overridden by the -aspect cli option
1986 av_d2q(ost
->frame_aspect_ratio
* enc_ctx
->height
/enc_ctx
->width
, 255) :
1987 ost
->filter
->filter
->inputs
[0]->sample_aspect_ratio
;
1988 enc_ctx
->pix_fmt
= ost
->filter
->filter
->inputs
[0]->format
;
1990 ost
->st
->avg_frame_rate
= ost
->frame_rate
;
1993 (enc_ctx
->width
!= dec_ctx
->width
||
1994 enc_ctx
->height
!= dec_ctx
->height
||
1995 enc_ctx
->pix_fmt
!= dec_ctx
->pix_fmt
)) {
1996 enc_ctx
->bits_per_raw_sample
= 0;
1999 if (ost
->forced_keyframes
)
2000 parse_forced_key_frames(ost
->forced_keyframes
, ost
,
2003 case AVMEDIA_TYPE_SUBTITLE
:
2004 enc_ctx
->time_base
= (AVRational
){1, 1000};
2014 static int init_output_stream(OutputStream
*ost
, char *error
, int error_len
)
2018 if (ost
->encoding_needed
) {
2019 AVCodec
*codec
= ost
->enc
;
2020 AVCodecContext
*dec
= NULL
;
2023 ret
= init_output_stream_encode(ost
);
2027 if ((ist
= get_input_stream(ost
)))
2029 if (dec
&& dec
->subtitle_header
) {
2030 ost
->enc_ctx
->subtitle_header
= av_malloc(dec
->subtitle_header_size
);
2031 if (!ost
->enc_ctx
->subtitle_header
)
2032 return AVERROR(ENOMEM
);
2033 memcpy(ost
->enc_ctx
->subtitle_header
, dec
->subtitle_header
, dec
->subtitle_header_size
);
2034 ost
->enc_ctx
->subtitle_header_size
= dec
->subtitle_header_size
;
2036 if (!av_dict_get(ost
->encoder_opts
, "threads", NULL
, 0))
2037 av_dict_set(&ost
->encoder_opts
, "threads", "auto", 0);
2039 if (ost
->filter
&& ost
->filter
->filter
->inputs
[0]->hw_frames_ctx
) {
2040 ost
->enc_ctx
->hw_frames_ctx
= av_buffer_ref(ost
->filter
->filter
->inputs
[0]->hw_frames_ctx
);
2041 if (!ost
->enc_ctx
->hw_frames_ctx
)
2042 return AVERROR(ENOMEM
);
2045 if ((ret
= avcodec_open2(ost
->enc_ctx
, codec
, &ost
->encoder_opts
)) < 0) {
2046 if (ret
== AVERROR_EXPERIMENTAL
)
2047 abort_codec_experimental(codec
, 1);
2048 snprintf(error
, error_len
,
2049 "Error while opening encoder for output stream #%d:%d - "
2050 "maybe incorrect parameters such as bit_rate, rate, width or height",
2051 ost
->file_index
, ost
->index
);
2054 assert_avoptions(ost
->encoder_opts
);
2055 if (ost
->enc_ctx
->bit_rate
&& ost
->enc_ctx
->bit_rate
< 1000)
2056 av_log(NULL
, AV_LOG_WARNING
, "The bitrate parameter is set too low."
2057 "It takes bits/s as argument, not kbits/s\n");
2059 ret
= avcodec_parameters_from_context(ost
->st
->codecpar
, ost
->enc_ctx
);
2061 av_log(NULL
, AV_LOG_FATAL
,
2062 "Error initializing the output stream codec context.\n");
2066 if (ost
->enc_ctx
->nb_coded_side_data
) {
2069 ost
->st
->side_data
= av_realloc_array(NULL
, ost
->enc_ctx
->nb_coded_side_data
,
2070 sizeof(*ost
->st
->side_data
));
2071 if (!ost
->st
->side_data
)
2072 return AVERROR(ENOMEM
);
2074 for (i
= 0; i
< ost
->enc_ctx
->nb_coded_side_data
; i
++) {
2075 const AVPacketSideData
*sd_src
= &ost
->enc_ctx
->coded_side_data
[i
];
2076 AVPacketSideData
*sd_dst
= &ost
->st
->side_data
[i
];
2078 sd_dst
->data
= av_malloc(sd_src
->size
);
2080 return AVERROR(ENOMEM
);
2081 memcpy(sd_dst
->data
, sd_src
->data
, sd_src
->size
);
2082 sd_dst
->size
= sd_src
->size
;
2083 sd_dst
->type
= sd_src
->type
;
2084 ost
->st
->nb_side_data
++;
2088 ost
->st
->time_base
= ost
->enc_ctx
->time_base
;
2089 } else if (ost
->stream_copy
) {
2090 ret
= init_output_stream_streamcopy(ost
);
2095 * FIXME: will the codec context used by the parser during streamcopy
2096 * This should go away with the new parser API.
2098 ret
= avcodec_parameters_to_context(ost
->parser_avctx
, ost
->st
->codecpar
);
2103 /* initialize bitstream filters for the output stream
2104 * needs to be done here, because the codec id for streamcopy is not
2105 * known until now */
2106 ret
= init_output_bsfs(ost
);
2110 ost
->initialized
= 1;
2112 ret
= check_init_output_file(output_files
[ost
->file_index
], ost
->file_index
);
2119 static int transcode_init(void)
2121 int ret
= 0, i
, j
, k
;
2126 /* init framerate emulation */
2127 for (i
= 0; i
< nb_input_files
; i
++) {
2128 InputFile
*ifile
= input_files
[i
];
2129 if (ifile
->rate_emu
)
2130 for (j
= 0; j
< ifile
->nb_streams
; j
++)
2131 input_streams
[j
+ ifile
->ist_index
]->start
= av_gettime_relative();
2134 /* init input streams */
2135 for (i
= 0; i
< nb_input_streams
; i
++)
2136 if ((ret
= init_input_stream(i
, error
, sizeof(error
))) < 0)
2139 /* open each encoder */
2140 for (i
= 0; i
< nb_output_streams
; i
++) {
2141 ret
= init_output_stream(output_streams
[i
], error
, sizeof(error
));
2147 /* discard unused programs */
2148 for (i
= 0; i
< nb_input_files
; i
++) {
2149 InputFile
*ifile
= input_files
[i
];
2150 for (j
= 0; j
< ifile
->ctx
->nb_programs
; j
++) {
2151 AVProgram
*p
= ifile
->ctx
->programs
[j
];
2152 int discard
= AVDISCARD_ALL
;
2154 for (k
= 0; k
< p
->nb_stream_indexes
; k
++)
2155 if (!input_streams
[ifile
->ist_index
+ p
->stream_index
[k
]]->discard
) {
2156 discard
= AVDISCARD_DEFAULT
;
2159 p
->discard
= discard
;
2164 /* dump the stream mapping */
2165 av_log(NULL
, AV_LOG_INFO
, "Stream mapping:\n");
2166 for (i
= 0; i
< nb_input_streams
; i
++) {
2167 ist
= input_streams
[i
];
2169 for (j
= 0; j
< ist
->nb_filters
; j
++) {
2170 if (!filtergraph_is_simple(ist
->filters
[j
]->graph
)) {
2171 av_log(NULL
, AV_LOG_INFO
, " Stream #%d:%d (%s) -> %s",
2172 ist
->file_index
, ist
->st
->index
, ist
->dec ? ist
->dec
->name
: "?",
2173 ist
->filters
[j
]->name
);
2174 if (nb_filtergraphs
> 1)
2175 av_log(NULL
, AV_LOG_INFO
, " (graph %d)", ist
->filters
[j
]->graph
->index
);
2176 av_log(NULL
, AV_LOG_INFO
, "\n");
2181 for (i
= 0; i
< nb_output_streams
; i
++) {
2182 ost
= output_streams
[i
];
2184 if (ost
->attachment_filename
) {
2185 /* an attached file */
2186 av_log(NULL
, AV_LOG_INFO
, " File %s -> Stream #%d:%d\n",
2187 ost
->attachment_filename
, ost
->file_index
, ost
->index
);
2191 if (ost
->filter
&& !filtergraph_is_simple(ost
->filter
->graph
)) {
2192 /* output from a complex graph */
2193 av_log(NULL
, AV_LOG_INFO
, " %s", ost
->filter
->name
);
2194 if (nb_filtergraphs
> 1)
2195 av_log(NULL
, AV_LOG_INFO
, " (graph %d)", ost
->filter
->graph
->index
);
2197 av_log(NULL
, AV_LOG_INFO
, " -> Stream #%d:%d (%s)\n", ost
->file_index
,
2198 ost
->index
, ost
->enc ? ost
->enc
->name
: "?");
2202 av_log(NULL
, AV_LOG_INFO
, " Stream #%d:%d -> #%d:%d",
2203 input_streams
[ost
->source_index
]->file_index
,
2204 input_streams
[ost
->source_index
]->st
->index
,
2207 if (ost
->sync_ist
!= input_streams
[ost
->source_index
])
2208 av_log(NULL
, AV_LOG_INFO
, " [sync #%d:%d]",
2209 ost
->sync_ist
->file_index
,
2210 ost
->sync_ist
->st
->index
);
2211 if (ost
->stream_copy
)
2212 av_log(NULL
, AV_LOG_INFO
, " (copy)");
2214 const AVCodec
*in_codec
= input_streams
[ost
->source_index
]->dec
;
2215 const AVCodec
*out_codec
= ost
->enc
;
2216 const char *decoder_name
= "?";
2217 const char *in_codec_name
= "?";
2218 const char *encoder_name
= "?";
2219 const char *out_codec_name
= "?";
2220 const AVCodecDescriptor
*desc
;
2223 decoder_name
= in_codec
->name
;
2224 desc
= avcodec_descriptor_get(in_codec
->id
);
2226 in_codec_name
= desc
->name
;
2227 if (!strcmp(decoder_name
, in_codec_name
))
2228 decoder_name
= "native";
2232 encoder_name
= out_codec
->name
;
2233 desc
= avcodec_descriptor_get(out_codec
->id
);
2235 out_codec_name
= desc
->name
;
2236 if (!strcmp(encoder_name
, out_codec_name
))
2237 encoder_name
= "native";
2240 av_log(NULL
, AV_LOG_INFO
, " (%s (%s) -> %s (%s))",
2241 in_codec_name
, decoder_name
,
2242 out_codec_name
, encoder_name
);
2244 av_log(NULL
, AV_LOG_INFO
, "\n");
2248 av_log(NULL
, AV_LOG_ERROR
, "%s\n", error
);
2255 /* Return 1 if there remain streams where more output is wanted, 0 otherwise. */
2256 static int need_output(void)
2260 for (i
= 0; i
< nb_output_streams
; i
++) {
2261 OutputStream
*ost
= output_streams
[i
];
2262 OutputFile
*of
= output_files
[ost
->file_index
];
2263 AVFormatContext
*os
= output_files
[ost
->file_index
]->ctx
;
2265 if (ost
->finished
||
2266 (os
->pb
&& avio_tell(os
->pb
) >= of
->limit_filesize
))
2268 if (ost
->frame_number
>= ost
->max_frames
) {
2270 for (j
= 0; j
< of
->ctx
->nb_streams
; j
++)
2271 output_streams
[of
->ost_index
+ j
]->finished
= 1;
2281 static InputFile
*select_input_file(void)
2283 InputFile
*ifile
= NULL
;
2284 int64_t ipts_min
= INT64_MAX
;
2287 for (i
= 0; i
< nb_input_streams
; i
++) {
2288 InputStream
*ist
= input_streams
[i
];
2289 int64_t ipts
= ist
->last_dts
;
2291 if (ist
->discard
|| input_files
[ist
->file_index
]->eagain
)
2293 if (!input_files
[ist
->file_index
]->eof_reached
) {
2294 if (ipts
< ipts_min
) {
2296 ifile
= input_files
[ist
->file_index
];
2305 static void *input_thread(void *arg
)
2310 while (!transcoding_finished
&& ret
>= 0) {
2312 ret
= av_read_frame(f
->ctx
, &pkt
);
2314 if (ret
== AVERROR(EAGAIN
)) {
2321 pthread_mutex_lock(&f
->fifo_lock
);
2322 while (!av_fifo_space(f
->fifo
))
2323 pthread_cond_wait(&f
->fifo_cond
, &f
->fifo_lock
);
2325 av_fifo_generic_write(f
->fifo
, &pkt
, sizeof(pkt
), NULL
);
2327 pthread_mutex_unlock(&f
->fifo_lock
);
2334 static void free_input_threads(void)
2338 if (nb_input_files
== 1)
2341 transcoding_finished
= 1;
2343 for (i
= 0; i
< nb_input_files
; i
++) {
2344 InputFile
*f
= input_files
[i
];
2347 if (!f
->fifo
|| f
->joined
)
2350 pthread_mutex_lock(&f
->fifo_lock
);
2351 while (av_fifo_size(f
->fifo
)) {
2352 av_fifo_generic_read(f
->fifo
, &pkt
, sizeof(pkt
), NULL
);
2353 av_packet_unref(&pkt
);
2355 pthread_cond_signal(&f
->fifo_cond
);
2356 pthread_mutex_unlock(&f
->fifo_lock
);
2358 pthread_join(f
->thread
, NULL
);
2361 while (av_fifo_size(f
->fifo
)) {
2362 av_fifo_generic_read(f
->fifo
, &pkt
, sizeof(pkt
), NULL
);
2363 av_packet_unref(&pkt
);
2365 av_fifo_free(f
->fifo
);
2369 static int init_input_threads(void)
2373 if (nb_input_files
== 1)
2376 for (i
= 0; i
< nb_input_files
; i
++) {
2377 InputFile
*f
= input_files
[i
];
2379 if (!(f
->fifo
= av_fifo_alloc(8*sizeof(AVPacket
))))
2380 return AVERROR(ENOMEM
);
2382 pthread_mutex_init(&f
->fifo_lock
, NULL
);
2383 pthread_cond_init (&f
->fifo_cond
, NULL
);
2385 if ((ret
= pthread_create(&f
->thread
, NULL
, input_thread
, f
)))
2386 return AVERROR(ret
);
2391 static int get_input_packet_mt(InputFile
*f
, AVPacket
*pkt
)
2395 pthread_mutex_lock(&f
->fifo_lock
);
2397 if (av_fifo_size(f
->fifo
)) {
2398 av_fifo_generic_read(f
->fifo
, pkt
, sizeof(*pkt
), NULL
);
2399 pthread_cond_signal(&f
->fifo_cond
);
2404 ret
= AVERROR(EAGAIN
);
2407 pthread_mutex_unlock(&f
->fifo_lock
);
2413 static int get_input_packet(InputFile
*f
, AVPacket
*pkt
)
2417 for (i
= 0; i
< f
->nb_streams
; i
++) {
2418 InputStream
*ist
= input_streams
[f
->ist_index
+ i
];
2419 int64_t pts
= av_rescale(ist
->last_dts
, 1000000, AV_TIME_BASE
);
2420 int64_t now
= av_gettime_relative() - ist
->start
;
2422 return AVERROR(EAGAIN
);
2427 if (nb_input_files
> 1)
2428 return get_input_packet_mt(f
, pkt
);
2430 return av_read_frame(f
->ctx
, pkt
);
2433 static int got_eagain(void)
2436 for (i
= 0; i
< nb_input_files
; i
++)
2437 if (input_files
[i
]->eagain
)
2442 static void reset_eagain(void)
2445 for (i
= 0; i
< nb_input_files
; i
++)
2446 input_files
[i
]->eagain
= 0;
2449 // set duration to max(tmp, duration) in a proper time base and return duration's time_base
2450 static AVRational
duration_max(int64_t tmp
, int64_t *duration
, AVRational tmp_time_base
,
2451 AVRational time_base
)
2457 return tmp_time_base
;
2460 ret
= av_compare_ts(*duration
, time_base
, tmp
, tmp_time_base
);
2463 return tmp_time_base
;
2469 static int seek_to_start(InputFile
*ifile
, AVFormatContext
*is
)
2472 AVCodecContext
*avctx
;
2473 int i
, ret
, has_audio
= 0;
2474 int64_t duration
= 0;
2476 ret
= av_seek_frame(is
, -1, is
->start_time
, 0);
2480 for (i
= 0; i
< ifile
->nb_streams
; i
++) {
2481 ist
= input_streams
[ifile
->ist_index
+ i
];
2482 avctx
= ist
->dec_ctx
;
2485 if (ist
->decoding_needed
) {
2486 process_input_packet(ist
, NULL
, 1);
2487 avcodec_flush_buffers(avctx
);
2490 /* duration is the length of the last frame in a stream
2491 * when audio stream is present we don't care about
2492 * last video frame length because it's not defined exactly */
2493 if (avctx
->codec_type
== AVMEDIA_TYPE_AUDIO
&& ist
->nb_samples
)
2497 for (i
= 0; i
< ifile
->nb_streams
; i
++) {
2498 ist
= input_streams
[ifile
->ist_index
+ i
];
2499 avctx
= ist
->dec_ctx
;
2502 if (avctx
->codec_type
== AVMEDIA_TYPE_AUDIO
&& ist
->nb_samples
) {
2503 AVRational sample_rate
= {1, avctx
->sample_rate
};
2505 duration
= av_rescale_q(ist
->nb_samples
, sample_rate
, ist
->st
->time_base
);
2509 if (ist
->framerate
.num
) {
2510 duration
= av_rescale_q(1, ist
->framerate
, ist
->st
->time_base
);
2511 } else if (ist
->st
->avg_frame_rate
.num
) {
2512 duration
= av_rescale_q(1, ist
->st
->avg_frame_rate
, ist
->st
->time_base
);
2513 } else duration
= 1;
2515 if (!ifile
->duration
)
2516 ifile
->time_base
= ist
->st
->time_base
;
2517 /* the total duration of the stream, max_pts - min_pts is
2518 * the duration of the stream without the last frame */
2519 duration
+= ist
->max_pts
- ist
->min_pts
;
2520 ifile
->time_base
= duration_max(duration
, &ifile
->duration
, ist
->st
->time_base
,
2524 if (ifile
->loop
> 0)
2531 * Read one packet from an input file and send it for
2532 * - decoding -> lavfi (audio/video)
2533 * - decoding -> encoding -> muxing (subtitles)
2534 * - muxing (streamcopy)
2537 * - 0 -- one packet was read and processed
2538 * - AVERROR(EAGAIN) -- no packets were available for selected file,
2539 * this function should be called again
2540 * - AVERROR_EOF -- this function should not be called again
2542 static int process_input(void)
2545 AVFormatContext
*is
;
2551 /* select the stream that we must read now */
2552 ifile
= select_input_file();
2553 /* if none, if is finished */
2558 return AVERROR(EAGAIN
);
2560 av_log(NULL
, AV_LOG_VERBOSE
, "No more inputs to read from.\n");
2565 ret
= get_input_packet(ifile
, &pkt
);
2567 if (ret
== AVERROR(EAGAIN
)) {
2571 if (ret
< 0 && ifile
->loop
) {
2572 if ((ret
= seek_to_start(ifile
, is
)) < 0)
2574 ret
= get_input_packet(ifile
, &pkt
);
2577 if (ret
!= AVERROR_EOF
) {
2578 print_error(is
->filename
, ret
);
2582 ifile
->eof_reached
= 1;
2584 for (i
= 0; i
< ifile
->nb_streams
; i
++) {
2585 ist
= input_streams
[ifile
->ist_index
+ i
];
2586 if (ist
->decoding_needed
)
2587 process_input_packet(ist
, NULL
, 0);
2589 /* mark all outputs that don't go through lavfi as finished */
2590 for (j
= 0; j
< nb_output_streams
; j
++) {
2591 OutputStream
*ost
= output_streams
[j
];
2593 if (ost
->source_index
== ifile
->ist_index
+ i
&&
2594 (ost
->stream_copy
|| ost
->enc
->type
== AVMEDIA_TYPE_SUBTITLE
))
2595 finish_output_stream(ost
);
2599 return AVERROR(EAGAIN
);
2605 av_pkt_dump_log2(NULL
, AV_LOG_DEBUG
, &pkt
, do_hex_dump
,
2606 is
->streams
[pkt
.stream_index
]);
2608 /* the following test is needed in case new streams appear
2609 dynamically in stream : we ignore them */
2610 if (pkt
.stream_index
>= ifile
->nb_streams
)
2611 goto discard_packet
;
2613 ist
= input_streams
[ifile
->ist_index
+ pkt
.stream_index
];
2615 ist
->data_size
+= pkt
.size
;
2619 goto discard_packet
;
2621 /* add the stream-global side data to the first packet */
2622 if (ist
->nb_packets
== 1)
2623 for (i
= 0; i
< ist
->st
->nb_side_data
; i
++) {
2624 AVPacketSideData
*src_sd
= &ist
->st
->side_data
[i
];
2627 if (av_packet_get_side_data(&pkt
, src_sd
->type
, NULL
))
2629 if (ist
->autorotate
&& src_sd
->type
== AV_PKT_DATA_DISPLAYMATRIX
)
2632 dst_data
= av_packet_new_side_data(&pkt
, src_sd
->type
, src_sd
->size
);
2636 memcpy(dst_data
, src_sd
->data
, src_sd
->size
);
2639 if (pkt
.dts
!= AV_NOPTS_VALUE
)
2640 pkt
.dts
+= av_rescale_q(ifile
->ts_offset
, AV_TIME_BASE_Q
, ist
->st
->time_base
);
2641 if (pkt
.pts
!= AV_NOPTS_VALUE
)
2642 pkt
.pts
+= av_rescale_q(ifile
->ts_offset
, AV_TIME_BASE_Q
, ist
->st
->time_base
);
2644 if (pkt
.pts
!= AV_NOPTS_VALUE
)
2645 pkt
.pts
*= ist
->ts_scale
;
2646 if (pkt
.dts
!= AV_NOPTS_VALUE
)
2647 pkt
.dts
*= ist
->ts_scale
;
2649 if ((ist
->dec_ctx
->codec_type
== AVMEDIA_TYPE_VIDEO
||
2650 ist
->dec_ctx
->codec_type
== AVMEDIA_TYPE_AUDIO
) &&
2651 pkt
.dts
!= AV_NOPTS_VALUE
&& ist
->next_dts
!= AV_NOPTS_VALUE
&&
2652 (is
->iformat
->flags
& AVFMT_TS_DISCONT
)) {
2653 int64_t pkt_dts
= av_rescale_q(pkt
.dts
, ist
->st
->time_base
, AV_TIME_BASE_Q
);
2654 int64_t delta
= pkt_dts
- ist
->next_dts
;
2656 if ((FFABS(delta
) > 1LL * dts_delta_threshold
* AV_TIME_BASE
|| pkt_dts
+ 1 < ist
->last_dts
) && !copy_ts
) {
2657 ifile
->ts_offset
-= delta
;
2658 av_log(NULL
, AV_LOG_DEBUG
,
2659 "timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
2660 delta
, ifile
->ts_offset
);
2661 pkt
.dts
-= av_rescale_q(delta
, AV_TIME_BASE_Q
, ist
->st
->time_base
);
2662 if (pkt
.pts
!= AV_NOPTS_VALUE
)
2663 pkt
.pts
-= av_rescale_q(delta
, AV_TIME_BASE_Q
, ist
->st
->time_base
);
2666 duration
= av_rescale_q(ifile
->duration
, ifile
->time_base
, ist
->st
->time_base
);
2667 if (pkt
.pts
!= AV_NOPTS_VALUE
) {
2668 pkt
.pts
+= duration
;
2669 ist
->max_pts
= FFMAX(pkt
.pts
, ist
->max_pts
);
2670 ist
->min_pts
= FFMIN(pkt
.pts
, ist
->min_pts
);
2673 if (pkt
.dts
!= AV_NOPTS_VALUE
)
2674 pkt
.dts
+= duration
;
2676 process_input_packet(ist
, &pkt
, 0);
2679 av_packet_unref(&pkt
);
2685 * The following code is the main loop of the file converter
2687 static int transcode(void)
2689 int ret
, i
, need_input
= 1;
2690 AVFormatContext
*os
;
2693 int64_t timer_start
;
2695 ret
= transcode_init();
2699 av_log(NULL
, AV_LOG_INFO
, "Press ctrl-c to stop encoding\n");
2702 timer_start
= av_gettime_relative();
2705 if ((ret
= init_input_threads()) < 0)
2709 while (!received_sigterm
) {
2710 /* check if there's any stream where output is still needed */
2711 if (!need_output()) {
2712 av_log(NULL
, AV_LOG_VERBOSE
, "No more output streams to write to, finishing.\n");
2716 /* read and process one input packet if needed */
2718 ret
= process_input();
2719 if (ret
== AVERROR_EOF
)
2723 ret
= poll_filters();
2724 if (ret
< 0 && ret
!= AVERROR_EOF
) {
2726 av_strerror(ret
, errbuf
, sizeof(errbuf
));
2728 av_log(NULL
, AV_LOG_ERROR
, "Error while filtering: %s\n", errbuf
);
2732 /* dump report by using the output first video and audio streams */
2733 print_report(0, timer_start
);
2736 free_input_threads();
2739 /* at the end of stream, we must flush the decoder buffers */
2740 for (i
= 0; i
< nb_input_streams
; i
++) {
2741 ist
= input_streams
[i
];
2742 if (!input_files
[ist
->file_index
]->eof_reached
&& ist
->decoding_needed
) {
2743 process_input_packet(ist
, NULL
, 0);
2751 /* write the trailer if needed and close file */
2752 for (i
= 0; i
< nb_output_files
; i
++) {
2753 os
= output_files
[i
]->ctx
;
2754 if (!output_files
[i
]->header_written
) {
2755 av_log(NULL
, AV_LOG_ERROR
,
2756 "Nothing was written into output file %d (%s), because "
2757 "at least one of its streams received no packets.\n",
2761 av_write_trailer(os
);
2764 /* dump report by using the first video and audio streams */
2765 print_report(1, timer_start
);
2767 /* close each encoder */
2768 for (i
= 0; i
< nb_output_streams
; i
++) {
2769 ost
= output_streams
[i
];
2770 if (ost
->encoding_needed
) {
2771 av_freep(&ost
->enc_ctx
->stats_in
);
2775 /* close each decoder */
2776 for (i
= 0; i
< nb_input_streams
; i
++) {
2777 ist
= input_streams
[i
];
2778 if (ist
->decoding_needed
) {
2779 avcodec_close(ist
->dec_ctx
);
2780 if (ist
->hwaccel_uninit
)
2781 ist
->hwaccel_uninit(ist
->dec_ctx
);
2785 av_buffer_unref(&hw_device_ctx
);
2792 free_input_threads();
2795 if (output_streams
) {
2796 for (i
= 0; i
< nb_output_streams
; i
++) {
2797 ost
= output_streams
[i
];
2800 fclose(ost
->logfile
);
2801 ost
->logfile
= NULL
;
2803 av_free(ost
->forced_kf_pts
);
2804 av_dict_free(&ost
->encoder_opts
);
2805 av_dict_free(&ost
->resample_opts
);
2812 static int64_t getutime(void)
2815 struct rusage rusage
;
2817 getrusage(RUSAGE_SELF
, &rusage
);
2818 return (rusage
.ru_utime
.tv_sec
* 1000000LL) + rusage
.ru_utime
.tv_usec
;
2819 #elif HAVE_GETPROCESSTIMES
2821 FILETIME c
, e
, k
, u
;
2822 proc
= GetCurrentProcess();
2823 GetProcessTimes(proc
, &c
, &e
, &k
, &u
);
2824 return ((int64_t) u
.dwHighDateTime
<< 32 | u
.dwLowDateTime
) / 10;
2826 return av_gettime_relative();
2830 static int64_t getmaxrss(void)
2832 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
2833 struct rusage rusage
;
2834 getrusage(RUSAGE_SELF
, &rusage
);
2835 return (int64_t)rusage
.ru_maxrss
* 1024;
2836 #elif HAVE_GETPROCESSMEMORYINFO
2838 PROCESS_MEMORY_COUNTERS memcounters
;
2839 proc
= GetCurrentProcess();
2840 memcounters
.cb
= sizeof(memcounters
);
2841 GetProcessMemoryInfo(proc
, &memcounters
, sizeof(memcounters
));
2842 return memcounters
.PeakPagefileUsage
;
2848 int main(int argc
, char **argv
)
2853 register_exit(avconv_cleanup
);
2855 av_log_set_flags(AV_LOG_SKIP_REPEATED
);
2856 parse_loglevel(argc
, argv
, options
);
2858 avcodec_register_all();
2860 avdevice_register_all();
2862 avfilter_register_all();
2864 avformat_network_init();
2868 /* parse options and open all input/output files */
2869 ret
= avconv_parse_options(argc
, argv
);
2873 if (nb_output_files
<= 0 && nb_input_files
== 0) {
2875 av_log(NULL
, AV_LOG_WARNING
, "Use -h to get full help or, even better, run 'man %s'\n", program_name
);
2879 /* file converter / grab */
2880 if (nb_output_files
<= 0) {
2881 fprintf(stderr
, "At least one output file must be specified\n");
2885 for (i
= 0; i
< nb_output_files
; i
++) {
2886 if (strcmp(output_files
[i
]->ctx
->oformat
->name
, "rtp"))
2891 if (transcode() < 0)
2893 ti
= getutime() - ti
;
2895 int maxrss
= getmaxrss() / 1024;
2896 printf("bench: utime=%0.3fs maxrss=%ikB\n", ti
/ 1000000.0, maxrss
);