diff mbox series

[FFmpeg-devel,3/6] avfilter/swscale: always fix interlaced chroma location

Message ID 20240704115202.1235954-3-ffmpeg@haasn.xyz
State New
Headers show
Series [FFmpeg-devel,1/6] swscale: document SWS_FULL_CHR_H_* flags | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 fail Make fate failed
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Niklas Haas July 4, 2024, 11:51 a.m. UTC
From: Niklas Haas <git@haasn.dev>

The current logic only fixes it when the user does not explicitly
specify the chroma location. However, this does not make a lot of sense.
Since there is no way to specify this property per-field, it effectively
*prevents* the user from being able to correctly scale interlaced frames
with top-aligned chroma.

It makes more sense to consider the user setting in the progressive case
only, and automatically adapt it to the correct interlaced field
positions, following the details of the MPEG specification.
---
 libavfilter/vf_scale.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c
index 841075193e..0b6701673f 100644
--- a/libavfilter/vf_scale.c
+++ b/libavfilter/vf_scale.c
@@ -709,12 +709,18 @@  static int config_props(AVFilterLink *outlink)
              * chroma positions. MPEG chroma positions are used by convention.
              * Note that this works for both MPEG-1/JPEG and MPEG-2/4 chroma
              * locations, since they share a vertical alignment */
-            if (desc->log2_chroma_h == 1 && scale->in_v_chr_pos == -513) {
-                in_v_chr_pos = (i == 0) ? 128 : (i == 1) ? 64 : 192;
+            if (desc->log2_chroma_h == 1) {
+                if (in_v_chr_pos == -513)
+                    in_v_chr_pos = 128; /* explicitly default missing info */
+                in_v_chr_pos += 256 * (i == 2); /* offset by one luma row for odd rows */
+                in_v_chr_pos >>= i > 0; /* double luma row distance */
             }
 
-            if (outdesc->log2_chroma_h == 1 && scale->out_v_chr_pos == -513) {
-                out_v_chr_pos = (i == 0) ? 128 : (i == 1) ? 64 : 192;
+            if (outdesc->log2_chroma_h == 1) {
+                if (out_v_chr_pos == -513)
+                    out_v_chr_pos = 128;
+                out_v_chr_pos += 256 * (i == 2);
+                out_v_chr_pos >>= i > 0;
             }
 
             av_opt_set_int(s, "src_h_chr_pos", scale->in_h_chr_pos, 0);