From patchwork Sat Nov 17 14:07:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 11050 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 9E8C244DF3F for ; Sat, 17 Nov 2018 16:07:57 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1EDF76891ED; Sat, 17 Nov 2018 16:07:58 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D7DD1688258 for ; Sat, 17 Nov 2018 16:07:51 +0200 (EET) Received: by mail-wr1-f66.google.com with SMTP id e3-v6so27630558wrs.5 for ; Sat, 17 Nov 2018 06:07:52 -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=Y4KITtm9GYsRIiFzZMeVMqaly+EUm38dnD31V0gOGo8=; b=jN8PKhmjB/0kxT2my6wNHtA99NfRt2QN9GZjUH9yfvkOpKF2lWZrBr4bIRpNKqdj4A EOYu7lOtWO5FX3ZhL2df64VpMC6IKYC1ZT4MnJoIuWE0HRRwVWpqTqvvRIY1mStLEtUV 6R0+Qmop4EmE9zWNN4dj3UrV2FST665xMG4ud7ynA/B8XCgzVqt/yXQl9BkQArdWF/s5 EXrUiXiXvIxD0S0b8ybLbr4FJrG/4l3qip+HDsIpTlfHTJ4D5fpeJcWewHXuentPpdyC jwKPQusq5vD7ihRZ2GgChvLP5BcsFn+i0ZQMHmTkitsBcrj1rSbF+9eujVJQmuhw1SvQ xG7g== 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=Y4KITtm9GYsRIiFzZMeVMqaly+EUm38dnD31V0gOGo8=; b=eLyvNNYpavgAxbfj7cqW1R4ds036Q9dDF5TQ4U++g5MQ+R+SypTka6e/Bro2ZeUdkW BN3xFnTdjbGqZ13u6RxwjDYYHaqYamlE24YiOwAgi2ALb+4lhHFHetOgvr1MBLyZJ4Wd yyeCrPJLP1Ss8d/tk4+OjRB0ktS7+dObdH3KNi2g7hRa3IszMEa2kYWeQ3hajDfY4MAn gH64rBDgC/uoN2GofJa0VIfivg/mHVG5McjKxGqbAxc28N7RYuXh+/5xMT1OIduD8yTB Ikohlb24qRUevoWleEOyekO33d/tOTQb8sY6q+KwOGCRGgWc70QAIaCTBgOb/qYqrInA zEdw== X-Gm-Message-State: AA+aEWaAH5ihRt1bifZtjwlmcllgFDvawqOC25wW625yTou1l7+yiVe/ /lZHsXhL66/TEjy+Bk3lfFOyR1hk X-Google-Smtp-Source: AFSGD/XO/8QnckaJmZFm/ljzU8KJQAyFSU8zLrkPHiPxWmwOEAqZZf6wFXIGocmOeKLtbE8OiSpEeA== X-Received: by 2002:adf:e6ce:: with SMTP id y14mr2493261wrm.239.1542463671173; Sat, 17 Nov 2018 06:07:51 -0800 (PST) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id h203-v6sm21887836wma.38.2018.11.17.06.07.49 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Nov 2018 06:07:50 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Nov 2018 15:07:43 +0100 Message-Id: <20181117140743.19341-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add chromashift filter 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 | 17 +++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_chromashift.c | 251 +++++++++++++++++++++++++++++++++++ 4 files changed, 270 insertions(+) create mode 100644 libavfilter/vf_chromashift.c diff --git a/doc/filters.texi b/doc/filters.texi index a697d3db14..77f57bfcd6 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -6253,6 +6253,23 @@ ffmpeg -f lavfi -i color=c=black:s=1280x720 -i video.mp4 -shortest -filter_compl @end example @end itemize +@section chromashift +Shift chroma pixels horizontally and/or vertically. + +The filter accepts the following options: +@table @option +@item cbh +Set amount to shift chroma-blue horizontally. +@item cbv +Set amount to shift chroma-blue vertically. +@item crh +Set amount to shift chroma-red horizontally. +@item crv +Set amount to shift chroma-red vertically. +@item edge +Set edge mode, can be @var{smear}, default, or @var{warp}. +@end table + @section ciescope Display CIE color diagram with pixels overlaid onto it. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 6058fb97bc..8f3fde8c27 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -169,6 +169,7 @@ OBJS-$(CONFIG_BOXBLUR_OPENCL_FILTER) += vf_avgblur_opencl.o opencl.o \ OBJS-$(CONFIG_BWDIF_FILTER) += vf_bwdif.o OBJS-$(CONFIG_CHROMAHOLD_FILTER) += vf_chromakey.o OBJS-$(CONFIG_CHROMAKEY_FILTER) += vf_chromakey.o +OBJS-$(CONFIG_CHROMASHIFT_FILTER) += vf_chromashift.o OBJS-$(CONFIG_CIESCOPE_FILTER) += vf_ciescope.o OBJS-$(CONFIG_CODECVIEW_FILTER) += vf_codecview.o OBJS-$(CONFIG_COLORBALANCE_FILTER) += vf_colorbalance.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 8dfa572fde..6f392b5632 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -158,6 +158,7 @@ extern AVFilter ff_vf_boxblur_opencl; extern AVFilter ff_vf_bwdif; extern AVFilter ff_vf_chromahold; extern AVFilter ff_vf_chromakey; +extern AVFilter ff_vf_chromashift; extern AVFilter ff_vf_ciescope; extern AVFilter ff_vf_codecview; extern AVFilter ff_vf_colorbalance; diff --git a/libavfilter/vf_chromashift.c b/libavfilter/vf_chromashift.c new file mode 100644 index 0000000000..458afa71e4 --- /dev/null +++ b/libavfilter/vf_chromashift.c @@ -0,0 +1,251 @@ +/* + * Copyright (c) 2018 Paul B Mahol + * + * 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/avstring.h" +#include "libavutil/eval.h" +#include "libavutil/imgutils.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" + +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "framesync.h" +#include "video.h" + +typedef struct ChromaShiftContext { + const AVClass *class; + int cbh, cbv; + int crh, crv; + int edge; + + int depth; + int height[4]; + int width[4]; + int linesize[4]; + + AVFrame *in; + + int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); +} ChromaShiftContext; + +static int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P, + AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P, + AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, + AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV440P10, + AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10, + AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12, + AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, + AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, + 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); +} + +#define DEFINE_SMEAR(depth, type, div) \ +static int smear_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \ +{ \ + ChromaShiftContext *s = ctx->priv; \ + AVFrame *in = s->in; \ + AVFrame *out = arg; \ + const int sulinesize = in->linesize[1] / div; \ + const int svlinesize = in->linesize[2] / div; \ + const int ulinesize = out->linesize[1] / div; \ + const int vlinesize = out->linesize[2] / div; \ + const int cbh = s->cbh; \ + const int cbv = s->cbv; \ + const int crh = s->crh; \ + const int crv = s->crv; \ + const int h = s->height[1]; \ + const int w = s->width[1]; \ + const int slice_start = (h * jobnr) / nb_jobs; \ + const int slice_end = (h * (jobnr+1)) / nb_jobs; \ + const type *su = (const type *)in->data[1]; \ + const type *sv = (const type *)in->data[2]; \ + type *du = (type *)out->data[1] + slice_start * ulinesize; \ + type *dv = (type *)out->data[2] + slice_start * vlinesize; \ + \ + for (int y = slice_start; y < slice_end; y++) { \ + for (int x = 0; x < w; x++) { \ + du[x] = su[av_clip(x - cbh, 0, w - 1) + av_clip(y - cbv, 0, h-1) * sulinesize];\ + dv[x] = sv[av_clip(x - crh, 0, w - 1) + av_clip(y - crv, 0, h-1) * svlinesize];\ + } \ + \ + du += ulinesize; \ + dv += vlinesize; \ + } \ + \ + return 0; \ +} + +DEFINE_SMEAR(8, uint8_t, 1) +DEFINE_SMEAR(16, uint16_t, 2) + +#define DEFINE_WRAP(depth, type, div) \ +static int wrap_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \ +{ \ + ChromaShiftContext *s = ctx->priv; \ + AVFrame *in = s->in; \ + AVFrame *out = arg; \ + const int sulinesize = in->linesize[1] / div; \ + const int svlinesize = in->linesize[2] / div; \ + const int ulinesize = out->linesize[1] / div; \ + const int vlinesize = out->linesize[2] / div; \ + const int cbh = s->cbh; \ + const int cbv = s->cbv; \ + const int crh = s->crh; \ + const int crv = s->crv; \ + const int h = s->height[1]; \ + const int w = s->width[1]; \ + const int slice_start = (h * jobnr) / nb_jobs; \ + const int slice_end = (h * (jobnr+1)) / nb_jobs; \ + const type *su = (const type *)in->data[1]; \ + const type *sv = (const type *)in->data[2]; \ + type *du = (type *)out->data[1] + slice_start * ulinesize; \ + type *dv = (type *)out->data[2] + slice_start * vlinesize; \ + \ + for (int y = slice_start; y < slice_end; y++) { \ + for (int x = 0; x < w; x++) { \ + int ux = (x - cbh) % w; \ + int vx = (x - crh) % w; \ + int uy = (y - cbv) % h; \ + int vy = (y - crv) % h; \ + \ + if (ux < 0) \ + ux += w; \ + if (uy < 0) \ + uy += h; \ + if (vx < 0) \ + vx += w; \ + if (vy < 0) \ + vy += h; \ + du[x] = su[ux + uy * sulinesize]; \ + dv[x] = sv[vx + vy * svlinesize]; \ + } \ + \ + du += ulinesize; \ + dv += vlinesize; \ + } \ + \ + return 0; \ +} + +DEFINE_WRAP(8, uint8_t, 1) +DEFINE_WRAP(16, uint16_t, 2) + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + ChromaShiftContext *s = ctx->priv; + AVFrame *out; + + 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); + + s->in = in; + av_image_copy_plane(out->data[0] + out->linesize[0], + out->linesize[0], + in->data[0], in->linesize[0], + s->linesize[0], s->height[0]); + ctx->internal->execute(ctx, s->filter_slice, out, NULL, + FFMIN3(s->height[1], + s->height[2], + ff_filter_get_nb_threads(ctx))); + s->in = NULL; + av_frame_free(&in); + return ff_filter_frame(outlink, out); +} + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + ChromaShiftContext *s = ctx->priv; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + + s->depth = desc->comp[0].depth; + if (s->edge) + s->filter_slice = s->depth > 8 ? wrap_slice16 : wrap_slice8; + else + s->filter_slice = s->depth > 8 ? smear_slice16 : smear_slice8; + s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); + s->height[0] = s->height[3] = inlink->h; + s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); + s->width[0] = s->width[3] = inlink->w; + + return av_image_fill_linesizes(s->linesize, inlink->format, inlink->w); +} + +#define OFFSET(x) offsetof(ChromaShiftContext, x) +#define VF AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption chromashift_options[] = { + { "cbh", "shift chroma-blue horizontally", OFFSET(cbh), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VF }, + { "cbv", "shift chroma-blue vertically", OFFSET(cbv), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VF }, + { "crh", "shift chroma-red horizontally", OFFSET(crh), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VF }, + { "crv", "shift chroma-red vertically", OFFSET(crv), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VF }, + { "edge", "set edge operation", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags = VF, "edge" }, + { "smear", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, .flags = VF, "edge" }, + { "wrap", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, .flags = VF, "edge" }, + { NULL }, +}; + +static const AVFilterPad inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(chromashift); + +AVFilter ff_vf_chromashift = { + .name = "chromashift", + .description = NULL_IF_CONFIG_SMALL("Shift chroma."), + .priv_size = sizeof(ChromaShiftContext), + .priv_class = &chromashift_class, + .query_formats = query_formats, + .outputs = outputs, + .inputs = inputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, +};