From patchwork Mon Jul 24 17:02:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 4442 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp4475491vsb; Mon, 24 Jul 2017 10:08:37 -0700 (PDT) X-Received: by 10.28.222.70 with SMTP id v67mr5045906wmg.156.1500916117076; Mon, 24 Jul 2017 10:08:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1500916117; cv=none; d=google.com; s=arc-20160816; b=bp9CxU9UWh+i+DhHhU2jS0RvYKAUSmFJCaqD5HUzPsf1fKgkuDdyDqcHw0bWFvck2B I/XVvmklvwIQrfWbq642KpQ9Ba7JOgyXqCtx77kj/LiXOuFlJxOQGalNwfZcGkw0NZQB VcqzdHBeY89uL43IlZ1j/EKLAJSF1HEN2mDHkIPCC6zYJNYsiXqHQQNb/ZuaqCvqY5Dr yEWqdD6hsZzTq2IAGnMyRTL7/yROCM22vckT4Aa90WzhefZfTXbXC0pPIN9TSS+yXRn/ +DeP1KPrxqVbAaYmlmZ3VdmSftcYR1tReakUTxmhf9HH8cPYGheYF1Ld45oryNNbVNy/ AmGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=QXVFMW69fsG7z4zoMJDAAZJhsz/YphqzS0o4Yu9qxDE=; b=oAviFvDEeW/VaDaehEPNXc6ykyRJ4HV0EGjT7qAczgFrluBEn36bssXyAAxTlfeIGP j7OOasHItAqjUEq7XaflxMZOW/rbSlwEdUdPUOenc+kmFMcogd0cCSVW4xoMAawG/IDk p5UVQL+e5GdEim8pAJ9qOyNcm33Q3NnP3g24WjYbYesuxWwGAURNiyDA04Dg/VfIqfNg fo+x8HwKIm8XYBFnSTcbtW/3D45knUVF+voK7H6fVbFhBYwHocdTNktJWaOY33gZKHKy 6Z6B1dRhZBOQ5UU5CbWv8j6QOjkihg1Fxor8FkrRmHc0+9EVr5d3OYRZyIvD6EjmiYNR XOBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.b=Xji2UKuG; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 45si2762670wrk.424.2017.07.24.10.08.34; Mon, 24 Jul 2017 10:08:37 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.b=Xji2UKuG; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 323D7689B17; Mon, 24 Jul 2017 20:08:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f193.google.com (mail-wr0-f193.google.com [209.85.128.193]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9375A689A4C for ; Mon, 24 Jul 2017 20:08:16 +0300 (EEST) Received: by mail-wr0-f193.google.com with SMTP id y67so17134047wrb.3 for ; Mon, 24 Jul 2017 10:08:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=NY0jbU0THw8XH5xWKWAXCDyo6oPOZF9XKS/ckkqOa34=; b=Xji2UKuG0FfCX+k6NtMfqneAmX/gE9KjRadrgSDFWSoQCJ6dZSnNc+eOcMzB9UzfuW d52aTgoozhqjOdeF9ZdTRaacszm1MIsiQ5YHqvTyMwvmT8maujvdqo/T9UxWt3YjMeka fwxSc7jpz6P5NmS7k4gN8qXI2wWD4FePiWK/FmjhgJ5dSMPlQz6Z10H1u2VczD4nauNF Pdz8zjBRqVMoOoYCZmKFTuSWxws1Kb4m8nuRSrIQoKnZAH4VRWpX50Iat5NOfHUoAoXY M1k9KPAPcxetUxIAVlQno7lYmEXvDOf/zezoj5dzc2UPXpw9cPyV1K7mrUYzFofX64WW rkvQ== 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=NY0jbU0THw8XH5xWKWAXCDyo6oPOZF9XKS/ckkqOa34=; b=eZ1+x+FL+bnvMDwDr6BLx45IJO3/Lt2L+O9vDLLwvYHbaVBLCPPhtepiv2d+NsIBQI aqmYqPpG0yK5WFQMgBzBgy9C/MgXDQCVvNux2rQtCj0wn9QHRcQ3RMnwK/yg5GUGcSNy CwCw2dh7KfQRFtOI0xa3vMKMO9FoXtmMNUNe9WBIfnDn6SuKnuog409U4KIa3giqW7ul SDNv/BDHFcLpi89yHVCWPQYsB30CroAFfXGMUhR+WH37DsPGscn1V7DFSHKAAPGtiBKa qULasl/sceFBOZomyowCS7dBj75rAuzfMKbHH/7uieiRS3iUzada+cUZv98kaVhNWXVu wR6A== X-Gm-Message-State: AIVw111hwtZeSAcVtTo52KHFrR9TgLR4oxsCGFktP8/h64izvm/b1eCz 7qCkah41Xx2QxmVd X-Received: by 10.223.168.70 with SMTP id l64mr17561301wrc.94.1500915731505; Mon, 24 Jul 2017 10:02:11 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id j137sm10423462wmf.43.2017.07.24.10.02.09 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Jul 2017 10:02:10 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Mon, 24 Jul 2017 19:02:00 +0200 Message-Id: <20170724170200.10566-1-onemda@gmail.com> X-Mailer: git-send-email 2.9.3 Subject: [FFmpeg-devel] [PATCH] avfilter: add pseudocolor 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 --- libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_pseudocolor.c | 199 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 libavfilter/vf_pseudocolor.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index ee16361..6b24745 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -258,6 +258,7 @@ OBJS-$(CONFIG_PP_FILTER) += vf_pp.o OBJS-$(CONFIG_PP7_FILTER) += vf_pp7.o OBJS-$(CONFIG_PREMULTIPLY_FILTER) += vf_premultiply.o framesync.o OBJS-$(CONFIG_PREWITT_FILTER) += vf_convolution.o +OBJS-$(CONFIG_PSEUDOCOLOR_FILTER) += vf_pseudocolor.o OBJS-$(CONFIG_PSNR_FILTER) += vf_psnr.o dualinput.o framesync.o OBJS-$(CONFIG_PULLUP_FILTER) += vf_pullup.o OBJS-$(CONFIG_QP_FILTER) += vf_qp.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index b1c2d11..da570b2 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -269,6 +269,7 @@ static void register_all(void) REGISTER_FILTER(PP7, pp7, vf); REGISTER_FILTER(PREMULTIPLY, premultiply, vf); REGISTER_FILTER(PREWITT, prewitt, vf); + REGISTER_FILTER(PSEUDOCOLOR, pseudocolor, vf); REGISTER_FILTER(PSNR, psnr, vf); REGISTER_FILTER(PULLUP, pullup, vf); REGISTER_FILTER(QP, qp, vf); diff --git a/libavfilter/vf_pseudocolor.c b/libavfilter/vf_pseudocolor.c new file mode 100644 index 0000000..413b6e2 --- /dev/null +++ b/libavfilter/vf_pseudocolor.c @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2017 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/attributes.h" +#include "libavutil/common.h" +#include "libavutil/imgutils.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct PseudoColorContext { + const AVClass *class; + int is_16bit; + int nb_planes; + int color; + int linesize[4]; + int width[4], height[4]; + uint16_t lut[3][256*256]; +} PseudoColorContext; + +#define OFFSET(x) offsetof(PseudoColorContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption pseudocolor_options[] = { + { "color", "set a color", OFFSET(color), AV_OPT_TYPE_INT, {.i64=0}, 0, 10, .flags=FLAGS }, + { NULL } +}; + +static const enum AVPixelFormat yuv_pix_fmts[] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_NONE }; + +static int query_formats(AVFilterContext *ctx) +{ + const enum AVPixelFormat *pix_fmts = yuv_pix_fmts; + 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 config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + PseudoColorContext *s = ctx->priv; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + int ret, hsub, vsub; + + s->is_16bit = desc->comp[0].depth > 8; + s->nb_planes = av_pix_fmt_count_planes(inlink->format); + + if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0) + return ret; + + hsub = desc->log2_chroma_w; + vsub = desc->log2_chroma_h; + s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub); + s->height[0] = s->height[3] = inlink->h; + s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub); + s->width[0] = s->width[3] = inlink->w; + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + PseudoColorContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + AVFrame *out; + int x, y, plane; + + if (av_frame_is_writable(in)) { + out = in; + } else { + 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); + } + + for (plane = 0; plane < s->nb_planes; plane++) { + const uint8_t *src = in->data[0]; + uint8_t *dst = out->data[plane]; + + for (y = 0; y < s->height[plane]; y++) { + for (x = 0; x < s->width[plane]; x++) { + dst[x] = s->lut[plane][src[x]]; + } + src += in->linesize[plane]; + dst += out->linesize[plane]; + } + } + + if (out != in) + av_frame_free(&in); + + return ff_filter_frame(outlink, out); +} + +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 } +}; + +static av_cold int init(AVFilterContext *ctx) +{ + PseudoColorContext *s = ctx->priv; + int i; + + for (i = 0; i < 256; i++) { + float div = i / 256. * 6; + float idiv; + int asc = modff(div, &idiv) * 255; + int desc = 255 - asc;; + + switch ((int)idiv) { + case 0: + s->lut[0][i] = i; + s->lut[1][i] = desc; + s->lut[2][i] = 0; + break; + case 1: + s->lut[0][i] = i; + s->lut[1][i] = 0; + s->lut[2][i] = asc; + break; + case 2: + s->lut[0][i] = i; + s->lut[1][i] = asc; + s->lut[2][i] = desc; + break; + case 3: + s->lut[0][i] = i; + s->lut[1][i] = desc; + s->lut[2][i] = asc; + break; + case 4: + s->lut[0][i] = i; + s->lut[1][i] = 0; + s->lut[2][i] = desc; + break; + case 5: + s->lut[0][i] = i; + s->lut[1][i] = asc; + s->lut[2][i] = 0; + break; + }; + } + + return 0; +} + +AVFILTER_DEFINE_CLASS(pseudocolor); + +AVFilter ff_vf_pseudocolor = { + .name = "pseudocolor", + .description = NULL_IF_CONFIG_SMALL("Generate a pseudocolored video frames"), + .priv_size = sizeof(PseudoColorContext), + .priv_class = &pseudocolor_class, + .init = init, + .query_formats = query_formats, + .inputs = inputs, + .outputs = outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, +};