From patchwork Fri Aug 16 03:05:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 14541 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id C4302448852 for ; Fri, 16 Aug 2019 06:13:55 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A142D68AD2B; Fri, 16 Aug 2019 06:13:55 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 92EFC68AD25 for ; Fri, 16 Aug 2019 06:13:48 +0300 (EEST) Received: by mail-wm1-f67.google.com with SMTP id v19so2891386wmj.5 for ; Thu, 15 Aug 2019 20:13:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kqDKXU/dkv2WlTLBY4jzrkY+YNg9wXh56QYb18kGf0k=; b=M7iqyKmUn1GeIvtVrzx+jx7J+08w584XN5IiORCgw6QAvO/RDZ29LlgjCkFbeeSFID Mwvq17rMHN9xDx5QDaSKrYm0KLZ/8EMFflX6LQ/u19eIXksQ1p1UWcbPO/6aWhy5Zw9i z9o39eAXJz8SQSgCy3T7tF3R+Sw//S4zRABSnz5ABLYlKjzZzOEnBWuMV+lH5c/UIj2O QTW20jdjyFiddfKReK4EJuaYbv/rwA/2D2TpJEtl9iurpBcf1Ffdm+RBk4aO0Jc6fRWL JZarGTbD/mOv7/i9v2a5h0sa34B+Ut25hTmN4TcITzr/ziH/Fc+tNaTG0v8BzGmr5CJ7 bIyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kqDKXU/dkv2WlTLBY4jzrkY+YNg9wXh56QYb18kGf0k=; b=MhFKbUCWNsvi24CprL0ua5hgKu17YRJZaRvd3w14tXWoaiPrMXK8pG5w+fJ72d47Mm 8RcFxOD+SmFKGfdqcmXuq51EleyiLYFIU70WEKIAORXwjZ1lVd55fSvjmEhw3crHs3Tn 9oqT6KzStMto8i4sciYOFTaixHr7Ft4b8HrvRYcREMH4Hb75hy6MZJkjmaGDciQ56Ut3 bw4MBzFFHvkoIJEY9CzBWqKnXxrcqYmDzNwfrJ0hWXwLF/+KeizxoWSA/BE4A5ACweSK qaidPsnkty5Jac2v2jZiJbGcgGdueTLo6J0tTCvQ3BXUtNkBfHgr4nOc7dUwZco/K7cS e6Mw== X-Gm-Message-State: APjAAAXGnWLLxFnI2FtLyEBeTKlVkW72IkLo0vGTyrwFx2ERr9YSGjiq k9zKy2K0Co16Sky+igW1di0Bdn4f X-Google-Smtp-Source: APXvYqyn+l/za8WAGIpJ+Kei3ZuEw4D1JvmfyeUO+L5wJuvvZsZVHN69vIfrPgVCoGbI6GAnUtQGCg== X-Received: by 2002:a1c:7014:: with SMTP id l20mr571824wmc.133.1565925227931; Thu, 15 Aug 2019 20:13:47 -0700 (PDT) Received: from localhost.localdomain (ipbcc08b69.dynamic.kabel-deutschland.de. [188.192.139.105]) by smtp.gmail.com with ESMTPSA id x20sm8501603wrg.10.2019.08.15.20.13.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Aug 2019 20:13:47 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 16 Aug 2019 05:05:24 +0200 Message-Id: <20190816030531.4775-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190816030531.4775-1-andreas.rheinhardt@gmail.com> References: <20190816030531.4775-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 04/11] avformat/utils: Try to reuse AvBufferRef 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" In case the parser flag PARSER_FLAG_COMPLETE_FRAMES is set, it is common that every input packet generates exactly one output packet whose underlying data coincides with the data of the input packet. If in this situation the input packet buffer is reference counted (as it usually is), the current code used to create a new reference to the underlying AVBuffer for the output packet. The old reference would be discarded afterwards when the input packet gets unreferenced. This has been changed: If it is known that no more output packets result from the current input packet, the input packet's reference is directly transferred to the last output packet, thus saving one malloc+free. Signed-off-by: Andreas Rheinhardt --- libavformat/utils.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/libavformat/utils.c b/libavformat/utils.c index b57e680089..64516a6a81 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -1478,10 +1478,17 @@ static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index) * to data in it and not in the parser's internal buffer. */ /* XXX: Ensure this is the case with all parsers when st->parser->flags * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */ - out_pkt.buf = av_buffer_ref(pkt->buf); - if (!out_pkt.buf) { - ret = AVERROR(ENOMEM); - goto fail; + if (size > 0) { + out_pkt.buf = av_buffer_ref(pkt->buf); + if (!out_pkt.buf) { + ret = AVERROR(ENOMEM); + goto fail; + } + } else { + /* flush_pkt does not have buf set, so size == 0 means + * that this is the last iteration of this loop. */ + out_pkt.buf = pkt->buf; + pkt->buf = NULL; } } else { ret = av_packet_make_refcounted(&out_pkt);