From a4f50110651117f6a0cb3cac81232a4ffb383b02 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Fri, 3 Aug 2012 22:09:58 +0200 Subject: avconv: maintain sync on lavfi outputs. Before this commit, poll_filters() reads all frames available on each lavfi output. This does not work for lavfi sources that produce an unlimited number of frames, e.g. color and similar. With this commit, poll_filters() reads from output with the lowest timestamp and returns to wait for more input if no frames are available on it. --- avconv.c | 162 ++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 102 insertions(+), 60 deletions(-) diff --git a/avconv.c b/avconv.c index 3b50dd4db1..f7ec841f3a 100644 --- a/avconv.c +++ b/avconv.c @@ -644,78 +644,120 @@ static void do_video_stats(AVFormatContext *os, OutputStream *ost, } } -/* check for new output on any of the filtergraphs */ -static int poll_filters(void) +/** + * Read one frame for lavfi output for ost and encode it. + */ +static int poll_filter(OutputStream *ost) { + OutputFile *of = output_files[ost->file_index]; AVFilterBufferRef *picref; AVFrame *filtered_frame = NULL; - int i, frame_size; + int frame_size, ret; - for (i = 0; i < nb_output_streams; i++) { - OutputStream *ost = output_streams[i]; - OutputFile *of = output_files[ost->file_index]; - int ret = 0; + if (!ost->filtered_frame && !(ost->filtered_frame = avcodec_alloc_frame())) { + return AVERROR(ENOMEM); + } else + avcodec_get_frame_defaults(ost->filtered_frame); + filtered_frame = ost->filtered_frame; + + if (ost->enc->type == AVMEDIA_TYPE_AUDIO && + !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) + ret = av_buffersink_read_samples(ost->filter->filter, &picref, + ost->st->codec->frame_size); + else + ret = av_buffersink_read(ost->filter->filter, &picref); - if (!ost->filter) - continue; + if (ret < 0) + return ret; - if (!ost->filtered_frame && !(ost->filtered_frame = avcodec_alloc_frame())) { - return AVERROR(ENOMEM); - } else - avcodec_get_frame_defaults(ost->filtered_frame); - filtered_frame = ost->filtered_frame; - - while (ret >= 0 && !ost->is_past_recording_time) { - if (ost->enc->type == AVMEDIA_TYPE_AUDIO && - !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) - ret = av_buffersink_read_samples(ost->filter->filter, &picref, - ost->st->codec->frame_size); - else - ret = av_buffersink_read(ost->filter->filter, &picref); + avfilter_copy_buf_props(filtered_frame, picref); + if (picref->pts != AV_NOPTS_VALUE) { + filtered_frame->pts = av_rescale_q(picref->pts, + ost->filter->filter->inputs[0]->time_base, + ost->st->codec->time_base) - + av_rescale_q(of->start_time, + AV_TIME_BASE_Q, + ost->st->codec->time_base); - if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) - break; - else if (ret < 0) - return ret; + if (of->start_time && filtered_frame->pts < 0) { + avfilter_unref_buffer(picref); + return 0; + } + } - avfilter_copy_buf_props(filtered_frame, picref); - if (picref->pts != AV_NOPTS_VALUE) { - filtered_frame->pts = av_rescale_q(picref->pts, - ost->filter->filter->inputs[0]->time_base, - ost->st->codec->time_base) - - av_rescale_q(of->start_time, - AV_TIME_BASE_Q, - ost->st->codec->time_base); - - if (of->start_time && filtered_frame->pts < 0) { - avfilter_unref_buffer(picref); - continue; - } - } + switch (ost->filter->filter->inputs[0]->type) { + case AVMEDIA_TYPE_VIDEO: + if (!ost->frame_aspect_ratio) + ost->st->codec->sample_aspect_ratio = picref->video->pixel_aspect; - switch (ost->filter->filter->inputs[0]->type) { - case AVMEDIA_TYPE_VIDEO: - if (!ost->frame_aspect_ratio) - ost->st->codec->sample_aspect_ratio = picref->video->pixel_aspect; - - do_video_out(of->ctx, ost, filtered_frame, &frame_size, - same_quant ? ost->last_quality : - ost->st->codec->global_quality); - if (vstats_filename && frame_size) - do_video_stats(of->ctx, ost, frame_size); - break; - case AVMEDIA_TYPE_AUDIO: - do_audio_out(of->ctx, ost, filtered_frame); - break; - default: - // TODO support subtitle filters - av_assert0(0); - } + do_video_out(of->ctx, ost, filtered_frame, &frame_size, + same_quant ? ost->last_quality : + ost->st->codec->global_quality); + if (vstats_filename && frame_size) + do_video_stats(of->ctx, ost, frame_size); + break; + case AVMEDIA_TYPE_AUDIO: + do_audio_out(of->ctx, ost, filtered_frame); + break; + default: + // TODO support subtitle filters + av_assert0(0); + } - avfilter_unref_buffer(picref); + avfilter_unref_buffer(picref); + + return 0; +} + +/** + * Read as many frames from possible from lavfi and encode them. + * + * Always read from the active stream with the lowest timestamp. If no frames + * are available for it then return EAGAIN and wait for more input. This way we + * can use lavfi sources that generate unlimited amount of frames without memory + * usage exploding. + */ +static int poll_filters(void) +{ + int i, ret = 0; + + while (ret >= 0 && !received_sigterm) { + OutputStream *ost = NULL; + int64_t min_pts = INT64_MAX; + + /* choose output stream with the lowest timestamp */ + for (i = 0; i < nb_output_streams; i++) { + int64_t pts = output_streams[i]->sync_opts; + + if (!output_streams[i]->filter || + output_streams[i]->is_past_recording_time) + continue; + + pts = av_rescale_q(pts, output_streams[i]->st->codec->time_base, + AV_TIME_BASE_Q); + if (pts < min_pts) { + min_pts = pts; + ost = output_streams[i]; + } } + + if (!ost) + break; + + ret = poll_filter(ost); + + if (ret == AVERROR_EOF) { + ost->is_past_recording_time = 1; + + if (opt_shortest) + return ret; + + ret = 0; + } else if (ret == AVERROR(EAGAIN)) + return 0; } - return 0; + + return ret; } static void print_report(int is_last_report, int64_t timer_start) -- cgit v1.2.3 From d64901f99d850881d4a9f8ea3f161e7a280d9147 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 4 Aug 2012 11:50:30 +0200 Subject: avconv: remove commented out cruft. --- avconv.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/avconv.c b/avconv.c index f7ec841f3a..46d16a1451 100644 --- a/avconv.c +++ b/avconv.c @@ -2203,10 +2203,6 @@ static int transcode(void) if (pkt.dts != AV_NOPTS_VALUE) pkt.dts *= ist->ts_scale; - //fprintf(stderr, "next:%"PRId64" dts:%"PRId64" off:%"PRId64" %d\n", - // ist->next_dts, - // pkt.dts, input_files[ist->file_index].ts_offset, - // ist->st->codec->codec_type); if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE && (is->iformat->flags & AVFMT_TS_DISCONT)) { int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q); @@ -2222,7 +2218,6 @@ static int transcode(void) } } - // fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->st->index, pkt.size); if (output_packet(ist, &pkt) < 0 || poll_filters() < 0) { av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d\n", ist->file_index, ist->st->index); -- cgit v1.2.3 From bed43d58b06938060f6ed01ef90733247a23f362 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 4 Aug 2012 11:53:08 +0200 Subject: avconv: remove unused variable from InputFile. --- avconv.h | 1 - 1 file changed, 1 deletion(-) diff --git a/avconv.h b/avconv.h index d8bf78b419..0ee3c138e6 100644 --- a/avconv.h +++ b/avconv.h @@ -231,7 +231,6 @@ typedef struct InputFile { AVFormatContext *ctx; int eof_reached; /* true if eof reached */ int ist_index; /* index of first stream in ist_table */ - int buffer_size; /* current total buffer size */ int64_t ts_offset; int nb_streams; /* number of stream that avconv is aware of; may be different from ctx.nb_streams if new streams appear during av_read_frame() */ -- cgit v1.2.3 From 0b26ef4228671051eef1bba92da37a53e56714da Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 4 Aug 2012 12:04:02 +0200 Subject: avconv: replace no_packet array in transcode() with a var in InputStream This simplifies splitting code for reading from input out of transcode(). --- avconv.c | 39 +++++++++++++++++++++++---------------- avconv.h | 1 + 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/avconv.c b/avconv.c index 46d16a1451..416c07c109 100644 --- a/avconv.c +++ b/avconv.c @@ -1954,7 +1954,7 @@ static int need_output(void) return 0; } -static int select_input_file(uint8_t *no_packet) +static int select_input_file(void) { int64_t ipts_min = INT64_MAX; int i, file_index = -1; @@ -1963,7 +1963,7 @@ static int select_input_file(uint8_t *no_packet) InputStream *ist = input_streams[i]; int64_t ipts = ist->last_dts; - if (ist->discard || no_packet[ist->file_index]) + if (ist->discard || input_files[ist->file_index]->eagain) continue; if (!input_files[ist->file_index]->eof_reached) { if (ipts < ipts_min) { @@ -2095,6 +2095,22 @@ static int get_input_packet(InputFile *f, AVPacket *pkt) return av_read_frame(f->ctx, pkt); } +static int got_eagain(void) +{ + int i; + for (i = 0; i < nb_input_files; i++) + if (input_files[i]->eagain) + return 1; + return 0; +} + +static void reset_eagain(void) +{ + int i; + for (i = 0; i < nb_input_files; i++) + input_files[i]->eagain = 0; +} + /* * The following code is the main loop of the file converter */ @@ -2104,13 +2120,8 @@ static int transcode(void) AVFormatContext *is, *os; OutputStream *ost; InputStream *ist; - uint8_t *no_packet; - int no_packet_count = 0; int64_t timer_start; - if (!(no_packet = av_mallocz(nb_input_files))) - exit_program(1); - ret = transcode_init(); if (ret < 0) goto fail; @@ -2136,12 +2147,11 @@ static int transcode(void) } /* select the stream that we must read now */ - file_index = select_input_file(no_packet); + file_index = select_input_file(); /* if none, if is finished */ if (file_index < 0) { - if (no_packet_count) { - no_packet_count = 0; - memset(no_packet, 0, nb_input_files); + if (got_eagain()) { + reset_eagain(); av_usleep(10000); continue; } @@ -2153,8 +2163,7 @@ static int transcode(void) ret = get_input_packet(input_files[file_index], &pkt); if (ret == AVERROR(EAGAIN)) { - no_packet[file_index] = 1; - no_packet_count++; + input_files[file_index]->eagain = 1; continue; } if (ret < 0) { @@ -2177,8 +2186,7 @@ static int transcode(void) continue; } - no_packet_count = 0; - memset(no_packet, 0, nb_input_files); + reset_eagain(); if (do_pkt_dump) { av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump, @@ -2279,7 +2287,6 @@ static int transcode(void) ret = 0; fail: - av_freep(&no_packet); #if HAVE_PTHREADS free_input_threads(); #endif diff --git a/avconv.h b/avconv.h index 0ee3c138e6..0cc53c5134 100644 --- a/avconv.h +++ b/avconv.h @@ -230,6 +230,7 @@ typedef struct InputStream { typedef struct InputFile { AVFormatContext *ctx; int eof_reached; /* true if eof reached */ + int eagain; /* true if last read attempt returned EAGAIN */ int ist_index; /* index of first stream in ist_table */ int64_t ts_offset; int nb_streams; /* number of stream that avconv is aware of; may be different -- cgit v1.2.3 From c0fbf9713c5e167501007351532de2400060c7e6 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 4 Aug 2012 12:06:30 +0200 Subject: avconv: cosmetics Replace for (;foo == 0;) with while (!foo) This is prettier. --- avconv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/avconv.c b/avconv.c index 416c07c109..e6ace30151 100644 --- a/avconv.c +++ b/avconv.c @@ -2136,7 +2136,7 @@ static int transcode(void) goto fail; #endif - for (; received_sigterm == 0;) { + while (!received_sigterm) { int file_index, ist_index; AVPacket pkt; -- cgit v1.2.3 From 8391602913fff177b2f7ba850f5767eec9f33784 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 4 Aug 2012 12:12:50 +0200 Subject: avconv: simplify transcode(). Operate with a pointer to InputFile instead of its index in input_files. --- avconv.c | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/avconv.c b/avconv.c index e6ace30151..9a2573dceb 100644 --- a/avconv.c +++ b/avconv.c @@ -1954,10 +1954,11 @@ static int need_output(void) return 0; } -static int select_input_file(void) +static InputFile *select_input_file(void) { + InputFile *ifile = NULL; int64_t ipts_min = INT64_MAX; - int i, file_index = -1; + int i; for (i = 0; i < nb_input_streams; i++) { InputStream *ist = input_streams[i]; @@ -1968,12 +1969,12 @@ static int select_input_file(void) if (!input_files[ist->file_index]->eof_reached) { if (ipts < ipts_min) { ipts_min = ipts; - file_index = ist->file_index; + ifile = input_files[ist->file_index]; } } } - return file_index; + return ifile; } #if HAVE_PTHREADS @@ -2137,7 +2138,8 @@ static int transcode(void) #endif while (!received_sigterm) { - int file_index, ist_index; + InputFile *ifile; + int ist_index; AVPacket pkt; /* check if there's any stream where output is still needed */ @@ -2147,9 +2149,9 @@ static int transcode(void) } /* select the stream that we must read now */ - file_index = select_input_file(); + ifile = select_input_file(); /* if none, if is finished */ - if (file_index < 0) { + if (!ifile) { if (got_eagain()) { reset_eagain(); av_usleep(10000); @@ -2159,11 +2161,11 @@ static int transcode(void) break; } - is = input_files[file_index]->ctx; - ret = get_input_packet(input_files[file_index], &pkt); + is = ifile->ctx; + ret = get_input_packet(ifile, &pkt); if (ret == AVERROR(EAGAIN)) { - input_files[file_index]->eagain = 1; + ifile->eagain = 1; continue; } if (ret < 0) { @@ -2172,10 +2174,10 @@ static int transcode(void) if (exit_on_error) exit_program(1); } - input_files[file_index]->eof_reached = 1; + ifile->eof_reached = 1; - for (i = 0; i < input_files[file_index]->nb_streams; i++) { - ist = input_streams[input_files[file_index]->ist_index + i]; + for (i = 0; i < ifile->nb_streams; i++) { + ist = input_streams[ifile->ist_index + i]; if (ist->decoding_needed) output_packet(ist, NULL); } @@ -2194,17 +2196,17 @@ static int transcode(void) } /* the following test is needed in case new streams appear dynamically in stream : we ignore them */ - if (pkt.stream_index >= input_files[file_index]->nb_streams) + if (pkt.stream_index >= ifile->nb_streams) goto discard_packet; - ist_index = input_files[file_index]->ist_index + pkt.stream_index; + ist_index = ifile->ist_index + pkt.stream_index; ist = input_streams[ist_index]; if (ist->discard) goto discard_packet; if (pkt.dts != AV_NOPTS_VALUE) - pkt.dts += av_rescale_q(input_files[ist->file_index]->ts_offset, AV_TIME_BASE_Q, ist->st->time_base); + pkt.dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base); if (pkt.pts != AV_NOPTS_VALUE) - pkt.pts += av_rescale_q(input_files[ist->file_index]->ts_offset, AV_TIME_BASE_Q, ist->st->time_base); + pkt.pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base); if (pkt.pts != AV_NOPTS_VALUE) pkt.pts *= ist->ts_scale; @@ -2216,10 +2218,10 @@ static int transcode(void) int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q); int64_t delta = pkt_dts - ist->next_dts; if ((FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE || pkt_dts + 1 < ist->last_dts) && !copy_ts) { - input_files[ist->file_index]->ts_offset -= delta; + ifile->ts_offset -= delta; av_log(NULL, AV_LOG_DEBUG, "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n", - delta, input_files[ist->file_index]->ts_offset); + delta, ifile->ts_offset); pkt.dts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base); if (pkt.pts != AV_NOPTS_VALUE) pkt.pts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base); -- cgit v1.2.3 From cc2133b66abb3fe3dbb4feebe7cc9ceae3f8ec64 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 4 Aug 2012 12:17:43 +0200 Subject: avconv: get rid of pointless temporary variable. --- avconv.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/avconv.c b/avconv.c index 9a2573dceb..b786d8d1d6 100644 --- a/avconv.c +++ b/avconv.c @@ -2139,7 +2139,6 @@ static int transcode(void) while (!received_sigterm) { InputFile *ifile; - int ist_index; AVPacket pkt; /* check if there's any stream where output is still needed */ @@ -2198,8 +2197,8 @@ static int transcode(void) dynamically in stream : we ignore them */ if (pkt.stream_index >= ifile->nb_streams) goto discard_packet; - ist_index = ifile->ist_index + pkt.stream_index; - ist = input_streams[ist_index]; + + ist = input_streams[ifile->ist_index + pkt.stream_index]; if (ist->discard) goto discard_packet; -- cgit v1.2.3 From f154ef1ae5b03f288dd8c025dab1884b4cb20c1a Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Wed, 8 Aug 2012 12:27:50 +0200 Subject: avconv: send EOF to lavfi even if flushing the decoder fails --- avconv.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/avconv.c b/avconv.c index b786d8d1d6..d46c8e0943 100644 --- a/avconv.c +++ b/avconv.c @@ -1069,15 +1069,11 @@ static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output) decoded_frame = ist->decoded_frame; ret = avcodec_decode_audio4(avctx, decoded_frame, got_output, pkt); - if (ret < 0) { - return ret; - } - - if (!*got_output) { - /* no audio frame */ - if (!pkt->size) + if (!*got_output || ret < 0) { + if (!pkt->size) { for (i = 0; i < ist->nb_filters; i++) av_buffersrc_buffer(ist->filters[i]->filter, NULL); + } return ret; } @@ -1216,17 +1212,15 @@ static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output) ret = avcodec_decode_video2(ist->st->codec, decoded_frame, got_output, pkt); - if (ret < 0) - return ret; - - quality = same_quant ? decoded_frame->quality : 0; - if (!*got_output) { - /* no picture yet */ - if (!pkt->size) + if (!*got_output || ret < 0) { + if (!pkt->size) { for (i = 0; i < ist->nb_filters; i++) av_buffersrc_buffer(ist->filters[i]->filter, NULL); + } return ret; } + + quality = same_quant ? decoded_frame->quality : 0; decoded_frame->pts = guess_correct_pts(&ist->pts_ctx, decoded_frame->pkt_pts, decoded_frame->pkt_dts); pkt->size = 0; -- cgit v1.2.3