avconv: init filtergraphs only after we have a frame on each input
[libav.git] / avconv.c
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"
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"
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 */
94 static int transcoding_finished;
95 #endif
96
97 InputStream **input_streams = NULL;
98 int nb_input_streams = 0;
99 InputFile **input_files = NULL;
100 int nb_input_files = 0;
101
102 OutputStream **output_streams = NULL;
103 int nb_output_streams = 0;
104 OutputFile **output_files = NULL;
105 int nb_output_files = 0;
106
107 FilterGraph **filtergraphs;
108 int nb_filtergraphs;
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
119 sigterm_handler(int sig)
120 {
121 received_sigterm = sig;
122 received_nb_signals++;
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
140 const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
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];
148 avfilter_graph_free(&fg->graph);
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 }
156 av_fifo_free(fg->inputs[j]->frame_queue);
157 av_buffer_unref(&fg->inputs[j]->hw_frames_ctx);
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);
182 avformat_free_context(s);
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);
193 av_freep(&ost->bitstream_filters);
194
195 av_frame_free(&ost->filtered_frame);
196
197 av_parser_close(ost->parser);
198 avcodec_free_context(&ost->parser_avctx);
199
200 av_freep(&ost->forced_keyframes);
201 av_freep(&ost->avfilter);
202 av_freep(&ost->logfile_prefix);
203
204 avcodec_free_context(&ost->enc_ctx);
205
206 while (av_fifo_size(ost->muxing_queue)) {
207 AVPacket pkt;
208 av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL);
209 av_packet_unref(&pkt);
210 }
211 av_fifo_free(ost->muxing_queue);
212
213 av_freep(&output_streams[i]);
214 }
215 for (i = 0; i < nb_input_files; i++) {
216 avformat_close_input(&input_files[i]->ctx);
217 av_freep(&input_files[i]);
218 }
219 for (i = 0; i < nb_input_streams; i++) {
220 InputStream *ist = input_streams[i];
221
222 av_frame_free(&ist->decoded_frame);
223 av_frame_free(&ist->filter_frame);
224 av_dict_free(&ist->decoder_opts);
225 av_freep(&ist->filters);
226 av_freep(&ist->hwaccel_device);
227
228 avcodec_free_context(&ist->dec_ctx);
229
230 av_freep(&input_streams[i]);
231 }
232
233 if (vstats_file)
234 fclose(vstats_file);
235 av_free(vstats_filename);
236
237 av_freep(&input_streams);
238 av_freep(&input_files);
239 av_freep(&output_streams);
240 av_freep(&output_files);
241
242 uninit_opts();
243
244 avformat_network_deinit();
245
246 if (received_sigterm) {
247 av_log(NULL, AV_LOG_INFO, "Received signal %d: terminating.\n",
248 (int) received_sigterm);
249 exit (255);
250 }
251 }
252
253 void assert_avoptions(AVDictionary *m)
254 {
255 AVDictionaryEntry *t;
256 if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
257 av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
258 exit_program(1);
259 }
260 }
261
262 static void abort_codec_experimental(AVCodec *c, int encoder)
263 {
264 const char *codec_string = encoder ? "encoder" : "decoder";
265 AVCodec *codec;
266 av_log(NULL, AV_LOG_FATAL, "%s '%s' is experimental and might produce bad "
267 "results.\nAdd '-strict experimental' if you want to use it.\n",
268 codec_string, c->name);
269 codec = encoder ? avcodec_find_encoder(c->id) : avcodec_find_decoder(c->id);
270 if (!(codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
271 av_log(NULL, AV_LOG_FATAL, "Or use the non experimental %s '%s'.\n",
272 codec_string, codec->name);
273 exit_program(1);
274 }
275
276 static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
277 {
278 AVFormatContext *s = of->ctx;
279 AVStream *st = ost->st;
280 int ret;
281
282 if (!of->header_written) {
283 AVPacket tmp_pkt;
284 /* the muxer is not initialized yet, buffer the packet */
285 if (!av_fifo_space(ost->muxing_queue)) {
286 int new_size = FFMIN(2 * av_fifo_size(ost->muxing_queue),
287 ost->max_muxing_queue_size);
288 if (new_size <= av_fifo_size(ost->muxing_queue)) {
289 av_log(NULL, AV_LOG_ERROR,
290 "Too many packets buffered for output stream %d:%d.\n",
291 ost->file_index, ost->st->index);
292 exit_program(1);
293 }
294 ret = av_fifo_realloc2(ost->muxing_queue, new_size);
295 if (ret < 0)
296 exit_program(1);
297 }
298 av_packet_move_ref(&tmp_pkt, pkt);
299 av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL);
300 return;
301 }
302
303 /*
304 * Audio encoders may split the packets -- #frames in != #packets out.
305 * But there is no reordering, so we can limit the number of output packets
306 * by simply dropping them here.
307 * Counting encoded video frames needs to be done separately because of
308 * reordering, see do_video_out()
309 */
310 if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) {
311 if (ost->frame_number >= ost->max_frames) {
312 av_packet_unref(pkt);
313 return;
314 }
315 ost->frame_number++;
316 }
317 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
318 uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_FACTOR,
319 NULL);
320 ost->quality = sd ? *(int *)sd : -1;
321
322 if (ost->frame_rate.num) {
323 pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate),
324 ost->mux_timebase);
325 }
326 }
327
328 if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
329 ost->last_mux_dts != AV_NOPTS_VALUE &&
330 pkt->dts < ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT)) {
331 av_log(NULL, AV_LOG_WARNING, "Non-monotonous DTS in output stream "
332 "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
333 ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts);
334 if (exit_on_error) {
335 av_log(NULL, AV_LOG_FATAL, "aborting.\n");
336 exit_program(1);
337 }
338 av_log(NULL, AV_LOG_WARNING, "changing to %"PRId64". This may result "
339 "in incorrect timestamps in the output file.\n",
340 ost->last_mux_dts + 1);
341 pkt->dts = ost->last_mux_dts + 1;
342 if (pkt->pts != AV_NOPTS_VALUE)
343 pkt->pts = FFMAX(pkt->pts, pkt->dts);
344 }
345 ost->last_mux_dts = pkt->dts;
346
347 ost->data_size += pkt->size;
348 ost->packets_written++;
349
350 pkt->stream_index = ost->index;
351 av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base);
352
353 ret = av_interleaved_write_frame(s, pkt);
354 if (ret < 0) {
355 print_error("av_interleaved_write_frame()", ret);
356 exit_program(1);
357 }
358 }
359
360 static void output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
361 {
362 int ret = 0;
363
364 /* apply the output bitstream filters, if any */
365 if (ost->nb_bitstream_filters) {
366 int idx;
367
368 ret = av_bsf_send_packet(ost->bsf_ctx[0], pkt);
369 if (ret < 0)
370 goto finish;
371
372 idx = 1;
373 while (idx) {
374 /* get a packet from the previous filter up the chain */
375 ret = av_bsf_receive_packet(ost->bsf_ctx[idx - 1], pkt);
376 if (ret == AVERROR(EAGAIN)) {
377 ret = 0;
378 idx--;
379 continue;
380 } else if (ret < 0)
381 goto finish;
382
383 /* send it to the next filter down the chain or to the muxer */
384 if (idx < ost->nb_bitstream_filters) {
385 ret = av_bsf_send_packet(ost->bsf_ctx[idx], pkt);
386 if (ret < 0)
387 goto finish;
388 idx++;
389 } else
390 write_packet(of, pkt, ost);
391 }
392 } else
393 write_packet(of, pkt, ost);
394
395 finish:
396 if (ret < 0 && ret != AVERROR_EOF) {
397 av_log(NULL, AV_LOG_FATAL, "Error applying bitstream filters to an output "
398 "packet for stream #%d:%d.\n", ost->file_index, ost->index);
399 exit_program(1);
400 }
401 }
402
403 static int check_recording_time(OutputStream *ost)
404 {
405 OutputFile *of = output_files[ost->file_index];
406
407 if (of->recording_time != INT64_MAX &&
408 av_compare_ts(ost->sync_opts - ost->first_pts, ost->enc_ctx->time_base, of->recording_time,
409 AV_TIME_BASE_Q) >= 0) {
410 ost->finished = 1;
411 return 0;
412 }
413 return 1;
414 }
415
416 static void do_audio_out(OutputFile *of, OutputStream *ost,
417 AVFrame *frame)
418 {
419 AVCodecContext *enc = ost->enc_ctx;
420 AVPacket pkt;
421 int ret;
422
423 av_init_packet(&pkt);
424 pkt.data = NULL;
425 pkt.size = 0;
426
427 if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
428 frame->pts = ost->sync_opts;
429 ost->sync_opts = frame->pts + frame->nb_samples;
430
431 ost->samples_encoded += frame->nb_samples;
432 ost->frames_encoded++;
433
434 ret = avcodec_send_frame(enc, frame);
435 if (ret < 0)
436 goto error;
437
438 while (1) {
439 ret = avcodec_receive_packet(enc, &pkt);
440 if (ret == AVERROR(EAGAIN))
441 break;
442 if (ret < 0)
443 goto error;
444
445 output_packet(of, &pkt, ost);
446 }
447
448 return;
449 error:
450 av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
451 exit_program(1);
452 }
453
454 static void do_subtitle_out(OutputFile *of,
455 OutputStream *ost,
456 InputStream *ist,
457 AVSubtitle *sub,
458 int64_t pts)
459 {
460 static uint8_t *subtitle_out = NULL;
461 int subtitle_out_max_size = 1024 * 1024;
462 int subtitle_out_size, nb, i;
463 AVCodecContext *enc;
464 AVPacket pkt;
465
466 if (pts == AV_NOPTS_VALUE) {
467 av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
468 if (exit_on_error)
469 exit_program(1);
470 return;
471 }
472
473 enc = ost->enc_ctx;
474
475 if (!subtitle_out) {
476 subtitle_out = av_malloc(subtitle_out_max_size);
477 }
478
479 /* Note: DVB subtitle need one packet to draw them and one other
480 packet to clear them */
481 /* XXX: signal it in the codec context ? */
482 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
483 nb = 2;
484 else
485 nb = 1;
486
487 for (i = 0; i < nb; i++) {
488 ost->sync_opts = av_rescale_q(pts, ist->st->time_base, enc->time_base);
489 if (!check_recording_time(ost))
490 return;
491
492 sub->pts = av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q);
493 // start_display_time is required to be 0
494 sub->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
495 sub->end_display_time -= sub->start_display_time;
496 sub->start_display_time = 0;
497
498 ost->frames_encoded++;
499
500 subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
501 subtitle_out_max_size, sub);
502 if (subtitle_out_size < 0) {
503 av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
504 exit_program(1);
505 }
506
507 av_init_packet(&pkt);
508 pkt.data = subtitle_out;
509 pkt.size = subtitle_out_size;
510 pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->mux_timebase);
511 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
512 /* XXX: the pts correction is handled here. Maybe handling
513 it in the codec would be better */
514 if (i == 0)
515 pkt.pts += 90 * sub->start_display_time;
516 else
517 pkt.pts += 90 * sub->end_display_time;
518 }
519 output_packet(of, &pkt, ost);
520 }
521 }
522
523 static void do_video_out(OutputFile *of,
524 OutputStream *ost,
525 AVFrame *in_picture,
526 int *frame_size)
527 {
528 int ret, format_video_sync;
529 AVPacket pkt;
530 AVCodecContext *enc = ost->enc_ctx;
531
532 *frame_size = 0;
533
534 format_video_sync = video_sync_method;
535 if (format_video_sync == VSYNC_AUTO)
536 format_video_sync = (of->ctx->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH :
537 (of->ctx->oformat->flags & AVFMT_VARIABLE_FPS) ? VSYNC_VFR : VSYNC_CFR;
538 if (format_video_sync != VSYNC_PASSTHROUGH &&
539 ost->frame_number &&
540 in_picture->pts != AV_NOPTS_VALUE &&
541 in_picture->pts < ost->sync_opts) {
542 nb_frames_drop++;
543 av_log(NULL, AV_LOG_WARNING,
544 "*** dropping frame %d from stream %d at ts %"PRId64"\n",
545 ost->frame_number, ost->st->index, in_picture->pts);
546 return;
547 }
548
549 if (in_picture->pts == AV_NOPTS_VALUE)
550 in_picture->pts = ost->sync_opts;
551 ost->sync_opts = in_picture->pts;
552
553
554 if (!ost->frame_number)
555 ost->first_pts = in_picture->pts;
556
557 av_init_packet(&pkt);
558 pkt.data = NULL;
559 pkt.size = 0;
560
561 if (ost->frame_number >= ost->max_frames)
562 return;
563
564 if (enc->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) &&
565 ost->top_field_first >= 0)
566 in_picture->top_field_first = !!ost->top_field_first;
567
568 in_picture->quality = enc->global_quality;
569 in_picture->pict_type = 0;
570 if (ost->forced_kf_index < ost->forced_kf_count &&
571 in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
572 in_picture->pict_type = AV_PICTURE_TYPE_I;
573 ost->forced_kf_index++;
574 }
575
576 ost->frames_encoded++;
577
578 ret = avcodec_send_frame(enc, in_picture);
579 if (ret < 0)
580 goto error;
581
582 /*
583 * For video, there may be reordering, so we can't throw away frames on
584 * encoder flush, we need to limit them here, before they go into encoder.
585 */
586 ost->frame_number++;
587
588 while (1) {
589 ret = avcodec_receive_packet(enc, &pkt);
590 if (ret == AVERROR(EAGAIN))
591 break;
592 if (ret < 0)
593 goto error;
594
595 output_packet(of, &pkt, ost);
596 *frame_size = pkt.size;
597
598 /* if two pass, output log */
599 if (ost->logfile && enc->stats_out) {
600 fprintf(ost->logfile, "%s", enc->stats_out);
601 }
602
603 ost->sync_opts++;
604 }
605
606 return;
607 error:
608 av_assert0(ret != AVERROR(EAGAIN) && ret != AVERROR_EOF);
609 av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
610 exit_program(1);
611 }
612
613 #if FF_API_CODED_FRAME && FF_API_ERROR_FRAME
614 static double psnr(double d)
615 {
616 return -10.0 * log(d) / log(10.0);
617 }
618 #endif
619
620 static void do_video_stats(OutputStream *ost, int frame_size)
621 {
622 AVCodecContext *enc;
623 int frame_number;
624 double ti1, bitrate, avg_bitrate;
625
626 /* this is executed just the first time do_video_stats is called */
627 if (!vstats_file) {
628 vstats_file = fopen(vstats_filename, "w");
629 if (!vstats_file) {
630 perror("fopen");
631 exit_program(1);
632 }
633 }
634
635 enc = ost->enc_ctx;
636 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
637 frame_number = ost->frame_number;
638 fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
639 ost->quality / (float)FF_QP2LAMBDA);
640
641 #if FF_API_CODED_FRAME && FF_API_ERROR_FRAME
642 FF_DISABLE_DEPRECATION_WARNINGS
643 if (enc->flags & AV_CODEC_FLAG_PSNR)
644 fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
645 FF_ENABLE_DEPRECATION_WARNINGS
646 #endif
647
648 fprintf(vstats_file,"f_size= %6d ", frame_size);
649 /* compute pts value */
650 ti1 = ost->sync_opts * av_q2d(enc->time_base);
651 if (ti1 < 0.01)
652 ti1 = 0.01;
653
654 bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
655 avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
656 fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
657 (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
658 #if FF_API_CODED_FRAME
659 FF_DISABLE_DEPRECATION_WARNINGS
660 fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type));
661 FF_ENABLE_DEPRECATION_WARNINGS
662 #endif
663 }
664 }
665
666 static int init_output_stream(OutputStream *ost, char *error, int error_len);
667
668 /*
669 * Read one frame for lavfi output for ost and encode it.
670 */
671 static int poll_filter(OutputStream *ost)
672 {
673 OutputFile *of = output_files[ost->file_index];
674 AVFrame *filtered_frame = NULL;
675 int frame_size, ret;
676
677 if (!ost->filtered_frame && !(ost->filtered_frame = av_frame_alloc())) {
678 return AVERROR(ENOMEM);
679 }
680 filtered_frame = ost->filtered_frame;
681
682 if (!ost->initialized) {
683 char error[1024];
684 ret = init_output_stream(ost, error, sizeof(error));
685 if (ret < 0) {
686 av_log(NULL, AV_LOG_ERROR, "Error initializing output stream %d:%d -- %s\n",
687 ost->file_index, ost->index, error);
688 exit_program(1);
689 }
690 }
691
692 if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
693 !(ost->enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
694 ret = av_buffersink_get_samples(ost->filter->filter, filtered_frame,
695 ost->enc_ctx->frame_size);
696 else
697 ret = av_buffersink_get_frame(ost->filter->filter, filtered_frame);
698
699 if (ret < 0)
700 return ret;
701
702 if (filtered_frame->pts != AV_NOPTS_VALUE) {
703 int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
704 filtered_frame->pts = av_rescale_q(filtered_frame->pts,
705 ost->filter->filter->inputs[0]->time_base,
706 ost->enc_ctx->time_base) -
707 av_rescale_q(start_time,
708 AV_TIME_BASE_Q,
709 ost->enc_ctx->time_base);
710 }
711
712 switch (ost->filter->filter->inputs[0]->type) {
713 case AVMEDIA_TYPE_VIDEO:
714 if (!ost->frame_aspect_ratio)
715 ost->enc_ctx->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
716
717 do_video_out(of, ost, filtered_frame, &frame_size);
718 if (vstats_filename && frame_size)
719 do_video_stats(ost, frame_size);
720 break;
721 case AVMEDIA_TYPE_AUDIO:
722 do_audio_out(of, ost, filtered_frame);
723 break;
724 default:
725 // TODO support subtitle filters
726 av_assert0(0);
727 }
728
729 av_frame_unref(filtered_frame);
730
731 return 0;
732 }
733
734 static void finish_output_stream(OutputStream *ost)
735 {
736 OutputFile *of = output_files[ost->file_index];
737 int i;
738
739 ost->finished = 1;
740
741 if (of->shortest) {
742 for (i = 0; i < of->ctx->nb_streams; i++)
743 output_streams[of->ost_index + i]->finished = 1;
744 }
745 }
746
747 /*
748 * Read as many frames from possible from lavfi and encode them.
749 *
750 * Always read from the active stream with the lowest timestamp. If no frames
751 * are available for it then return EAGAIN and wait for more input. This way we
752 * can use lavfi sources that generate unlimited amount of frames without memory
753 * usage exploding.
754 */
755 static int poll_filters(void)
756 {
757 int i, ret = 0;
758
759 while (ret >= 0 && !received_sigterm) {
760 OutputStream *ost = NULL;
761 int64_t min_pts = INT64_MAX;
762
763 /* choose output stream with the lowest timestamp */
764 for (i = 0; i < nb_output_streams; i++) {
765 int64_t pts = output_streams[i]->sync_opts;
766
767 if (!output_streams[i]->filter || output_streams[i]->finished ||
768 !output_streams[i]->filter->graph->graph)
769 continue;
770
771 pts = av_rescale_q(pts, output_streams[i]->enc_ctx->time_base,
772 AV_TIME_BASE_Q);
773 if (pts < min_pts) {
774 min_pts = pts;
775 ost = output_streams[i];
776 }
777 }
778
779 if (!ost)
780 break;
781
782 ret = poll_filter(ost);
783
784 if (ret == AVERROR_EOF) {
785 finish_output_stream(ost);
786 ret = 0;
787 } else if (ret == AVERROR(EAGAIN))
788 return 0;
789 }
790
791 return ret;
792 }
793
794 static void print_final_stats(int64_t total_size)
795 {
796 uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
797 uint64_t data_size = 0;
798 float percent = -1.0;
799 int i, j;
800
801 for (i = 0; i < nb_output_streams; i++) {
802 OutputStream *ost = output_streams[i];
803 switch (ost->enc_ctx->codec_type) {
804 case AVMEDIA_TYPE_VIDEO: video_size += ost->data_size; break;
805 case AVMEDIA_TYPE_AUDIO: audio_size += ost->data_size; break;
806 default: other_size += ost->data_size; break;
807 }
808 extra_size += ost->enc_ctx->extradata_size;
809 data_size += ost->data_size;
810 }
811
812 if (data_size && total_size >= data_size)
813 percent = 100.0 * (total_size - data_size) / data_size;
814
815 av_log(NULL, AV_LOG_INFO, "\n");
816 av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ",
817 video_size / 1024.0,
818 audio_size / 1024.0,
819 other_size / 1024.0,
820 extra_size / 1024.0);
821 if (percent >= 0.0)
822 av_log(NULL, AV_LOG_INFO, "%f%%", percent);
823 else
824 av_log(NULL, AV_LOG_INFO, "unknown");
825 av_log(NULL, AV_LOG_INFO, "\n");
826
827 /* print verbose per-stream stats */
828 for (i = 0; i < nb_input_files; i++) {
829 InputFile *f = input_files[i];
830 uint64_t total_packets = 0, total_size = 0;
831
832 av_log(NULL, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
833 i, f->ctx->filename);
834
835 for (j = 0; j < f->nb_streams; j++) {
836 InputStream *ist = input_streams[f->ist_index + j];
837 enum AVMediaType type = ist->dec_ctx->codec_type;
838
839 total_size += ist->data_size;
840 total_packets += ist->nb_packets;
841
842 av_log(NULL, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
843 i, j, media_type_string(type));
844 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
845 ist->nb_packets, ist->data_size);
846
847 if (ist->decoding_needed) {
848 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames decoded",
849 ist->frames_decoded);
850 if (type == AVMEDIA_TYPE_AUDIO)
851 av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
852 av_log(NULL, AV_LOG_VERBOSE, "; ");
853 }
854
855 av_log(NULL, AV_LOG_VERBOSE, "\n");
856 }
857
858 av_log(NULL, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
859 total_packets, total_size);
860 }
861
862 for (i = 0; i < nb_output_files; i++) {
863 OutputFile *of = output_files[i];
864 uint64_t total_packets = 0, total_size = 0;
865
866 av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
867 i, of->ctx->filename);
868
869 for (j = 0; j < of->ctx->nb_streams; j++) {
870 OutputStream *ost = output_streams[of->ost_index + j];
871 enum AVMediaType type = ost->enc_ctx->codec_type;
872
873 total_size += ost->data_size;
874 total_packets += ost->packets_written;
875
876 av_log(NULL, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ",
877 i, j, media_type_string(type));
878 if (ost->encoding_needed) {
879 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
880 ost->frames_encoded);
881 if (type == AVMEDIA_TYPE_AUDIO)
882 av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
883 av_log(NULL, AV_LOG_VERBOSE, "; ");
884 }
885
886 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
887 ost->packets_written, ost->data_size);
888
889 av_log(NULL, AV_LOG_VERBOSE, "\n");
890 }
891
892 av_log(NULL, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
893 total_packets, total_size);
894 }
895 }
896
897 static void print_report(int is_last_report, int64_t timer_start)
898 {
899 char buf[1024];
900 OutputStream *ost;
901 AVFormatContext *oc;
902 int64_t total_size;
903 AVCodecContext *enc;
904 int frame_number, vid, i;
905 double bitrate, ti1, pts;
906 static int64_t last_time = -1;
907 static int qp_histogram[52];
908
909 if (!print_stats && !is_last_report)
910 return;
911
912 if (!is_last_report) {
913 int64_t cur_time;
914 /* display the report every 0.5 seconds */
915 cur_time = av_gettime_relative();
916 if (last_time == -1) {
917 last_time = cur_time;
918 return;
919 }
920 if ((cur_time - last_time) < 500000)
921 return;
922 last_time = cur_time;
923 }
924
925
926 oc = output_files[0]->ctx;
927
928 total_size = avio_size(oc->pb);
929 if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too
930 total_size = avio_tell(oc->pb);
931 if (total_size < 0) {
932 char errbuf[128];
933 av_strerror(total_size, errbuf, sizeof(errbuf));
934 av_log(NULL, AV_LOG_VERBOSE, "Bitrate not available, "
935 "avio_tell() failed: %s\n", errbuf);
936 total_size = 0;
937 }
938
939 buf[0] = '\0';
940 ti1 = 1e10;
941 vid = 0;
942 for (i = 0; i < nb_output_streams; i++) {
943 float q = -1;
944 ost = output_streams[i];
945 enc = ost->enc_ctx;
946 if (!ost->stream_copy)
947 q = ost->quality / (float) FF_QP2LAMBDA;
948
949 if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
950 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q);
951 }
952 if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
953 float t = (av_gettime_relative() - timer_start) / 1000000.0;
954
955 frame_number = ost->frame_number;
956 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3d q=%3.1f ",
957 frame_number, (t > 1) ? (int)(frame_number / t + 0.5) : 0, q);
958 if (is_last_report)
959 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
960 if (qp_hist) {
961 int j;
962 int qp = lrintf(q);
963 if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
964 qp_histogram[qp]++;
965 for (j = 0; j < 32; j++)
966 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log2(qp_histogram[j] + 1)));
967 }
968
969 #if FF_API_CODED_FRAME && FF_API_ERROR_FRAME
970 FF_DISABLE_DEPRECATION_WARNINGS
971 if (enc->flags & AV_CODEC_FLAG_PSNR) {
972 int j;
973 double error, error_sum = 0;
974 double scale, scale_sum = 0;
975 char type[3] = { 'Y','U','V' };
976 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
977 for (j = 0; j < 3; j++) {
978 if (is_last_report) {
979 error = enc->error[j];
980 scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
981 } else {
982 error = enc->coded_frame->error[j];
983 scale = enc->width * enc->height * 255.0 * 255.0;
984 }
985 if (j)
986 scale /= 4;
987 error_sum += error;
988 scale_sum += scale;
989 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error / scale));
990 }
991 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum / scale_sum));
992 }
993 FF_ENABLE_DEPRECATION_WARNINGS
994 #endif
995 vid = 1;
996 }
997 /* compute min output value */
998 pts = (double)ost->last_mux_dts * av_q2d(ost->mux_timebase);
999 if ((pts < ti1) && (pts > 0))
1000 ti1 = pts;
1001 }
1002 if (ti1 < 0.01)
1003 ti1 = 0.01;
1004
1005 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1006
1007 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1008 "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
1009 (double)total_size / 1024, ti1, bitrate);
1010
1011 if (nb_frames_drop)
1012 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " drop=%d",
1013 nb_frames_drop);
1014
1015 av_log(NULL, AV_LOG_INFO, "%s \r", buf);
1016
1017 fflush(stderr);
1018
1019 if (is_last_report)
1020 print_final_stats(total_size);
1021
1022 }
1023
1024 static void flush_encoders(void)
1025 {
1026 int i, ret;
1027
1028 for (i = 0; i < nb_output_streams; i++) {
1029 OutputStream *ost = output_streams[i];
1030 AVCodecContext *enc = ost->enc_ctx;
1031 OutputFile *of = output_files[ost->file_index];
1032 int stop_encoding = 0;
1033
1034 if (!ost->encoding_needed)
1035 continue;
1036
1037 if (enc->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
1038 continue;
1039
1040 if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
1041 continue;
1042
1043 avcodec_send_frame(enc, NULL);
1044
1045 for (;;) {
1046 const char *desc = NULL;
1047
1048 switch (enc->codec_type) {
1049 case AVMEDIA_TYPE_AUDIO:
1050 desc = "Audio";
1051 break;
1052 case AVMEDIA_TYPE_VIDEO:
1053 desc = "Video";
1054 break;
1055 default:
1056 av_assert0(0);
1057 }
1058
1059 if (1) {
1060 AVPacket pkt;
1061 av_init_packet(&pkt);
1062 pkt.data = NULL;
1063 pkt.size = 0;
1064
1065 ret = avcodec_receive_packet(enc, &pkt);
1066 if (ret < 0 && ret != AVERROR_EOF) {
1067 av_log(NULL, AV_LOG_FATAL, "%s encoding failed\n", desc);
1068 exit_program(1);
1069 }
1070 if (ost->logfile && enc->stats_out) {
1071 fprintf(ost->logfile, "%s", enc->stats_out);
1072 }
1073 if (ret == AVERROR_EOF) {
1074 stop_encoding = 1;
1075 break;
1076 }
1077 output_packet(of, &pkt, ost);
1078 }
1079
1080 if (stop_encoding)
1081 break;
1082 }
1083 }
1084 }
1085
1086 /*
1087 * Check whether a packet from ist should be written into ost at this time
1088 */
1089 static int check_output_constraints(InputStream *ist, OutputStream *ost)
1090 {
1091 OutputFile *of = output_files[ost->file_index];
1092 int ist_index = input_files[ist->file_index]->ist_index + ist->st->index;
1093
1094 if (ost->source_index != ist_index)
1095 return 0;
1096
1097 if (of->start_time != AV_NOPTS_VALUE && ist->last_dts < of->start_time)
1098 return 0;
1099
1100 return 1;
1101 }
1102
1103 static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
1104 {
1105 OutputFile *of = output_files[ost->file_index];
1106 InputFile *f = input_files [ist->file_index];
1107 int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
1108 int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase);
1109 AVPacket opkt;
1110
1111 av_init_packet(&opkt);
1112
1113 if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
1114 !ost->copy_initial_nonkeyframes)
1115 return;
1116
1117 if (of->recording_time != INT64_MAX &&
1118 ist->last_dts >= of->recording_time + start_time) {
1119 ost->finished = 1;
1120 return;
1121 }
1122
1123 if (f->recording_time != INT64_MAX) {
1124 start_time = f->ctx->start_time;
1125 if (f->start_time != AV_NOPTS_VALUE)
1126 start_time += f->start_time;
1127 if (ist->last_dts >= f->recording_time + start_time) {
1128 ost->finished = 1;
1129 return;
1130 }
1131 }
1132
1133 /* force the input stream PTS */
1134 if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
1135 ost->sync_opts++;
1136
1137 if (pkt->pts != AV_NOPTS_VALUE)
1138 opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->mux_timebase) - ost_tb_start_time;
1139 else
1140 opkt.pts = AV_NOPTS_VALUE;
1141
1142 if (pkt->dts == AV_NOPTS_VALUE)
1143 opkt.dts = av_rescale_q(ist->last_dts, AV_TIME_BASE_Q, ost->mux_timebase);
1144 else
1145 opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->mux_timebase);
1146 opkt.dts -= ost_tb_start_time;
1147
1148 opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->mux_timebase);
1149 opkt.flags = pkt->flags;
1150
1151 // FIXME remove the following 2 lines they shall be replaced by the bitstream filters
1152 if ( ost->enc_ctx->codec_id != AV_CODEC_ID_H264
1153 && ost->enc_ctx->codec_id != AV_CODEC_ID_MPEG1VIDEO
1154 && ost->enc_ctx->codec_id != AV_CODEC_ID_MPEG2VIDEO
1155 && ost->enc_ctx->codec_id != AV_CODEC_ID_VC1
1156 ) {
1157 if (av_parser_change(ost->parser, ost->parser_avctx,
1158 &opkt.data, &opkt.size,
1159 pkt->data, pkt->size,
1160 pkt->flags & AV_PKT_FLAG_KEY)) {
1161 opkt.buf = av_buffer_create(opkt.data, opkt.size, av_buffer_default_free, NULL, 0);
1162 if (!opkt.buf)
1163 exit_program(1);
1164 }
1165 } else {
1166 opkt.data = pkt->data;
1167 opkt.size = pkt->size;
1168 }
1169
1170 output_packet(of, &opkt, ost);
1171 }
1172
1173 static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame)
1174 {
1175 FilterGraph *fg = ifilter->graph;
1176 int need_reinit, ret, i;
1177
1178 /* determine if the parameters for this input changed */
1179 need_reinit = ifilter->format != frame->format;
1180 if (!!ifilter->hw_frames_ctx != !!frame->hw_frames_ctx ||
1181 (ifilter->hw_frames_ctx && ifilter->hw_frames_ctx->data != frame->hw_frames_ctx->data))
1182 need_reinit = 1;
1183
1184 switch (ifilter->ist->st->codecpar->codec_type) {
1185 case AVMEDIA_TYPE_AUDIO:
1186 need_reinit |= ifilter->sample_rate != frame->sample_rate ||
1187 ifilter->channel_layout != frame->channel_layout;
1188 break;
1189 case AVMEDIA_TYPE_VIDEO:
1190 need_reinit |= ifilter->width != frame->width ||
1191 ifilter->height != frame->height;
1192 break;
1193 }
1194
1195 if (need_reinit) {
1196 ret = ifilter_parameters_from_frame(ifilter, frame);
1197 if (ret < 0)
1198 return ret;
1199 }
1200
1201 /* (re)init the graph if possible, otherwise buffer the frame and return */
1202 if (need_reinit || !fg->graph) {
1203 for (i = 0; i < fg->nb_inputs; i++) {
1204 if (fg->inputs[i]->format < 0) {
1205 AVFrame *tmp = av_frame_clone(frame);
1206 if (!tmp)
1207 return AVERROR(ENOMEM);
1208 av_frame_unref(frame);
1209
1210 if (!av_fifo_space(ifilter->frame_queue)) {
1211 ret = av_fifo_realloc2(ifilter->frame_queue, 2 * av_fifo_size(ifilter->frame_queue));
1212 if (ret < 0)
1213 return ret;
1214 }
1215 av_fifo_generic_write(ifilter->frame_queue, &tmp, sizeof(tmp), NULL);
1216 return 0;
1217 }
1218 }
1219
1220 ret = poll_filters();
1221 if (ret < 0 && ret != AVERROR_EOF) {
1222 char errbuf[128];
1223 av_strerror(ret, errbuf, sizeof(errbuf));
1224
1225 av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", errbuf);
1226 return ret;
1227 }
1228
1229 ret = configure_filtergraph(fg);
1230 if (ret < 0) {
1231 av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
1232 return ret;
1233 }
1234
1235 for (i = 0; i < fg->nb_inputs; i++) {
1236 while (av_fifo_size(fg->inputs[i]->frame_queue)) {
1237 AVFrame *tmp;
1238 av_fifo_generic_read(fg->inputs[i]->frame_queue, &tmp, sizeof(tmp), NULL);
1239 ret = av_buffersrc_add_frame(fg->inputs[i]->filter, tmp);
1240 av_frame_free(&tmp);
1241 if (ret < 0)
1242 return ret;
1243 }
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 // This does not quite work like avcodec_decode_audio4/avcodec_decode_video2.
1257 // There is the following difference: if you got a frame, you must call
1258 // it again with pkt=NULL. pkt==NULL is treated differently from pkt.size==0
1259 // (pkt==NULL means get more output, pkt.size==0 is a flush/drain packet)
1260 static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
1261 {
1262 int ret;
1263
1264 *got_frame = 0;
1265
1266 if (pkt) {
1267 ret = avcodec_send_packet(avctx, pkt);
1268 // In particular, we don't expect AVERROR(EAGAIN), because we read all
1269 // decoded frames with avcodec_receive_frame() until done.
1270 if (ret < 0)
1271 return ret == AVERROR_EOF ? 0 : ret;
1272 }
1273
1274 ret = avcodec_receive_frame(avctx, frame);
1275 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
1276 return ret;
1277 if (ret >= 0)
1278 *got_frame = 1;
1279
1280 return 0;
1281 }
1282
1283 int guess_input_channel_layout(InputStream *ist)
1284 {
1285 AVCodecContext *dec = ist->dec_ctx;
1286
1287 if (!dec->channel_layout) {
1288 char layout_name[256];
1289
1290 dec->channel_layout = av_get_default_channel_layout(dec->channels);
1291 if (!dec->channel_layout)
1292 return 0;
1293 av_get_channel_layout_string(layout_name, sizeof(layout_name),
1294 dec->channels, dec->channel_layout);
1295 av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for Input Stream "
1296 "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
1297 }
1298 return 1;
1299 }
1300
1301 static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
1302 {
1303 AVFrame *decoded_frame, *f;
1304 AVCodecContext *avctx = ist->dec_ctx;
1305 int i, ret, err = 0;
1306
1307 if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
1308 return AVERROR(ENOMEM);
1309 if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
1310 return AVERROR(ENOMEM);
1311 decoded_frame = ist->decoded_frame;
1312
1313 ret = decode(avctx, decoded_frame, got_output, pkt);
1314 if (!*got_output || ret < 0)
1315 return ret;
1316
1317 ist->samples_decoded += decoded_frame->nb_samples;
1318 ist->frames_decoded++;
1319
1320 /* if the decoder provides a pts, use it instead of the last packet pts.
1321 the decoder could be delaying output by a packet or more. */
1322 if (decoded_frame->pts != AV_NOPTS_VALUE)
1323 ist->next_dts = decoded_frame->pts;
1324 else if (pkt && pkt->pts != AV_NOPTS_VALUE) {
1325 decoded_frame->pts = pkt->pts;
1326 }
1327
1328 if (decoded_frame->pts != AV_NOPTS_VALUE)
1329 decoded_frame->pts = av_rescale_q(decoded_frame->pts,
1330 ist->st->time_base,
1331 (AVRational){1, avctx->sample_rate});
1332 ist->nb_samples = decoded_frame->nb_samples;
1333 for (i = 0; i < ist->nb_filters; i++) {
1334 if (i < ist->nb_filters - 1) {
1335 f = ist->filter_frame;
1336 err = av_frame_ref(f, decoded_frame);
1337 if (err < 0)
1338 break;
1339 } else
1340 f = decoded_frame;
1341
1342 err = ifilter_send_frame(ist->filters[i], f);
1343 if (err < 0)
1344 break;
1345 }
1346
1347 av_frame_unref(ist->filter_frame);
1348 av_frame_unref(decoded_frame);
1349 return err < 0 ? err : ret;
1350 }
1351
1352 static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
1353 {
1354 AVFrame *decoded_frame, *f;
1355 int i, ret = 0, err = 0;
1356
1357 if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
1358 return AVERROR(ENOMEM);
1359 if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
1360 return AVERROR(ENOMEM);
1361 decoded_frame = ist->decoded_frame;
1362
1363 ret = decode(ist->dec_ctx, decoded_frame, got_output, pkt);
1364 if (!*got_output || ret < 0)
1365 return ret;
1366
1367 ist->frames_decoded++;
1368
1369 if (ist->hwaccel_retrieve_data && decoded_frame->format == ist->hwaccel_pix_fmt) {
1370 err = ist->hwaccel_retrieve_data(ist->dec_ctx, decoded_frame);
1371 if (err < 0)
1372 goto fail;
1373 }
1374 ist->hwaccel_retrieved_pix_fmt = decoded_frame->format;
1375
1376 decoded_frame->pts = guess_correct_pts(&ist->pts_ctx, decoded_frame->pts,
1377 decoded_frame->pkt_dts);
1378
1379 if (ist->st->sample_aspect_ratio.num)
1380 decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
1381
1382 for (i = 0; i < ist->nb_filters; i++) {
1383 if (i < ist->nb_filters - 1) {
1384 f = ist->filter_frame;
1385 err = av_frame_ref(f, decoded_frame);
1386 if (err < 0)
1387 break;
1388 } else
1389 f = decoded_frame;
1390
1391 err = ifilter_send_frame(ist->filters[i], f);
1392 if (err < 0)
1393 break;
1394 }
1395
1396 fail:
1397 av_frame_unref(ist->filter_frame);
1398 av_frame_unref(decoded_frame);
1399 return err < 0 ? err : ret;
1400 }
1401
1402 static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
1403 {
1404 AVSubtitle subtitle;
1405 int i, ret = avcodec_decode_subtitle2(ist->dec_ctx,
1406 &subtitle, got_output, pkt);
1407 if (ret < 0)
1408 return ret;
1409 if (!*got_output)
1410 return ret;
1411
1412 ist->frames_decoded++;
1413
1414 for (i = 0; i < nb_output_streams; i++) {
1415 OutputStream *ost = output_streams[i];
1416
1417 if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
1418 continue;
1419
1420 do_subtitle_out(output_files[ost->file_index], ost, ist, &subtitle, pkt->pts);
1421 }
1422
1423 avsubtitle_free(&subtitle);
1424 return ret;
1425 }
1426
1427 static int send_filter_eof(InputStream *ist)
1428 {
1429 int i, j, ret;
1430 for (i = 0; i < ist->nb_filters; i++) {
1431 if (ist->filters[i]->filter) {
1432 ret = av_buffersrc_add_frame(ist->filters[i]->filter, NULL);
1433 if (ret < 0)
1434 return ret;
1435 } else {
1436 // the filtergraph was never configured
1437 FilterGraph *fg = ist->filters[i]->graph;
1438 for (j = 0; j < fg->nb_outputs; j++)
1439 finish_output_stream(fg->outputs[j]->ost);
1440 }
1441 }
1442 return 0;
1443 }
1444
1445 /* pkt = NULL means EOF (needed to flush decoder buffers) */
1446 static void process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
1447 {
1448 int i;
1449 int repeating = 0;
1450 AVPacket avpkt;
1451
1452 if (ist->next_dts == AV_NOPTS_VALUE)
1453 ist->next_dts = ist->last_dts;
1454
1455 if (!pkt) {
1456 /* EOF handling */
1457 av_init_packet(&avpkt);
1458 avpkt.data = NULL;
1459 avpkt.size = 0;
1460 } else {
1461 avpkt = *pkt;
1462 }
1463
1464 if (pkt && pkt->dts != AV_NOPTS_VALUE)
1465 ist->next_dts = ist->last_dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
1466
1467 // while we have more to decode or while the decoder did output something on EOF
1468 while (ist->decoding_needed && (!pkt || avpkt.size > 0)) {
1469 int ret = 0;
1470 int got_output = 0;
1471
1472 if (!repeating)
1473 ist->last_dts = ist->next_dts;
1474
1475 switch (ist->dec_ctx->codec_type) {
1476 case AVMEDIA_TYPE_AUDIO:
1477 ret = decode_audio (ist, repeating ? NULL : &avpkt, &got_output);
1478 break;
1479 case AVMEDIA_TYPE_VIDEO:
1480 ret = decode_video (ist, repeating ? NULL : &avpkt, &got_output);
1481 if (repeating && !got_output)
1482 ;
1483 else if (pkt && pkt->duration)
1484 ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
1485 else if (ist->st->avg_frame_rate.num)
1486 ist->next_dts += av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate),
1487 AV_TIME_BASE_Q);
1488 else if (ist->dec_ctx->framerate.num != 0) {
1489 int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 :
1490 ist->dec_ctx->ticks_per_frame;
1491 ist->next_dts += av_rescale_q(ticks, ist->dec_ctx->framerate, AV_TIME_BASE_Q);
1492 }
1493 break;
1494 case AVMEDIA_TYPE_SUBTITLE:
1495 if (repeating)
1496 break;
1497 ret = transcode_subtitles(ist, &avpkt, &got_output);
1498 break;
1499 default:
1500 return;
1501 }
1502
1503 if (ret < 0) {
1504 av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d\n",
1505 ist->file_index, ist->st->index);
1506 if (exit_on_error)
1507 exit_program(1);
1508 break;
1509 }
1510
1511 if (!got_output)
1512 break;
1513
1514 repeating = 1;
1515 }
1516
1517 /* after flushing, send an EOF on all the filter inputs attached to the stream */
1518 /* except when looping we need to flush but not to send an EOF */
1519 if (!pkt && ist->decoding_needed && !no_eof) {
1520 int ret = send_filter_eof(ist);
1521 if (ret < 0) {
1522 av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n");
1523 exit_program(1);
1524 }
1525 }
1526
1527 /* handle stream copy */
1528 if (!ist->decoding_needed) {
1529 ist->last_dts = ist->next_dts;
1530 switch (ist->dec_ctx->codec_type) {
1531 case AVMEDIA_TYPE_AUDIO:
1532 ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->frame_size) /
1533 ist->dec_ctx->sample_rate;
1534 break;
1535 case AVMEDIA_TYPE_VIDEO:
1536 if (ist->dec_ctx->framerate.num != 0) {
1537 int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame;
1538 ist->next_dts += ((int64_t)AV_TIME_BASE *
1539 ist->dec_ctx->framerate.den * ticks) /
1540 ist->dec_ctx->framerate.num;
1541 }
1542 break;
1543 }
1544 }
1545 for (i = 0; pkt && i < nb_output_streams; i++) {
1546 OutputStream *ost = output_streams[i];
1547
1548 if (!check_output_constraints(ist, ost) || ost->encoding_needed)
1549 continue;
1550
1551 do_streamcopy(ist, ost, pkt);
1552 }
1553
1554 return;
1555 }
1556
1557 static void print_sdp(void)
1558 {
1559 char sdp[16384];
1560 int i;
1561 AVFormatContext **avc;
1562
1563 for (i = 0; i < nb_output_files; i++) {
1564 if (!output_files[i]->header_written)
1565 return;
1566 }
1567
1568 avc = av_malloc(sizeof(*avc) * nb_output_files);
1569 if (!avc)
1570 exit_program(1);
1571 for (i = 0; i < nb_output_files; i++)
1572 avc[i] = output_files[i]->ctx;
1573
1574 av_sdp_create(avc, nb_output_files, sdp, sizeof(sdp));
1575 printf("SDP:\n%s\n", sdp);
1576 fflush(stdout);
1577 av_freep(&avc);
1578 }
1579
1580 static const HWAccel *get_hwaccel(enum AVPixelFormat pix_fmt)
1581 {
1582 int i;
1583 for (i = 0; hwaccels[i].name; i++)
1584 if (hwaccels[i].pix_fmt == pix_fmt)
1585 return &hwaccels[i];
1586 return NULL;
1587 }
1588
1589 static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
1590 {
1591 InputStream *ist = s->opaque;
1592 const enum AVPixelFormat *p;
1593 int ret;
1594
1595 for (p = pix_fmts; *p != -1; p++) {
1596 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
1597 const HWAccel *hwaccel;
1598
1599 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
1600 break;
1601
1602 hwaccel = get_hwaccel(*p);
1603 if (!hwaccel ||
1604 (ist->active_hwaccel_id && ist->active_hwaccel_id != hwaccel->id) ||
1605 (ist->hwaccel_id != HWACCEL_AUTO && ist->hwaccel_id != hwaccel->id))
1606 continue;
1607
1608 ret = hwaccel->init(s);
1609 if (ret < 0) {
1610 if (ist->hwaccel_id == hwaccel->id) {
1611 av_log(NULL, AV_LOG_FATAL,
1612 "%s hwaccel requested for input stream #%d:%d, "
1613 "but cannot be initialized.\n", hwaccel->name,
1614 ist->file_index, ist->st->index);
1615 return AV_PIX_FMT_NONE;
1616 }
1617 continue;
1618 }
1619 ist->active_hwaccel_id = hwaccel->id;
1620 ist->hwaccel_pix_fmt = *p;
1621 break;
1622 }
1623
1624 return *p;
1625 }
1626
1627 static int get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
1628 {
1629 InputStream *ist = s->opaque;
1630
1631 if (ist->hwaccel_get_buffer && frame->format == ist->hwaccel_pix_fmt)
1632 return ist->hwaccel_get_buffer(s, frame, flags);
1633
1634 return avcodec_default_get_buffer2(s, frame, flags);
1635 }
1636
1637 static int init_input_stream(int ist_index, char *error, int error_len)
1638 {
1639 int ret;
1640 InputStream *ist = input_streams[ist_index];
1641
1642 if (ist->decoding_needed) {
1643 AVCodec *codec = ist->dec;
1644 if (!codec) {
1645 snprintf(error, error_len, "Decoder (codec id %d) not found for input stream #%d:%d",
1646 ist->dec_ctx->codec_id, ist->file_index, ist->st->index);
1647 return AVERROR(EINVAL);
1648 }
1649
1650 ist->dec_ctx->opaque = ist;
1651 ist->dec_ctx->get_format = get_format;
1652 ist->dec_ctx->get_buffer2 = get_buffer;
1653 ist->dec_ctx->thread_safe_callbacks = 1;
1654
1655 av_opt_set_int(ist->dec_ctx, "refcounted_frames", 1, 0);
1656
1657 if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
1658 av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
1659 if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
1660 char errbuf[128];
1661 if (ret == AVERROR_EXPERIMENTAL)
1662 abort_codec_experimental(codec, 0);
1663
1664 av_strerror(ret, errbuf, sizeof(errbuf));
1665
1666 snprintf(error, error_len,
1667 "Error while opening decoder for input stream "
1668 "#%d:%d : %s",
1669 ist->file_index, ist->st->index, errbuf);
1670 return ret;
1671 }
1672 assert_avoptions(ist->decoder_opts);
1673 }
1674
1675 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;
1676 ist->next_dts = AV_NOPTS_VALUE;
1677 init_pts_correction(&ist->pts_ctx);
1678
1679 return 0;
1680 }
1681
1682 static InputStream *get_input_stream(OutputStream *ost)
1683 {
1684 if (ost->source_index >= 0)
1685 return input_streams[ost->source_index];
1686
1687 if (ost->filter) {
1688 FilterGraph *fg = ost->filter->graph;
1689 int i;
1690
1691 for (i = 0; i < fg->nb_inputs; i++)
1692 if (fg->inputs[i]->ist->dec_ctx->codec_type == ost->enc_ctx->codec_type)
1693 return fg->inputs[i]->ist;
1694 }
1695
1696 return NULL;
1697 }
1698
1699 /* open the muxer when all the streams are initialized */
1700 static int check_init_output_file(OutputFile *of, int file_index)
1701 {
1702 int ret, i;
1703
1704 for (i = 0; i < of->ctx->nb_streams; i++) {
1705 OutputStream *ost = output_streams[of->ost_index + i];
1706 if (!ost->initialized)
1707 return 0;
1708 }
1709
1710 of->ctx->interrupt_callback = int_cb;
1711
1712 ret = avformat_write_header(of->ctx, &of->opts);
1713 if (ret < 0) {
1714 char errbuf[128];
1715
1716 av_strerror(ret, errbuf, sizeof(errbuf));
1717
1718 av_log(NULL, AV_LOG_ERROR,
1719 "Could not write header for output file #%d "
1720 "(incorrect codec parameters ?): %s",
1721 file_index, errbuf);
1722 return ret;
1723 }
1724 assert_avoptions(of->opts);
1725 of->header_written = 1;
1726
1727 av_dump_format(of->ctx, file_index, of->ctx->filename, 1);
1728
1729 if (want_sdp)
1730 print_sdp();
1731
1732 /* flush the muxing queues */
1733 for (i = 0; i < of->ctx->nb_streams; i++) {
1734 OutputStream *ost = output_streams[of->ost_index + i];
1735
1736 while (av_fifo_size(ost->muxing_queue)) {
1737 AVPacket pkt;
1738 av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL);
1739 write_packet(of, &pkt, ost);
1740 }
1741 }
1742
1743 return 0;
1744 }
1745
1746 static int init_output_bsfs(OutputStream *ost)
1747 {
1748 AVBSFContext *ctx;
1749 int i, ret;
1750
1751 if (!ost->nb_bitstream_filters)
1752 return 0;
1753
1754 ost->bsf_ctx = av_mallocz_array(ost->nb_bitstream_filters, sizeof(*ost->bsf_ctx));
1755 if (!ost->bsf_ctx)
1756 return AVERROR(ENOMEM);
1757
1758 for (i = 0; i < ost->nb_bitstream_filters; i++) {
1759 ret = av_bsf_alloc(ost->bitstream_filters[i], &ctx);
1760 if (ret < 0) {
1761 av_log(NULL, AV_LOG_ERROR, "Error allocating a bitstream filter context\n");
1762 return ret;
1763 }
1764 ost->bsf_ctx[i] = ctx;
1765
1766 ret = avcodec_parameters_copy(ctx->par_in,
1767 i ? ost->bsf_ctx[i - 1]->par_out : ost->st->codecpar);
1768 if (ret < 0)
1769 return ret;
1770
1771 ctx->time_base_in = i ? ost->bsf_ctx[i - 1]->time_base_out : ost->st->time_base;
1772
1773 ret = av_bsf_init(ctx);
1774 if (ret < 0) {
1775 av_log(NULL, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
1776 ost->bitstream_filters[i]->name);
1777 return ret;
1778 }
1779 }
1780
1781 ctx = ost->bsf_ctx[ost->nb_bitstream_filters - 1];
1782 ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
1783 if (ret < 0)
1784 return ret;
1785
1786 ost->st->time_base = ctx->time_base_out;
1787
1788 return 0;
1789 }
1790
1791 static int init_output_stream_streamcopy(OutputStream *ost)
1792 {
1793 OutputFile *of = output_files[ost->file_index];
1794 InputStream *ist = get_input_stream(ost);
1795 AVCodecParameters *par_dst = ost->st->codecpar;
1796 AVCodecParameters *par_src = ist->st->codecpar;
1797 AVRational sar;
1798 int i;
1799 uint64_t extra_size;
1800
1801 extra_size = (uint64_t)par_src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE;
1802 if (extra_size > INT_MAX) {
1803 return AVERROR(EINVAL);
1804 }
1805
1806 ost->st->disposition = ist->st->disposition;
1807
1808 /* if stream_copy is selected, no need to decode or encode */
1809 par_dst->codec_id = par_src->codec_id;
1810 par_dst->codec_type = par_src->codec_type;
1811
1812 if (!par_dst->codec_tag) {
1813 if (!of->ctx->oformat->codec_tag ||
1814 av_codec_get_id (of->ctx->oformat->codec_tag, par_src->codec_tag) == par_dst->codec_id ||
1815 av_codec_get_tag(of->ctx->oformat->codec_tag, par_src->codec_id) <= 0)
1816 par_dst->codec_tag = par_src->codec_tag;
1817 }
1818
1819 par_dst->bit_rate = par_src->bit_rate;
1820 par_dst->field_order = par_src->field_order;
1821 par_dst->chroma_location = par_src->chroma_location;
1822
1823 if (par_src->extradata) {
1824 par_dst->extradata = av_mallocz(extra_size);
1825 if (!par_dst->extradata) {
1826 return AVERROR(ENOMEM);
1827 }
1828 memcpy(par_dst->extradata, par_src->extradata, par_src->extradata_size);
1829 par_dst->extradata_size = par_src->extradata_size;
1830 }
1831
1832 ost->st->time_base = ist->st->time_base;
1833
1834 if (ist->st->nb_side_data) {
1835 ost->st->side_data = av_realloc_array(NULL, ist->st->nb_side_data,
1836 sizeof(*ist->st->side_data));
1837 if (!ost->st->side_data)
1838 return AVERROR(ENOMEM);
1839
1840 for (i = 0; i < ist->st->nb_side_data; i++) {
1841 const AVPacketSideData *sd_src = &ist->st->side_data[i];
1842 AVPacketSideData *sd_dst = &ost->st->side_data[i];
1843
1844 sd_dst->data = av_malloc(sd_src->size);
1845 if (!sd_dst->data)
1846 return AVERROR(ENOMEM);
1847 memcpy(sd_dst->data, sd_src->data, sd_src->size);
1848 sd_dst->size = sd_src->size;
1849 sd_dst->type = sd_src->type;
1850 ost->st->nb_side_data++;
1851 }
1852 }
1853
1854 ost->parser = av_parser_init(par_dst->codec_id);
1855 ost->parser_avctx = avcodec_alloc_context3(NULL);
1856 if (!ost->parser_avctx)
1857 return AVERROR(ENOMEM);
1858
1859 switch (par_dst->codec_type) {
1860 case AVMEDIA_TYPE_AUDIO:
1861 if (audio_volume != 256) {
1862 av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
1863 exit_program(1);
1864 }
1865 par_dst->channel_layout = par_src->channel_layout;
1866 par_dst->sample_rate = par_src->sample_rate;
1867 par_dst->channels = par_src->channels;
1868 par_dst->block_align = par_src->block_align;
1869 break;
1870 case AVMEDIA_TYPE_VIDEO:
1871 par_dst->format = par_src->format;
1872 par_dst->width = par_src->width;
1873 par_dst->height = par_src->height;
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 break;
1882 case AVMEDIA_TYPE_SUBTITLE:
1883 par_dst->width = par_src->width;
1884 par_dst->height = par_src->height;
1885 break;
1886 case AVMEDIA_TYPE_DATA:
1887 case AVMEDIA_TYPE_ATTACHMENT:
1888 break;
1889 default:
1890 abort();
1891 }
1892
1893 return 0;
1894 }
1895
1896 static void set_encoder_id(OutputFile *of, OutputStream *ost)
1897 {
1898 AVDictionaryEntry *e;
1899
1900 uint8_t *encoder_string;
1901 int encoder_string_len;
1902 int format_flags = 0;
1903
1904 e = av_dict_get(of->opts, "fflags", NULL, 0);
1905 if (e) {
1906 const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0);
1907 if (!o)
1908 return;
1909 av_opt_eval_flags(of->ctx, o, e->value, &format_flags);
1910 }
1911
1912 encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2;
1913 encoder_string = av_mallocz(encoder_string_len);
1914 if (!encoder_string)
1915 exit_program(1);
1916
1917 if (!(format_flags & AVFMT_FLAG_BITEXACT))
1918 av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
1919 av_strlcat(encoder_string, ost->enc->name, encoder_string_len);
1920 av_dict_set(&ost->st->metadata, "encoder", encoder_string,
1921 AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
1922 }
1923
1924 static void parse_forced_key_frames(char *kf, OutputStream *ost,
1925 AVCodecContext *avctx)
1926 {
1927 char *p;
1928 int n = 1, i;
1929 int64_t t;
1930
1931 for (p = kf; *p; p++)
1932 if (*p == ',')
1933 n++;
1934 ost->forced_kf_count = n;
1935 ost->forced_kf_pts = av_malloc(sizeof(*ost->forced_kf_pts) * n);
1936 if (!ost->forced_kf_pts) {
1937 av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
1938 exit_program(1);
1939 }
1940
1941 p = kf;
1942 for (i = 0; i < n; i++) {
1943 char *next = strchr(p, ',');
1944
1945 if (next)
1946 *next++ = 0;
1947
1948 t = parse_time_or_die("force_key_frames", p, 1);
1949 ost->forced_kf_pts[i] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
1950
1951 p = next;
1952 }
1953 }
1954
1955 static int init_output_stream_encode(OutputStream *ost)
1956 {
1957 InputStream *ist = get_input_stream(ost);
1958 AVCodecContext *enc_ctx = ost->enc_ctx;
1959 AVCodecContext *dec_ctx = NULL;
1960
1961 set_encoder_id(output_files[ost->file_index], ost);
1962
1963 if (ist) {
1964 ost->st->disposition = ist->st->disposition;
1965
1966 dec_ctx = ist->dec_ctx;
1967
1968 enc_ctx->bits_per_raw_sample = dec_ctx->bits_per_raw_sample;
1969 enc_ctx->chroma_sample_location = dec_ctx->chroma_sample_location;
1970 }
1971
1972 switch (enc_ctx->codec_type) {
1973 case AVMEDIA_TYPE_AUDIO:
1974 enc_ctx->sample_fmt = ost->filter->filter->inputs[0]->format;
1975 enc_ctx->sample_rate = ost->filter->filter->inputs[0]->sample_rate;
1976 enc_ctx->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
1977 enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);
1978 enc_ctx->time_base = (AVRational){ 1, enc_ctx->sample_rate };
1979 break;
1980 case AVMEDIA_TYPE_VIDEO:
1981 enc_ctx->time_base = ost->filter->filter->inputs[0]->time_base;
1982
1983 enc_ctx->width = ost->filter->filter->inputs[0]->w;
1984 enc_ctx->height = ost->filter->filter->inputs[0]->h;
1985 enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
1986 ost->frame_aspect_ratio ? // overridden by the -aspect cli option
1987 av_d2q(ost->frame_aspect_ratio * enc_ctx->height/enc_ctx->width, 255) :
1988 ost->filter->filter->inputs[0]->sample_aspect_ratio;
1989 enc_ctx->pix_fmt = ost->filter->filter->inputs[0]->format;
1990
1991 ost->st->avg_frame_rate = ost->frame_rate;
1992
1993 if (dec_ctx &&
1994 (enc_ctx->width != dec_ctx->width ||
1995 enc_ctx->height != dec_ctx->height ||
1996 enc_ctx->pix_fmt != dec_ctx->pix_fmt)) {
1997 enc_ctx->bits_per_raw_sample = 0;
1998 }
1999
2000 if (ost->forced_keyframes)
2001 parse_forced_key_frames(ost->forced_keyframes, ost,
2002 ost->enc_ctx);
2003 break;
2004 case AVMEDIA_TYPE_SUBTITLE:
2005 enc_ctx->time_base = (AVRational){1, 1000};
2006 break;
2007 default:
2008 abort();
2009 break;
2010 }
2011
2012 return 0;
2013 }
2014
2015 static int init_output_stream(OutputStream *ost, char *error, int error_len)
2016 {
2017 int ret = 0;
2018
2019 if (ost->encoding_needed) {
2020 AVCodec *codec = ost->enc;
2021 AVCodecContext *dec = NULL;
2022 InputStream *ist;
2023
2024 ret = init_output_stream_encode(ost);
2025 if (ret < 0)
2026 return ret;
2027
2028 if ((ist = get_input_stream(ost)))
2029 dec = ist->dec_ctx;
2030 if (dec && dec->subtitle_header) {
2031 ost->enc_ctx->subtitle_header = av_malloc(dec->subtitle_header_size);
2032 if (!ost->enc_ctx->subtitle_header)
2033 return AVERROR(ENOMEM);
2034 memcpy(ost->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
2035 ost->enc_ctx->subtitle_header_size = dec->subtitle_header_size;
2036 }
2037 if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
2038 av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
2039
2040 if (ost->filter && ost->filter->filter->inputs[0]->hw_frames_ctx) {
2041 ost->enc_ctx->hw_frames_ctx = av_buffer_ref(ost->filter->filter->inputs[0]->hw_frames_ctx);
2042 if (!ost->enc_ctx->hw_frames_ctx)
2043 return AVERROR(ENOMEM);
2044 }
2045
2046 if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {
2047 if (ret == AVERROR_EXPERIMENTAL)
2048 abort_codec_experimental(codec, 1);
2049 snprintf(error, error_len,
2050 "Error while opening encoder for output stream #%d:%d - "
2051 "maybe incorrect parameters such as bit_rate, rate, width or height",
2052 ost->file_index, ost->index);
2053 return ret;
2054 }
2055 assert_avoptions(ost->encoder_opts);
2056 if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000)
2057 av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
2058 "It takes bits/s as argument, not kbits/s\n");
2059
2060 ret = avcodec_parameters_from_context(ost->st->codecpar, ost->enc_ctx);
2061 if (ret < 0) {
2062 av_log(NULL, AV_LOG_FATAL,
2063 "Error initializing the output stream codec context.\n");
2064 exit_program(1);
2065 }
2066
2067 if (ost->enc_ctx->nb_coded_side_data) {
2068 int i;
2069
2070 ost->st->side_data = av_realloc_array(NULL, ost->enc_ctx->nb_coded_side_data,
2071 sizeof(*ost->st->side_data));
2072 if (!ost->st->side_data)
2073 return AVERROR(ENOMEM);
2074
2075 for (i = 0; i < ost->enc_ctx->nb_coded_side_data; i++) {
2076 const AVPacketSideData *sd_src = &ost->enc_ctx->coded_side_data[i];
2077 AVPacketSideData *sd_dst = &ost->st->side_data[i];
2078
2079 sd_dst->data = av_malloc(sd_src->size);
2080 if (!sd_dst->data)
2081 return AVERROR(ENOMEM);
2082 memcpy(sd_dst->data, sd_src->data, sd_src->size);
2083 sd_dst->size = sd_src->size;
2084 sd_dst->type = sd_src->type;
2085 ost->st->nb_side_data++;
2086 }
2087 }
2088
2089 ost->st->time_base = ost->enc_ctx->time_base;
2090 } else if (ost->stream_copy) {
2091 ret = init_output_stream_streamcopy(ost);
2092 if (ret < 0)
2093 return ret;
2094
2095 /*
2096 * FIXME: will the codec context used by the parser during streamcopy
2097 * This should go away with the new parser API.
2098 */
2099 ret = avcodec_parameters_to_context(ost->parser_avctx, ost->st->codecpar);
2100 if (ret < 0)
2101 return ret;
2102 }
2103
2104 /* initialize bitstream filters for the output stream
2105 * needs to be done here, because the codec id for streamcopy is not
2106 * known until now */
2107 ret = init_output_bsfs(ost);
2108 if (ret < 0)
2109 return ret;
2110
2111 ost->mux_timebase = ost->st->time_base;
2112
2113 ost->initialized = 1;
2114
2115 ret = check_init_output_file(output_files[ost->file_index], ost->file_index);
2116 if (ret < 0)
2117 return ret;
2118
2119 return ret;
2120 }
2121
2122 static int transcode_init(void)
2123 {
2124 int ret = 0, i, j, k;
2125 OutputStream *ost;
2126 InputStream *ist;
2127 char error[1024];
2128
2129 /* init framerate emulation */
2130 for (i = 0; i < nb_input_files; i++) {
2131 InputFile *ifile = input_files[i];
2132 if (ifile->rate_emu)
2133 for (j = 0; j < ifile->nb_streams; j++)
2134 input_streams[j + ifile->ist_index]->start = av_gettime_relative();
2135 }
2136
2137 /* init input streams */
2138 for (i = 0; i < nb_input_streams; i++)
2139 if ((ret = init_input_stream(i, error, sizeof(error))) < 0)
2140 goto dump_format;
2141
2142 /* open each encoder */
2143 for (i = 0; i < nb_output_streams; i++) {
2144 // skip streams fed from filtergraphs until we have a frame for them
2145 if (output_streams[i]->filter)
2146 continue;
2147
2148 ret = init_output_stream(output_streams[i], error, sizeof(error));
2149 if (ret < 0)
2150 goto dump_format;
2151 }
2152
2153
2154 /* discard unused programs */
2155 for (i = 0; i < nb_input_files; i++) {
2156 InputFile *ifile = input_files[i];
2157 for (j = 0; j < ifile->ctx->nb_programs; j++) {
2158 AVProgram *p = ifile->ctx->programs[j];
2159 int discard = AVDISCARD_ALL;
2160
2161 for (k = 0; k < p->nb_stream_indexes; k++)
2162 if (!input_streams[ifile->ist_index + p->stream_index[k]]->discard) {
2163 discard = AVDISCARD_DEFAULT;
2164 break;
2165 }
2166 p->discard = discard;
2167 }
2168 }
2169
2170 dump_format:
2171 /* dump the stream mapping */
2172 av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
2173 for (i = 0; i < nb_input_streams; i++) {
2174 ist = input_streams[i];
2175
2176 for (j = 0; j < ist->nb_filters; j++) {
2177 if (!filtergraph_is_simple(ist->filters[j]->graph)) {
2178 av_log(NULL, AV_LOG_INFO, " Stream #%d:%d (%s) -> %s",
2179 ist->file_index, ist->st->index, ist->dec ? ist->dec->name : "?",
2180 ist->filters[j]->name);
2181 if (nb_filtergraphs > 1)
2182 av_log(NULL, AV_LOG_INFO, " (graph %d)", ist->filters[j]->graph->index);
2183 av_log(NULL, AV_LOG_INFO, "\n");
2184 }
2185 }
2186 }
2187
2188 for (i = 0; i < nb_output_streams; i++) {
2189 ost = output_streams[i];
2190
2191 if (ost->attachment_filename) {
2192 /* an attached file */
2193 av_log(NULL, AV_LOG_INFO, " File %s -> Stream #%d:%d\n",
2194 ost->attachment_filename, ost->file_index, ost->index);
2195 continue;
2196 }
2197
2198 if (ost->filter && !filtergraph_is_simple(ost->filter->graph)) {
2199 /* output from a complex graph */
2200 av_log(NULL, AV_LOG_INFO, " %s", ost->filter->name);
2201 if (nb_filtergraphs > 1)
2202 av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);
2203
2204 av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index,
2205 ost->index, ost->enc ? ost->enc->name : "?");
2206 continue;
2207 }
2208
2209 av_log(NULL, AV_LOG_INFO, " Stream #%d:%d -> #%d:%d",
2210 input_streams[ost->source_index]->file_index,
2211 input_streams[ost->source_index]->st->index,
2212 ost->file_index,
2213 ost->index);
2214 if (ost->sync_ist != input_streams[ost->source_index])
2215 av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]",
2216 ost->sync_ist->file_index,
2217 ost->sync_ist->st->index);
2218 if (ost->stream_copy)
2219 av_log(NULL, AV_LOG_INFO, " (copy)");
2220 else {
2221 const AVCodec *in_codec = input_streams[ost->source_index]->dec;
2222 const AVCodec *out_codec = ost->enc;
2223 const char *decoder_name = "?";
2224 const char *in_codec_name = "?";
2225 const char *encoder_name = "?";
2226 const char *out_codec_name = "?";
2227 const AVCodecDescriptor *desc;
2228
2229 if (in_codec) {
2230 decoder_name = in_codec->name;
2231 desc = avcodec_descriptor_get(in_codec->id);
2232 if (desc)
2233 in_codec_name = desc->name;
2234 if (!strcmp(decoder_name, in_codec_name))
2235 decoder_name = "native";
2236 }
2237
2238 if (out_codec) {
2239 encoder_name = out_codec->name;
2240 desc = avcodec_descriptor_get(out_codec->id);
2241 if (desc)
2242 out_codec_name = desc->name;
2243 if (!strcmp(encoder_name, out_codec_name))
2244 encoder_name = "native";
2245 }
2246
2247 av_log(NULL, AV_LOG_INFO, " (%s (%s) -> %s (%s))",
2248 in_codec_name, decoder_name,
2249 out_codec_name, encoder_name);
2250 }
2251 av_log(NULL, AV_LOG_INFO, "\n");
2252 }
2253
2254 if (ret) {
2255 av_log(NULL, AV_LOG_ERROR, "%s\n", error);
2256 return ret;
2257 }
2258
2259 return 0;
2260 }
2261
2262 /* Return 1 if there remain streams where more output is wanted, 0 otherwise. */
2263 static int need_output(void)
2264 {
2265 int i;
2266
2267 for (i = 0; i < nb_output_streams; i++) {
2268 OutputStream *ost = output_streams[i];
2269 OutputFile *of = output_files[ost->file_index];
2270 AVFormatContext *os = output_files[ost->file_index]->ctx;
2271
2272 if (ost->finished ||
2273 (os->pb && avio_tell(os->pb) >= of->limit_filesize))
2274 continue;
2275 if (ost->frame_number >= ost->max_frames) {
2276 int j;
2277 for (j = 0; j < of->ctx->nb_streams; j++)
2278 output_streams[of->ost_index + j]->finished = 1;
2279 continue;
2280 }
2281
2282 return 1;
2283 }
2284
2285 return 0;
2286 }
2287
2288 static InputFile *select_input_file(void)
2289 {
2290 InputFile *ifile = NULL;
2291 int64_t ipts_min = INT64_MAX;
2292 int i;
2293
2294 for (i = 0; i < nb_input_streams; i++) {
2295 InputStream *ist = input_streams[i];
2296 int64_t ipts = ist->last_dts;
2297
2298 if (ist->discard || input_files[ist->file_index]->eagain)
2299 continue;
2300 if (!input_files[ist->file_index]->eof_reached) {
2301 if (ipts < ipts_min) {
2302 ipts_min = ipts;
2303 ifile = input_files[ist->file_index];
2304 }
2305 }
2306 }
2307
2308 return ifile;
2309 }
2310
2311 #if HAVE_PTHREADS
2312 static void *input_thread(void *arg)
2313 {
2314 InputFile *f = arg;
2315 int ret = 0;
2316
2317 while (!transcoding_finished && ret >= 0) {
2318 AVPacket pkt;
2319 ret = av_read_frame(f->ctx, &pkt);
2320
2321 if (ret == AVERROR(EAGAIN)) {
2322 av_usleep(10000);
2323 ret = 0;
2324 continue;
2325 } else if (ret < 0)
2326 break;
2327
2328 pthread_mutex_lock(&f->fifo_lock);
2329 while (!av_fifo_space(f->fifo))
2330 pthread_cond_wait(&f->fifo_cond, &f->fifo_lock);
2331
2332 av_fifo_generic_write(f->fifo, &pkt, sizeof(pkt), NULL);
2333
2334 pthread_mutex_unlock(&f->fifo_lock);
2335 }
2336
2337 f->finished = 1;
2338 return NULL;
2339 }
2340
2341 static void free_input_threads(void)
2342 {
2343 int i;
2344
2345 if (nb_input_files == 1)
2346 return;
2347
2348 transcoding_finished = 1;
2349
2350 for (i = 0; i < nb_input_files; i++) {
2351 InputFile *f = input_files[i];
2352 AVPacket pkt;
2353
2354 if (!f->fifo || f->joined)
2355 continue;
2356
2357 pthread_mutex_lock(&f->fifo_lock);
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 pthread_cond_signal(&f->fifo_cond);
2363 pthread_mutex_unlock(&f->fifo_lock);
2364
2365 pthread_join(f->thread, NULL);
2366 f->joined = 1;
2367
2368 while (av_fifo_size(f->fifo)) {
2369 av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL);
2370 av_packet_unref(&pkt);
2371 }
2372 av_fifo_free(f->fifo);
2373 }
2374 }
2375
2376 static int init_input_threads(void)
2377 {
2378 int i, ret;
2379
2380 if (nb_input_files == 1)
2381 return 0;
2382
2383 for (i = 0; i < nb_input_files; i++) {
2384 InputFile *f = input_files[i];
2385
2386 if (!(f->fifo = av_fifo_alloc(8*sizeof(AVPacket))))
2387 return AVERROR(ENOMEM);
2388
2389 pthread_mutex_init(&f->fifo_lock, NULL);
2390 pthread_cond_init (&f->fifo_cond, NULL);
2391
2392 if ((ret = pthread_create(&f->thread, NULL, input_thread, f)))
2393 return AVERROR(ret);
2394 }
2395 return 0;
2396 }
2397
2398 static int get_input_packet_mt(InputFile *f, AVPacket *pkt)
2399 {
2400 int ret = 0;
2401
2402 pthread_mutex_lock(&f->fifo_lock);
2403
2404 if (av_fifo_size(f->fifo)) {
2405 av_fifo_generic_read(f->fifo, pkt, sizeof(*pkt), NULL);
2406 pthread_cond_signal(&f->fifo_cond);
2407 } else {
2408 if (f->finished)
2409 ret = AVERROR_EOF;
2410 else
2411 ret = AVERROR(EAGAIN);
2412 }
2413
2414 pthread_mutex_unlock(&f->fifo_lock);
2415
2416 return ret;
2417 }
2418 #endif
2419
2420 static int get_input_packet(InputFile *f, AVPacket *pkt)
2421 {
2422 if (f->rate_emu) {
2423 int i;
2424 for (i = 0; i < f->nb_streams; i++) {
2425 InputStream *ist = input_streams[f->ist_index + i];
2426 int64_t pts = av_rescale(ist->last_dts, 1000000, AV_TIME_BASE);
2427 int64_t now = av_gettime_relative() - ist->start;
2428 if (pts > now)
2429 return AVERROR(EAGAIN);
2430 }
2431 }
2432
2433 #if HAVE_PTHREADS
2434 if (nb_input_files > 1)
2435 return get_input_packet_mt(f, pkt);
2436 #endif
2437 return av_read_frame(f->ctx, pkt);
2438 }
2439
2440 static int got_eagain(void)
2441 {
2442 int i;
2443 for (i = 0; i < nb_input_files; i++)
2444 if (input_files[i]->eagain)
2445 return 1;
2446 return 0;
2447 }
2448
2449 static void reset_eagain(void)
2450 {
2451 int i;
2452 for (i = 0; i < nb_input_files; i++)
2453 input_files[i]->eagain = 0;
2454 }
2455
2456 // set duration to max(tmp, duration) in a proper time base and return duration's time_base
2457 static AVRational duration_max(int64_t tmp, int64_t *duration, AVRational tmp_time_base,
2458 AVRational time_base)
2459 {
2460 int ret;
2461
2462 if (!*duration) {
2463 *duration = tmp;
2464 return tmp_time_base;
2465 }
2466
2467 ret = av_compare_ts(*duration, time_base, tmp, tmp_time_base);
2468 if (ret < 0) {
2469 *duration = tmp;
2470 return tmp_time_base;
2471 }
2472
2473 return time_base;
2474 }
2475
2476 static int seek_to_start(InputFile *ifile, AVFormatContext *is)
2477 {
2478 InputStream *ist;
2479 AVCodecContext *avctx;
2480 int i, ret, has_audio = 0;
2481 int64_t duration = 0;
2482
2483 ret = av_seek_frame(is, -1, is->start_time, 0);
2484 if (ret < 0)
2485 return ret;
2486
2487 for (i = 0; i < ifile->nb_streams; i++) {
2488 ist = input_streams[ifile->ist_index + i];
2489 avctx = ist->dec_ctx;
2490
2491 // flush decoders
2492 if (ist->decoding_needed) {
2493 process_input_packet(ist, NULL, 1);
2494 avcodec_flush_buffers(avctx);
2495 }
2496
2497 /* duration is the length of the last frame in a stream
2498 * when audio stream is present we don't care about
2499 * last video frame length because it's not defined exactly */
2500 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->nb_samples)
2501 has_audio = 1;
2502 }
2503
2504 for (i = 0; i < ifile->nb_streams; i++) {
2505 ist = input_streams[ifile->ist_index + i];
2506 avctx = ist->dec_ctx;
2507
2508 if (has_audio) {
2509 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->nb_samples) {
2510 AVRational sample_rate = {1, avctx->sample_rate};
2511
2512 duration = av_rescale_q(ist->nb_samples, sample_rate, ist->st->time_base);
2513 } else
2514 continue;
2515 } else {
2516 if (ist->framerate.num) {
2517 duration = av_rescale_q(1, ist->framerate, ist->st->time_base);
2518 } else if (ist->st->avg_frame_rate.num) {
2519 duration = av_rescale_q(1, ist->st->avg_frame_rate, ist->st->time_base);
2520 } else duration = 1;
2521 }
2522 if (!ifile->duration)
2523 ifile->time_base = ist->st->time_base;
2524 /* the total duration of the stream, max_pts - min_pts is
2525 * the duration of the stream without the last frame */
2526 duration += ist->max_pts - ist->min_pts;
2527 ifile->time_base = duration_max(duration, &ifile->duration, ist->st->time_base,
2528 ifile->time_base);
2529 }
2530
2531 if (ifile->loop > 0)
2532 ifile->loop--;
2533
2534 return ret;
2535 }
2536
2537 /*
2538 * Read one packet from an input file and send it for
2539 * - decoding -> lavfi (audio/video)
2540 * - decoding -> encoding -> muxing (subtitles)
2541 * - muxing (streamcopy)
2542 *
2543 * Return
2544 * - 0 -- one packet was read and processed
2545 * - AVERROR(EAGAIN) -- no packets were available for selected file,
2546 * this function should be called again
2547 * - AVERROR_EOF -- this function should not be called again
2548 */
2549 static int process_input(void)
2550 {
2551 InputFile *ifile;
2552 AVFormatContext *is;
2553 InputStream *ist;
2554 AVPacket pkt;
2555 int ret, i, j;
2556 int64_t duration;
2557
2558 /* select the stream that we must read now */
2559 ifile = select_input_file();
2560 /* if none, if is finished */
2561 if (!ifile) {
2562 if (got_eagain()) {
2563 reset_eagain();
2564 av_usleep(10000);
2565 return AVERROR(EAGAIN);
2566 }
2567 av_log(NULL, AV_LOG_VERBOSE, "No more inputs to read from.\n");
2568 return AVERROR_EOF;
2569 }
2570
2571 is = ifile->ctx;
2572 ret = get_input_packet(ifile, &pkt);
2573
2574 if (ret == AVERROR(EAGAIN)) {
2575 ifile->eagain = 1;
2576 return ret;
2577 }
2578 if (ret < 0 && ifile->loop) {
2579 if ((ret = seek_to_start(ifile, is)) < 0)
2580 return ret;
2581 ret = get_input_packet(ifile, &pkt);
2582 }
2583 if (ret < 0) {
2584 if (ret != AVERROR_EOF) {
2585 print_error(is->filename, ret);
2586 if (exit_on_error)
2587 exit_program(1);
2588 }
2589 ifile->eof_reached = 1;
2590
2591 for (i = 0; i < ifile->nb_streams; i++) {
2592 ist = input_streams[ifile->ist_index + i];
2593 if (ist->decoding_needed)
2594 process_input_packet(ist, NULL, 0);
2595
2596 /* mark all outputs that don't go through lavfi as finished */
2597 for (j = 0; j < nb_output_streams; j++) {
2598 OutputStream *ost = output_streams[j];
2599
2600 if (ost->source_index == ifile->ist_index + i &&
2601 (ost->stream_copy || ost->enc->type == AVMEDIA_TYPE_SUBTITLE))
2602 finish_output_stream(ost);
2603 }
2604 }
2605
2606 return AVERROR(EAGAIN);
2607 }
2608
2609 reset_eagain();
2610
2611 if (do_pkt_dump) {
2612 av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump,
2613 is->streams[pkt.stream_index]);
2614 }
2615 /* the following test is needed in case new streams appear
2616 dynamically in stream : we ignore them */
2617 if (pkt.stream_index >= ifile->nb_streams)
2618 goto discard_packet;
2619
2620 ist = input_streams[ifile->ist_index + pkt.stream_index];
2621
2622 ist->data_size += pkt.size;
2623 ist->nb_packets++;
2624
2625 if (ist->discard)
2626 goto discard_packet;
2627
2628 /* add the stream-global side data to the first packet */
2629 if (ist->nb_packets == 1)
2630 for (i = 0; i < ist->st->nb_side_data; i++) {
2631 AVPacketSideData *src_sd = &ist->st->side_data[i];
2632 uint8_t *dst_data;
2633
2634 if (av_packet_get_side_data(&pkt, src_sd->type, NULL))
2635 continue;
2636 if (ist->autorotate && src_sd->type == AV_PKT_DATA_DISPLAYMATRIX)
2637 continue;
2638
2639 dst_data = av_packet_new_side_data(&pkt, src_sd->type, src_sd->size);
2640 if (!dst_data)
2641 exit_program(1);
2642
2643 memcpy(dst_data, src_sd->data, src_sd->size);
2644 }
2645
2646 if (pkt.dts != AV_NOPTS_VALUE)
2647 pkt.dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
2648 if (pkt.pts != AV_NOPTS_VALUE)
2649 pkt.pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
2650
2651 if (pkt.pts != AV_NOPTS_VALUE)
2652 pkt.pts *= ist->ts_scale;
2653 if (pkt.dts != AV_NOPTS_VALUE)
2654 pkt.dts *= ist->ts_scale;
2655
2656 if ((ist->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2657 ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) &&
2658 pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE &&
2659 (is->iformat->flags & AVFMT_TS_DISCONT)) {
2660 int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
2661 int64_t delta = pkt_dts - ist->next_dts;
2662
2663 if ((FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE || pkt_dts + 1 < ist->last_dts) && !copy_ts) {
2664 ifile->ts_offset -= delta;
2665 av_log(NULL, AV_LOG_DEBUG,
2666 "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
2667 delta, ifile->ts_offset);
2668 pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
2669 if (pkt.pts != AV_NOPTS_VALUE)
2670 pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
2671 }
2672 }
2673 duration = av_rescale_q(ifile->duration, ifile->time_base, ist->st->time_base);
2674 if (pkt.pts != AV_NOPTS_VALUE) {
2675 pkt.pts += duration;
2676 ist->max_pts = FFMAX(pkt.pts, ist->max_pts);
2677 ist->min_pts = FFMIN(pkt.pts, ist->min_pts);
2678 }
2679
2680 if (pkt.dts != AV_NOPTS_VALUE)
2681 pkt.dts += duration;
2682
2683 process_input_packet(ist, &pkt, 0);
2684
2685 discard_packet:
2686 av_packet_unref(&pkt);
2687
2688 return 0;
2689 }
2690
2691 /*
2692 * The following code is the main loop of the file converter
2693 */
2694 static int transcode(void)
2695 {
2696 int ret, i, need_input = 1;
2697 AVFormatContext *os;
2698 OutputStream *ost;
2699 InputStream *ist;
2700 int64_t timer_start;
2701
2702 ret = transcode_init();
2703 if (ret < 0)
2704 goto fail;
2705
2706 av_log(NULL, AV_LOG_INFO, "Press ctrl-c to stop encoding\n");
2707 term_init();
2708
2709 timer_start = av_gettime_relative();
2710
2711 #if HAVE_PTHREADS
2712 if ((ret = init_input_threads()) < 0)
2713 goto fail;
2714 #endif
2715
2716 while (!received_sigterm) {
2717 /* check if there's any stream where output is still needed */
2718 if (!need_output()) {
2719 av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");
2720 break;
2721 }
2722
2723 /* read and process one input packet if needed */
2724 if (need_input) {
2725 ret = process_input();
2726 if (ret == AVERROR_EOF)
2727 need_input = 0;
2728 }
2729
2730 ret = poll_filters();
2731 if (ret < 0 && ret != AVERROR_EOF) {
2732 char errbuf[128];
2733 av_strerror(ret, errbuf, sizeof(errbuf));
2734
2735 av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", errbuf);
2736 break;
2737 }
2738
2739 /* dump report by using the output first video and audio streams */
2740 print_report(0, timer_start);
2741 }
2742 #if HAVE_PTHREADS
2743 free_input_threads();
2744 #endif
2745
2746 /* at the end of stream, we must flush the decoder buffers */
2747 for (i = 0; i < nb_input_streams; i++) {
2748 ist = input_streams[i];
2749 if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {
2750 process_input_packet(ist, NULL, 0);
2751 }
2752 }
2753 poll_filters();
2754 flush_encoders();
2755
2756 term_exit();
2757
2758 /* write the trailer if needed and close file */
2759 for (i = 0; i < nb_output_files; i++) {
2760 os = output_files[i]->ctx;
2761 if (!output_files[i]->header_written) {
2762 av_log(NULL, AV_LOG_ERROR,
2763 "Nothing was written into output file %d (%s), because "
2764 "at least one of its streams received no packets.\n",
2765 i, os->filename);
2766 continue;
2767 }
2768 av_write_trailer(os);
2769 }
2770
2771 /* dump report by using the first video and audio streams */
2772 print_report(1, timer_start);
2773
2774 /* close each encoder */
2775 for (i = 0; i < nb_output_streams; i++) {
2776 ost = output_streams[i];
2777 if (ost->encoding_needed) {
2778 av_freep(&ost->enc_ctx->stats_in);
2779 }
2780 }
2781
2782 /* close each decoder */
2783 for (i = 0; i < nb_input_streams; i++) {
2784 ist = input_streams[i];
2785 if (ist->decoding_needed) {
2786 avcodec_close(ist->dec_ctx);
2787 if (ist->hwaccel_uninit)
2788 ist->hwaccel_uninit(ist->dec_ctx);
2789 }
2790 }
2791
2792 av_buffer_unref(&hw_device_ctx);
2793
2794 /* finished ! */
2795 ret = 0;
2796
2797 fail:
2798 #if HAVE_PTHREADS
2799 free_input_threads();
2800 #endif
2801
2802 if (output_streams) {
2803 for (i = 0; i < nb_output_streams; i++) {
2804 ost = output_streams[i];
2805 if (ost) {
2806 if (ost->logfile) {
2807 fclose(ost->logfile);
2808 ost->logfile = NULL;
2809 }
2810 av_free(ost->forced_kf_pts);
2811 av_dict_free(&ost->encoder_opts);
2812 av_dict_free(&ost->resample_opts);
2813 }
2814 }
2815 }
2816 return ret;
2817 }
2818
2819 static int64_t getutime(void)
2820 {
2821 #if HAVE_GETRUSAGE
2822 struct rusage rusage;
2823
2824 getrusage(RUSAGE_SELF, &rusage);
2825 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2826 #elif HAVE_GETPROCESSTIMES
2827 HANDLE proc;
2828 FILETIME c, e, k, u;
2829 proc = GetCurrentProcess();
2830 GetProcessTimes(proc, &c, &e, &k, &u);
2831 return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
2832 #else
2833 return av_gettime_relative();
2834 #endif
2835 }
2836
2837 static int64_t getmaxrss(void)
2838 {
2839 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
2840 struct rusage rusage;
2841 getrusage(RUSAGE_SELF, &rusage);
2842 return (int64_t)rusage.ru_maxrss * 1024;
2843 #elif HAVE_GETPROCESSMEMORYINFO
2844 HANDLE proc;
2845 PROCESS_MEMORY_COUNTERS memcounters;
2846 proc = GetCurrentProcess();
2847 memcounters.cb = sizeof(memcounters);
2848 GetProcessMemoryInfo(proc, &memcounters, sizeof(memcounters));
2849 return memcounters.PeakPagefileUsage;
2850 #else
2851 return 0;
2852 #endif
2853 }
2854
2855 int main(int argc, char **argv)
2856 {
2857 int i, ret;
2858 int64_t ti;
2859
2860 register_exit(avconv_cleanup);
2861
2862 av_log_set_flags(AV_LOG_SKIP_REPEATED);
2863 parse_loglevel(argc, argv, options);
2864
2865 avcodec_register_all();
2866 #if CONFIG_AVDEVICE
2867 avdevice_register_all();
2868 #endif
2869 avfilter_register_all();
2870 av_register_all();
2871 avformat_network_init();
2872
2873 show_banner();
2874
2875 /* parse options and open all input/output files */
2876 ret = avconv_parse_options(argc, argv);
2877 if (ret < 0)
2878 exit_program(1);
2879
2880 if (nb_output_files <= 0 && nb_input_files == 0) {
2881 show_usage();
2882 av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
2883 exit_program(1);
2884 }
2885
2886 /* file converter / grab */
2887 if (nb_output_files <= 0) {
2888 fprintf(stderr, "At least one output file must be specified\n");
2889 exit_program(1);
2890 }
2891
2892 for (i = 0; i < nb_output_files; i++) {
2893 if (strcmp(output_files[i]->ctx->oformat->name, "rtp"))
2894 want_sdp = 0;
2895 }
2896
2897 ti = getutime();
2898 if (transcode() < 0)
2899 exit_program(1);
2900 ti = getutime() - ti;
2901 if (do_benchmark) {
2902 int maxrss = getmaxrss() / 1024;
2903 printf("bench: utime=%0.3fs maxrss=%ikB\n", ti / 1000000.0, maxrss);
2904 }
2905
2906 exit_program(0);
2907 return 0;
2908 }