From patchwork Sun Nov 26 20:51:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 6369 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.94 with SMTP id m30csp880938jah; Sun, 26 Nov 2017 12:51:42 -0800 (PST) X-Google-Smtp-Source: AGs4zMapEjka1Hr4RVj663XMKwVkrivIe+AoLQoY1Ei+cAD8KkG/uLp8JDPxc5hCkdAxJrikJxty X-Received: by 10.28.215.194 with SMTP id o185mr14602406wmg.105.1511729502212; Sun, 26 Nov 2017 12:51:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511729502; cv=none; d=google.com; s=arc-20160816; b=bsTqjF+JQ+r05qY5Jk2oNaeOh2w78KIcbivRnPV5lC20Zdrry0snH6hS0jywM+kuTK MA/uswiymbDgopZRjYvufkSWuHr/qpbueGqlrOpmwfKNn/X7fxB0xv37a0T+h0JJFxNM 9EWc4/suWG91eGc/BZ02morYNqQtaPWNhaIkR+L5c1NYm7lG6x0yQXeoj0NfvfO8ahTn Y5/LT8x6f6cQMbrBgrDJJAbitPfv5ZymSuQhkk0yd9zFLn4QGSe/5B/S6gHfI/QxzZcw 2rtpEU2vzECbhlHUIWqaNqRU8vEyQz+yaybsiIz+dQTbZE4u0gpwCmxi/KMC73971Qfk zyPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=n4NWLJif6eOl4/WFs5AYIJ6+MereOql8hjotOwJ3Qgo=; b=LVUb7rqEMXTxFARVCjD4ndvaMXDX4AZoh7oJqqjFMTN54kLzER5ickoeXvZxzNl4Ku dNLnpNsSdFq4oRrec5Mbu8PgnzSpe9tVOSdYgPxs07w6fuFktQxYlEVDN7BYpQxBap3R K6Q5OoWi2EulS/PDGD5+fMlOp+b4JLIyyQZYXcoEQjsuO2WIUGKlqZGX+s9lhxZOWXev Il7uwK1bX/Q3XzcYsytVcGAFp2BsBxR8r9GGp1x5HGdwxvbscfepc1a41yWdP8mHR8R6 GayNorh1M/mTQj+qkkjrVKF8YEB16ir7JyLL3WZHQOwl/n8v5RyB7hZwTqHiXLLvzFoM R6Gw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=hhovXRnE; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id h5si10190771wmh.17.2017.11.26.12.51.41; Sun, 26 Nov 2017 12:51:42 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=hhovXRnE; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1358768A15B; Sun, 26 Nov 2017 22:51:39 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk0-f195.google.com (mail-qk0-f195.google.com [209.85.220.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5A338689FF3 for ; Sun, 26 Nov 2017 22:51:32 +0200 (EET) Received: by mail-qk0-f195.google.com with SMTP id 78so30052078qkz.0 for ; Sun, 26 Nov 2017 12:51:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=HlnkIdmPt7R9gKSLexiSbkl5j96qHfBEtliWD9Hl9/8=; b=hhovXRnES8TmDpos1cxrkQ5Zb8GHBryMmIUe1aBYF+uxnL9NTOzrnCsTawgquSISAK y98HkloPvVo6eod4stkYSJU3Y1DOlWv+Wnpe0wbvlIOLBQ13QrGtzfKz/14I+GFT/Tyz x7H4UMS/V2eJ8TyGfWSsPXKAg0nhi7JG5A8NxnxXJB2DJKLR9Uh5+NWdyhs9QYoCn1mL i9+J6lzLc5TckSLXQGaTWN1xiz4uiKeuEVW5a/cBUEtOySJOYGdVNg7ERzmelyO5rWAm v6zMmuoHE1flGM35/UBpqYTRRh/l0jBslbJ5foh6X0+812Y3iOA4CVe5nE4JNLcXT6o+ BZaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=HlnkIdmPt7R9gKSLexiSbkl5j96qHfBEtliWD9Hl9/8=; b=iyeu6bTi1i2XwUbhKVj1iQGA1UgnpVj5G5qKxCHm8u2SCLsglPPBmgeZQG/VAYcjrh 48+OQzRVQxSg7H3/UipPiEQ0h2ONMZx/dKaXs6UaNRB1CX0yv1DmKeRmf1A+b5/4bX5w YeEA+Mw+WPVJJaOHdOg/t4CI/Nigwgj8EiWX9mENACwtQLvmDoHwu8nVztHLo8MDVQUv yY98YKJ2Ngx2DB00/YgT0VKwPpA6sAxECvO6fElp0omPzS+C8pB6MQtI9ERmy7JPS5Vw Mx55E9y4nfwQjs8BvcfhKrAd7MIh+AeP0vdQmn4sXwebX5+7/kQ3egnG86Gisy2yqpcP UIGg== X-Gm-Message-State: AJaThX4Tn1ByTpWN4f9wvzaKiY9DyOr5tM1gD1pcL/Z3YyiCGpoUg8Xx vuZLNDCVGVDiWxUlxHFRB6juFw== X-Received: by 10.55.191.135 with SMTP id p129mr34607796qkf.211.1511729492952; Sun, 26 Nov 2017 12:51:32 -0800 (PST) Received: from localhost.localdomain ([181.231.59.220]) by smtp.gmail.com with ESMTPSA id g35sm15425640qtk.88.2017.11.26.12.51.31 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 26 Nov 2017 12:51:32 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sun, 26 Nov 2017 17:51:02 -0300 Message-Id: <20171126205104.1064-1-jamrial@gmail.com> X-Mailer: git-send-email 2.15.0 Subject: [FFmpeg-devel] [PATCH 1/3] ffmpeg: use avformat_init_output to initialize output files X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Postpone writing the header until the first output packet is ready to be written. This makes sure any stream parameter change that could take place while processing an input frame will be taken into account when writing the output file header. Signed-off-by: James Almer --- fftools/ffmpeg.c | 31 ++++++++++++++++++++++++++----- fftools/ffmpeg.h | 3 +++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 0c16e75ab0..07476e88e7 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -700,7 +700,7 @@ static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int u ost->frame_number++; } - if (!of->header_written) { + if (!of->initialized) { AVPacket tmp_pkt = {0}; /* the muxer is not initialized yet, buffer the packet */ if (!av_fifo_space(ost->muxing_queue)) { @@ -804,6 +804,17 @@ static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int u ); } + if (!of->header_written) { + ret = avformat_write_header(s, &of->opts); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, + "Could not write header for output file #%d: %s\n", + ost->file_index, av_err2str(ret)); + exit_program(1); + } + of->header_written = 1; + } + ret = av_interleaved_write_frame(s, pkt); if (ret < 0) { print_error("av_interleaved_write_frame()", ret); @@ -2756,7 +2767,7 @@ static void print_sdp(void) AVFormatContext **avc; for (i = 0; i < nb_output_files; i++) { - if (!output_files[i]->header_written) + if (!output_files[i]->initialized) return; } @@ -2947,16 +2958,26 @@ static int check_init_output_file(OutputFile *of, int file_index) of->ctx->interrupt_callback = int_cb; - ret = avformat_write_header(of->ctx, &of->opts); + ret = avformat_init_output(of->ctx, &of->opts); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, - "Could not write header for output file #%d " + "Could not initialize output file #%d " "(incorrect codec parameters ?): %s\n", file_index, av_err2str(ret)); return ret; } //assert_avoptions(of->opts); - of->header_written = 1; + of->initialized = ret; + if (!ret) { + ret = avformat_write_header(of->ctx, &of->opts); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, + "Could not write header for output file #%d: %s\n", + file_index, av_err2str(ret)); + return ret; + } + of->initialized = of->header_written = 1; + } av_dump_format(of->ctx, file_index, of->ctx->filename, 1); diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index e0977e1bf1..c46ffd8b03 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -571,6 +571,9 @@ typedef struct OutputFile { int shortest; + // avformat_init_output() has been called for this file + int initialized; + // avformat_write_header() has been called for this file int header_written; } OutputFile;