From patchwork Sat May 11 20:50:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 48736 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:1706:b0:1af:cdee:28c5 with SMTP id nv6csp298061pzb; Sat, 11 May 2024 13:52:55 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVDIYfIDoVK7UhR13yx5q0xavKwfRDFW0aZHayMc/nvD5aEmkyQC3CQRFYOe/6YJWNNlpFPf3R8OypLL9nXBQ8LlKIcu5ayJlynJg== X-Google-Smtp-Source: AGHT+IGDaHmfT1KPQjQLOT+4j9WUCqD3GnISUBPzJmiwaTm5di3NTJNkicr12LN0vf6uAvaRHuyc X-Received: by 2002:a05:6512:201:b0:51d:3a99:f22e with SMTP id 2adb3069b0e04-5220ff72e3amr3320422e87.59.1715460775230; Sat, 11 May 2024 13:52:55 -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 a640c23a62f3a-a5a179461fbsi335781266b.102.2024.05.11.13.52.54; Sat, 11 May 2024 13:52:55 -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=GFC6xYDt; 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 4EE1B68D5D9; Sat, 11 May 2024 23:52:24 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2096.outbound.protection.outlook.com [40.92.89.96]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9451368D5DA for ; Sat, 11 May 2024 23:52:22 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=i3wBIsxv+wrsh/AlB9veBiRcXmvo0b3DGpSKQU07Sl9f69BsKMHtgO32GBI8tngLl2HCut6I4EUKUpPQy6rdznxRXvGF4Gy+geIFOI+mxfRkFa3Tda38n9UlBKTic96cuTjpV8EWPWN61/vehdlDj/6J0iZzTD5WJm0gvc8qFTJAifXvgXJ3MZDPT6q79R77EmcgrDVMR/m2WAjKWYe/hdUgLOXfNSM/RQ4skylQUb59q5vZ8G+oQwfj8+gSSClmwEqIZ6u3ui8fCoBeqjey7R/pYOEfweiZHaEqZyFsDXgpi+ZdTxVXASvdSEWdVdCcsDKcAQRqnJsAQFYp8ViJfQ== 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=IPeSM8o4di4y2h1O7ATk3wqLnFOi8xL8L2UZ97N6Sig=; b=lQlxgRVurUVkUeDA/iwuBf1q6GUSZm0cGiyHQTnhlKrkoVm8BArjbOHDOvHABF2IFKbjPX2TcOy2D2EcbMveT/eBTyrtUvZGkklndAK7tx3/D/1K+Ig8rJoM3pEIgCN0UMsbrIhe4qLUKFFMgG/Rx9EIpCPR9fQZyj/Rty1jYcW212mgUIf8jclVLsqxXmhst4FdZjZBsqKo5FkP6Pg0318ujpuz3jFgqKNiiO+xWMm2eUAO4PM2QYi+lENXsw0koouRdjAPtr+KfLqzVpEdKb6/XskcLmA5kt3wMpTaGryYQ+JvzIGKrhfkft3aDMuymwUNTd+suT0cvR/M1GRf3A== 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=IPeSM8o4di4y2h1O7ATk3wqLnFOi8xL8L2UZ97N6Sig=; b=GFC6xYDt5e3+TnkAnNE1TUqV3gh6lo9AdhdNywUmDt/jMGK+3QJIe4+I74NX3OQsfe7dqA1iqsl5aL7PFr+MvWr3iVQuujDu/9f/z7zO5s7VQrSUmjjXGri6Xl3wZvzZxbz/3MjLxkuioDOnSsCnbCpk+68ydBEIQSt4uF/JqZ46IChIF04fNt3bCI9AOa98orttjvdljp7pQYe0PMcMTNcL6gr9XMqQLcbJR9cD3rT7PghRGnqp1M4HpbFIepckCzUqG61QXvw8Ny1XkGgb2+bmDnNEO9D42vMtSz8TiHYahZGkTqQHg74h5gxEScNYQpCVVO1unqRf0x95ZaLuCQ== Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) by DB9P250MB0449.EURP250.PROD.OUTLOOK.COM (2603:10a6:10:325::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.51; Sat, 11 May 2024 20:52:18 +0000 Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::d6a1:e3af:a5f1:b614]) by GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::d6a1:e3af:a5f1:b614%7]) with mapi id 15.20.7544.052; Sat, 11 May 2024 20:52:18 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 11 May 2024 22:50:31 +0200 Message-ID: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: X-TMN: [GC6vWV1cRsigytel1dq4RshD48asdCRPikKMPCH2LPQ=] X-ClientProxiedBy: ZR0P278CA0172.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:45::6) To GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) X-Microsoft-Original-Message-ID: <20240511205135.2411886-6-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV1P250MB0737:EE_|DB9P250MB0449:EE_ X-MS-Office365-Filtering-Correlation-Id: 35a2077b-e896-48ee-323a-08dc71fc3edc X-Microsoft-Antispam: BCL:0; ARA:14566002|461199019|3412199016|440099019|56899024|1710799017; X-Microsoft-Antispam-Message-Info: RJjykaYkoBv2A+37p0L2enoRjYYytexLnyftskNxMwZ5ifD8hGYTPnPInBZCs22LoGSAiG+iO6kbCGtgXombhdSQ/Q/LO4fJHV8KHqQ34qz8+veu63v8mlsRN42mfT9makbEzmkDhhPV3i09JhEcPGs89k6K37otCBQF9L3hLvVVKC3UNXHPmHi9ZyKm7PWJL2sETUyswPcBh1jixPTPW+5lxP0gjSkTCerXxCpolAFLzVQsY8i0kBvoBra7Moe+XqbLzZksU+pS7H9t7YNdj1nZp+vLb/JEbob5z+NuAprmBE1SiMqt5cSHnxy+xHV7Bof3kF6rYACEqRUdLf6a2joy1kQWo1mqntVoGbrD0/7+B1Bpb6lnvA6jKiNq5T/81Aj1TcJtQQ0tCxdQjyznsDvKAJqlZ9qV/RUJTwdi/95ODjMqGQvzwl8smoQM2YE25cH9gDka9EKLKRVxngvC9vv+cQpXSy1qCBwNhQBdQ4J2oPE+1JdVdiHasE/ifim9G/jVjuzC0q1mObUNLcd3Ovvcv852VKXsnEm/OxL5AEhuwUPxhWb7tp64zuCUE4fxkODpnETFNsuUo5IS7lvvLX7wyzxBKawxbglMbhZMIShuzw99n2vLGl6flpG1qdws X-MS-Exchange-AntiSpam-MessageData-Ch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OIQ56eBg6AfjD3sbPnmIpwf0Qbgbh2nwnTrIcaB5GiQZkTDVBVOOQX+4IF6uzJXX0e/nJPPRinoUU3bW673rBtCtymNcxrkaKYXSc6NY5SvfrUEk1l1CWnugBLXsWFk5I0VSvDZ2t5N66cV4T9lwEgVNsgN3CRd4ou8Q6l0k3VfBvffd6sw1JCqmxC/UBn1pMDNoFHXHuAi/EbxYf5WfvgABLGewsfW7MuIUPjJv1aG3n7AcH+hqTPaBgWx1yDCXxQLAg== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 35a2077b-e896-48ee-323a-08dc71fc3edc X-MS-Exchange-CrossTenant-AuthSource: GV1P250MB0737.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 May 2024 20:52:18.2464 (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: DB9P250MB0449 Subject: [FFmpeg-devel] [PATCH v2 07/71] 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: 8lOPSRDBqeTT 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 --- 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,