From patchwork Tue Feb 14 19:44:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Daniel Oberhoff X-Patchwork-Id: 2546 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp1612982vsb; Tue, 14 Feb 2017 11:45:08 -0800 (PST) X-Received: by 10.28.91.16 with SMTP id p16mr4755411wmb.111.1487101508133; Tue, 14 Feb 2017 11:45:08 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 33si1982411wro.132.2017.02.14.11.45.07; Tue, 14 Feb 2017 11:45:08 -0800 (PST) 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=@googlemail.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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 98D4D680CCB; Tue, 14 Feb 2017 21:44:58 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com [74.125.82.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 299D1680A86 for ; Tue, 14 Feb 2017 21:44:52 +0200 (EET) Received: by mail-wm0-f66.google.com with SMTP id r18so5136843wmd.3 for ; Tue, 14 Feb 2017 11:44:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:content-transfer-encoding:mime-version:subject:message-id:date :cc:to; bh=OWBrVejLtTFhQ98SV+Hgo4ZjfdEo9MMeYfXsi09HOec=; b=Tb/RnXMoRRMLzvWdIQDHu5F71ftw7qgcqzyt1lFO2NKJP9aT4j1mNTa73VPjxvcW5M V1rlHKwvmuTHVpPEg6SGie/87cHvsGTzG0q3/8CznKoIXd01XOt9Img0q6VLfc8FByTm ZFCOKZBKCErQ2UdzQ4+crKBmaC7nAMsDwzd1cTW3r3CdBqXcQMvdPQ5AELeHmKDpNmlX 90yXbiAcWxbqA2xZ0HXTMLijJwrJAsLIILBceGuJWsqD7RuIWQfa7v1fGl3SvslCZq34 x3/bc3d1Duw/k9wPmVp5AivQcMUiXuC/MiefcPvcFZK7WokH6g6NDWS3or6fcuNEMEzz kyaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:content-transfer-encoding:mime-version :subject:message-id:date:cc:to; bh=OWBrVejLtTFhQ98SV+Hgo4ZjfdEo9MMeYfXsi09HOec=; b=eDkvAZ8XI5cbKQNirDp2CNehNPEYzteBp8AaT/GQMflQqP7KpukhA/U5dLDC8W5wBi OKWMiFaa/B/BseQU0KG1HTgPp6ufG0AX3UqF2qe0mPx3veUThqXSQI3g6HYdkjshtm8u FUqrVoRZGLxx9gTpjqNB4gZbieUKmuPtvnAzGNdgBKTxzn/OuLix0bQP0KbA6lED7daG jRd7caT4diNViayD4JFqStWmuaAi5nkf9VMj9mZK0waI+y9IimC9igG6+EHgFeUu+wlD Fx84IChgmciugfZHJtgQhgqSynonNq9BcaJpB7LP49+u7j6oLw7RnobrR7BqwMkFgjga +OFw== X-Gm-Message-State: AMke39n4+7L0ZF5v6UPwLgY+cs7SavBKnBr8nc2UhIPcLrMh76/6BembsurJn+tWcK7QsQ== X-Received: by 10.28.126.11 with SMTP id z11mr4938319wmc.13.1487101496801; Tue, 14 Feb 2017 11:44:56 -0800 (PST) Received: from macbook-pro.fritz.box (x4db517f1.dyn.telefonica.de. [77.181.23.241]) by smtp.googlemail.com with ESMTPSA id 65sm1968440wri.53.2017.02.14.11.44.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 Feb 2017 11:44:56 -0800 (PST) From: Daniel Oberhoff Mime-Version: 1.0 (Mac OS X Mail 10.2 \(3259\)) Message-Id: <39DDB4BE-507B-4A83-B052-7073EBA158CF@googlemail.com> Date: Tue, 14 Feb 2017 20:44:54 +0100 To: FFmpeg development discussions and patches X-Mailer: Apple Mail (2.3259) Subject: [FFmpeg-devel] [PATCH] avfilter: implement halve 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 Cc: Michael Riss Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" filter strictly “halves” the image efficiently, which is often exactly what is needed likely much faster than using scale fully slice parallelized Signed-off-by: Daniel Oberhoff --- libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_halve.c | 367 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 369 insertions(+) create mode 100644 libavfilter/vf_halve.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 9ab65eb..a986322 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -250,6 +250,7 @@ OBJS-$(CONFIG_RANDOM_FILTER) += vf_random.o OBJS-$(CONFIG_READVITC_FILTER) += vf_readvitc.o OBJS-$(CONFIG_REALTIME_FILTER) += f_realtime.o OBJS-$(CONFIG_REMAP_FILTER) += vf_remap.o framesync.o +OBJS-$(CONFIG_HALVE_FILTER) += vf_halve.o framesync.o OBJS-$(CONFIG_REMOVEGRAIN_FILTER) += vf_removegrain.o OBJS-$(CONFIG_REMOVELOGO_FILTER) += bbox.o lswsutils.o lavfutils.o vf_removelogo.o OBJS-$(CONFIG_REPEATFIELDS_FILTER) += vf_repeatfields.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 2c37818..0b9b69c 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -265,6 +265,7 @@ void avfilter_register_all(void) REGISTER_FILTER(READVITC, readvitc, vf); REGISTER_FILTER(REALTIME, realtime, vf); REGISTER_FILTER(REMAP, remap, vf); + REGISTER_FILTER(HALVE, halve, vf); REGISTER_FILTER(REMOVEGRAIN, removegrain, vf); REGISTER_FILTER(REMOVELOGO, removelogo, vf); REGISTER_FILTER(REPEATFIELDS, repeatfields, vf); diff --git a/libavfilter/vf_halve.c b/libavfilter/vf_halve.c new file mode 100644 index 0000000..dd77eb8 --- /dev/null +++ b/libavfilter/vf_halve.c @@ -0,0 +1,367 @@ +/* + * Copyright (c) 2016 Floris Sluiter + * + * 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 + */ + +/** + * @file + * Pixel halve filter + * This filter makes the target image exactly halve in size in x and y + * dimension of the source image by summing the corresponding four source + * pixels. + */ + +#include "libavutil/imgutils.h" +#include "libavutil/pixdesc.h" +#include "libavutil/opt.h" +#include "avfilter.h" +#include "formats.h" +#include "framesync.h" +#include "internal.h" +#include "video.h" + +typedef struct HalveContext { + const AVClass *class; + int nb_planes; + int nb_components; + int step; + FFFrameSync fs; + void (*halve_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); +} HalveContext; + +#define OFFSET(x) offsetof(HalveContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption halve_options[] = { + { NULL } +}; + +AVFILTER_DEFINE_CLASS(halve); + +typedef struct ThreadData { + AVFrame *in, *out; + int nb_planes; + int nb_components; + int step; +} ThreadData; + +static int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_YUVA444P, + AV_PIX_FMT_YUV444P, + AV_PIX_FMT_YUVJ444P, + AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, + AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, + AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, + AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12, + AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16, + AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P16, + AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, + AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, + AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, + AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48, + AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64, + AV_PIX_FMT_NONE + }; + AVFilterFormats *pix_formats = NULL; + int ret; + + if (!(pix_formats = ff_make_format_list(pix_fmts))) { + ret = AVERROR(ENOMEM); + goto fail; + } + if ((ret = ff_formats_ref(pix_formats, &ctx->inputs[0]->out_formats)) < 0 || + (ret = ff_formats_ref(pix_formats, &ctx->outputs[0]->in_formats)) < 0) + goto fail; + return 0; +fail: + if (pix_formats) + av_freep(&pix_formats->formats); + av_freep(&pix_formats); + return ret; +} + +/** + * halve_planar algorithm expects planes of same size + * pixels are copied from source to target using : + * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ]; + */ +static void halve_planar_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + const ThreadData *td = (ThreadData*)arg; + const AVFrame *in = td->in; + const AVFrame *out = td->out; + int x , y, plane; + + for (plane = 0; plane < td->nb_planes ; plane++) { + uint8_t *dst = out->data[plane]; + const int dlinesize = out->linesize[plane]; + const uint8_t *src = in->data[plane]; + const int slinesize = in->linesize[plane]; + for (y = 0; y < out->height; y++) { + for (x = 0; x < out->width; x++, ++dst, src += 2) { + dst[0] = (uint8_t)( + ( + (uint16_t)(src[0]) + + (uint16_t)(src[slinesize]) + + (uint16_t)(src[1]) + + (uint16_t)(src[slinesize + 1]) + ) >> 2); + } + src += 2 * slinesize - in->width; + dst += dlinesize - out->width; + } + } +} + +static void halve_planar16_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + const ThreadData *td = (ThreadData*)arg; + const AVFrame *in = td->in; + const AVFrame *out = td->out; + int x , y, plane; + + for (plane = 0; plane < td->nb_planes ; plane++) { + uint16_t *dst = (uint16_t *)out->data[plane]; + const int dlinesize = out->linesize[plane] / 2; + const uint16_t *src = (const uint16_t *)in->data[plane]; + const int slinesize = in->linesize[plane] / 2; + for (y = 0; y < out->height; y++) { + for (x = 0; x < out->width; x++, ++dst, ++src) { + dst[0] = (uint16_t)( + ( + (uint32_t)(src[0]) + + (uint32_t)(src[slinesize]) + + (uint32_t)(src[1]) + + (uint32_t)(src[slinesize + 1]) + ) >> 2); + } + src += 2 * slinesize - in->width; + dst += dlinesize - out->width; + } + } +} + + + +/** + * halve_packed algorithm expects pixels with both padded bits (step) and + * number of components correctly set. + * pixels are copied from source to target using : + * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ]; + */ +static void halve_packed_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + const ThreadData *td = (ThreadData*)arg; + const AVFrame *in = td->in; + const AVFrame *out = td->out; + uint8_t *dst = out->data[0]; + const uint8_t *src = in->data[0]; + const int dlinesize = out->linesize[0]; + const int slinesize = in->linesize[0]; + const int step = td->step; + int c, x, y; + + for (y = 0; y < out->height; y++) { + for (x = 0; x < out->width; x++, dst += step, src += step) { + for (c = 0; c < td->nb_components; ++c) { + dst[c] = (uint8_t)( + ( + (uint16_t)(src[c]) + + (uint16_t)(src[slinesize + c]) + + (uint16_t)(src[step + c]) + + (uint16_t)(src[slinesize + step + c]) + ) >> 2); + } + } + src += 2 * slinesize - in->width * step; + dst += dlinesize - out->width * step; + } +} + +static void halve_packed16_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + const ThreadData *td = (ThreadData*)arg; + const AVFrame *in = td->in; + const AVFrame *out = td->out; + uint16_t *dst = (uint16_t *)out->data[0]; + const uint16_t *src = (const uint16_t *)in->data[0]; + const int dlinesize = out->linesize[0] / 2; + const int slinesize = in->linesize[0] / 2; + const int step = td->step / 2; + int c, x, y; + + for (y = 0; y < out->height; y++) { + for (x = 0; x < out->width; x++, dst += step, src += step) { + for (c = 0; c < td->nb_components; ++c) { + dst[c] = (uint16_t)( + ( + (uint32_t)(src[c]) + + (uint32_t)(src[slinesize + c]) + + (uint32_t)(src[step + c]) + + (uint32_t)(src[slinesize + step + c]) + ) >> 2); + } + } + src += 2 * slinesize - in->width * step; + dst += dlinesize - out->width * step; + } +} + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + HalveContext *s = ctx->priv; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + + s->nb_planes = av_pix_fmt_count_planes(inlink->format); + s->nb_components = desc->nb_components; + + if (desc->comp[0].depth == 8) { + if (s->nb_planes > 1 || s->nb_components == 1) { + s->halve_slice = halve_planar_slice; + } else { + s->halve_slice = halve_packed_slice; + } + } else { + if (s->nb_planes > 1 || s->nb_components == 1) { + s->halve_slice = halve_planar16_slice; + } else { + s->halve_slice = halve_packed16_slice; + } + } + + s->step = av_get_padded_bits_per_pixel(desc) >> 3; + return 0; +} + +static int process_frame(FFFrameSync *fs) +{ + AVFilterContext *ctx = fs->parent; + HalveContext *s = fs->opaque; + AVFilterLink *outlink = ctx->outputs[0]; + AVFrame *out, *in; + int ret; + + if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0) + return ret; + + if (ctx->is_disabled) { + out = av_frame_clone(in); + if (!out) + return AVERROR(ENOMEM); + } else { + ThreadData td; + + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) + return AVERROR(ENOMEM); + av_frame_copy_props(out, in); + + td.in = in; + td.out = out; + td.nb_planes = s->nb_planes; + td.nb_components = s->nb_components; + td.step = s->step; + ctx->internal->execute(ctx, s->halve_slice, &td, NULL, FFMIN(outlink->h, ctx->graph->nb_threads)); + } + out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base); + + return ff_filter_frame(outlink, out); +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + HalveContext *s = ctx->priv; + AVFilterLink *srclink = ctx->inputs[0]; + FFFrameSyncIn *in; + int ret; + + outlink->w = srclink->w / 2; + outlink->h = srclink->h / 2; + outlink->time_base = srclink->time_base; + outlink->sample_aspect_ratio = srclink->sample_aspect_ratio; + outlink->frame_rate = srclink->frame_rate; + + ret = ff_framesync_init(&s->fs, ctx, 1); + if (ret < 0) + return ret; + + in = s->fs.in; + in[0].time_base = srclink->time_base; + in[0].sync = 2; + in[0].before = EXT_STOP; + in[0].after = EXT_STOP; + s->fs.opaque = s; + s->fs.on_event = process_frame; + + return ff_framesync_configure(&s->fs); +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *buf) +{ + HalveContext *s = inlink->dst->priv; + return ff_framesync_filter_frame(&s->fs, inlink, buf); +} + +static int request_frame(AVFilterLink *outlink) +{ + HalveContext *s = outlink->src->priv; + return ff_framesync_request_frame(&s->fs, outlink); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + HalveContext *s = ctx->priv; + + ff_framesync_uninit(&s->fs); +} + +static const AVFilterPad halve_inputs[] = { + { + .name = "source", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad halve_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + .request_frame = request_frame, + }, + { NULL } +}; + +AVFilter ff_vf_halve = { + .name = "halve", + .description = NULL_IF_CONFIG_SMALL("Halve image"), + .priv_size = sizeof(HalveContext), + .uninit = uninit, + .query_formats = query_formats, + .inputs = halve_inputs, + .outputs = halve_outputs, + .priv_class = &halve_class, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, +};