From patchwork Wed Apr 10 13:31:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 48000 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c90a:b0:1a7:a0dc:8de5 with SMTP id gx10csp556907pzb; Wed, 10 Apr 2024 06:32:06 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVVIfDJnxpRX2MXCUkFYSoyue+qRPmEpw+TKqjkBROHCgEGNLPk6KX657XqGqNm0ghuNQFStyKSrnLcmqPygTrlr4IvmW89jjlegg== X-Google-Smtp-Source: AGHT+IHkIWaclTOyd/AaB/AqTbggVHGiRxi2XnsWvAQgXMWArzsfxXBgrqChorpKbXt9LHKyqmiS X-Received: by 2002:a05:6512:371a:b0:513:aef5:b431 with SMTP id z26-20020a056512371a00b00513aef5b431mr1333731lfr.4.1712755926583; Wed, 10 Apr 2024 06:32:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1712755926; cv=none; d=google.com; s=arc-20160816; b=j/mejNQwY3Z0tOr88MNRWG8swnDdoCSh4phXhxYBkaHuVVCYKMZRcufhxiBDmliPPk Hbgbfqbu5EKhSwyoFhX15E0WaoNAkCtbsm3UwdxfhqnEuVTFVtu5A9rXdZfCgEfUzyyM 0+iGgQhz8yIxpp2/B6R7j/8CrKbG1U2tYNpF4veZJbxm78DpRBVjIUJAMlKPGNVvWmoq RQRz0XwwrG+hmyews6V6Eav0LYKEzjdeMH1JVEHMhDNPCSbPupR5s+5JqQxPHT2tW65o EAljFt1QCP7Nfn22FJZ6/VXTh98iwbOAq+8BprqStgEwtxUv+6pDa8+UbyjyA04l2+KB G0og== 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=Oc+ifo0mH44+JZfJb+qBCmK/re3Uw8g5saZPLg8AHJA=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=IcJ5SoymmB6etuWmOaN3YLiwl0sa57QerbDkNqhIf95i/4roobmQ2kqCn9DW7JxG3Q 8ZDolzpZenIK1IBfwF23pzVEjALRwoXgX12nEsaGKR35MLMF1Eaf5NcL/AGm8+gyRDWu 5V9W/NZy0Or8m36mfZ7OdO9DYW3F4LHLmcyYmjpEoW/CoQf2kYBagRcFtmwe+4Z879CI 84TcPurzzF4FQEbbIWQ16Ozvpn/0WLqP6ihPfqSnshlWPkaaU1lqi35d8uf0AFSkozjH mgCIf7/RkwlFxPfo8ccU89XgiQGiBzoHHGg3D34hDqpu1PomFRoKkSHXpLxNcWqFlSyM qkDA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@khirnov.net header.s=mail header.b=fRbYs34V; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id w18-20020a17090652d200b00a51c4d610e5si4137633ejn.641.2024.04.10.06.32.06; Wed, 10 Apr 2024 06:32:06 -0700 (PDT) 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=@khirnov.net header.s=mail header.b=fRbYs34V; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5077868CF46; Wed, 10 Apr 2024 16:31:37 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 60BEA68CEE6 for ; Wed, 10 Apr 2024 16:31:27 +0300 (EEST) Authentication-Results: mail1.khirnov.net; dkim=pass (2048-bit key; unprotected) header.d=khirnov.net header.i=@khirnov.net header.a=rsa-sha256 header.s=mail header.b=fRbYs34V; dkim-atps=neutral Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id 2B4824D6E for ; Wed, 10 Apr 2024 15:31:26 +0200 (CEST) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id urJuahZlDkS3 for ; Wed, 10 Apr 2024 15:31:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1712755884; bh=yD3xfZVzLgylUzG8gzFzdKJhWCsYUQ8tc8CCzHBUosQ=; h=From:To:Subject:Date:In-Reply-To:References:From; b=fRbYs34VWZqPr9uakoNveOyTO0GrdhvylqoS1md7w2CP7e91Ak0PX/LwiiZig+DXv BWUx7O0sgQjMqTzKejKoB2JVqivY0hQeruE5ZI/MMxT2W8PKSqQ1Osd0qOMYob4CmF 7LlrJqvlOp7GdxlLJ2nLyg9ZNc81EZggPX/3+zTtdEjxwr5B5sFxNB7NvcvJslqY3u UN4RXbz+4vpJnG8oBecDlSmRILdNkA1YCY6MssxCrF1duw8g4+Biu9g0jOiyU5g8UL 3O2xY3/ciLCr8+R6ZrbpuSy6Bars3q27NPGdUWonQLFj1m/LtDQrHKnfzOI0x1yzPw o/wNjh0dY4MeA== Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail1.khirnov.net (Postfix) with ESMTPS id B1B654D72 for ; Wed, 10 Apr 2024 15:31:24 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 9B92D3A03BC for ; Wed, 10 Apr 2024 15:31:24 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 10 Apr 2024 15:31:11 +0200 Message-ID: <20240410133118.28144-3-anton@khirnov.net> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240410133118.28144-1-anton@khirnov.net> References: <20240410133118.28144-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 03/10] lavc/hevcdec: allocate local_ctx as array of structs rather than pointers 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: AP9i/EXk2hYh It is more efficient and easier to manage. --- libavcodec/hevcdec.c | 57 +++++++++++++++++++++++++------------------- libavcodec/hevcdec.h | 2 +- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c index 55f72af972..47226ef0ab 100644 --- a/libavcodec/hevcdec.c +++ b/libavcodec/hevcdec.c @@ -2598,7 +2598,7 @@ static int hls_slice_data(HEVCContext *s) static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *hevc_lclist, int job, int self_id) { - HEVCLocalContext *lc = ((HEVCLocalContext**)hevc_lclist)[self_id]; + HEVCLocalContext *lc = &((HEVCLocalContext*)hevc_lclist)[self_id]; const HEVCContext *const s = lc->parent; int ctb_size = 1 << s->ps.sps->log2_ctb_size; int more_data = 1; @@ -2682,7 +2682,7 @@ static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal) { const uint8_t *data = nal->data; int length = nal->size; - HEVCLocalContext *lc = s->HEVClc; + HEVCLocalContext *lc; int *ret; int64_t offset; int64_t startheader, cmpt = 0; @@ -2696,19 +2696,31 @@ static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal) return AVERROR_INVALIDDATA; } - for (i = 1; i < s->threads_number; i++) { - if (i < s->nb_local_ctx) - continue; - s->local_ctx[i] = av_mallocz(sizeof(HEVCLocalContext)); - if (!s->local_ctx[i]) - return AVERROR(ENOMEM); - s->nb_local_ctx++; + if (s->threads_number > s->nb_local_ctx) { + HEVCLocalContext *tmp = av_malloc_array(s->threads_number, sizeof(*s->local_ctx)); - s->local_ctx[i]->logctx = s->avctx; - s->local_ctx[i]->parent = s; - s->local_ctx[i]->common_cabac_state = &s->cabac; + if (!tmp) + return AVERROR(ENOMEM); + + memcpy(tmp, s->local_ctx, sizeof(*s->local_ctx) * s->nb_local_ctx); + av_free(s->local_ctx); + s->local_ctx = tmp; + s->HEVClc = &s->local_ctx[0]; + + for (unsigned i = s->nb_local_ctx; i < s->threads_number; i++) { + tmp = &s->local_ctx[i]; + + memset(tmp, 0, sizeof(*tmp)); + + tmp->logctx = s->avctx; + tmp->parent = s; + tmp->common_cabac_state = &s->cabac; + } + + s->nb_local_ctx = s->threads_number; } + lc = &s->local_ctx[0]; offset = (lc->gb.index >> 3); for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) { @@ -2744,8 +2756,8 @@ static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal) s->data = data; for (i = 1; i < s->threads_number; i++) { - s->local_ctx[i]->first_qp_group = 1; - s->local_ctx[i]->qp_y = s->HEVClc->qp_y; + s->local_ctx[i].first_qp_group = 1; + s->local_ctx[i].qp_y = s->HEVClc->qp_y; } atomic_store(&s->wpp_err, 0); @@ -3476,12 +3488,6 @@ static av_cold int hevc_decode_free(AVCodecContext *avctx) av_freep(&s->sh.offset); av_freep(&s->sh.size); - if (s->local_ctx) { - for (i = 1; i < s->nb_local_ctx; i++) { - av_freep(&s->local_ctx[i]); - } - } - av_freep(&s->HEVClc); av_freep(&s->local_ctx); ff_h2645_packet_uninit(&s->pkt); @@ -3498,15 +3504,16 @@ static av_cold int hevc_init_context(AVCodecContext *avctx) s->avctx = avctx; - s->HEVClc = av_mallocz(sizeof(HEVCLocalContext)); - s->local_ctx = av_mallocz(sizeof(HEVCLocalContext*) * s->threads_number); - if (!s->HEVClc || !s->local_ctx) + s->local_ctx = av_mallocz(sizeof(*s->local_ctx)); + if (!s->local_ctx) return AVERROR(ENOMEM); + s->nb_local_ctx = 1; + + s->HEVClc = &s->local_ctx[0]; + s->HEVClc->parent = s; s->HEVClc->logctx = avctx; s->HEVClc->common_cabac_state = &s->cabac; - s->local_ctx[0] = s->HEVClc; - s->nb_local_ctx = 1; s->output_frame = av_frame_alloc(); if (!s->output_frame) diff --git a/libavcodec/hevcdec.h b/libavcodec/hevcdec.h index a881eb9981..5a4ed270e8 100644 --- a/libavcodec/hevcdec.h +++ b/libavcodec/hevcdec.h @@ -441,7 +441,7 @@ typedef struct HEVCContext { const AVClass *c; // needed by private avoptions AVCodecContext *avctx; - HEVCLocalContext **local_ctx; + HEVCLocalContext *local_ctx; unsigned nb_local_ctx; HEVCLocalContext *HEVClc;