From patchwork Fri Jan 22 20:20:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 25116 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 0813244A55C for ; Fri, 22 Jan 2021 22:20:46 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D6EDF68A2B9; Fri, 22 Jan 2021 22:20:45 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A871D689805 for ; Fri, 22 Jan 2021 22:20:38 +0200 (EET) Received: by mail-ej1-f47.google.com with SMTP id l9so9470064ejx.3 for ; Fri, 22 Jan 2021 12:20:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=/J9pfUmTN+C0LAYAHcILPuv1bGx8112HQsb6jQAf/HI=; b=fENDe47DAAYgXm+CoqlE0wRlYF40edrYGOrK2E9iNeuM0Dkz1zW9oWt0TkecVuuAFJ M93/wC/vk2wfTvjOOuQxawTAZjMDkbSlcsfQyb9bEgvvir+iCTckegiy5Am8UwK3aT17 YAqQw/oPcicQCw9wHxxeq98Qgar0u1cWLJprF85rrbnSQMLHc+dnNPl3yNCfqFraPfNi gjCCL0fJAWHQClTJyHV0tFK5otw6+OUnnWbfjXUBFbASQ5qqCK3VOEYxdIgc5ML5bCx0 9w/y7vAZsc/ssxXGFDwAzuAzICDua04BU5yx1ncP5sWMfB9r9MAh/qBbnPO2yeKuVuNe Q05A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=/J9pfUmTN+C0LAYAHcILPuv1bGx8112HQsb6jQAf/HI=; b=Ae0+8cowRPaZXvXvDBow8O9I7lY9BxmQitE6uAmtCen/Fim1/e4xhTpKGTzEPdo84Q +eYp2iOtwthgSv4mmUexu9UGOns6IvYD8NTqTyK8ME4hzGTnmh0HJamOtm/xMS7RJnjz l9pO+El0UVVGbmvfVOYHm//ABAoi74YmjMw7zf01fcWLZC6f/zX5tw6MOt4Kjz9MqRJP D5+7JKJx/kDCT6DKTomQYUjOfIrlqQG3+fUWo+crjcr4C75aPic3njCDtXanWOTUncYJ eHPIRUmtuTpEiFJiKnYAEJfcSu3+j8yu08h6myzWHIZ9Klo2ZuK33shWy6+EdyNDH76x wGCQ== X-Gm-Message-State: AOAM5320bKHE8jHRuGSYkIcG+yBqnfsx+1UsOcf0AieJzF1/Him/dx4v axa58FhY0b0SO+AlJaKjvlJdJ8iq7F8= X-Google-Smtp-Source: ABdhPJw6CwyVKOIvIpsqafspaNgcYpfZsH6Gy/UGcc56ber9+C3XCEX2mIS4W3TSl1zFPmeD9NED1g== X-Received: by 2002:a17:906:a669:: with SMTP id en9mr4221775ejb.353.1611346838108; Fri, 22 Jan 2021 12:20:38 -0800 (PST) Received: from localhost.localdomain ([31.45.254.141]) by smtp.gmail.com with ESMTPSA id pj11sm5025034ejb.58.2021.01.22.12.20.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jan 2021 12:20:37 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Fri, 22 Jan 2021 21:20:27 +0100 Message-Id: <20210122202027.20152-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add epx pixel art scaler X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- doc/filters.texi | 12 ++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_epx.c | 287 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 301 insertions(+) create mode 100644 libavfilter/vf_epx.c diff --git a/doc/filters.texi b/doc/filters.texi index d75a1f5a78..7cb0d868d0 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -10940,6 +10940,18 @@ Can be either @var{normal} or @var{diff}. Default is @var{normal}. between neighbour histogram values. @end table +@section epx +Apply the EPX magnification filter which is designed for pixel art. + +It accepts the following option: + +@table @option +@item n +Set the scaling dimension: @code{2} for @code{2xEPX}, @code{3} for +@code{3xEPX}. +Default is @code{3}. +@end table + @section eq Set brightness, contrast, saturation and approximate gamma adjustment. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 44afa79963..fb7bc8f140 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -248,6 +248,7 @@ OBJS-$(CONFIG_DRAWTEXT_FILTER) += vf_drawtext.o OBJS-$(CONFIG_EDGEDETECT_FILTER) += vf_edgedetect.o OBJS-$(CONFIG_ELBG_FILTER) += vf_elbg.o OBJS-$(CONFIG_ENTROPY_FILTER) += vf_entropy.o +OBJS-$(CONFIG_EPX_FILTER) += vf_epx.o OBJS-$(CONFIG_EQ_FILTER) += vf_eq.o OBJS-$(CONFIG_EROSION_FILTER) += vf_neighbor.o OBJS-$(CONFIG_EROSION_OPENCL_FILTER) += vf_neighbor_opencl.o opencl.o \ diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 471844a603..139ba15f7c 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -234,6 +234,7 @@ extern AVFilter ff_vf_drawtext; extern AVFilter ff_vf_edgedetect; extern AVFilter ff_vf_elbg; extern AVFilter ff_vf_entropy; +extern AVFilter ff_vf_epx; extern AVFilter ff_vf_eq; extern AVFilter ff_vf_erosion; extern AVFilter ff_vf_erosion_opencl; diff --git a/libavfilter/vf_epx.c b/libavfilter/vf_epx.c new file mode 100644 index 0000000000..65b8975c23 --- /dev/null +++ b/libavfilter/vf_epx.c @@ -0,0 +1,287 @@ +/* + * 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 + */ + +#include "libavutil/opt.h" +#include "libavutil/avassert.h" +#include "libavutil/pixdesc.h" +#include "internal.h" + +typedef struct EPXContext { + const AVClass *class; + + int n; + + int (*epx_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); +} EPXContext; + +typedef struct ThreadData { + AVFrame *in, *out; +} ThreadData; + +#define OFFSET(x) offsetof(EPXContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM +static const AVOption epx_options[] = { + { "n", "set scale factor", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 3, .flags = FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(epx); + +static int epx2_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ThreadData *td = arg; + const AVFrame *in = td->in; + AVFrame *out = td->out; + const int slice_start = (in->height * jobnr ) / nb_jobs; + const int slice_end = (in->height * (jobnr+1)) / nb_jobs; + + for (int p = 0; p < 1; p++) { + const int width = in->width; + const int height = in->height; + const int src_linesize = in->linesize[p] / 4; + const int dst_linesize = out->linesize[p] / 4; + const uint32_t *src = (const uint32_t *)in->data[p]; + uint32_t *dst = (uint32_t *)out->data[p]; + const uint32_t *src_line[3]; + + src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0); + src_line[1] = src + src_linesize * slice_start; + src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1); + + for (int y = slice_start; y < slice_end; y++) { + uint32_t *dst_line[2]; + + dst_line[0] = dst + dst_linesize*2*y; + dst_line[1] = dst + dst_linesize*(2*y+1); + + for (int x = 0; x < width; x++) { + uint32_t E0, E1, E2, E3; + uint32_t B, D, E, F, H; + + B = src_line[0][x]; + D = src_line[1][FFMAX(x-1, 0)]; + E = src_line[1][x]; + F = src_line[1][FFMIN(x+1, width - 1)]; + H = src_line[2][x]; + + if (B != H && D != F) { + E0 = D == B ? D : E; + E1 = B == F ? F : E; + E2 = D == H ? D : E; + E3 = H == F ? F : E; + } else { + E0 = E; + E1 = E; + E2 = E; + E3 = E; + } + + dst_line[0][x*2] = E0; + dst_line[0][x*2+1] = E1; + dst_line[1][x*2] = E2; + dst_line[1][x*2+1] = E3; + } + + src_line[0] = src_line[1]; + src_line[1] = src_line[2]; + src_line[2] = src_line[1]; + + if (y < height - 1) + src_line[2] += src_linesize; + } + } + + return 0; +} + +static int epx3_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ThreadData *td = arg; + const AVFrame *in = td->in; + AVFrame *out = td->out; + const int slice_start = (in->height * jobnr ) / nb_jobs; + const int slice_end = (in->height * (jobnr+1)) / nb_jobs; + + for (int p = 0; p < 1; p++) { + const int width = in->width; + const int height = in->height; + const int src_linesize = in->linesize[p] / 4; + const int dst_linesize = out->linesize[p] / 4; + const uint32_t *src = (const uint32_t *)in->data[p]; + uint32_t *dst = (uint32_t *)out->data[p]; + const uint32_t *src_line[3]; + + src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0); + src_line[1] = src + src_linesize * slice_start; + src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1); + + for (int y = slice_start; y < slice_end; y++) { + uint32_t *dst_line[3]; + + dst_line[0] = dst + dst_linesize*3*y; + dst_line[1] = dst + dst_linesize*(3*y+1); + dst_line[2] = dst + dst_linesize*(3*y+2); + + for (int x = 0; x < width; x++) { + uint32_t E0, E1, E2, E3, E4, E5, E6, E7, E8; + uint32_t A, B, C, D, E, F, G, H, I; + + A = src_line[0][FFMAX(x-1, 0)]; + B = src_line[0][x]; + C = src_line[0][FFMIN(x+1, width - 1)]; + D = src_line[1][FFMAX(x-1, 0)]; + E = src_line[1][x]; + F = src_line[1][FFMIN(x+1, width - 1)]; + G = src_line[2][FFMAX(x-1, 0)]; + H = src_line[2][x]; + I = src_line[2][FFMIN(x+1, width - 1)]; + + if (B != H && D != F) { + E0 = D == B ? D : E; + E1 = (D == B && E != C) || (B == F && E != A) ? B : E; + E2 = B == F ? F : E; + E3 = (D == B && E != G) || (D == H && E != A) ? D : E; + E4 = E; + E5 = (B == F && E != I) || (H == F && E != C) ? F : E; + E6 = D == H ? D : E; + E7 = (D == H && E != I) || (H == F && E != G) ? H : E; + E8 = H == F ? F : E; + } else { + E0 = E; + E1 = E; + E2 = E; + E3 = E; + E4 = E; + E5 = E; + E6 = E; + E7 = E; + E8 = E; + } + + dst_line[0][x*3] = E0; + dst_line[0][x*3+1] = E1; + dst_line[0][x*3+2] = E2; + dst_line[1][x*3] = E3; + dst_line[1][x*3+1] = E4; + dst_line[1][x*3+2] = E5; + dst_line[2][x*3] = E6; + dst_line[2][x*3+1] = E7; + dst_line[2][x*3+2] = E8; + } + + src_line[0] = src_line[1]; + src_line[1] = src_line[2]; + src_line[2] = src_line[1]; + + if (y < height - 1) + src_line[2] += src_linesize; + } + } + + return 0; +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + EPXContext *s = ctx->priv; + AVFilterLink *inlink = ctx->inputs[0]; + const AVPixFmtDescriptor *desc; + + desc = av_pix_fmt_desc_get(outlink->format); + if (!desc) + return AVERROR_BUG; + + outlink->w = inlink->w * s->n; + outlink->h = inlink->h * s->n; + + switch (s->n) { + case 2: + s->epx_slice = epx2_slice; + break; + case 3: + s->epx_slice = epx3_slice; + break; + } + + return 0; +} + +static int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, + AV_PIX_FMT_NONE, + }; + + AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); + if (!fmts_list) + return AVERROR(ENOMEM); + return ff_set_common_formats(ctx, fmts_list); +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + EPXContext *s = ctx->priv; + ThreadData td; + + AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + + av_frame_copy_props(out, in); + + td.in = in, td.out = out; + ctx->internal->execute(ctx, s->epx_slice, &td, NULL, FFMIN(inlink->h, ff_filter_get_nb_threads(ctx))); + + av_frame_free(&in); + return ff_filter_frame(outlink, out); +} + +static const AVFilterPad inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + }, + { NULL } +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + }, + { NULL } +}; + +AVFilter ff_vf_epx = { + .name = "epx", + .description = NULL_IF_CONFIG_SMALL("Scale the input using EPX algorithm."), + .inputs = inputs, + .outputs = outputs, + .query_formats = query_formats, + .priv_size = sizeof(EPXContext), + .priv_class = &epx_class, + .flags = AVFILTER_FLAG_SLICE_THREADS, +};