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: Re: [FFmpeg-devel] [PATCH v3 1/3] avfilter/vf_colordetect: add new color range detection filter
Date: Fri, 18 Jul 2025 14:18:01 +0200
Message-ID: <20250718141801.GB303629@haasn.xyz> (raw)
In-Reply-To: <20250718095716.68346-1-ffmpeg@haasn.xyz>

On Fri, 18 Jul 2025 11:57:14 +0200 Niklas Haas <ffmpeg@haasn.xyz> wrote:
> From: Niklas Haas <git@haasn.dev>
>
> This filter can detect various properties about the image, including
> whether or not there are out-of-range values, or whether the input appears
> to use straight or premultiplied alpha.
>
> Of course, these can only be heuristics, with "undetermined" as the base
> case. While we can definitely prove the existence of full range or
> straight alpha colors, we can never infer the opposite.
> ---
> [...]
> +static int detect_alpha(AVFilterContext *ctx, void *arg,
> +                        int jobnr, int nb_jobs)
> +{
> +    ColorDetectContext *s = ctx->priv;
> +    const AVFrame *in = arg;
> +    const int w = in->width;
> +    const int h = in->height;
> +    const int y_start = (h * jobnr) / nb_jobs;
> +    const int y_end = (h * (jobnr + 1)) / nb_jobs;
> +    const int h_slice = y_end - y_start;
> +
> +    const int nb_planes = (s->desc->flags & AV_PIX_FMT_FLAG_RGB) ? 3 : 1;
> +    const ptrdiff_t alpha_stride = in->linesize[s->idx_a];
> +    const uint8_t *alpha = in->data[s->idx_a] + y_start * alpha_stride;
> +
> +    const int p = (1 << s->depth) - 1;
> +    const int q = s->mpeg_max - s->mpeg_min;
> +    const int k = s->mpeg_min * p + 128;

I replaced this by a slightly tighter bound, and also added some explanation:

    /**
     * To check if a value is out of range, we need to compare the color value
     * against the maximum possible color for a given alpha value:
     *   x > (((mpeg_max - mpeg_min) * (a + ((a >> 1) & 1) + (1 << (depth - 1))) >> depth) + mpeg_min
     *   (cf. premultiply16offset in vf_premultiply.c)
     *
     * This rearranges to:
     *   (x - mpeg_min << depth) - (1 << (depth - 1)) > (mpeg_max - mpeg_min) * (a + ((a >> 1) & 1)
     *
     * And since a + 1 >= a + ((a >> 1) & 1) we can relax the RHS slightly, giving us:
     *   (1 << depth) * x - (mpeg_min << depth) - (1 << (depth - 1)) - (mpeg_max - mpeg_min) > (mpeg_max - mpeg_min) * a
     *   = P * x - K > Q * a in the below formula
     */
    const int p = 1 << s->depth;
    const int q = s->mpeg_max - s->mpeg_min;
    const int k = p * s->mpeg_min + q + (1 << (s->depth - 1));

I won't bother sending a v4 just for this diff.

> [...]
> diff --git a/libavfilter/vf_colordetect.h b/libavfilter/vf_colordetect.h
> new file mode 100644
> index 0000000000..8998ed83d4
> --- /dev/null
> +++ b/libavfilter/vf_colordetect.h
> @@ -0,0 +1,149 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#ifndef AVFILTER_VF_COLORDETECT_H
> +#define AVFILTER_VF_COLORDETECT_H

Fixed locally to unbreak fate-source.
_______________________________________________
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:[~2025-07-18 12:18 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-18  9:57 Niklas Haas
2025-07-18  9:57 ` [FFmpeg-devel] [PATCH v3 2/3] tests/checkasm: add check for vf_colordetect Niklas Haas
2025-07-18  9:57 ` [FFmpeg-devel] [PATCH v3 3/3] avfilter/vf_colordetect: add x86 SIMD implementation Niklas Haas
2025-07-18 12:18 ` Niklas Haas [this message]
2025-07-18 12:38 ` [FFmpeg-devel] [PATCH v3 1/3] avfilter/vf_colordetect: add new color range detection filter Kacper Michajlow
2025-07-18 12:46   ` Niklas Haas
2025-07-18 14:51     ` Kacper Michajlow

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=20250718141801.GB303629@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