From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 636E4493EE for ; Fri, 9 Feb 2024 19:30:47 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4607568D081; Fri, 9 Feb 2024 21:30:45 +0200 (EET) Received: from w4.tutanota.de (w4.tutanota.de [81.3.6.165]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6050968CEB8 for ; Fri, 9 Feb 2024 21:30:39 +0200 (EET) Received: from tutadb.w10.tutanota.de (unknown [192.168.1.10]) by w4.tutanota.de (Postfix) with ESMTP id 09D3A10601E3 for ; Fri, 9 Feb 2024 19:30:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1707507038; s=s1; d=lynne.ee; h=From:From:To:To:Subject:Subject:Content-Description:Content-ID:Content-Type:Content-Type:Content-Transfer-Encoding:Content-Transfer-Encoding:Cc:Date:Date:In-Reply-To:In-Reply-To:MIME-Version:MIME-Version:Message-ID:Message-ID:Reply-To:References:References:Sender; bh=gsaIHWFoS0CCApLSAeivojkTePAGf8k+kvIyBbrtfh8=; b=rfTtabJb0i/o/h36oALReiIBFZtG2/T3NAI/hLFIgK+9N9k/0bhPEZwgMfOa5Hld doGW9pf2gvyT/OY+wQTgVn7f6D1K2kEZuaalLTAmYs7vJdOk+i0btqo1twjoIZ2XBMh Shaz6lce1z4VbqZ2cZqJSVNFuehbs2sIaJzfD7uu0xuL1Bsg/uHaMU8/VHVoDSpbJsh fjMz8Bnee0n+g1WKKgFFrWEANh7xAT2NGgAKV26hlkASuU84AX962KdmpAdpUAYao4k VG7XPs+C6TxFp6bZKMCtzz0sBDAhL5CuOK4LHn3hOX/nNcl2h7X+SCsZzK+JgBUjwKC jzlxCqY/rA== Date: Fri, 9 Feb 2024 20:30:38 +0100 (CET) From: Lynne To: FFmpeg development discussions and patches Message-ID: In-Reply-To: References: MIME-Version: 1.0 Subject: Re: [FFmpeg-devel] [PATCH] avfft: avoid overreads with RDFT API users 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: Feb 9, 2024, 20:19 by matthieu.bouron@gmail.com: > On Fri, Feb 09, 2024 at 06:27:50PM +0100, Lynne wrote: > >> The new API requires an extra array member at the very end, >> which old API users did not do. >> >> This disables in-place RDFT transforms and instead >> does the transform out of place by copying once, there shouldn't >> be a significant loss of speed as our in-place FFT requires a reorder >> which is likely more expensive in the majority of cases to do. >> >> Patch attached. >> >> From 763f2e8941dc3dd4282ad42574bd8d451a256a53 Mon Sep 17 00:00:00 2001 >> From: Lynne >> Date: Fri, 9 Feb 2024 18:17:54 +0100 >> Subject: [PATCH] avfft: avoid overreads with RDFT API users >> >> The new API requires an extra array member at the very end, >> which old API users did not do. >> >> This disables in-place RDFT transforms and instead >> does the transform out of place by copying once, there shouldn't >> be a significant loss of speed as our in-place FFT requires a reorder >> which is likely more expensive in the majority of cases to do. >> --- >> libavcodec/avfft.c | 31 +++++++++++++++++++++++++------ >> 1 file changed, 25 insertions(+), 6 deletions(-) >> >> diff --git a/libavcodec/avfft.c b/libavcodec/avfft.c >> index 999b5ed79a..485f216427 100644 >> --- a/libavcodec/avfft.c >> +++ b/libavcodec/avfft.c >> @@ -152,7 +152,7 @@ RDFTContext *av_rdft_init(int nbits, enum RDFTransformType trans) >> return NULL; >> >> ret = av_tx_init(&s->ctx, &s->fn, AV_TX_FLOAT_RDFT, trans == IDFT_C2R, >> - 1 << nbits, &scale, AV_TX_INPLACE); >> + 1 << nbits, &scale, 0x0); >> if (ret < 0) { >> av_free(s); >> return NULL; >> @@ -162,17 +162,35 @@ RDFTContext *av_rdft_init(int nbits, enum RDFTransformType trans) >> s->len = 1 << nbits; >> s->inv = trans == IDFT_C2R; >> >> + s->tmp = av_malloc((s->len + 2)*sizeof(float)); >> + if (!s->tmp) { >> + av_tx_uninit(&s->ctx); >> + av_free(s); >> + return NULL; >> + } >> + >> return (RDFTContext *)s; >> } >> >> void av_rdft_calc(RDFTContext *s, FFTSample *data) >> { >> AVTXWrapper *w = (AVTXWrapper *)s; >> - if (w->inv) >> - FFSWAP(float, data[1], data[w->len]); >> - w->fn(w->ctx, data, (void *)data, w->stride); >> - if (!w->inv) >> - FFSWAP(float, data[1], data[w->len]); >> + float *src = w->inv ? w->tmp : (float *)data; >> + float *dst = w->inv ? (float *)data : w->tmp; >> + >> + if (w->inv) { >> + memcpy(src, data, w->len*sizeof(float)); >> + >> + src[w->len] = src[1]; >> + src[1] = 0.0f; >> + } >> + >> + w->fn(w->ctx, dst, (void *)src, w->stride); >> + >> + if (!w->inv) { >> + dst[1] = dst[w->len]; >> + memcpy(data, dst, w->len*sizeof(float)); >> + } >> } >> >> av_cold void av_rdft_end(RDFTContext *s) >> @@ -180,6 +198,7 @@ av_cold void av_rdft_end(RDFTContext *s) >> if (s) { >> AVTXWrapper *w = (AVTXWrapper *)s; >> av_tx_uninit(&w->ctx); >> + av_free(&w->tmp); >> > > av_free(w->tmp); > Yup, discussed on IRC. >> av_free(w); >> } >> } >> -- >> 2.43.0.381.gb435a96ce8 >> > > Hi, > > I tested the patch with the av_free() adjustment and it fixed the crash / > memory corruption I was encountering on macOS. > Thanks, will push soon. _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".