From patchwork Sun Apr 9 16:03:46 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: 3348 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.44.195 with SMTP id s186csp1033023vss; Sun, 9 Apr 2017 09:04:22 -0700 (PDT) X-Received: by 10.28.0.78 with SMTP id 75mr6240705wma.138.1491753862275; Sun, 09 Apr 2017 09:04:22 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id j29si9136725wre.45.2017.04.09.09.04.21; Sun, 09 Apr 2017 09:04:22 -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; 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 824E26883E7; Sun, 9 Apr 2017 19:04:13 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f194.google.com (mail-wr0-f194.google.com [209.85.128.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4960F68828F for ; Sun, 9 Apr 2017 19:04:07 +0300 (EEST) Received: by mail-wr0-f194.google.com with SMTP id l28so1331318wre.0 for ; Sun, 09 Apr 2017 09:04:12 -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=tjBjdxeUWCNK6oAUszp0KH8PW3+WjRO2Mit4S0nKO+w=; b=qkhG5/2Lg5xok3YK2U0nMyNpUJoB+aEwEYmEq8N76bXdDsFaqd4VAC0Mq5H54/Q7yu f+R1wt9Q3oQhS8i0YPPsrbMUtfOOU5JfRNtIKI/WQI/4H90a3vdyssCvvIaqBOJvX1NN YoGWN6NDBGXl27KfmBDS0DyI4IwSUaO7dVw+IzY8GfFyTfa2vuSUQO4PS5yamTz/HFLV rPqay1oWiUvoR/EGswT3TjtdyKzVc9eLmWGqPZrGf9VbBK7TrpM+WV8Q7rOLGHYQImoD +1anB3iK74sew9FECS1ZqTewAYI2hxWGARMVLuWKoL9Wv/HdzgniO2SsUuFlL/ydeXu1 wVuQ== 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=tjBjdxeUWCNK6oAUszp0KH8PW3+WjRO2Mit4S0nKO+w=; b=oD0aF/W/rbBe8pU1KUYCrTtosxshbkI7mYQ6+gW/xFCxzQPM9XiIrlbFKRExvqSbJF epfK8yQMbA9kJ8+guaZTogj0rxXTrWrQ69CG9mLvw7ceW42HUfsnIyL6g0bBh26elvIK s3aisvDkjqWQCmEsCI3G/hOygeGDa2AZN0Ntq/Phkfzve9jhbysk588keDYiTDz6jUYa 11oWm7pXtTKgSHdGaI7KqBDEK3dDZoEzdyl3x2ck61UVBVZl0zJOms5npFv1V1oi6eD1 Fb8dvVTtfxavAyauV3djEOXIiv6ohwWyhaJA11Tnu0wyr7TJN9D97kSbbWL9uhsjYR7l +8nw== X-Gm-Message-State: AN3rC/4iDgydnk+DrAc0xI8UJd9hJas/OBYWqaTeXH8nCSqJ/0sOHNB7NuXM3xtdJ2sRFw== X-Received: by 10.223.154.129 with SMTP id a1mr13800523wrc.5.1491753851187; Sun, 09 Apr 2017 09:04:11 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id d4sm14006214wrb.24.2017.04.09.09.04.09 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 09 Apr 2017 09:04:10 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 9 Apr 2017 18:03:46 +0200 Message-Id: <20170409160346.1765-1-onemda@gmail.com> X-Mailer: git-send-email 2.9.3 Subject: [FFmpeg-devel] [PATCH] avfilter: add rangescope 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_rangescope.c | 287 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 289 insertions(+) create mode 100644 libavfilter/vf_rangescope.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index d6daa7a..d9f2385 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -250,6 +250,7 @@ OBJS-$(CONFIG_PSNR_FILTER) += vf_psnr.o dualinput.o framesync. OBJS-$(CONFIG_PULLUP_FILTER) += vf_pullup.o OBJS-$(CONFIG_QP_FILTER) += vf_qp.o OBJS-$(CONFIG_RANDOM_FILTER) += vf_random.o +OBJS-$(CONFIG_RANGESCOPE_FILTER) += vf_rangescope.o OBJS-$(CONFIG_READEIA608_FILTER) += vf_readeia608.o OBJS-$(CONFIG_READVITC_FILTER) += vf_readvitc.o OBJS-$(CONFIG_REALTIME_FILTER) += f_realtime.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index ec6ec04..feb6ce1 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -260,6 +260,7 @@ static void register_all(void) REGISTER_FILTER(PULLUP, pullup, vf); REGISTER_FILTER(QP, qp, vf); REGISTER_FILTER(RANDOM, random, vf); + REGISTER_FILTER(RANGESCOPE, rangescope, vf); REGISTER_FILTER(READEIA608, readeia608, vf); REGISTER_FILTER(READVITC, readvitc, vf); REGISTER_FILTER(REALTIME, realtime, vf); diff --git a/libavfilter/vf_rangescope.c b/libavfilter/vf_rangescope.c new file mode 100644 index 0000000..bd7365b --- /dev/null +++ b/libavfilter/vf_rangescope.c @@ -0,0 +1,287 @@ +/* + * Copyright (c) 2016 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/avassert.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/pixdesc.h" +#include "libavutil/xga_font_data.h" +#include "avfilter.h" +#include "drawutils.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct Range { + int min; + int max; + int imin; + int imax; +} Range; + +typedef struct RangescopeContext { + const AVClass *class; + int ow, oh; + + int planewidth[4]; + int planeheight[4]; + int nb_planes; + int depth; + FFDrawContext draw; + FFDrawColor black; + uint8_t map[4]; + + Range range[4]; + float *history[4]; + int max; + void (*get_ranges)(AVFilterContext *ctx, AVFrame *in); +} RangescopeContext; + +#define OFFSET(x) offsetof(RangescopeContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption rangescope_options[] = { + { "width", "set output width", OFFSET(ow), AV_OPT_TYPE_INT, {.i64=640}, 9, 4000, FLAGS }, + { "w", "set output width", OFFSET(ow), AV_OPT_TYPE_INT, {.i64=640}, 9, 4000, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(rangescope); + +static const enum AVPixelFormat in_pix_fmts[] = { + AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, + AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, + AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, + AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, + AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, + AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P, + AV_PIX_FMT_GRAY8, + AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9, + AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9, + AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10, + AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10, + AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV440P12, + AV_PIX_FMT_NONE +}; + +static const enum AVPixelFormat out_pix_fmts[] = { + AV_PIX_FMT_GBRP, + AV_PIX_FMT_NONE +}; + +static int query_formats(AVFilterContext *ctx) +{ + int ret; + + if ((ret = ff_formats_ref(ff_make_format_list(in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0) + return ret; + return ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats); +} + +static void get_ranges8(AVFilterContext *ctx, AVFrame *in) +{ + RangescopeContext *s = ctx->priv; + int p, x, y; + + for (p = 0; p < s->nb_planes; p++) { + uint8_t *value = in->data[p]; + Range *range = &s->range[p]; + + memset(s->history[p], 0, s->max * sizeof(*s->history[p])); + range->max = range->min = value[0]; + for (y = 0; y < s->planeheight[p]; y++) { + value = in->data[p] + y * in->linesize[p]; + for (x = 0; x < s->planewidth[p]; x++) { + range->max = FFMAX(value[x], range->max); + range->min = FFMIN(value[x], range->min); + s->history[p][value[x]]++; + } + } + } +} + +static void get_ranges16(AVFilterContext *ctx, AVFrame *in) +{ + RangescopeContext *s = ctx->priv; + int p, x, y; + + for (p = 0; p < s->nb_planes; p++) { + uint16_t *value = (uint16_t *)in->data[p]; + Range *range = &s->range[p]; + + memset(s->history[p], 0, s->max * sizeof(*s->history[p])); + range->max = range->min = value[0]; + for (y = 0; y < s->planeheight[p]; y++) { + value = (uint16_t *)(in->data[p] + y * in->linesize[p]); + for (x = 0; x < s->planewidth[p]; x++) { + range->max = FFMAX(value[x], range->max); + range->min = FFMIN(value[x], range->min); + s->history[p][value[x]]++; + } + } + } +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + RangescopeContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + int width = outlink->w / (s->nb_planes * 2); + AVFrame *out; + int c; + + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + out->pts = in->pts; + + ff_fill_rectangle(&s->draw, &s->black, out->data, out->linesize, + 0, 0, outlink->w, outlink->h); + + if (av_frame_get_color_range(in) == AVCOL_RANGE_MPEG) { + s->range[0].imin = 16 * (1 << (s->depth - 8)); + s->range[0].imax = 235 * (1 << (s->depth - 8)); + s->range[1].imin = 16 * (1 << (s->depth - 8)); + s->range[1].imax = 240 * (1 << (s->depth - 8)); + s->range[2].imin = 16 * (1 << (s->depth - 8)); + s->range[2].imax = 240 * (1 << (s->depth - 8)); + s->range[3].imin = 0; + s->range[3].imax = 255 * (1 << (s->depth - 8)); + } else { + for (c = 0; c < 4; c++) { + s->range[c].imin = 0; + s->range[c].imax = 255 * (1 << (s->depth - 8)); + } + } + + s->get_ranges(ctx, in); + av_frame_free(&in); + + for (c = 0; c < s->nb_planes; c++) { + int y, x, i; + float max = 1; + + for (i = 0; i < s->max; i++) { + max = FFMAX(s->history[c][i], max); + } + + for (y = 0; y < outlink->h; y++) { + for (x = width / 2 + c * outlink->w / s->nb_planes; x < width / 2 + c * outlink->w / s->nb_planes + width; x++) { + out->data[c][y * out->linesize[c] + x] = s->history[c][y] / max * s->max; + } + } + } + + return ff_filter_frame(outlink, out); +} + +static int config_input(AVFilterLink *inlink) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + RangescopeContext *s = inlink->dst->priv; + int i; + + s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); + s->planeheight[0] = s->planeheight[3] = inlink->h; + s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); + s->planewidth[0] = s->planewidth[3] = inlink->w; + + s->nb_planes = av_pix_fmt_count_planes(inlink->format); + ff_draw_init(&s->draw, inlink->format, 0); + if (desc->flags & AV_PIX_FMT_FLAG_RGB) { + ff_fill_rgba_map(s->map, inlink->format); + } else { + s->map[1] = 1; s->map[2] = 2; s->map[3] = 3; + } + + s->depth = desc->comp[0].depth; + s->max = (1 << s->depth) - 1; + if (s->depth == 8) + s->get_ranges = get_ranges8; + else + s->get_ranges = get_ranges16; + + for (i = 0; i < s->nb_planes; i++) { + s->history[i] = av_malloc_array(s->max, sizeof(*s->history[i])); + if (!s->history[i]) + return AVERROR(ENOMEM); + } + + return 0; +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + RangescopeContext *s = ctx->priv; + + outlink->h = s->max; + outlink->w = s->ow; + outlink->sample_aspect_ratio = (AVRational){1,1}; + + ff_draw_init(&s->draw, outlink->format, 0); + ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 0} ); + + return 0; +} + +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, + .config_props = config_output, + }, + { NULL } +}; + +static av_cold void uninit(AVFilterContext *ctx) +{ + RangescopeContext *s = ctx->priv; + int i; + + for (i = 0; i < s->nb_planes; i++) { + av_freep(&s->history[i]); + } +} + +AVFilter ff_vf_rangescope = { + .name = "rangescope", + .description = NULL_IF_CONFIG_SMALL("Video pixel component range analysis."), + .priv_size = sizeof(RangescopeContext), + .priv_class = &rangescope_class, + .uninit = uninit, + .query_formats = query_formats, + .inputs = inputs, + .outputs = outputs, +};