matrix filter

This commit is contained in:
mrbesen 2021-10-20 20:09:38 +02:00
parent a202604c32
commit c8e9ee7319
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
3 changed files with 208 additions and 0 deletions

View File

@ -340,6 +340,7 @@ OBJS-$(CONFIG_MASKEDMERGE_FILTER) += vf_maskedmerge.o framesync.o
OBJS-$(CONFIG_MASKEDMIN_FILTER) += vf_maskedminmax.o framesync.o
OBJS-$(CONFIG_MASKEDTHRESHOLD_FILTER) += vf_maskedthreshold.o framesync.o
OBJS-$(CONFIG_MASKFUN_FILTER) += vf_maskfun.o
OBJS-$(CONFIG_MASKFUN_FILTER) += vf_matrix.o
OBJS-$(CONFIG_MCDEINT_FILTER) += vf_mcdeint.o
OBJS-$(CONFIG_MEDIAN_FILTER) += vf_median.o
OBJS-$(CONFIG_MERGEPLANES_FILTER) += vf_mergeplanes.o framesync.o

View File

@ -325,6 +325,7 @@ extern const AVFilter ff_vf_maskedmerge;
extern const AVFilter ff_vf_maskedmin;
extern const AVFilter ff_vf_maskedthreshold;
extern const AVFilter ff_vf_maskfun;
extern const AVFilter ff_vf_matrix;
extern const AVFilter ff_vf_mcdeint;
extern const AVFilter ff_vf_median;
extern const AVFilter ff_vf_mergeplanes;

206
libavfilter/vf_matrix.cpp Normal file
View File

@ -0,0 +1,206 @@
/*
* 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
*/
extern "C" {
#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
#include "avfilter.h"
#include "formats.h"
#include "internal.h"
#include "video.h"
}
#include <iostream>
extern "C" {
typedef struct MatrixContext {
const AVClass *classs;
// user input
unsigned int w;
unsigned int h;
int depth; // bit depth of planes
// size of input planes
int planewidth[4];
int planeheight[4];
// ptr to multithreaded functions
// one function for the y plane one for uv
int (*do_plane)(AVFilterContext *s, void *arg, int plane);
} MatrixContext;
} // extern c
// luminance slices
template<typename T>
static inline T getPxl(T* planeData, uint32_t linesize, uint32_t h, uint32_t x, uint32_t y) {
uint32_t yc = (y % h);
uint32_t xc = (x % linesize);
return (planeData[yc * linesize + xc]);
}
template<typename T>
static int matrix_processplane(AVFilterContext *ctx, void *arg, int plane)
{
MatrixContext *s = (MatrixContext *) ctx->priv;
AVFrame *frame = (AVFrame *) arg;
const int width = s->planewidth[plane];
const int height = s->planeheight[plane];
const unsigned int w = s->w;
const unsigned int h = s->h;
const int linesize = frame->linesize[plane] / sizeof(T);
T *ptr = (T*) frame->data[plane];
// copy frame
T* origframe = new T[width * height];
memcpy(origframe, ptr, width * height * sizeof(T));
// apply mosaik
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
ptr[x] = getPxl(origframe, linesize, height, x * w, y * h);
}
ptr += linesize;
}
delete[] origframe;
return 0;
}
extern "C" {
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
{
AVFilterContext *ctx = (AVFilterContext *) inlink->dst;
MatrixContext *s = (MatrixContext *) ctx->priv;
s->do_plane(ctx, frame, 0); // y plane
s->do_plane(ctx, frame, 1); // u plane
s->do_plane(ctx, frame, 2); // v plane
return ff_filter_frame(ctx->outputs[0], frame);
}
static const enum AVPixelFormat pixel_fmts[] = {
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
AV_PIX_FMT_YUVJ411P,
AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
AV_PIX_FMT_YUV440P10,
AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
AV_PIX_FMT_YUV440P12,
AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
AV_PIX_FMT_NONE
};
static av_cold int config_input(AVFilterLink *inlink)
{
AVFilterContext *ctx = (AVFilterContext *) inlink->dst;
MatrixContext *s = (MatrixContext *) ctx->priv;
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get((AVPixelFormat) inlink->format);
int depth;
s->depth = 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->do_plane = depth <= 8 ? matrix_processplane<uint8_t> : matrix_processplane<uint16_t>;
return 0;
}
static const AVFilterPad matrix_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
.filter_frame = filter_frame,
.config_props = config_input,
},
};
static const AVFilterPad matrix_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
},
};
#define OFFSET(x) offsetof(MatrixContext, x)
#define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
static const AVOption matrix_options[] = {
{ "w", "width", OFFSET(w), AV_OPT_TYPE_INT, {.i64=2}, 1, 1000, VF},
{ "h", "height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=2}, 1, 1000, VF},
{ NULL }
};
AVFILTER_DEFINE_CLASS(matrix);
extern "C" const AVFilter ff_vf_matrix = {
.name = "matrix",
.description = NULL_IF_CONFIG_SMALL("Multiply a video into a Matrix"),
// FILTER_INPUTS(matrix_inputs),
// FILTER_OUTPUTS(matrix_outputs),
.inputs = matrix_inputs,
.outputs = matrix_outputs,
.priv_class = &matrix_class,
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC , // | AVFILTER_FLAG_SLICE_THREADS,
.nb_inputs = 1,
.nb_outputs = 1,
.formats_state = FF_FILTER_FORMATS_PIXFMT_LIST,
// preinit
// init
// init_dict
// uninit
.formats = { .pixels_list = pixel_fmts },
.priv_size = sizeof(MatrixContext),
// FILTER_PIXFMTS_ARRAY(pixel_fmts),
.process_command = NULL, // ff_filter_process_command
// activate
};
} // extern c