From patchwork Sat Dec 3 15:50:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Kern X-Patchwork-Id: 39575 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:999a:b0:a4:2148:650a with SMTP id ve26csp2076969pzb; Sat, 3 Dec 2022 07:51:30 -0800 (PST) X-Google-Smtp-Source: AA0mqf7HxHVF8tp5JPiIUfQDMLuCxEiYBR+hdKAwDJJuDcgJs3CM/hJ8vU3bqSt33rH+6v214RY7 X-Received: by 2002:a17:906:d295:b0:7c0:aff1:f1b6 with SMTP id ay21-20020a170906d29500b007c0aff1f1b6mr9762061ejb.418.1670082689851; Sat, 03 Dec 2022 07:51:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670082689; cv=none; d=google.com; s=arc-20160816; b=RrIeD1kJ1gJM0WaABPckfrUTr5byVBBeHqdTHBiB+6PS/LRLzfk4PCfG/ZHyacUxXV guMYN+ZldYkXEdFFGGxndTT7lJL2/sNy1GfBHS+ATum+M2fAB5RqQH2C4ZmrWyjdICOW JvybM8/qf1bzpmyZx0X20wTefJTJszYVHUPbkPm7QwGBZIcD1cory3RqfIsXklTwgbSv BOEFWtIotxSCQd8NAD2OYHzIJxWaRfmbgCvtogQX+S5XwX+IvWygF/8W7qYmZbRvTI1U wzrf4R722a/zFOOzJOoOMONwWrfQ7WUjxdaj7oZAoj7U4djMTFJWMnaYtorShzQumb8e 7Nag== 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=NbkJzjVLnLyKLBBQDtq2/O/iLULJTVwwEt83+WPz4Gg=; b=Nu/bbDp0OHgAV+nGpCwY121Q4wscw9lfiMrpC0hj4lJIrZsXois25krpjJU57Y9lPr UW4mX+Z6hyYwRUnuqk09sSilXTL5qBnQk+acdMX3lVITtLicLs4m3d6CMAkc5Kh+8NDD VcAsu/9Nb0xB2kDJo4mMwDtIUbQqADD+V+fSzk2rScU3w9qP+SvTuvRF8/t8g9UMMFti 7bTGl43uVFCATmKyOqN9KCKn+VsJOef9+w4YT3oTe/bD3D+iEL0ahp9EG1FoKwt0VVFu /us6WDADUPyw2+uo5e49SBl29Bx7O6yLTcskih/aSuxONkRON7vPy6i69YnLwfFr2Npk GPLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=lv+foLZv; 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 qa16-20020a170907869000b007b9bf05f0edsi8203813ejc.520.2022.12.03.07.51.29; Sat, 03 Dec 2022 07:51:29 -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=lv+foLZv; 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 C054568B834; Sat, 3 Dec 2022 17:51:10 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f172.google.com (mail-qt1-f172.google.com [209.85.160.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5398868B92B for ; Sat, 3 Dec 2022 17:51:02 +0200 (EET) Received: by mail-qt1-f172.google.com with SMTP id e15so8721323qts.1 for ; Sat, 03 Dec 2022 07:51:02 -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=BGYheYaUIEH/EglLnbtgBVPl6dnxrEP+1VBI94ysfRI=; b=lv+foLZvJwEqY3VORRwIoaFCCIm/DUMpw391pfURQY6eHA+P4KdW5/0HlMkp7UR9/U rDV35BhJq3QC4PqZuEloalG5Vl88eMB163ux5O+dbQeUJsxK/O7pc4uxE7C6MDW5urtx gusC+kO3HZ3nwWYtFZJ4NowvP6Tw3N776qZl23zcNdFCz1mWNDuJBxYhTeX3gOpSjhBx W3X2MJJZlvvOgzU99R5tNzR7lX6e1+eroXLVMIrS7FtUYKIz5WfQcGJ0VcgCduGLhpd1 Oe7gJK+Dv1dHHKWWxcFxsDDIjOiglV9Hcmn+M5USVTpk9IHtYhbvupxZcOuqVqMaMn64 Fbug== 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=BGYheYaUIEH/EglLnbtgBVPl6dnxrEP+1VBI94ysfRI=; b=7H/f3+4Tdc8VRd2nTEqH2MZ+vgoPke53RKtOdngzEKP5ps9f9FyK8XaGpnrTcHjW7W dte0PoqwvLT//iSZ4GhZOGFb08+v6Te0f0UCDPcXGXlARdvca/azTsXGQUUZK8wONOyb P9RpPz087CkZPxJ6SrMUvH7A+WsH+9Ii5DYoLpDIXyIHAcnR1XWJvlkT1qslX8Oxs9Gd 06R2nMNaBaIhHZso5ep6sLTYjOHA94eW0M1IqpEpDUxoQnQL7J3PbF9iRLRGvAK79iYC YVNFigFX5ym7/uJX16p6Uk12Ve7pEJgjFEaQtWe3mHxsaEUSp4dOfOMPL9fhoerYkxcS vfdw== X-Gm-Message-State: ANoB5pnC7WuPpbEbwLVpb5MRO9AuWuj1c0HtVP9N0Hy74DdLZr+vtYT5 jAz3Aua4T/bCwxLsVOykTiTi++Z+GBMKrg== X-Received: by 2002:a05:622a:4892:b0:3a5:252c:2bcc with SMTP id fc18-20020a05622a489200b003a5252c2bccmr20754380qtb.224.1670082660639; Sat, 03 Dec 2022 07:51:00 -0800 (PST) Received: from localhost.localdomain ([2603:3007:2608:b900:38fa:c53b:cbdc:b66a]) by smtp.gmail.com with ESMTPSA id h4-20020a05620a284400b006cfc7f9eea0sm8156531qkp.122.2022.12.03.07.50.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Dec 2022 07:50:59 -0800 (PST) From: Rick Kern To: ffmpeg-devel@ffmpeg.org Date: Sat, 3 Dec 2022 10:50:56 -0500 Message-Id: <20221203155056.74093-3-kernrj@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221203155056.74093-1-kernrj@gmail.com> References: <20221203155056.74093-1-kernrj@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 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: h484yquTsq28 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..184feb029b 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; + + AVObjPool *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(AVObjPool **pool_out) { + AVObjPool *pool = NULL; + + if (pool_out == NULL) { + return AVERROR(EINVAL); + } + + pool = av_objpool_alloc( + (AVObjPoolCBAlloc)output_node_alloc, + (AVObjPoolCBReset)output_node_reset, + (AVObjPoolCBFree)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); + av_objpool_release(vtctx->output_node_pool, (void**)&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); + av_objpool_release(vtctx->output_node_pool, (void**)&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 = av_objpool_get(vtctx->output_node_pool, (void**)&info); + + if (status < 0) { + set_async_error(vtctx, status); + return; + } else if (!info) { set_async_error(vtctx, AVERROR(ENOMEM)); return; } @@ -1569,6 +1617,16 @@ static av_cold int vtenc_init(AVCodecContext *avctx) pthread_mutex_init(&vtctx->lock, NULL); pthread_cond_init(&vtctx->cv_sample_sent, NULL); + 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); + } + vtctx->session = NULL; status = vtenc_configure_encoder(avctx); if (status) return status; @@ -1589,7 +1647,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx) } avctx->has_b_frames = vtctx->has_b_frames; - return 0; + 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) { + av_objpool_free(&vtctx->output_node_pool); + } + return 0; }