From patchwork Thu Oct 17 08:29:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 15807 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 2FFE1449FA2 for ; Thu, 17 Oct 2019 11:30:57 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1340F68A7DA; Thu, 17 Oct 2019 11:30:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B488F68A754 for ; Thu, 17 Oct 2019 11:30:49 +0300 (EEST) Received: by mail-wm1-f66.google.com with SMTP id r17so6465978wme.0 for ; Thu, 17 Oct 2019 01:30:49 -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=VSEB2TIn1+omW6kRlKpEqcozRtlqliYaS1meABeivAA=; b=Raqzf7l0xxjV9Dika8vhIxm68yVMrROsAK4adWUtJX0pZMpkds3YliFRc/M2BV1QHO iT0dWYXqu9js/DulW36A/Nd2gh/cAjPBmvKhdogzEhKXCLeI+nhrOdabu03QTyEwu4t6 h0NGi2UV1HE2rKxkIaxDZLXIdCznfQ13fCIDpkx+nJdlWQUSeSl824tv53YTkKjxWY2f XvVZOMc/QJs1LfsBxHOqYf6sn/kh5+VWRe5muU4hGgOaxjWUPfqXGomVZJcjb0rWzqky btmUgT+nwcEi4xi7y+LMDlyfd1MoYOeOwYiLPd+GnMUsM+kfD/Y4Jfy+kb90hPPWjjC6 cnnA== 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=VSEB2TIn1+omW6kRlKpEqcozRtlqliYaS1meABeivAA=; b=ee451iZ086cp+M/sFv5Ca/wHIpNZb4WtKAT8LHrRb4A7h6gG35fL3EfMD8x8dHXTya WBUHfMDAMbAERDdH00Rn03fdWEW32YxO8BWRE56Dyk92y+o/QimzFeCNnJ5COKTZ+8/9 EXfwXRlAFaeLUFYpkcbfQzOEKsVFJ5muzPhnwCSm9wi+HFPcuToNn7rzPOKDPrckA24a n3DK/hth16UsqlxChJXtz0+HM3hjFmdPvd6b/Isx+ewcWFGckwBCsjAXDXkLZviWb8x5 4aqU57KRNOborXusnBS9Nh2TgrCQH+/+h71+Y6b3iVoZddUGsGDrGsPlompVe8DDhj6f bjng== X-Gm-Message-State: APjAAAVyckkzJ8dH0NMx2UlxVEgjKRndurGYA1vgDYbRHNk/zYa5W9av wRpWsAXBnh2NF+e9uBw0eqsFmlRp X-Google-Smtp-Source: APXvYqyXiZ0gvpB/JBw9UBvv2PsysqYA+MhqiObxopYuzlVD3n185Mj6VBNEVeewsVxUXBWOmtQdog== X-Received: by 2002:a1c:7e10:: with SMTP id z16mr1712215wmc.11.1571301048948; Thu, 17 Oct 2019 01:30:48 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08937.dynamic.kabel-deutschland.de. [188.192.137.55]) by smtp.gmail.com with ESMTPSA id l7sm1369273wrv.77.2019.10.17.01.30.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2019 01:30:48 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 17 Oct 2019 10:29:45 +0200 Message-Id: <20191017082945.13534-15-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191017082945.13534-1-andreas.rheinhardt@gmail.com> References: <20191017082945.13534-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 15/15] vp9_superframe: Avoid allocations and copies of packet structures 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" Up until now, the vp9_superframe bsf used distinct packets for input and output. But at no point in the bsf are the input and output packets used at the same time (except during a call to av_packet_move_ref()), so that one can avoid using two packets if one switches to ff_bsf_get_packet_ref at the same time. This also saves one malloc+free of an AVPacket structure per filtered packet. Signed-off-by: Andreas Rheinhardt --- libavcodec/vp9_superframe_bsf.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/libavcodec/vp9_superframe_bsf.c b/libavcodec/vp9_superframe_bsf.c index 23933d4136..b79911bb2c 100644 --- a/libavcodec/vp9_superframe_bsf.c +++ b/libavcodec/vp9_superframe_bsf.c @@ -97,26 +97,25 @@ static int merge_superframe(AVPacket * const *in, int n_in, AVPacket *out) return 0; } -static int vp9_superframe_filter(AVBSFContext *ctx, AVPacket *out) +static int vp9_superframe_filter(AVBSFContext *ctx, AVPacket *pkt) { GetBitContext gb; VP9BSFContext *s = ctx->priv_data; - AVPacket *in; int res, invisible, profile, marker, uses_superframe_syntax = 0, n; - res = ff_bsf_get_packet(ctx, &in); + res = ff_bsf_get_packet_ref(ctx, pkt); if (res < 0) return res; - marker = in->data[in->size - 1]; + marker = pkt->data[pkt->size - 1]; if ((marker & 0xe0) == 0xc0) { int nbytes = 1 + ((marker >> 3) & 0x3); int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes; - uses_superframe_syntax = in->size >= idx_sz && in->data[in->size - idx_sz] == marker; + uses_superframe_syntax = pkt->size >= idx_sz && pkt->data[pkt->size - idx_sz] == marker; } - if ((res = init_get_bits8(&gb, in->data, in->size)) < 0) + if ((res = init_get_bits8(&gb, pkt->data, pkt->size)) < 0) goto done; get_bits(&gb, 2); // frame marker @@ -138,8 +137,7 @@ static int vp9_superframe_filter(AVBSFContext *ctx, AVPacket *out) goto done; } else if ((!invisible || uses_superframe_syntax) && !s->n_cache) { // passthrough - av_packet_move_ref(out, in); - goto done; + return 0; } else if (s->n_cache + 1 >= MAX_CACHE) { av_log(ctx, AV_LOG_ERROR, "Too many invisible frames\n"); @@ -147,19 +145,18 @@ static int vp9_superframe_filter(AVBSFContext *ctx, AVPacket *out) goto done; } - av_packet_move_ref(s->cache[s->n_cache++], in); + av_packet_move_ref(s->cache[s->n_cache++], pkt); if (invisible) { - res = AVERROR(EAGAIN); - goto done; + return AVERROR(EAGAIN); } av_assert0(s->n_cache > 0); // build superframe - if ((res = merge_superframe(s->cache, s->n_cache, out)) < 0) + if ((res = merge_superframe(s->cache, s->n_cache, pkt)) < 0) goto done; - res = av_packet_copy_props(out, s->cache[s->n_cache - 1]); + res = av_packet_copy_props(pkt, s->cache[s->n_cache - 1]); if (res < 0) goto done; @@ -169,8 +166,7 @@ static int vp9_superframe_filter(AVBSFContext *ctx, AVPacket *out) done: if (res < 0) - av_packet_unref(out); - av_packet_free(&in); + av_packet_unref(pkt); return res; }