2015-02-24 08:47:30 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Arwa Arif <arwaarif1994@gmail.com>
|
2017-12-23 12:33:18 +02:00
|
|
|
* Copyright (c) 2017 Paul B Mahol
|
2015-02-24 08:47:30 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* FFT domain filtering.
|
|
|
|
*/
|
|
|
|
|
2024-08-06 08:50:21 +02:00
|
|
|
#include "filters.h"
|
2023-08-03 13:03:17 +02:00
|
|
|
#include "internal.h"
|
|
|
|
#include "video.h"
|
2015-02-24 08:47:30 +02:00
|
|
|
#include "libavutil/common.h"
|
2022-02-14 20:00:53 +02:00
|
|
|
#include "libavutil/cpu.h"
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2015-02-24 08:47:30 +02:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
2022-02-14 16:21:49 +02:00
|
|
|
#include "libavutil/tx.h"
|
2015-02-24 08:47:30 +02:00
|
|
|
#include "libavutil/eval.h"
|
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
#define MAX_THREADS 32
|
2015-05-12 20:08:18 +02:00
|
|
|
#define MAX_PLANES 4
|
|
|
|
|
2017-09-04 19:29:37 +02:00
|
|
|
enum EvalMode {
|
|
|
|
EVAL_MODE_INIT,
|
|
|
|
EVAL_MODE_FRAME,
|
|
|
|
EVAL_MODE_NB
|
|
|
|
};
|
|
|
|
|
2017-05-12 20:00:49 +02:00
|
|
|
typedef struct FFTFILTContext {
|
2015-02-24 08:47:30 +02:00
|
|
|
const AVClass *class;
|
|
|
|
|
2017-09-04 19:29:37 +02:00
|
|
|
int eval_mode;
|
2017-09-04 18:36:37 +02:00
|
|
|
int depth;
|
|
|
|
int nb_planes;
|
2021-10-14 00:57:03 +02:00
|
|
|
int nb_threads;
|
2017-09-04 18:36:37 +02:00
|
|
|
int planewidth[MAX_PLANES];
|
|
|
|
int planeheight[MAX_PLANES];
|
|
|
|
|
2022-02-14 16:21:49 +02:00
|
|
|
AVTXContext *hrdft[MAX_THREADS][MAX_PLANES];
|
|
|
|
AVTXContext *vrdft[MAX_THREADS][MAX_PLANES];
|
|
|
|
AVTXContext *ihrdft[MAX_THREADS][MAX_PLANES];
|
|
|
|
AVTXContext *ivrdft[MAX_THREADS][MAX_PLANES];
|
|
|
|
|
|
|
|
av_tx_fn htx_fn, ihtx_fn;
|
|
|
|
av_tx_fn vtx_fn, ivtx_fn;
|
|
|
|
|
2015-05-12 20:08:18 +02:00
|
|
|
int rdft_hbits[MAX_PLANES];
|
|
|
|
int rdft_vbits[MAX_PLANES];
|
2022-02-14 16:21:49 +02:00
|
|
|
size_t rdft_hstride[MAX_PLANES];
|
|
|
|
size_t rdft_vstride[MAX_PLANES];
|
2015-05-12 20:08:18 +02:00
|
|
|
size_t rdft_hlen[MAX_PLANES];
|
|
|
|
size_t rdft_vlen[MAX_PLANES];
|
2022-02-14 16:21:49 +02:00
|
|
|
float *rdft_hdata_in[MAX_PLANES];
|
|
|
|
float *rdft_vdata_in[MAX_PLANES];
|
|
|
|
float *rdft_hdata_out[MAX_PLANES];
|
|
|
|
float *rdft_vdata_out[MAX_PLANES];
|
2015-05-12 20:08:18 +02:00
|
|
|
|
|
|
|
int dc[MAX_PLANES];
|
|
|
|
char *weight_str[MAX_PLANES];
|
|
|
|
AVExpr *weight_expr[MAX_PLANES];
|
|
|
|
double *weight[MAX_PLANES];
|
2015-02-24 08:47:30 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
int (*rdft_horizontal)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
|
|
|
|
int (*irdft_horizontal)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
|
2015-02-24 08:47:30 +02:00
|
|
|
} FFTFILTContext;
|
|
|
|
|
2021-10-14 20:22:20 +02:00
|
|
|
static const char *const var_names[] = { "X", "Y", "W", "H", "N", "WS", "HS", NULL };
|
|
|
|
enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_WS, VAR_HS, VAR_VARS_NB };
|
2015-02-24 08:47:30 +02:00
|
|
|
|
|
|
|
enum { Y = 0, U, V };
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(FFTFILTContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
|
|
|
|
static const AVOption fftfilt_options[] = {
|
|
|
|
{ "dc_Y", "adjust gain in Y plane", OFFSET(dc[Y]), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1000, FLAGS },
|
|
|
|
{ "dc_U", "adjust gain in U plane", OFFSET(dc[U]), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1000, FLAGS },
|
|
|
|
{ "dc_V", "adjust gain in V plane", OFFSET(dc[V]), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1000, FLAGS },
|
2020-03-17 23:46:36 +02:00
|
|
|
{ "weight_Y", "set luminance expression in Y plane", OFFSET(weight_str[Y]), AV_OPT_TYPE_STRING, {.str = "1"}, 0, 0, FLAGS },
|
|
|
|
{ "weight_U", "set chrominance expression in U plane", OFFSET(weight_str[U]), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
|
|
|
|
{ "weight_V", "set chrominance expression in V plane", OFFSET(weight_str[V]), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
|
2024-02-11 16:41:05 +02:00
|
|
|
{ "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, .unit = "eval" },
|
2017-09-04 19:29:37 +02:00
|
|
|
{ "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
|
|
|
|
{ "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
|
2015-02-24 08:47:30 +02:00
|
|
|
{NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(fftfilt);
|
|
|
|
|
|
|
|
static inline double lum(void *priv, double x, double y, int plane)
|
|
|
|
{
|
2016-01-14 23:19:52 +02:00
|
|
|
FFTFILTContext *s = priv;
|
2022-02-14 16:21:49 +02:00
|
|
|
return s->rdft_vdata_out[plane][(int)x * s->rdft_vstride[plane] + (int)y];
|
2015-02-24 08:47:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static double weight_Y(void *priv, double x, double y) { return lum(priv, x, y, Y); }
|
|
|
|
static double weight_U(void *priv, double x, double y) { return lum(priv, x, y, U); }
|
|
|
|
static double weight_V(void *priv, double x, double y) { return lum(priv, x, y, V); }
|
|
|
|
|
2022-02-14 16:21:49 +02:00
|
|
|
static void copy_rev(float *dest, int w, int w2)
|
2015-02-24 08:47:30 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = w; i < w + (w2-w)/2; i++)
|
|
|
|
dest[i] = dest[2*w - i - 1];
|
|
|
|
|
|
|
|
for (; i < w2; i++)
|
|
|
|
dest[i] = dest[w2 - i];
|
|
|
|
}
|
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
static int rdft_horizontal8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
2015-02-24 08:47:30 +02:00
|
|
|
{
|
2021-10-14 00:57:03 +02:00
|
|
|
FFTFILTContext *s = ctx->priv;
|
|
|
|
AVFrame *in = arg;
|
|
|
|
|
|
|
|
for (int plane = 0; plane < s->nb_planes; plane++) {
|
|
|
|
const int w = s->planewidth[plane];
|
|
|
|
const int h = s->planeheight[plane];
|
|
|
|
const int slice_start = (h * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (h * (jobnr+1)) / nb_jobs;
|
2015-02-24 08:47:30 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int i = slice_start; i < slice_end; i++) {
|
|
|
|
const uint8_t *src = in->data[plane] + i * in->linesize[plane];
|
2022-02-14 16:21:49 +02:00
|
|
|
float *hdata_in = s->rdft_hdata_in[plane] + i * s->rdft_hstride[plane];
|
2021-10-14 00:57:03 +02:00
|
|
|
|
|
|
|
for (int j = 0; j < w; j++)
|
2022-02-14 16:21:49 +02:00
|
|
|
hdata_in[j] = src[j];
|
2021-10-14 00:57:03 +02:00
|
|
|
|
2022-02-14 16:21:49 +02:00
|
|
|
copy_rev(s->rdft_hdata_in[plane] + i * s->rdft_hstride[plane], w, s->rdft_hlen[plane]);
|
2021-10-14 00:57:03 +02:00
|
|
|
}
|
2015-02-24 08:47:30 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int i = slice_start; i < slice_end; i++)
|
2022-02-14 16:21:49 +02:00
|
|
|
s->htx_fn(s->hrdft[jobnr][plane],
|
|
|
|
s->rdft_hdata_out[plane] + i * s->rdft_hstride[plane],
|
|
|
|
s->rdft_hdata_in[plane] + i * s->rdft_hstride[plane],
|
|
|
|
sizeof(float));
|
2015-02-24 08:47:30 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
return 0;
|
2015-02-24 08:47:30 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
static int rdft_horizontal16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
2017-12-23 12:33:18 +02:00
|
|
|
{
|
2021-10-14 00:57:03 +02:00
|
|
|
FFTFILTContext *s = ctx->priv;
|
|
|
|
AVFrame *in = arg;
|
2017-12-23 12:33:18 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int plane = 0; plane < s->nb_planes; plane++) {
|
|
|
|
const int w = s->planewidth[plane];
|
|
|
|
const int h = s->planeheight[plane];
|
|
|
|
const int slice_start = (h * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (h * (jobnr+1)) / nb_jobs;
|
2017-12-23 12:33:18 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int i = slice_start; i < slice_end; i++) {
|
|
|
|
const uint16_t *src = (const uint16_t *)(in->data[plane] + i * in->linesize[plane]);
|
2022-02-14 16:21:49 +02:00
|
|
|
float *hdata_in = s->rdft_hdata_in[plane] + i * s->rdft_hstride[plane];
|
2017-12-23 12:33:18 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int j = 0; j < w; j++)
|
2022-02-14 16:21:49 +02:00
|
|
|
hdata_in[j] = src[j];
|
2017-12-23 12:33:18 +02:00
|
|
|
|
2022-02-14 16:21:49 +02:00
|
|
|
copy_rev(s->rdft_hdata_in[plane] + i * s->rdft_hstride[plane], w, s->rdft_hlen[plane]);
|
2021-10-14 00:57:03 +02:00
|
|
|
}
|
2015-02-24 08:47:30 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int i = slice_start; i < slice_end; i++)
|
2022-02-14 16:21:49 +02:00
|
|
|
s->htx_fn(s->hrdft[jobnr][plane],
|
|
|
|
s->rdft_hdata_out[plane] + i * s->rdft_hstride[plane],
|
|
|
|
s->rdft_hdata_in[plane] + i * s->rdft_hstride[plane],
|
|
|
|
sizeof(float));
|
2015-02-24 08:47:30 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
return 0;
|
2015-02-24 08:47:30 +02:00
|
|
|
}
|
2021-10-14 00:57:03 +02:00
|
|
|
|
|
|
|
static int irdft_horizontal8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
2015-02-24 08:47:30 +02:00
|
|
|
{
|
2021-10-14 00:57:03 +02:00
|
|
|
FFTFILTContext *s = ctx->priv;
|
|
|
|
AVFrame *out = arg;
|
2017-09-04 18:24:00 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int plane = 0; plane < s->nb_planes; plane++) {
|
|
|
|
const int w = s->planewidth[plane];
|
|
|
|
const int h = s->planeheight[plane];
|
|
|
|
const int slice_start = (h * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (h * (jobnr+1)) / nb_jobs;
|
2015-02-24 08:47:30 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int i = slice_start; i < slice_end; i++)
|
2022-02-14 16:21:49 +02:00
|
|
|
s->ihtx_fn(s->ihrdft[jobnr][plane],
|
|
|
|
s->rdft_hdata_out[plane] + i * s->rdft_hstride[plane],
|
|
|
|
s->rdft_hdata_in[plane] + i * s->rdft_hstride[plane],
|
2022-11-19 01:04:06 +02:00
|
|
|
sizeof(AVComplexFloat));
|
2015-02-24 08:47:30 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int i = slice_start; i < slice_end; i++) {
|
2022-02-14 16:21:49 +02:00
|
|
|
const float scale = 1.f / (s->rdft_hlen[plane] * s->rdft_vlen[plane]);
|
|
|
|
const float *src = s->rdft_hdata_out[plane] + i * s->rdft_hstride[plane];
|
2021-10-14 00:57:03 +02:00
|
|
|
uint8_t *dst = out->data[plane] + i * out->linesize[plane];
|
2017-09-04 18:24:00 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int j = 0; j < w; j++)
|
2021-10-14 08:29:08 +02:00
|
|
|
dst[j] = av_clip_uint8(lrintf(src[j] * scale));
|
2021-10-14 00:57:03 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-24 08:47:30 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
return 0;
|
2015-02-24 08:47:30 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
static int irdft_horizontal16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
2017-12-23 12:33:18 +02:00
|
|
|
{
|
2021-10-14 00:57:03 +02:00
|
|
|
FFTFILTContext *s = ctx->priv;
|
|
|
|
AVFrame *out = arg;
|
|
|
|
|
|
|
|
for (int plane = 0; plane < s->nb_planes; plane++) {
|
|
|
|
int max = (1 << s->depth) - 1;
|
|
|
|
const int w = s->planewidth[plane];
|
|
|
|
const int h = s->planeheight[plane];
|
|
|
|
const int slice_start = (h * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (h * (jobnr+1)) / nb_jobs;
|
|
|
|
|
|
|
|
for (int i = slice_start; i < slice_end; i++)
|
2022-02-14 16:21:49 +02:00
|
|
|
s->ihtx_fn(s->ihrdft[jobnr][plane],
|
|
|
|
s->rdft_hdata_out[plane] + i * s->rdft_hstride[plane],
|
|
|
|
s->rdft_hdata_in[plane] + i * s->rdft_hstride[plane],
|
2022-11-19 01:04:06 +02:00
|
|
|
sizeof(AVComplexFloat));
|
2021-10-14 00:57:03 +02:00
|
|
|
|
|
|
|
for (int i = slice_start; i < slice_end; i++) {
|
2022-02-14 16:21:49 +02:00
|
|
|
const float scale = 1.f / (s->rdft_hlen[plane] * s->rdft_vlen[plane]);
|
|
|
|
const float *src = s->rdft_hdata_out[plane] + i * s->rdft_hstride[plane];
|
2021-10-14 00:57:03 +02:00
|
|
|
uint16_t *dst = (uint16_t *)(out->data[plane] + i * out->linesize[plane]);
|
2017-12-23 12:33:18 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int j = 0; j < w; j++)
|
|
|
|
dst[j] = av_clip(lrintf(src[j] * scale), 0, max);
|
|
|
|
}
|
|
|
|
}
|
2017-12-23 12:33:18 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
return 0;
|
2017-12-23 12:33:18 +02:00
|
|
|
}
|
|
|
|
|
2015-02-24 08:47:30 +02:00
|
|
|
static av_cold int initialize(AVFilterContext *ctx)
|
|
|
|
{
|
2016-01-14 23:19:52 +02:00
|
|
|
FFTFILTContext *s = ctx->priv;
|
2015-02-24 08:47:30 +02:00
|
|
|
int ret = 0, plane;
|
|
|
|
|
2016-01-14 23:19:52 +02:00
|
|
|
if (!s->dc[U] && !s->dc[V]) {
|
|
|
|
s->dc[U] = s->dc[Y];
|
|
|
|
s->dc[V] = s->dc[Y];
|
2015-02-24 08:47:30 +02:00
|
|
|
} else {
|
2016-01-14 23:19:52 +02:00
|
|
|
if (!s->dc[U]) s->dc[U] = s->dc[V];
|
|
|
|
if (!s->dc[V]) s->dc[V] = s->dc[U];
|
2015-02-24 08:47:30 +02:00
|
|
|
}
|
|
|
|
|
2016-01-14 23:19:52 +02:00
|
|
|
if (!s->weight_str[U] && !s->weight_str[V]) {
|
|
|
|
s->weight_str[U] = av_strdup(s->weight_str[Y]);
|
|
|
|
s->weight_str[V] = av_strdup(s->weight_str[Y]);
|
2015-02-24 08:47:30 +02:00
|
|
|
} else {
|
2016-01-14 23:19:52 +02:00
|
|
|
if (!s->weight_str[U]) s->weight_str[U] = av_strdup(s->weight_str[V]);
|
|
|
|
if (!s->weight_str[V]) s->weight_str[V] = av_strdup(s->weight_str[U]);
|
2015-02-24 08:47:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (plane = 0; plane < 3; plane++) {
|
|
|
|
static double (*p[])(void *, double, double) = { weight_Y, weight_U, weight_V };
|
|
|
|
const char *const func2_names[] = {"weight_Y", "weight_U", "weight_V", NULL };
|
|
|
|
double (*func2[])(void *, double, double) = { weight_Y, weight_U, weight_V, p[plane], NULL };
|
|
|
|
|
2016-01-14 23:19:52 +02:00
|
|
|
ret = av_expr_parse(&s->weight_expr[plane], s->weight_str[plane], var_names,
|
2015-02-24 08:47:30 +02:00
|
|
|
NULL, NULL, func2_names, func2, 0, ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-04 19:29:37 +02:00
|
|
|
static void do_eval(FFTFILTContext *s, AVFilterLink *inlink, int plane)
|
|
|
|
{
|
2024-08-06 08:50:21 +02:00
|
|
|
FilterLink *l = ff_filter_link(inlink);
|
2017-09-04 19:29:37 +02:00
|
|
|
double values[VAR_VARS_NB];
|
|
|
|
int i, j;
|
|
|
|
|
2024-08-06 08:50:21 +02:00
|
|
|
values[VAR_N] = l->frame_count_out;
|
2017-09-04 19:29:37 +02:00
|
|
|
values[VAR_W] = s->planewidth[plane];
|
|
|
|
values[VAR_H] = s->planeheight[plane];
|
2021-10-14 20:22:20 +02:00
|
|
|
values[VAR_WS] = s->rdft_hlen[plane];
|
|
|
|
values[VAR_HS] = s->rdft_vlen[plane];
|
2017-09-04 19:29:37 +02:00
|
|
|
|
|
|
|
for (i = 0; i < s->rdft_hlen[plane]; i++) {
|
|
|
|
values[VAR_X] = i;
|
|
|
|
for (j = 0; j < s->rdft_vlen[plane]; j++) {
|
|
|
|
values[VAR_Y] = j;
|
|
|
|
s->weight[plane][i * s->rdft_vlen[plane] + j] =
|
|
|
|
av_expr_eval(s->weight_expr[plane], values, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 08:47:30 +02:00
|
|
|
static int config_props(AVFilterLink *inlink)
|
|
|
|
{
|
2016-01-14 23:19:52 +02:00
|
|
|
FFTFILTContext *s = inlink->dst->priv;
|
2015-02-24 08:47:30 +02:00
|
|
|
const AVPixFmtDescriptor *desc;
|
2022-11-09 17:23:53 +02:00
|
|
|
int ret, i, plane;
|
2015-02-24 08:47:30 +02:00
|
|
|
|
|
|
|
desc = av_pix_fmt_desc_get(inlink->format);
|
2017-09-04 18:36:37 +02:00
|
|
|
s->depth = desc->comp[0].depth;
|
|
|
|
s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
|
|
|
|
s->planewidth[0] = s->planewidth[3] = inlink->w;
|
|
|
|
s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
|
|
|
|
s->planeheight[0] = s->planeheight[3] = inlink->h;
|
|
|
|
|
|
|
|
s->nb_planes = av_pix_fmt_count_planes(inlink->format);
|
2021-10-14 00:57:03 +02:00
|
|
|
s->nb_threads = FFMIN(32, ff_filter_get_nb_threads(inlink->dst));
|
2017-09-04 18:36:37 +02:00
|
|
|
|
2015-02-24 08:47:30 +02:00
|
|
|
for (i = 0; i < desc->nb_components; i++) {
|
2017-09-04 18:36:37 +02:00
|
|
|
int w = s->planewidth[i];
|
|
|
|
int h = s->planeheight[i];
|
2015-02-24 08:47:30 +02:00
|
|
|
|
|
|
|
/* RDFT - Array initialization for Horizontal pass*/
|
2021-10-13 23:10:05 +02:00
|
|
|
s->rdft_hlen[i] = 1 << (32 - ff_clz(w));
|
2022-02-14 16:21:49 +02:00
|
|
|
s->rdft_hstride[i] = FFALIGN(s->rdft_hlen[i] + 2, av_cpu_max_align());
|
2021-10-13 23:10:05 +02:00
|
|
|
s->rdft_hbits[i] = av_log2(s->rdft_hlen[i]);
|
2022-02-14 16:21:49 +02:00
|
|
|
if (!(s->rdft_hdata_in[i] = av_calloc(h, s->rdft_hstride[i] * sizeof(float))))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
if (!(s->rdft_hdata_out[i] = av_calloc(h, s->rdft_hstride[i] * sizeof(float))))
|
2015-02-24 08:47:30 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int j = 0; j < s->nb_threads; j++) {
|
2022-02-14 16:21:49 +02:00
|
|
|
float scale = 1.f, iscale = 1.f;
|
|
|
|
|
2022-11-09 17:23:53 +02:00
|
|
|
ret = av_tx_init(&s->hrdft[j][i], &s->htx_fn, AV_TX_FLOAT_RDFT,
|
|
|
|
0, 1 << s->rdft_hbits[i], &scale, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ret = av_tx_init(&s->ihrdft[j][i], &s->ihtx_fn, AV_TX_FLOAT_RDFT,
|
|
|
|
1, 1 << s->rdft_hbits[i], &iscale, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2021-10-14 00:57:03 +02:00
|
|
|
}
|
2017-09-04 18:24:00 +02:00
|
|
|
|
2015-02-24 08:47:30 +02:00
|
|
|
/* RDFT - Array initialization for Vertical pass*/
|
2021-10-13 23:10:05 +02:00
|
|
|
s->rdft_vlen[i] = 1 << (32 - ff_clz(h));
|
2022-02-14 16:21:49 +02:00
|
|
|
s->rdft_vstride[i] = FFALIGN(s->rdft_vlen[i] + 2, av_cpu_max_align());
|
2021-10-13 23:10:05 +02:00
|
|
|
s->rdft_vbits[i] = av_log2(s->rdft_vlen[i]);
|
2022-02-14 16:21:49 +02:00
|
|
|
if (!(s->rdft_vdata_in[i] = av_calloc(s->rdft_hstride[i], s->rdft_vstride[i] * sizeof(float))))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
if (!(s->rdft_vdata_out[i] = av_calloc(s->rdft_hstride[i], s->rdft_vstride[i] * sizeof(float))))
|
2015-02-24 08:47:30 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2017-09-04 18:24:00 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int j = 0; j < s->nb_threads; j++) {
|
2022-02-14 16:21:49 +02:00
|
|
|
float scale = 1.f, iscale = 1.f;
|
|
|
|
|
2022-11-09 17:23:53 +02:00
|
|
|
ret = av_tx_init(&s->vrdft[j][i], &s->vtx_fn, AV_TX_FLOAT_RDFT,
|
|
|
|
0, 1 << s->rdft_vbits[i], &scale, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ret = av_tx_init(&s->ivrdft[j][i], &s->ivtx_fn, AV_TX_FLOAT_RDFT,
|
|
|
|
1, 1 << s->rdft_vbits[i], &iscale, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2021-10-14 00:57:03 +02:00
|
|
|
}
|
2015-02-24 08:47:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*Luminance value - Array initialization*/
|
2017-09-04 18:36:37 +02:00
|
|
|
for (plane = 0; plane < 3; plane++) {
|
2022-02-14 16:21:49 +02:00
|
|
|
if(!(s->weight[plane] = av_calloc(s->rdft_hlen[plane], s->rdft_vlen[plane] * sizeof(double))))
|
2015-02-24 08:47:30 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2017-09-04 19:29:37 +02:00
|
|
|
|
|
|
|
if (s->eval_mode == EVAL_MODE_INIT)
|
|
|
|
do_eval(s, inlink, plane);
|
2015-02-24 08:47:30 +02:00
|
|
|
}
|
2017-12-23 12:33:18 +02:00
|
|
|
|
|
|
|
if (s->depth <= 8) {
|
|
|
|
s->rdft_horizontal = rdft_horizontal8;
|
|
|
|
s->irdft_horizontal = irdft_horizontal8;
|
2024-07-08 00:59:40 +02:00
|
|
|
} else {
|
2017-12-23 12:33:18 +02:00
|
|
|
s->rdft_horizontal = rdft_horizontal16;
|
|
|
|
s->irdft_horizontal = irdft_horizontal16;
|
|
|
|
}
|
2015-02-24 08:47:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
static int multiply_data(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
|
|
|
{
|
|
|
|
FFTFILTContext *s = ctx->priv;
|
|
|
|
|
|
|
|
for (int plane = 0; plane < s->nb_planes; plane++) {
|
|
|
|
const int height = s->rdft_hlen[plane];
|
|
|
|
const int slice_start = (height * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (height * (jobnr+1)) / nb_jobs;
|
|
|
|
/*Change user defined parameters*/
|
|
|
|
for (int i = slice_start; i < slice_end; i++) {
|
|
|
|
const double *weight = s->weight[plane] + i * s->rdft_vlen[plane];
|
2022-02-14 16:21:49 +02:00
|
|
|
float *vdata = s->rdft_vdata_out[plane] + i * s->rdft_vstride[plane];
|
2021-10-14 00:57:03 +02:00
|
|
|
|
|
|
|
for (int j = 0; j < s->rdft_vlen[plane]; j++)
|
|
|
|
vdata[j] *= weight[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int copy_vertical(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
|
|
|
{
|
|
|
|
FFTFILTContext *s = ctx->priv;
|
|
|
|
|
|
|
|
for (int plane = 0; plane < s->nb_planes; plane++) {
|
|
|
|
const int hlen = s->rdft_hlen[plane];
|
|
|
|
const int vlen = s->rdft_vlen[plane];
|
2022-02-14 16:21:49 +02:00
|
|
|
const int hstride = s->rdft_hstride[plane];
|
|
|
|
const int vstride = s->rdft_vstride[plane];
|
2021-10-14 00:57:03 +02:00
|
|
|
const int slice_start = (hlen * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (hlen * (jobnr+1)) / nb_jobs;
|
|
|
|
const int h = s->planeheight[plane];
|
2022-02-14 16:21:49 +02:00
|
|
|
float *hdata = s->rdft_hdata_out[plane];
|
|
|
|
float *vdata = s->rdft_vdata_in[plane];
|
2021-10-14 00:57:03 +02:00
|
|
|
|
|
|
|
for (int i = slice_start; i < slice_end; i++) {
|
|
|
|
for (int j = 0; j < h; j++)
|
2022-02-14 16:21:49 +02:00
|
|
|
vdata[i * vstride + j] = hdata[j * hstride + i];
|
|
|
|
copy_rev(vdata + i * vstride, h, vlen);
|
2021-10-14 00:57:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rdft_vertical(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
|
|
|
{
|
|
|
|
FFTFILTContext *s = ctx->priv;
|
|
|
|
|
|
|
|
for (int plane = 0; plane < s->nb_planes; plane++) {
|
|
|
|
const int height = s->rdft_hlen[plane];
|
|
|
|
const int slice_start = (height * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (height * (jobnr+1)) / nb_jobs;
|
|
|
|
|
|
|
|
for (int i = slice_start; i < slice_end; i++)
|
2022-02-14 16:21:49 +02:00
|
|
|
s->vtx_fn(s->vrdft[jobnr][plane],
|
|
|
|
s->rdft_vdata_out[plane] + i * s->rdft_vstride[plane],
|
|
|
|
s->rdft_vdata_in[plane] + i * s->rdft_vstride[plane],
|
|
|
|
sizeof(float));
|
2021-10-14 00:57:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int irdft_vertical(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
|
|
|
{
|
|
|
|
FFTFILTContext *s = ctx->priv;
|
|
|
|
|
|
|
|
for (int plane = 0; plane < s->nb_planes; plane++) {
|
|
|
|
const int height = s->rdft_hlen[plane];
|
|
|
|
const int slice_start = (height * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (height * (jobnr+1)) / nb_jobs;
|
|
|
|
|
|
|
|
for (int i = slice_start; i < slice_end; i++)
|
2022-02-14 16:21:49 +02:00
|
|
|
s->ivtx_fn(s->ivrdft[jobnr][plane],
|
|
|
|
s->rdft_vdata_in[plane] + i * s->rdft_vstride[plane],
|
|
|
|
s->rdft_vdata_out[plane] + i * s->rdft_vstride[plane],
|
2022-11-19 01:04:06 +02:00
|
|
|
sizeof(AVComplexFloat));
|
2021-10-14 00:57:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int copy_horizontal(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
|
|
|
{
|
|
|
|
FFTFILTContext *s = ctx->priv;
|
|
|
|
|
|
|
|
for (int plane = 0; plane < s->nb_planes; plane++) {
|
|
|
|
const int hlen = s->rdft_hlen[plane];
|
2022-02-14 16:21:49 +02:00
|
|
|
const int hstride = s->rdft_hstride[plane];
|
|
|
|
const int vstride = s->rdft_vstride[plane];
|
2021-10-14 00:57:03 +02:00
|
|
|
const int slice_start = (hlen * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (hlen * (jobnr+1)) / nb_jobs;
|
|
|
|
const int h = s->planeheight[plane];
|
2022-02-14 16:21:49 +02:00
|
|
|
float *hdata = s->rdft_hdata_in[plane];
|
|
|
|
float *vdata = s->rdft_vdata_in[plane];
|
2021-10-14 00:57:03 +02:00
|
|
|
|
|
|
|
for (int i = slice_start; i < slice_end; i++)
|
|
|
|
for (int j = 0; j < h; j++)
|
2022-02-14 16:21:49 +02:00
|
|
|
hdata[j * hstride + i] = vdata[i * vstride + j];
|
2021-10-14 00:57:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-24 08:47:30 +02:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AVFilterLink *outlink = inlink->dst->outputs[0];
|
2016-01-14 23:19:52 +02:00
|
|
|
FFTFILTContext *s = ctx->priv;
|
2015-02-24 08:47:30 +02:00
|
|
|
AVFrame *out;
|
|
|
|
|
|
|
|
out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
|
2017-06-24 18:36:46 +02:00
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
2015-02-24 08:47:30 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2017-06-24 18:36:46 +02:00
|
|
|
}
|
2015-02-24 08:47:30 +02:00
|
|
|
|
|
|
|
av_frame_copy_props(out, in);
|
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
ff_filter_execute(ctx, s->rdft_horizontal, in, NULL,
|
|
|
|
FFMIN(s->planeheight[1], s->nb_threads));
|
2015-02-24 08:47:30 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
ff_filter_execute(ctx, copy_vertical, NULL, NULL,
|
|
|
|
FFMIN(s->planeheight[1], s->nb_threads));
|
|
|
|
|
|
|
|
ff_filter_execute(ctx, rdft_vertical, NULL, NULL,
|
|
|
|
FFMIN(s->planeheight[1], s->nb_threads));
|
2015-02-24 08:47:30 +02:00
|
|
|
|
2022-02-14 16:21:49 +02:00
|
|
|
for (int plane = 0; plane < s->nb_planes; plane++) {
|
|
|
|
if (s->eval_mode == EVAL_MODE_FRAME)
|
|
|
|
do_eval(s, inlink, plane);
|
|
|
|
}
|
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
ff_filter_execute(ctx, multiply_data, NULL, NULL,
|
|
|
|
FFMIN(s->planeheight[1], s->nb_threads));
|
|
|
|
|
|
|
|
for (int plane = 0; plane < s->nb_planes; plane++)
|
2022-02-14 16:21:49 +02:00
|
|
|
s->rdft_vdata_out[plane][0] += s->rdft_hlen[plane] * s->rdft_vlen[plane] * s->dc[plane] * (1 << (s->depth - 8));
|
2015-02-24 08:47:30 +02:00
|
|
|
|
2021-10-14 00:57:03 +02:00
|
|
|
ff_filter_execute(ctx, irdft_vertical, NULL, NULL,
|
|
|
|
FFMIN(s->planeheight[1], s->nb_threads));
|
|
|
|
|
|
|
|
ff_filter_execute(ctx, copy_horizontal, NULL, NULL,
|
|
|
|
FFMIN(s->planeheight[1], s->nb_threads));
|
|
|
|
|
|
|
|
ff_filter_execute(ctx, s->irdft_horizontal, out, NULL,
|
|
|
|
FFMIN(s->planeheight[1], s->nb_threads));
|
2015-02-24 08:47:30 +02:00
|
|
|
|
|
|
|
av_frame_free(&in);
|
|
|
|
return ff_filter_frame(outlink, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
2016-01-14 23:19:52 +02:00
|
|
|
FFTFILTContext *s = ctx->priv;
|
2022-02-14 16:21:49 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < MAX_PLANES; i++) {
|
|
|
|
av_freep(&s->rdft_hdata_in[i]);
|
|
|
|
av_freep(&s->rdft_vdata_in[i]);
|
|
|
|
av_freep(&s->rdft_hdata_out[i]);
|
|
|
|
av_freep(&s->rdft_vdata_out[i]);
|
2016-01-14 23:19:52 +02:00
|
|
|
av_expr_free(s->weight_expr[i]);
|
2022-02-14 16:21:49 +02:00
|
|
|
av_freep(&s->weight[i]);
|
2021-10-14 00:57:03 +02:00
|
|
|
for (int j = 0; j < s->nb_threads; j++) {
|
2022-02-14 16:21:49 +02:00
|
|
|
av_tx_uninit(&s->hrdft[j][i]);
|
|
|
|
av_tx_uninit(&s->ihrdft[j][i]);
|
|
|
|
av_tx_uninit(&s->vrdft[j][i]);
|
|
|
|
av_tx_uninit(&s->ivrdft[j][i]);
|
2021-10-14 00:57:03 +02:00
|
|
|
}
|
2015-02-24 08:47:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 15:57:43 +02:00
|
|
|
static const enum AVPixelFormat pixel_fmts_fftfilt[] = {
|
|
|
|
AV_PIX_FMT_GRAY8,
|
2021-10-14 10:08:01 +02:00
|
|
|
AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
|
|
|
|
AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
|
2021-09-29 15:57:43 +02:00
|
|
|
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
|
|
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
|
|
|
|
AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
|
|
|
|
AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10,
|
|
|
|
AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV420P14,
|
|
|
|
AV_PIX_FMT_YUV420P16,
|
|
|
|
AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV422P10,
|
|
|
|
AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV422P14,
|
|
|
|
AV_PIX_FMT_YUV422P16,
|
|
|
|
AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
|
|
|
|
AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14,
|
|
|
|
AV_PIX_FMT_YUV444P16,
|
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
2015-02-24 08:47:30 +02:00
|
|
|
|
|
|
|
static const AVFilterPad fftfilt_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_props,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_vf_fftfilt = {
|
2015-02-24 08:47:30 +02:00
|
|
|
.name = "fftfilt",
|
2016-01-14 23:20:25 +02:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Apply arbitrary expressions to pixels in frequency domain."),
|
2015-02-24 08:47:30 +02:00
|
|
|
.priv_size = sizeof(FFTFILTContext),
|
|
|
|
.priv_class = &fftfilt_class,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(fftfilt_inputs),
|
2023-08-03 14:37:51 +02:00
|
|
|
FILTER_OUTPUTS(ff_video_default_filterpad),
|
2021-09-27 21:58:53 +02:00
|
|
|
FILTER_PIXFMTS_ARRAY(pixel_fmts_fftfilt),
|
2015-02-24 08:47:30 +02:00
|
|
|
.init = initialize,
|
|
|
|
.uninit = uninit,
|
2021-10-14 00:57:03 +02:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
|
2015-02-24 08:47:30 +02:00
|
|
|
};
|