From patchwork Mon Apr 29 15:09:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 48366 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:1509:b0:1a9:af23:56c1 with SMTP id nq9csp2057021pzb; Mon, 29 Apr 2024 08:10:20 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXkOLSctQ49yNQtHK1H78s9r+kHPG31ci6DC2vLYOa2cOJKyov2Uk3ogNMC7bOZe9UIHmJzfIJZQeCZFE5of17HLr1srzMjgDA9pQ== X-Google-Smtp-Source: AGHT+IFvTyK9pll1f+f609DqmyfOBBaaGP/O+Q9wD3bwJXSAxb9WXPGMj0lAgQEM/B/ppdscco0z X-Received: by 2002:a17:906:2dda:b0:a55:9e16:f005 with SMTP id h26-20020a1709062dda00b00a559e16f005mr6948823eji.57.1714403419258; Mon, 29 Apr 2024 08:10:19 -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 k19-20020a170906579300b00a5876598b05si8400288ejq.551.2024.04.29.08.10.18; Mon, 29 Apr 2024 08:10:19 -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=gPYYppL9; 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 37F0168D4C2; Mon, 29 Apr 2024 18:10:14 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04olkn2076.outbound.protection.outlook.com [40.92.74.76]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7766868D391 for ; Mon, 29 Apr 2024 18:10:07 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LUUIF2jTVFVOKTWngQcfpfEEgcgQ9occoh248IJyKUO9kWlM/Sj/3vbuhDHE/15TU88utxHQwCFKiJOJDfCWDnm9FUSheIyIS20iuAuFOYTJq1HIK+xrPGtnbO4I+KNZmxPBmQGeSQ9dKWg+7+DOnX20goj5W6nGUWVl+X4qDptoK4H4yTApmHw5WjVrG1KyjPvea3cb4NmZpi40OKyEkmxV099mW8pIWjejcg6n1JL+okhnnZV37bxWpD3OX9LN7jrFRW0z0iiGvD+tPYYKWBFLlOFWwoqS9Z9RJldfvo76PVwFTBc6+jRKikLfMdFUgWyc7KNWgXtDs+0AIJoYVw== 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=ZYgL222wBl1moOfugUZabSq4mmLKqGjRsTH+l3kZeiI=; b=ee6y+2T8OclfCAECVz8HLi+M9z+0cbqrsRKrMQrtgzMoCWfumKFkhCzUD2nP8MCWxnhrIZaW3tJ6/fljv4jd41j8lk5S0UpkIhTsbLZNyPt9Dnjhhv5gn4VBlJaPBvhf0kj8XQ6vHyTD6zmKUJiXUsekg8k0A0xGrD/XYu0av8levKX/B/lP4NQ6DP/0ByFmHeId8QL7l7fgyZ5VuDVPlc9Lltv1HccJKeuIP2Bd5BS35aSeBptnHyp1iKNjGKq3XyNBILVvh/IXsNDVnSgHKvoGyxKyC1/jmM3F/1ZuDJ1ajn10u6ptz2uKx5OgJvkfLI58MiCZp/4J4gDT5YbrKg== 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=ZYgL222wBl1moOfugUZabSq4mmLKqGjRsTH+l3kZeiI=; b=gPYYppL9u38ZLQW4WSfXw7AZo7xuyvxDm4piNzcesPaMLiih9dtTPd9dIqX34eA+cnWDBTBNnlLk0VSy6u0V12TIbOY9zNXgrBsf6NpRRuFfRe8SEFcAx1jSinXxMtBdG4+Kfi/UwAyu+pceYKsZsK+SBpg+TJ/vc4DDT1xbJs9BEK//4tO4wiKRSsmvjKAB+MliC9ASCiRRBScbVE4PA+tLi/2ysplIz1ob8vYeq/ELYwYA0/UkckTQTFYFz5jp1rRuxPc7h/gwfsHy23qTQjeaf5iWjGpMKJ/ShEoE13old3Lwd1kM7nzjHuG2OIVFix2BF4ca7tKzB2Xxtto9AA== Received: from AS8P250MB0744.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:541::14) by AS8P250MB0267.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:37e::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.35; Mon, 29 Apr 2024 15:10:04 +0000 Received: from AS8P250MB0744.EURP250.PROD.OUTLOOK.COM ([fe80::1f29:8206:b8c3:45bb]) by AS8P250MB0744.EURP250.PROD.OUTLOOK.COM ([fe80::1f29:8206:b8c3:45bb%3]) with mapi id 15.20.7519.031; Mon, 29 Apr 2024 15:10:04 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 29 Apr 2024 17:09:56 +0200 Message-ID: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: X-TMN: [R8d8b+Qct0ZA7vAaOWxfFAscWVz5twilPFryFJRk7LA=] X-ClientProxiedBy: ZR2P278CA0065.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:52::6) To AS8P250MB0744.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:541::14) X-Microsoft-Original-Message-ID: <20240429150956.1737679-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8P250MB0744:EE_|AS8P250MB0267:EE_ X-MS-Office365-Filtering-Correlation-Id: 111bf0d3-c39f-4259-9e52-08dc685e72ac X-Microsoft-Antispam: BCL:0; ARA:14566002|461199019|440099019|3412199016|56899024|1710799017; X-Microsoft-Antispam-Message-Info: hOoK2EFV59U9ah+TohynNv/pPHoDFIqCBMhFTWBWNmjQmSDfud0BFVyiScubse31ygqMRpDLXRlJcFesGp0ohIo2NpgJ4oeRGw1mO+qZsxs++aJQDnSiiSOwbQnaxLLj0YaDe2V2m6JCW32caT0YHfjwExh7feK/yX2KlwDi7Cey1oTciPu3HsY19ktpItT/j1aHzqrLSjmJaIfVzGh05xzhnezBFquA0BLUUnSwa042tbSmPheiJT5A5QqZt4stlcPwXsxKnlYLA7WRJpv3FhRdY4B/mVYvHdNDZm4VT7kLFZOuNW4IhB7K35ghN54n7qv+xpEJ1dYZyz7Kqn2RuK4qY1g18kqAi0OmrCrxJ+dBlZ4DQIVTBjUD+qPBErei/ALUzPQl705g/jboO7PvRFI8N+SqsKk/Ry1C3HgWkP3yWxDlyMcXBgFNvyaZG2o2e2/pvzQBTPyXGpuJYaKCjJ/+QNulMr4DfYGnB0+tdCZTA08hNHZuKYtkuLzjSyw6cFqwEXZPktGS7h0fNpraNmbVS11AQz2IW0qiRU6RJlisS8ZfQ4AeuW+5CoxTf/E1aM3FQnZ0DS1JdkAZBuGbgoKDK6pUoe4KcHA7ZE5etS7feQqiOM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LxbBkLCNvuxm5QAFQiFza8hX8+nEVJnY+EeS0ShnDRlAQKLZV30L5su1vUZLqUpWsiD02xP5kVvek7lzwSbwanbJdN/e13JzsgY7bZohScVFoyNn3v61BXD5lURb0SA9gPFAeVcXNGQWYqPgunKeC/ngrZbkIpbWaGKEggBzVY+sgTQJC/pqGmshQsLau4Eh7DLEZnKIrdvOi8CLByiF0P0iWrjBsObS/DSbfaYa7RG2d8Z9EwTuCK2oH7dfFh68wcKif4H3/wnAF5Mea0RgAyhe+6AANXRNKr9Ske6R590emg== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 111bf0d3-c39f-4259-9e52-08dc685e72ac X-MS-Exchange-CrossTenant-AuthSource: AS8P250MB0744.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Apr 2024 15:10:04.2953 (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: AS8P250MB0267 Subject: [FFmpeg-devel] [PATCH v2 2/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: Ml+QjytpFRFA 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. Also don't emit an error message in case the source ctx's edge_emu_buffer is unset in ff_mpeg_update_thread_context(). It need not be an error at all; e.g. it is a perfectly normal state in case a hardware acceleration is used as the scratch buffers are not allocated in this case (it is easy to run into this issue with MPEG-4) or if the src context was not initialized at all (e.g. because the first packet contained garbage). Signed-off-by: Andreas Rheinhardt --- Updated the commit message to include that this also removes error spam for MPEG-4 frame-threading with hwaccel. 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,