From patchwork Thu Jul 4 14:31:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 50333 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:cc64:0:b0:482:c625:d099 with SMTP id k4csp3754859vqv; Thu, 4 Jul 2024 07:32:04 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVSoK6VpG9Y9AeFY48dvGkV6g6ikD3yRSaNd3RqqIgGSJiQxrkWtrbTSMI7sULgS1VoyoTVVAUi2Lk+MDatPeruHidFc70ld6NCUQ== X-Google-Smtp-Source: AGHT+IGDfdIXGSB2oMkABBZ5eKBqxMupkfvVZUvv0XA7i9fOqC5uWGb96zA2yZmIYUN42NInm6zk X-Received: by 2002:a17:906:f753:b0:a6f:1872:4e1f with SMTP id a640c23a62f3a-a77ba84a5fbmr113379366b.77.1720103523987; Thu, 04 Jul 2024 07:32:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1720103523; cv=none; d=google.com; s=arc-20160816; b=cGIBP7LKX/Z3+WGMzLmBjimn1SJc9M8pPEDHvq7xH+SNZJOI92zkFD8z+KM1j8Ihzn P1m6tPMZgLrxnB3BCKiVl+jErjLh4ojc4aJaSkvGBjGR+sCaJGSXWESjMez9s/xsJ+BS 6IoXDSM983rrofEi8lJfx8aL9iy6DGlorQzdQr4bi+rdPOyh/+4MAKhmLMbVkPgo2o20 vodixGBId7HxGLAsK6oKL0PeocjuTNhBIsSzpMKHekFdT1XTZizE2CGQZbxr9YWykX+3 4gHehdKSSXZxMcBX+J5fRNGMeYlyvolWri3ywVep7UqJInb1zMQxBkxaubmmvmuHcReI Rrjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc: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=Ep4K2zu5ZiXSgdlpZGAHAlIrJtuwJCwK61/Z+R2hL+U=; fh=xmAeKtysnShNOmkhiJmYkS30uw4Fu2hvBJ7qlIwukxQ=; b=R5vYTpotFA8OIRRTcjg1Zmnogeg8sZfyygC1atz9DEQo+w3sXvWAmpeVu+nsQRgJCT v44M7w+x0WqScFqgx9tWe6M3ZKugHkt48y3hskAoQVgFcrh7qi1ixlHUgIAXc0SsMmzh fxnTLtr9Lcu7ut/Fk5oxJi11ceD671Zc5OCkl9AH0guY3ECHXhP0tjOZAw/SRES1c9pR LXo/tomsqOQbDpxGqqz7b1u5uge49nIUEk7Qh9ywsVN6A99RErjwzllLTqSd0Z8YvLDD 3wTJvvqY+rB3vSxQt0kwT/emXTvDtqSYCumYbg0QeMW5Odd7BdAjBAMBlEJlZXDtM/Zu +LJw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=BFc84If2; 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-a72ab04a9f3si688376366b.366.2024.07.04.07.32.03; Thu, 04 Jul 2024 07:32:03 -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=@haasn.xyz header.s=mail header.b=BFc84If2; 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 2CB1E68DA24; Thu, 4 Jul 2024 17:31:23 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id F338368D9C9 for ; Thu, 4 Jul 2024 17:31:07 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1720103467; bh=D5v0MOZlroThsb6dP6LBE3OQGcsdVuu3DrW6XJWo7WE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BFc84If2Hj4T5ihvCJOmqvkW8kz1PpBCSM8ePjvApCG/owwAr5m5XvuWMVmwmjuW2 OvKLSaJuVQ5Uxz9ktKNvA578iV6z88b8dLCwR7l/jJqn4d/emzICx14d/VsK2T9IRO LhDTY2LE1hSr1nF3qLGOxyQuliHr9tP40jxNAiF0= Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id BC04F41A55; Thu, 4 Jul 2024 16:31:07 +0200 (CEST) From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Thu, 4 Jul 2024 16:31:01 +0200 Message-ID: <20240704143104.1821386-5-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240704143104.1821386-1-ffmpeg@haasn.xyz> References: <20240704143104.1821386-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 5/8] avfilter/vf_scale: add in/out_chroma_loc 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: Niklas Haas Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: sGXWEMODBb2J From: Niklas Haas Currently, this just functions as a more principled and user-friendly replacement for the (undocumented and hard to use) *_chr_pos fields. However, the goal is to automatically infer these values from the input frames' chroma location, and deprecate the manual use of *_chr_pos altogether. (Indeed, my plans for an swscale replacement will most likely also end up limiting the set of legal chroma locations to those permissible by AVFrame properties) --- doc/filters.texi | 15 ++++++++ libavfilter/vf_scale.c | 86 ++++++++++++++++++++++++++++++------------ 2 files changed, 77 insertions(+), 24 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index ca8f6e461a..3cff4eec1c 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -21137,6 +21137,21 @@ Set full range (0-255 in case of 8-bit luma). Set "MPEG" range (16-235 in case of 8-bit luma). @end table +@item in_chroma_loc +@item out_chroma_loc +Set in/output chroma sample location. If not specified, center-sited chroma +is used by default. Possible values: + +@table @samp +@item auto, unknown +@item left +@item center +@item topleft +@item top +@item bottomleft +@item bottom +@end table + @item force_original_aspect_ratio Enable decreasing or increasing output video width or height if necessary to keep the original aspect ratio. Possible values: diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c index 0b6701673f..b2c9d0b187 100644 --- a/libavfilter/vf_scale.c +++ b/libavfilter/vf_scale.c @@ -168,6 +168,8 @@ typedef struct ScaleContext { int in_range; int out_range; + int in_chroma_loc; + int out_chroma_loc; int out_h_chr_pos; int out_v_chr_pos; int in_h_chr_pos; @@ -617,6 +619,40 @@ fail: return ret; } +static void calc_chroma_pos(int *h_pos_out, int *v_pos_out, int chroma_loc, + int h_pos_override, int v_pos_override, + int h_sub, int v_sub, int index) +{ + int h_pos, v_pos; + + /* Explicitly default to center siting for compatibility with swscale */ + if (chroma_loc == AVCHROMA_LOC_UNSPECIFIED) + chroma_loc = AVCHROMA_LOC_CENTER; + + /* av_chroma_location_enum_to_pos() always gives us values in the range from + * 0 to 256, but we need to adjust this to the true value range of the + * subsampling grid, which may be larger for h/v_sub > 1 */ + av_chroma_location_enum_to_pos(&h_pos, &v_pos, chroma_loc); + h_pos *= (1 << h_sub) - 1; + v_pos *= (1 << v_sub) - 1; + + if (h_pos_override != -513) + h_pos = h_pos_override; + if (v_pos_override != -513) + 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 */ + } + + /* Explicitly strip chroma offsets when not subsampling, because it + * interferes with the operation of flags like SWS_FULL_CHR_H_INP */ + *h_pos_out = h_sub ? h_pos : -513; + *v_pos_out = v_sub ? v_pos : -513; +} + static int config_props(AVFilterLink *outlink) { AVFilterContext *ctx = outlink->src; @@ -673,15 +709,16 @@ static int config_props(AVFilterLink *outlink) inlink0->h == outlink->h && in_range == outlink->color_range && in_colorspace == outlink->colorspace && - inlink0->format == outlink->format) + inlink0->format == outlink->format && + scale->in_chroma_loc == scale->out_chroma_loc) ; else { struct SwsContext **swscs[3] = {&scale->sws, &scale->isws[0], &scale->isws[1]}; int i; for (i = 0; i < 3; i++) { - int in_v_chr_pos = scale->in_v_chr_pos, out_v_chr_pos = scale->out_v_chr_pos; int in_full, out_full, brightness, contrast, saturation; + int h_chr_pos, v_chr_pos; const int *inv_table, *table; struct SwsContext *const s = sws_alloc_context(); if (!s) @@ -705,28 +742,17 @@ static int config_props(AVFilterLink *outlink) av_opt_set_int(s, "dst_range", outlink->color_range == AVCOL_RANGE_JPEG, 0); - /* Override chroma location default settings to have the correct - * 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) { - 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) { - 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); - av_opt_set_int(s, "src_v_chr_pos", in_v_chr_pos, 0); - av_opt_set_int(s, "dst_h_chr_pos", scale->out_h_chr_pos, 0); - av_opt_set_int(s, "dst_v_chr_pos", out_v_chr_pos, 0); + calc_chroma_pos(&h_chr_pos, &v_chr_pos, scale->in_chroma_loc, + scale->in_h_chr_pos, scale->in_v_chr_pos, + desc->log2_chroma_w, desc->log2_chroma_h, i); + av_opt_set_int(s, "src_h_chr_pos", h_chr_pos, 0); + av_opt_set_int(s, "src_v_chr_pos", v_chr_pos, 0); + + calc_chroma_pos(&h_chr_pos, &v_chr_pos, scale->out_chroma_loc, + scale->out_h_chr_pos, scale->out_v_chr_pos, + outdesc->log2_chroma_w, outdesc->log2_chroma_h, i); + av_opt_set_int(s, "dst_h_chr_pos", h_chr_pos, 0); + av_opt_set_int(s, "dst_v_chr_pos", v_chr_pos, 0); if ((ret = sws_init_context(s, NULL, NULL)) < 0) return ret; @@ -987,6 +1013,8 @@ scale: out->height = outlink->h; out->color_range = outlink->color_range; out->colorspace = outlink->colorspace; + if (scale->out_chroma_loc != AVCHROMA_LOC_UNSPECIFIED) + out->chroma_location = scale->out_chroma_loc; if (scale->output_is_pal) avpriv_set_systematic_pal2((uint32_t*)out->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format); @@ -1217,6 +1245,16 @@ static const AVOption scale_options[] = { { "mpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, .unit = "range" }, { "tv", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, .unit = "range" }, { "pc", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, .unit = "range" }, + { "in_chroma_loc", "set input chroma sample location", OFFSET(in_chroma_loc), AV_OPT_TYPE_INT, { .i64 = AVCHROMA_LOC_UNSPECIFIED }, 0, AVCHROMA_LOC_NB-1, .flags = FLAGS, .unit = "chroma_loc" }, + { "out_chroma_loc", "set output chroma sample location", OFFSET(out_chroma_loc), AV_OPT_TYPE_INT, { .i64 = AVCHROMA_LOC_UNSPECIFIED }, 0, AVCHROMA_LOC_NB-1, .flags = FLAGS, .unit = "chroma_loc" }, + {"auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_UNSPECIFIED}, 0, 0, FLAGS, .unit = "chroma_loc"}, + {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_UNSPECIFIED}, 0, 0, FLAGS, .unit = "chroma_loc"}, + {"left", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_LEFT}, 0, 0, FLAGS, .unit = "chroma_loc"}, + {"center", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_CENTER}, 0, 0, FLAGS, .unit = "chroma_loc"}, + {"topleft", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_TOPLEFT}, 0, 0, FLAGS, .unit = "chroma_loc"}, + {"top", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_TOP}, 0, 0, FLAGS, .unit = "chroma_loc"}, + {"bottomleft", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_BOTTOMLEFT}, 0, 0, FLAGS, .unit = "chroma_loc"}, + {"bottom", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCHROMA_LOC_BOTTOM}, 0, 0, FLAGS, .unit = "chroma_loc"}, { "in_v_chr_pos", "input vertical chroma position in luma grid/256" , OFFSET(in_v_chr_pos), AV_OPT_TYPE_INT, { .i64 = -513}, -513, 512, FLAGS }, { "in_h_chr_pos", "input horizontal chroma position in luma grid/256", OFFSET(in_h_chr_pos), AV_OPT_TYPE_INT, { .i64 = -513}, -513, 512, FLAGS }, { "out_v_chr_pos", "output vertical chroma position in luma grid/256" , OFFSET(out_v_chr_pos), AV_OPT_TYPE_INT, { .i64 = -513}, -513, 512, FLAGS },