[FFmpeg-devel] Flushing remaining streams when one stream fails on av_interleaved_write_frame()
Ramiro Polla
ramiro.polla at gmail.com
Fri Jan 17 18:43:42 CET 2014
On Thu, Jan 16, 2014 at 9:55 PM, Michael Niedermayer <michaelni at gmx.at> wrote:
> On Thu, Jan 16, 2014 at 03:32:36PM -0200, Ramiro Polla wrote:
>> On Wed, Jan 15, 2014 at 3:03 PM, Michael Niedermayer <michaelni at gmx.at> wrote:
>> > On Tue, Jan 14, 2014 at 01:58:05PM -0200, Ramiro Polla wrote:
>> >> On Tue, Jan 14, 2014 at 1:58 AM, Michael Niedermayer <michaelni at gmx.at> wrote:
>> >> > On Thu, Jan 09, 2014 at 01:58:15AM -0200, Ramiro Polla wrote:
>> >> >> Hi,
>> >> >>
>> >> >> When one stream fails in av_interleaved_write_frame(), ffmpeg just
>> >> >> quits. If there are multiple streams/files, it may be possible that
>> >> >> the other streams/files are still OK. ffmpeg should at least try to
>> >> >> flush the remaining streams so that they become valid files.
>> >> >>
>> >> >> Here is an example to reproduce the problem:
>> >> >> $ mkdir 1
>> >> >> $ ffmpeg -i input -f image2 "%d/x.jpg" output.mpg
>> >> >>
>> >> >> output.mpg will not be flushed properly, even though the problem is in image2.
>> >> >>
>> >> >> I tried to fix this by propagating the error from write_frame() but
>> >> >> the point I got to is that ffmpeg hangs on reading from the input
>> >> >> (attached patch is for reference, not for review). This whole
>> >> >> input/output code with filterchains in ffmpeg is confusing to me. Can
>> >> >> anyone shed some light on the proper way to either a) close just the
>> >> >> misbehaving stream and keep on trying the other ones or b) flush and
>> >> >> close all streams on error.
>> >> >>
>> >> >> Ramiro
>> >> >
>> >> >> ffmpeg.c | 98 ++++++++++++++++++++++++++++++++++++++++++++-------------------
>> >> >> 1 file changed, 69 insertions(+), 29 deletions(-)
>> >> >> 365f871d295f127a47ddacd21a8e4ef6ce1461a0 0001-NOT-FOR-REVIEW.patch
>> >> >> From 7d1c1b0f12aa871bd6630d9a69adff8182aacea4 Mon Sep 17 00:00:00 2001
>> >> >> From: Ramiro Polla <ramiro.polla at gmail.com>
>> >> >> Date: Thu, 9 Jan 2014 01:51:15 -0200
>> >> >> Subject: [PATCH] NOT FOR REVIEW
>> >> >
>> >> > See: [FFmpeg-devel] [PATCH 3/3] ffmpeg: properly close down muxers on av_interleaved_write_frame() failure
>> >>
>> >> That patchset works but, while flushing the streams that are still
>> >> working, write_frame is still being called (and giving errors) for the
>> >> failed stream.
>> >
>> > better patchset posted
>>
>> Hm, now the video isn't being flushed properly. Maybe it's better to
>> keep the old patchset, even though it keeps on calling write_frame for
>> the bad stream?
>
> what about this one: (with the others from the latest patchset)
>
> commit c9b8697124e3c2c41c11eb0cb6816225b66aa797
> Author: Michael Niedermayer <michaelni at gmx.at>
> Date: Tue Jan 14 04:33:12 2014 +0100
>
> ffmpeg: properly close down muxers on av_interleaved_write_frame() failure
>
> Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
>
> diff --git a/ffmpeg.c b/ffmpeg.c
> index 5831a78..aa74cfc 100644
> --- a/ffmpeg.c
> +++ b/ffmpeg.c
> @@ -313,6 +313,7 @@ void term_exit(void)
>
> static volatile int received_sigterm = 0;
> static volatile int received_nb_signals = 0;
> +static int main_return_code = 0;
>
> static void
> sigterm_handler(int sig)
> @@ -547,6 +548,15 @@ static void update_benchmark(const char *fmt, ...)
> }
> }
>
> +static void close_all_output_streams(OutputStream *ost, int this_stream, int others)
> +{
> + int i;
> + for (i = 0; i < nb_output_streams; i++) {
> + OutputStream *ost2 = output_streams[i];
> + ost2->finished |= ost == ost2 ? this_stream : others;
> + }
> +}
> +
> static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
> {
> AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
> @@ -647,7 +657,8 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
> ret = av_interleaved_write_frame(s, pkt);
> if (ret < 0) {
> print_error("av_interleaved_write_frame()", ret);
> - exit_program(1);
> + main_return_code = 1;
> + close_all_output_streams(ost, 3, 1);
> }
> }
>
> @@ -655,7 +666,7 @@ static void close_output_stream(OutputStream *ost)
> {
> OutputFile *of = output_files[ost->file_index];
>
> - ost->finished = 1;
> + ost->finished |= 1;
> if (of->shortest) {
> int64_t end = av_rescale_q(ost->sync_opts - ost->first_pts, ost->st->codec->time_base, AV_TIME_BASE_Q);
> of->recording_time = FFMIN(of->recording_time, end);
> @@ -3559,6 +3570,6 @@ int main(int argc, char **argv)
> if ((decode_error_stat[0] + decode_error_stat[1]) * max_error_rate < decode_error_stat[1])
> exit_program(69);
>
> - exit_program(received_nb_signals ? 255 : 0);
> - return 0;
> + exit_program(received_nb_signals ? 255 : main_return_code);
> + return main_return_code;
> }
>
This works now. How about an enum for finished, instead of |1 and |3?
Or close_all_output_streams(); close_output_stream(ost, DONT_FLUSH);
More information about the ffmpeg-devel
mailing list