From patchwork Tue Oct 1 20:31:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 51980 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:cb8a:0:b0:48e:c0f8:d0de with SMTP id d10csp547511vqv; Tue, 1 Oct 2024 13:32:00 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVrnOGMrElbkrPVZFF8kRu1bLTDgwy3mUDaWCPtJv00RJmzNnbQX5/lh9lsEM/n24DclLBLkJKBHZIjHfiKtviW@gmail.com X-Google-Smtp-Source: AGHT+IFyWor1ZoymKmBKhmkgYvXNyo+ID3bA2QRCU1Qg3yFRO11hcw+RPoDs+ohUo5QbIHoGGPif X-Received: by 2002:a17:906:bc07:b0:a8a:6db7:665d with SMTP id a640c23a62f3a-a98f825a874mr88111366b.17.1727814719924; Tue, 01 Oct 2024 13:31:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1727814719; cv=none; d=google.com; s=arc-20240605; b=NQ4JPCCyrL4UiJg/1zj7jbmgVOUfdva3sSgJRb3mjv34E1xqM8iDB3Tc9X8SnWvcBs uIgMmIu1OmTGJoRJ3h/6uX9sonyJVZFq8kl1vBHCCw+ddbnvVzhgpsvmFFCkL7U+exHv Ju2p6Ljnl5kzNjnG60xncXQdCVk4E1ZJu+xfOF5wKBx0RrL79gJ7ubEv72c031JJwTe9 eZgp7H3i+gq65sqCpc1nDopaIIyumxGFC1mc59y/VI7B4gvhqAVvFPfgGqmmX4/ugJBe ZSubYtNGaWzaGCOiNmRgEuLkk/Aws+nOjx1kb4pjGs4Xv7CF1ltTA94SM57RZDDuTi5p cyEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=vyqJ28Aclu5ZUZNFjLWJ/nIG1KkcGAH6pES2wXUubbA=; fh=e5zN9xSzcxLA6bGo3lF+CqTbY/oLwzApV03EO/RBfgQ=; b=H+eCVxrFdC8HPqJmdzkR+DJTEUqi6m83OdU7nEdOwQh/QftRTvN50vCrPuBiFSRauI 0OjKPyRCG27YAVYlM13ibNNu4NDOAxi8UzY/u6Ase766DdyE9Z9NwGDds2Gm6d7Nfqgh 6l2Xov0W3gObD3kBNJVKCIf2eeNCGJxFC6gO2+qgpX7mrU2FJzfWye6clvAIlva0S3Q/ AukSMEQqvMEnK6iXTzzyHX5M2PKfp8FGBlQZv0Dosgbo4ySMEEF+1UCRTWZy0B4bnZIV xtOCS+Of3uivFqO13Cugftxa6cJoObMN/3y98rPg0eNXRKysPx60yj/mRUviCgxs4ZEL CuXg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@niedermayer.cc header.s=gm1 header.b="Vk/nYUvD"; 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 a640c23a62f3a-a93c2a5387csi787350266b.889.2024.10.01.13.31.59; Tue, 01 Oct 2024 13:31:59 -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=@niedermayer.cc header.s=gm1 header.b="Vk/nYUvD"; 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 9302068DCCE; Tue, 1 Oct 2024 23:31:37 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 775CF68D50D for ; Tue, 1 Oct 2024 23:31:30 +0300 (EEST) Received: by mail.gandi.net (Postfix) with ESMTPSA id C434D40004 for ; Tue, 1 Oct 2024 20:31:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=niedermayer.cc; s=gm1; t=1727814689; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gwjr7RSpaa1gSMxBQe1rATR4FQiDAlyyYNPirHf0sCY=; b=Vk/nYUvDjq9yhJMW2sQP3DeBlkL3IAVPMEkbKlxfdbJybdRKJi/Gtnj8eSKEdYEt6GNjtt Q2gcH7iZW8p/WS5Sjvjo18jgykJT4Xb7cdzwSXI38CVPzoEANn/bkyIjRUVMukcfRCwA/2 xKMLhBePuP91Jr/AfFtXOiRXIVsZxHvfIwl1XQ9LYd57NHvvvEgn0dA8SWKHXcYYk4aCWO VjoPYMfpSAJwOWEmFSalbK95eQasqPW70Fm7i7DQDNHXthAkB5Mvt3zCsibYt51Jxpx17G mREXdSJen9gMF3xoIFHxAUyOEusPc1MA/s29GrmWI6G17LDvLZ6INw25MBO6VQ== From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Tue, 1 Oct 2024 22:31:26 +0200 Message-ID: <20241001203126.1656651-3-michael@niedermayer.cc> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241001203126.1656651-1-michael@niedermayer.cc> References: <20241001203126.1656651-1-michael@niedermayer.cc> MIME-Version: 1.0 X-GND-Sasl: michael@niedermayer.cc Subject: [FFmpeg-devel] [PATCH 3/3] avcodec/ffv1: Implement new slice tiling 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: mu/C52Q6Y4yW This fixes corner cases (requires version 4 or a spec update) Fixes: Ticket5548 Sponsored-by: Sovereign Tech Fund Signed-off-by: Michael Niedermayer --- libavcodec/ffv1.c | 21 +++++++++++++++++---- libavcodec/ffv1.h | 1 + libavcodec/ffv1dec.c | 8 ++++---- libavcodec/ffv1enc.c | 2 +- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/libavcodec/ffv1.c b/libavcodec/ffv1.c index 2b8564c2f56..6c953e860f8 100644 --- a/libavcodec/ffv1.c +++ b/libavcodec/ffv1.c @@ -126,6 +126,19 @@ int ff_need_new_slices(int width, int num_h_slices, int chroma_shift) { return width % mpw && (width - i) % mpw == 0; } +int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift) { + int mpw = 1<version < 4 || f->version == 4 && f->micro_version < 3) + return width * sx / num_h_slices; + + sx = (2LL * awidth * sx + num_h_slices * mpw) / (2 * num_h_slices * mpw) * mpw; + if (sx == awidth) + sx = width; + return sx; +} + av_cold int ff_ffv1_init_slice_contexts(FFV1Context *f) { int max_slice_count = f->num_h_slices * f->num_v_slices; @@ -142,10 +155,10 @@ av_cold int ff_ffv1_init_slice_contexts(FFV1Context *f) FFV1SliceContext *sc = &f->slices[i]; int sx = i % f->num_h_slices; int sy = i / f->num_h_slices; - int sxs = f->avctx->width * sx / f->num_h_slices; - int sxe = f->avctx->width * (sx + 1) / f->num_h_slices; - int sys = f->avctx->height * sy / f->num_v_slices; - int sye = f->avctx->height * (sy + 1) / f->num_v_slices; + int sxs = ff_slice_coord(f, f->avctx->width , sx , f->num_h_slices, f->chroma_h_shift); + int sxe = ff_slice_coord(f, f->avctx->width , sx + 1, f->num_h_slices, f->chroma_h_shift); + int sys = ff_slice_coord(f, f->avctx->height, sy , f->num_v_slices, f->chroma_v_shift); + int sye = ff_slice_coord(f, f->avctx->height, sy + 1, f->num_v_slices, f->chroma_v_shift); sc->slice_width = sxe - sxs; sc->slice_height = sye - sys; diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h index e19c0df0142..10f46c80ee1 100644 --- a/libavcodec/ffv1.h +++ b/libavcodec/ffv1.h @@ -172,6 +172,7 @@ int ff_ffv1_allocate_initial_states(FFV1Context *f); void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc); int ff_ffv1_close(AVCodecContext *avctx); int ff_need_new_slices(int width, int num_h_slices, int chroma_shift); +int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift); static av_always_inline int fold(int diff, int bits) { diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c index 0afdeabd915..da31b863da5 100644 --- a/libavcodec/ffv1dec.c +++ b/libavcodec/ffv1dec.c @@ -187,10 +187,10 @@ static int decode_slice_header(const FFV1Context *f, if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh) return AVERROR_INVALIDDATA; - sc->slice_x = sx * (int64_t)f->width / f->num_h_slices; - sc->slice_y = sy * (int64_t)f->height / f->num_v_slices; - sc->slice_width = (sx + sw) * (int64_t)f->width / f->num_h_slices - sc->slice_x; - sc->slice_height = (sy + sh) * (int64_t)f->height / f->num_v_slices - sc->slice_y; + sc->slice_x = ff_slice_coord(f, f->width , sx , f->num_h_slices, f->chroma_h_shift); + sc->slice_y = ff_slice_coord(f, f->height, sy , f->num_v_slices, f->chroma_v_shift); + sc->slice_width = ff_slice_coord(f, f->width , sx + sw, f->num_h_slices, f->chroma_h_shift) - sc->slice_x; + sc->slice_height = ff_slice_coord(f, f->height, sy + sh, f->num_v_slices, f->chroma_v_shift) - sc->slice_y; av_assert0((unsigned)sc->slice_width <= f->width && (unsigned)sc->slice_height <= f->height); diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c index 56af7dc4274..8b23e73c841 100644 --- a/libavcodec/ffv1enc.c +++ b/libavcodec/ffv1enc.c @@ -416,7 +416,7 @@ static int write_extradata(FFV1Context *f) if (f->version == 3) { f->micro_version = 4; } else if (f->version == 4) - f->micro_version = 2; + f->micro_version = 3; put_symbol(&c, state, f->micro_version, 0); }