From patchwork Mon Apr 29 00:35:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 48346 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:1509:b0:1a9:af23:56c1 with SMTP id nq9csp1698381pzb; Sun, 28 Apr 2024 17:35:57 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW8famz+lUHg30lEXa+JqV2oeGr3lrOyT01TDuPs/bS/K0vVraVrfHZv5snVNdt6QoJCQ2AllznrUE5Fa8b60bqj/kjYB4B3NBhIQ== X-Google-Smtp-Source: AGHT+IEbb6fsI9+2sZTdZ6uOfUh/GZ8Gk7vH1KK9gzJ2Q6fS9WJO7WkwFcgQ8Bt8zH9pLyM1vf2Y X-Received: by 2002:a19:7604:0:b0:51b:d382:9fe6 with SMTP id c4-20020a197604000000b0051bd3829fe6mr5087858lff.47.1714350957442; Sun, 28 Apr 2024 17:35:57 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id t12-20020ac24c0c000000b00518755e4af2si7825490lfq.382.2024.04.28.17.35.56; Sun, 28 Apr 2024 17:35:57 -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=@outlook.com header.s=selector1 header.b=uTMpkP4t; arc=fail (body hash mismatch); 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=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B8D5868D41F; Mon, 29 Apr 2024 03:35:52 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR03-AM7-obe.outbound.protection.outlook.com (mail-am7eur03olkn2092.outbound.protection.outlook.com [40.92.59.92]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1D42268CFCE for ; Mon, 29 Apr 2024 03:35:44 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=X9pHzKczfTV9IwoW7wK6l9ow/ao5ixDaN/2AvJK3P84hpv8C6cWCiLKNb9oLWhBrvz8AfpvBOE/+ObAT2XCMtRDO8hPhoSo+IIOl+ewtkFzwXAJlDwo8lLo+FBbp711I+H9HyiW5N2JGLzqa6vG00nTL/fjUftciNsetYbQznlICkYeJ6zMXdjysQ25dKjYfNRA3sWO22kOOhYT33KkcoyqA+D/MaCudoxteYuaq4E23jEH2sA49ini1G163HO2vXNZIZhY5/esth9hqNtcWQGMzQy9X1peo6KNEyPjtVrK4VtGJnUEuxOGEiQ1tCllAXr9vv9x0Dxd8rgDkZ8V7Ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=aM9pl8FHXpPkkgmMoCbkUeRCNyUyR5yFb7KhPFrtNAI=; b=i187LKQWl34BKkHx5gRbb79rUU+KAXgRc3Emp/ynCU4QbS7Lt+nXCb0x7jmo5FY7lM8g/tbDoTsFtu1C12+7MXTB2k9nQNlu9cmJQmps7p6pwKeWIqusbf0E4+DsqgKAGK6kcvD5xaC64qZlVH7Yhf5roLRJvVXad9qfQzz23q4FDM/qFlRTa/wWctcRnQWKiPgUGTeMUF7y2ZGSIRsVgbjfGVyQfHTyPs9jESE7s3uG2k0f9M4WWwuAS/l6cA3DjM3d3WxdhwzcLDWmfXTlBeuHHXYiBb5MHWMZevCrHIY6NYXZYhxSNgoK9UaobnG4PVAFOwH9ZgygLcVeIs0owA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=aM9pl8FHXpPkkgmMoCbkUeRCNyUyR5yFb7KhPFrtNAI=; b=uTMpkP4tb/BIOShbSFUlmUs37md4H8Mm3QzxmREfBX7QCJ6S/tdRygwhuPNYcB5RI4hWoBQ57fPw3c3wJCOIM2w/GSfA6gvHq8Bt4Tq+rmQpl3Aaxa5QLkIunMMCxOvYDNLPEp8308CZfkks088daSZqoKhdXjKeWXGh9+CV2ItXPxSyGIpwo6a6xxmdEjLlfEcOjyoannMy6509/vZnKHAxb9cCZfICwdO7oYfXyxk9lNUl3sh42lmr/sM09ZrJnnmC36vqeaz/oTR318OU5CO/IIN2Z5o2eh2W7IikcTgyFa6e4Ib6mwtYHmPoC/sNVCUZTuJ/bvZPkvmi54YvkA== Received: from GV1SPRMB0021.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:60::6) by PR3P250MB0068.EURP250.PROD.OUTLOOK.COM (2603:10a6:102:173::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.34; Mon, 29 Apr 2024 00:35:43 +0000 Received: from GV1SPRMB0021.EURP250.PROD.OUTLOOK.COM ([fe80::4eb:f54f:b451:34fd]) by GV1SPRMB0021.EURP250.PROD.OUTLOOK.COM ([fe80::4eb:f54f:b451:34fd%4]) with mapi id 15.20.7519.021; Mon, 29 Apr 2024 00:35:43 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 29 Apr 2024 02:35:23 +0200 Message-ID: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: X-TMN: [JLfl0FT9WJKMr9d3xV9Zu263BeHTVzK04l36baRUngg=] X-ClientProxiedBy: FR2P281CA0080.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:9a::13) To GV1SPRMB0021.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:60::6) X-Microsoft-Original-Message-ID: <20240429003535.1516058-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV1SPRMB0021:EE_|PR3P250MB0068:EE_ X-MS-Office365-Filtering-Correlation-Id: f790ac0b-a16b-4567-ec55-08dc67e44d91 X-Microsoft-Antispam: BCL:0; ARA:14566002|461199019|440099019|3412199016|56899024|1710799017; X-Microsoft-Antispam-Message-Info: HTPv05ozUH4jvTNR+TIVn3ztlRwCCXnlIxCmbSVXEf7H4CQfnyVErnUO88oofPwEDB57rr/7mJCvPa6sfxX8OartNyjOKGPTI7RBQBIINvD4fQF+IxNNtK0G4sIIkVK/wVOYCPZXeCV2+/5G7lYyIEFhrifRvLs68mBdkPIkgDQqaE0j8Vhp07jgZrQzjK8QUoY9xnIvv0iXwT21DIS1wTKTFWCakAhqWibRxhl7UAYudb6CY5R0DgcvMEz8/xKsANgWmlDf8ln8Aw6B068L24D0yIX6VbYkBM6t87DgDcL9DLOq2rWrSLnf1Nuln5mbZyOZq1mrHAd2W/biwnC6tV2aiYiHEpdvEKrISWM40FNLbdb0PLROWJp/nUkwP+U5W8CmSH/CoM3KGlsuKLaBtbYJXBUDPyuj5NkrhBoofmCCPBhhailcRK1LVOnkZBcx+H0SbyNghoHoxRGSrLI6VHSu/TMK61t1uPjtUt57XkXA2XzySc4XnpND2DjfMaG8LK9hdQg5meQ5WE4Isq/+wp2o0xC+D0gLjELKYaju6xPzHQoYvo0B7jMlEDnOnYTclNCveVFVAnCmZakJc0GzwkLYADWv6yYcIINrLDcSS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HKONxDf7ki3edm25rcOYfjbyP2CzXGG+PgjPBiLqJliXeej15e6hRFrNxu4HunYNYSkMhmqUlDF0j58H0kVjCadXvpg4nC+A8mKdNlsyVmJ5fxHhcpf2JNzB7A5oUccEU5XSCz0qmihgBeokMVUMRnjZFWMg3uP1nTzjY5dqlPDrLgUE0+s71SFtIrkvjJavr+FIbIQWwlYfPF6cqfKnnH0dNKjAZWuk9XIdHg/+Rny4OxthAad5BNWN4+nf5gleHQ2M4JcW2miR4ehMjn1F/TEWBplpdosakX97og3dF+FDLobz/mVVFQw== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: f790ac0b-a16b-4567-ec55-08dc67e44d91 X-MS-Exchange-CrossTenant-AuthSource: GV1SPRMB0021.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Apr 2024 00:35:43.3036 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3P250MB0068 Subject: [FFmpeg-devel] [PATCH 02/14] avcodec/mpegpicture: Store linesize in ScratchpadContext 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: x2tdcaNFT/Bo The mpegvideo-based codecs currently require the linesize to be constant (except when the frame dimensions change); one reason for this is that certain scratch buffers whose size depend on linesize are only allocated once and are presumed to be correctly sized if the pointers are != NULL. This commit changes this by storing the actual linesize these buffers belong to and reallocating the buffers if it does not suffice. This is not enough to actually support changing linesizes, but it is a start. And it is a prerequisite for the next patch. Signed-off-by: Andreas Rheinhardt --- libavcodec/mpegpicture.c | 19 ++++++++++++++----- libavcodec/mpegpicture.h | 1 + libavcodec/mpegvideo.c | 19 +++++++------------ libavcodec/mpegvideo_dec.c | 19 +++++++------------ 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/libavcodec/mpegpicture.c b/libavcodec/mpegpicture.c index 06b6daa01a..aa882cf747 100644 --- a/libavcodec/mpegpicture.c +++ b/libavcodec/mpegpicture.c @@ -89,12 +89,16 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me, ScratchpadContext *sc, int linesize) { # define EMU_EDGE_HEIGHT (4 * 70) - int alloc_size = FFALIGN(FFABS(linesize) + 64, 32); + int linesizeabs = FFABS(linesize); + int alloc_size = FFALIGN(linesizeabs + 64, 32); + + if (linesizeabs <= sc->linesize) + return 0; if (avctx->hwaccel) return 0; - if (linesize < 24) { + if (linesizeabs < 24) { av_log(avctx, AV_LOG_ERROR, "Image too small, temporary buffers cannot function\n"); return AVERROR_PATCHWELCOME; } @@ -102,6 +106,9 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me, if (av_image_check_size2(alloc_size, EMU_EDGE_HEIGHT, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0) return AVERROR(ENOMEM); + av_freep(&sc->edge_emu_buffer); + av_freep(&me->scratchpad); + // edge emu needs blocksize + filter length - 1 // (= 17x17 for halfpel / 21x21 for H.264) // VC-1 computes luma and chroma simultaneously and needs 19X19 + 9x9 @@ -110,9 +117,11 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me, // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines if (!FF_ALLOCZ_TYPED_ARRAY(sc->edge_emu_buffer, alloc_size * EMU_EDGE_HEIGHT) || !FF_ALLOCZ_TYPED_ARRAY(me->scratchpad, alloc_size * 4 * 16 * 2)) { + sc->linesize = 0; av_freep(&sc->edge_emu_buffer); return AVERROR(ENOMEM); } + sc->linesize = linesizeabs; me->temp = me->scratchpad; sc->rd_scratchpad = me->scratchpad; @@ -149,9 +158,9 @@ static int handle_pic_linesizes(AVCodecContext *avctx, Picture *pic, return -1; } - if (!sc->edge_emu_buffer && - (ret = ff_mpeg_framesize_alloc(avctx, me, sc, - pic->f->linesize[0])) < 0) { + ret = ff_mpeg_framesize_alloc(avctx, me, sc, + pic->f->linesize[0]); + if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed to allocate context scratch buffers.\n"); ff_mpeg_unref_picture(pic); diff --git a/libavcodec/mpegpicture.h b/libavcodec/mpegpicture.h index a457586be5..215e7388ef 100644 --- a/libavcodec/mpegpicture.h +++ b/libavcodec/mpegpicture.h @@ -38,6 +38,7 @@ typedef struct ScratchpadContext { uint8_t *rd_scratchpad; ///< scratchpad for rate distortion mb decision uint8_t *obmc_scratchpad; uint8_t *b_scratchpad; ///< scratchpad used for writing into write only buffers + int linesize; ///< linesize that the buffers in this context have been allocated for } ScratchpadContext; /** diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c index 7af823b8bd..130ccb4c97 100644 --- a/libavcodec/mpegvideo.c +++ b/libavcodec/mpegvideo.c @@ -443,6 +443,7 @@ static void free_duplicate_context(MpegEncContext *s) s->sc.rd_scratchpad = s->sc.b_scratchpad = s->sc.obmc_scratchpad = NULL; + s->sc.linesize = 0; av_freep(&s->dct_error_sum); av_freep(&s->me.map); @@ -464,12 +465,9 @@ static void free_duplicate_contexts(MpegEncContext *s) static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src) { #define COPY(a) bak->a = src->a - COPY(sc.edge_emu_buffer); + COPY(sc); COPY(me.scratchpad); COPY(me.temp); - COPY(sc.rd_scratchpad); - COPY(sc.b_scratchpad); - COPY(sc.obmc_scratchpad); COPY(me.map); COPY(me.score_map); COPY(blocks); @@ -503,9 +501,9 @@ int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src) // exchange uv FFSWAP(void *, dst->pblocks[4], dst->pblocks[5]); } - if (!dst->sc.edge_emu_buffer && - (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me, - &dst->sc, dst->linesize)) < 0) { + ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me, + &dst->sc, dst->linesize); + if (ret < 0) { av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context " "scratch buffers.\n"); return ret; @@ -646,12 +644,9 @@ static void clear_context(MpegEncContext *s) s->ac_val[0] = s->ac_val[1] = s->ac_val[2] =NULL; - s->sc.edge_emu_buffer = NULL; s->me.scratchpad = NULL; - s->me.temp = - s->sc.rd_scratchpad = - s->sc.b_scratchpad = - s->sc.obmc_scratchpad = NULL; + s->me.temp = NULL; + memset(&s->sc, 0, sizeof(s->sc)); s->bitstream_buffer = NULL; diff --git a/libavcodec/mpegvideo_dec.c b/libavcodec/mpegvideo_dec.c index 4353f1fd68..31403d9acc 100644 --- a/libavcodec/mpegvideo_dec.c +++ b/libavcodec/mpegvideo_dec.c @@ -167,18 +167,13 @@ do {\ } // linesize-dependent scratch buffer allocation - if (!s->sc.edge_emu_buffer) - if (s1->linesize) { - if (ff_mpeg_framesize_alloc(s->avctx, &s->me, - &s->sc, s1->linesize) < 0) { - av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context " - "scratch buffers.\n"); - return AVERROR(ENOMEM); - } - } else { - av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not " - "be allocated due to unknown size.\n"); - } + ret = ff_mpeg_framesize_alloc(s->avctx, &s->me, + &s->sc, s1->linesize); + if (ret < 0) { + av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context " + "scratch buffers.\n"); + return ret; + } // MPEG-2/interlacing info memcpy(&s->progressive_sequence, &s1->progressive_sequence,