Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Niklas Haas <ffmpeg@haasn.xyz>
To: ffmpeg-devel@ffmpeg.org
Cc: Niklas Haas <git@haasn.dev>
Subject: [FFmpeg-devel] [PATCH v2 6/8] avfilter/vf_scale: fix 4:1:0 interlaced chroma pos
Date: Thu,  4 Jul 2024 16:31:02 +0200
Message-ID: <20240704143104.1821386-6-ffmpeg@haasn.xyz> (raw)
In-Reply-To: <20240704143104.1821386-1-ffmpeg@haasn.xyz>

From: Niklas Haas <git@haasn.dev>

The current logic hard-coded a check for v_sub == 1. We can extend this
logic slightly to cover the case of interlaced 4:1:0 (which has v_sub ==
2).

Here is a diagram explaining this scenario (with center-siting):

a   a   a   a   a   a   a   a

b   b   b   b   b   b   b   b
      X               X
a   a   a   a   a   a   a   a

b   b   b   b   b   b   b   b

a   a   a   a   a   a   a   a

b   b   b   b   b   b   b   b
      Y               Y
a   a   a   a   a   a   a   a

b   b   b   b   b   b   b   b

a = even luma rows
b = odd luma rows
X = even chroma sample
Y = odd chroma sample

In progressive mode, the chroma samples sit at (384, 384) respectively.

Relative to the 8x4 grid of even luma samples (a), the X sample sits at:
  h_chr_pos = 384
  v_chr_pos = 192

Relative to the 8x4 grid of odd luma samples (b), the Y sample sits at:
  h_chr_pos = 384
  v_chr_pos = 576

The new code calculates the correct values in all circumstances.
---
 libavfilter/vf_scale.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c
index b2c9d0b187..2d82141b70 100644
--- a/libavfilter/vf_scale.c
+++ b/libavfilter/vf_scale.c
@@ -642,9 +642,19 @@ static void calc_chroma_pos(int *h_pos_out, int *v_pos_out, int chroma_loc,
         v_pos = v_pos_override;
 
     /* Fix vertical chroma position for interlaced frames */
-    if (v_sub == 1 && index > 0) {
-        v_pos += 256 * (index == 2); /* offset by one luma row for odd rows */
-        v_pos >>= 1; /* double luma row distance */
+    if (v_sub && index > 0) {
+        /* When vertically subsampling, chroma samples are effectively only
+         * placed next to even rows. To access them from the odd field, we need
+         * to account for this shift by offsetting the distance of one luma row.
+         *
+         * For 4x vertical subsampling (v_sub == 2), they are only placed
+         * next to every *other* even row, so we need to shift by three luma
+         * rows to get to the chroma sample. */
+        if (index == 2)
+            v_pos += (256 << v_sub) - 256;
+
+        /* Luma row distance is doubled for fields, so halve offsets */
+        v_pos >>= 1;
     }
 
     /* Explicitly strip chroma offsets when not subsampling, because it
-- 
2.45.2

_______________________________________________
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".

  parent reply	other threads:[~2024-07-04 14:32 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-04 14:30 [FFmpeg-devel] [PATCH v2 1/8] swscale: document SWS_FULL_CHR_H_* flags Niklas Haas
2024-07-04 14:30 ` [FFmpeg-devel] [PATCH v2 2/8] avfilter/vf_setparams: allow setting chroma location Niklas Haas
2024-07-04 14:30 ` [FFmpeg-devel] [PATCH v2 3/8] swscale/options: relax src/dst_h/v_chr_pos value range Niklas Haas
2024-07-07 18:56   ` Michael Niedermayer
2024-07-04 14:31 ` [FFmpeg-devel] [PATCH v2 4/8] avfilter/swscale: always fix interlaced chroma location Niklas Haas
2024-07-04 14:31 ` [FFmpeg-devel] [PATCH v2 5/8] avfilter/vf_scale: add in/out_chroma_loc Niklas Haas
2024-07-04 14:31 ` Niklas Haas [this message]
2024-07-04 14:31 ` [FFmpeg-devel] [PATCH v2 7/8] avfilter/vf_zscale: remove unused fields Niklas Haas
2024-07-04 14:31 ` [FFmpeg-devel] [PATCH v2 8/8] fate/scalechroma: switch to standard chroma location Niklas Haas
2024-07-04 14:33 ` [FFmpeg-devel] [PATCH v2 1/8] swscale: document SWS_FULL_CHR_H_* flags Niklas Haas
2024-07-04 15:24 ` Andrew Sayers
2024-07-04 17:56   ` Niklas Haas
2024-07-04 17:59     ` Niklas Haas
2024-07-04 18:14       ` Andrew Sayers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240704143104.1821386-6-ffmpeg@haasn.xyz \
    --to=ffmpeg@haasn.xyz \
    --cc=ffmpeg-devel@ffmpeg.org \
    --cc=git@haasn.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git