From patchwork Sat Nov 19 21:35:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Kern X-Patchwork-Id: 39342 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:999a:b0:a4:2148:650a with SMTP id ve26csp1243478pzb; Sat, 19 Nov 2022 13:35:48 -0800 (PST) X-Google-Smtp-Source: AA0mqf7210HSEz1fXbf4MAWqrI8DghSLTCd+Is4UDtPyVl8ne+MfcxyyEGJSpzNVfdo76VWYOasC X-Received: by 2002:a17:906:2ac3:b0:7ad:f2f9:2b49 with SMTP id m3-20020a1709062ac300b007adf2f92b49mr9912819eje.94.1668893748759; Sat, 19 Nov 2022 13:35:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668893748; cv=none; d=google.com; s=arc-20160816; b=IGuBD1nHxMazPddFXuA0N2M/O7RhxbM62LJTMkZXb2AqnHreHIkbsuF6bCOF3rVLIE 9MSXvIqPxED6UvxKRi12L3M6Jcf+RV0CgwK2W/1f67pscQMBsPgfCk5DY4j4pM3qLPhI dDb/MFHYF9bjlrGvmwee+0D3QhPM6agSBSBcfYsXDiElov0IGdSl7JCU9PxvrWfj+MMu 80WGipeTWpKURW307qMRgdgHolULuDJfMXjlZATFgt0fvAEpCh7GXUTK2TMOqX9fBPKV zU0aYhTh1IG+1qTeijk4J7eDIw+TiOJdX++6+WvkPgy5J3FbDmGcaNa0Cd3vrEjbdyWX 8TOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=FBKvrSMMviokUVgGIHRiSk6Zu0IGjEq8ugYBxKHKIAw=; b=vNdS9gYXFf3Q4Y2aFAekcDd28FtjhIridZduqa6XpVKdXnq0XOJuhnObh1+E8zfaWz q2E+WJX4KozNiuH8BGKINbTJouuOoeyNQ9k8ynf/fVneKKWjc4NAxksnUgjfsOURn09V q7LdVe9tkwuO1epYkrmI5v1JB7Bb4Zr2UNdvbpMI5+L85qKNaSdCLhxN6csRJKXNyIR3 g4tPV7i52RlJNsoGeJPvh+2orwZhJTZVEjypfqToVmEb1QCXqu0ALoikD+UCk34Mxz3X XpPx6nGVCrGuVsn5D0050/HgcsEF6A7brjRp5Ab9cET/P7CnCbXlaaHFI+k25ZWoL7Ff T7mQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Ecwu9XmN; 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=QUARANTINE 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 d21-20020a1709067f1500b007707b853e46si4882964ejr.882.2022.11.19.13.35.48; Sat, 19 Nov 2022 13:35:48 -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=20210112 header.b=Ecwu9XmN; 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=QUARANTINE 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 4B00768B640; Sat, 19 Nov 2022 23:35:30 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7B6CE68A4CB for ; Sat, 19 Nov 2022 23:35:22 +0200 (EET) Received: by mail-qk1-f180.google.com with SMTP id k2so5795881qkk.7 for ; Sat, 19 Nov 2022 13:35:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=pAm+PXx0IXUSYmWkzc4qeKYCxMTNGNxtdOAVELKK0yU=; b=Ecwu9XmNMjm/fhoSMN3Q94wHvXDcj1KljxuogEArENbfvkaR01cNWk2H2QdZUv4/sq Kxk6nBZDbfaCN0RnYGF+XEY1x3YwoPMw2PFMQ5gfV4hM92oTNbT8u+aLsiybj48tQprT MVkDR5vUES9zlgn6p271zXB377DOs4+sLWGmu0lRfRjEDOZed7q1fpOijaOoMoeNu4a9 W9hglKrZX5hwxJDcS6YehRKTvd0yrOQMC17E3PNQLtc99GU4qDL80SgJxwE5JuUXT0Yk d0AVvvJqrz2PkCW1aBoi0HYtxImj+UESUwrYV4xNQJcz/8uN5NO6cK7jOVP99M+05d/2 Eucg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pAm+PXx0IXUSYmWkzc4qeKYCxMTNGNxtdOAVELKK0yU=; b=k5a87Ks3D1x9ehKZMB5Ciplmp1tTL1ZD0AHQLbKkdQjJm9KOvWFvhEQXwGMxRHmNK6 0VdO2uBnFui3b5j6cmmTFwjH26do/Exzny4EK8b3JO0J72oUDl/Onjs7IpO7tSAl6nhi kw7LYNo6FTCGXA8IhaEhTbNEvZ+jaHZkYUme6UTEoTi66gJ2M8gN+L+yhtXSAzLzF50m UYvqQnSgAog3KjOvIPoQW2+VgAlISZa8W8m1nmXXGwZ24EpbOZQS8fDcRsqLyYf9nZZR Q27v+EahEptCBQOBhEOLSDgtvbMoTGq+LNwmS5yYKe7ZQtKcIER0C0mvSFRqRZjxYtwr ygWQ== X-Gm-Message-State: ANoB5pnUTo345BAm+e9mm5+DLiGO0L7cULIqfjqjSDq4ZxWji83y4NiT rr/u2+WrO8H2IiuMugXw/pEyIHb5GdM= X-Received: by 2002:a05:620a:488d:b0:6fa:74df:f8de with SMTP id ea13-20020a05620a488d00b006fa74dff8demr11137999qkb.7.1668893720840; Sat, 19 Nov 2022 13:35:20 -0800 (PST) Received: from localhost.localdomain ([50.201.95.250]) by smtp.gmail.com with ESMTPSA id w128-20020a379486000000b006cf19068261sm4954756qkd.116.2022.11.19.13.35.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Nov 2022 13:35:20 -0800 (PST) From: Rick Kern To: ffmpeg-devel@ffmpeg.org Date: Sat, 19 Nov 2022 16:35:16 -0500 Message-Id: <20221119213516.45148-3-kernrj@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221119213516.45148-1-kernrj@gmail.com> References: <20221119213516.45148-1-kernrj@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] lavc/videotoolboxenc: use objectpool for encoder output information X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: GnFiikwgWDnX Instead of allocating/freeing, use objpool for data passed from an input frame to an output packet. Signed-off-by: Rick Kern --- libavcodec/videotoolboxenc.c | 80 ++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 9 deletions(-) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index dc9e321d3d..205cbfbc68 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -24,6 +24,7 @@ #include #include #include "avcodec.h" +#include "libavutil/objpool.h" #include "libavutil/opt.h" #include "libavutil/avassert.h" #include "libavutil/avstring.h" @@ -251,8 +252,49 @@ typedef struct VTEncContext { /* can't be bool type since AVOption will access it as int */ int a53_cc; + + ObjPool *output_node_pool; } VTEncContext; +static BufNode *output_node_alloc(void) { return av_mallocz(sizeof(BufNode)); } +static void output_node_free(BufNode **node) { av_freep(node); } +static void output_node_reset(BufNode *node) { + if (node->cm_buffer != NULL) { + CFRelease(node->cm_buffer); + } + + if (node->sei != NULL) { + if (node->sei->data != NULL) { + av_free(node->sei->data); + } + + av_free(node->sei); + } + + memset(node, 0, sizeof(*node)); +} + +static int alloc_output_node_pool(ObjPool **pool_out) { + ObjPool *pool = NULL; + + if (pool_out == NULL) { + return AVERROR(EINVAL); + } + + pool = objpool_alloc( + output_node_alloc, + output_node_reset, + output_node_free); + + if (pool == NULL) { + return AVERROR(ENOMEM); + } + + *pool_out = pool; + + return 0; +} + static int vtenc_populate_extradata(AVCodecContext *avctx, CMVideoCodecType codec_type, CFStringRef profile_level, @@ -285,8 +327,7 @@ static void set_async_error(VTEncContext *vtctx, int err) while (info) { BufNode *next = info->next; - CFRelease(info->cm_buffer); - av_free(info); + objpool_release(vtctx->output_node_pool, &info); info = next; } @@ -336,22 +377,29 @@ static int vtenc_q_pop(VTEncContext *vtctx, bool wait, CMSampleBufferRef *buf, E pthread_mutex_unlock(&vtctx->lock); *buf = info->cm_buffer; + if (*buf != NULL) { + CFRetain(*buf); + } + if (sei && *buf) { *sei = info->sei; - } else if (info->sei) { - if (info->sei->data) av_free(info->sei->data); - av_free(info->sei); + info->sei = NULL; } - av_free(info); + objpool_release(vtctx->output_node_pool, &info); return 0; } static void vtenc_q_push(VTEncContext *vtctx, CMSampleBufferRef buffer, ExtraSEI *sei) { - BufNode *info = av_malloc(sizeof(BufNode)); - if (!info) { + BufNode *info = NULL; + int status = objpool_get(vtctx->output_node_pool, &info); + + if (status < 0) { + set_async_error(vtctx, status); + return; + } else if (!info) { set_async_error(vtctx, AVERROR(ENOMEM)); return; } @@ -1589,7 +1637,17 @@ static av_cold int vtenc_init(AVCodecContext *avctx) } avctx->has_b_frames = vtctx->has_b_frames; - return 0; + status = alloc_output_node_pool(&vtctx->output_node_pool); + + if (status < 0) { + av_log( + avctx, + AV_LOG_ERROR, + "Failed to allocate output pool (%d)\n", + status); + } + + return status; } static void vtenc_get_frame_info(CMSampleBufferRef buffer, bool *is_key_frame) @@ -2647,6 +2705,10 @@ static av_cold int vtenc_close(AVCodecContext *avctx) vtctx->ycbcr_matrix = NULL; } + if (vtctx->output_node_pool != NULL) { + objpool_free(&vtctx->output_node_pool); + } + return 0; }