Signed-off-by: Paul B Mahol <one...@gmail.com> --- doc/filters.texi | 24 +++++ libavfilter/Makefile | 2 + libavfilter/allfilters.c | 2 + libavfilter/vf_stack.c | 255 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 283 insertions(+) create mode 100644 libavfilter/vf_stack.c
diff --git a/doc/filters.texi b/doc/filters.texi index 2f7b0fa..7b52971 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -6589,6 +6589,18 @@ Set the scaling dimension: @code{2} for @code{hq2x}, @code{3} for Default is @code{3}. @end table +@section hstack +Stack streams horizontally. + +All streams must be of same pixel format and of same height. + +The filter accept the following option: + +@table @option +@item nb_inputs +Set number of input streams. Default is 2. +@end table + @section hue Modify the hue and/or the saturation of the input. @@ -10892,6 +10904,18 @@ vignette='PI/4+random(1)*PI/50':eval=frame @end itemize +@section vstack +Stack streams vertically. + +All streams must be of same pixel format and of same width. + +The filter accept the following option: + +@table @option +@item nb_inputs +Set number of input streams. Default is 2. +@end table + @section w3fdif Deinterlace the input video ("w3fdif" stands for "Weston 3 Field diff --git a/libavfilter/Makefile b/libavfilter/Makefile index aa34d8b..e9cd9c6 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -153,6 +153,7 @@ OBJS-$(CONFIG_HISTEQ_FILTER) += vf_histeq.o OBJS-$(CONFIG_HISTOGRAM_FILTER) += vf_histogram.o OBJS-$(CONFIG_HQDN3D_FILTER) += vf_hqdn3d.o OBJS-$(CONFIG_HQX_FILTER) += vf_hqx.o +OBJS-$(CONFIG_HSTACK_FILTER) += vf_stack.o OBJS-$(CONFIG_HUE_FILTER) += vf_hue.o OBJS-$(CONFIG_IDET_FILTER) += vf_idet.o OBJS-$(CONFIG_IL_FILTER) += vf_il.o @@ -231,6 +232,7 @@ OBJS-$(CONFIG_VFLIP_FILTER) += vf_vflip.o OBJS-$(CONFIG_VIDSTABDETECT_FILTER) += vidstabutils.o vf_vidstabdetect.o OBJS-$(CONFIG_VIDSTABTRANSFORM_FILTER) += vidstabutils.o vf_vidstabtransform.o OBJS-$(CONFIG_VIGNETTE_FILTER) += vf_vignette.o +OBJS-$(CONFIG_VSTACK_FILTER) += vf_stack.o OBJS-$(CONFIG_W3FDIF_FILTER) += vf_w3fdif.o OBJS-$(CONFIG_WAVEFORM_FILTER) += vf_waveform.o OBJS-$(CONFIG_XBR_FILTER) += vf_xbr.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 353a1dc..0a1bdfd 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -169,6 +169,7 @@ void avfilter_register_all(void) REGISTER_FILTER(HISTOGRAM, histogram, vf); REGISTER_FILTER(HQDN3D, hqdn3d, vf); REGISTER_FILTER(HQX, hqx, vf); + REGISTER_FILTER(HSTACK, hstack, vf); REGISTER_FILTER(HUE, hue, vf); REGISTER_FILTER(IDET, idet, vf); REGISTER_FILTER(IL, il, vf); @@ -246,6 +247,7 @@ void avfilter_register_all(void) REGISTER_FILTER(VIDSTABDETECT, vidstabdetect, vf); REGISTER_FILTER(VIDSTABTRANSFORM, vidstabtransform, vf); REGISTER_FILTER(VIGNETTE, vignette, vf); + REGISTER_FILTER(VSTACK, vstack, vf); REGISTER_FILTER(W3FDIF, w3fdif, vf); REGISTER_FILTER(WAVEFORM, waveform, vf); REGISTER_FILTER(XBR, xbr, vf); diff --git a/libavfilter/vf_stack.c b/libavfilter/vf_stack.c new file mode 100644 index 0000000..a8062fc --- /dev/null +++ b/libavfilter/vf_stack.c @@ -0,0 +1,255 @@ +/* + * Copyright (c) 2015 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/imgutils.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" + +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct StackContext { + const AVClass *class; + const AVPixFmtDescriptor *desc; + int nb_inputs; + int is_vertical; + int nb_planes; + + AVFrame **frames; +} StackContext; + +static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, + AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVJ420P, + AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, + AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, + AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, + AV_PIX_FMT_GRAY8, + AV_PIX_FMT_NONE +}; + +static int query_formats(AVFilterContext *ctx) +{ + 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 *frame) +{ + AVFilterContext *ctx = inlink->dst; + StackContext *s = ctx->priv; + int i; + + for (i = 0; i < s->nb_inputs; i++) + if (ctx->inputs[i] == inlink) + break; + + s->frames[i] = frame; + + return 0; +} + +static av_cold int init(AVFilterContext *ctx) +{ + StackContext *s = ctx->priv; + int i, ret; + + if (!strcmp(ctx->filter->name, "vstack")) + s->is_vertical = 1; + + s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames)); + if (!s->frames) + return AVERROR(ENOMEM); + + for (i = 0; i < s->nb_inputs; i++) { + AVFilterPad pad = { 0 }; + + pad.type = AVMEDIA_TYPE_VIDEO; + pad.name = av_asprintf("input%d", i); + if (!pad.name) + return AVERROR(ENOMEM); + pad.filter_frame = filter_frame; + pad.needs_fifo = 1; + + if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) { + av_freep(&pad.name); + return ret; + } + } + + return 0; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + StackContext *s = ctx->priv; + int i; + + if (s->frames) + for (i = 0; i < s->nb_inputs; i++) + av_frame_free(&s->frames[i]); + + av_freep(&s->frames); +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + StackContext *s = ctx->priv; + AVRational time_base = ctx->inputs[0]->time_base; + AVRational frame_rate = ctx->inputs[0]->frame_rate; + int height = ctx->inputs[0]->h; + int width = ctx->inputs[0]->w; + int i; + + if (s->is_vertical) { + for (i = 1; i < s->nb_inputs; i++) { + if (ctx->inputs[i]->w != width) { + av_log(ctx, AV_LOG_ERROR, "Input %d width %d does not match input %d width %d.\n", i, ctx->inputs[i]->w, 0, width); + return AVERROR(EINVAL); + } + height += ctx->inputs[i]->h; + } + } else { + for (i = 1; i < s->nb_inputs; i++) { + if (ctx->inputs[i]->h != height) { + av_log(ctx, AV_LOG_ERROR, "Input %d height %d does not match input %d height %d.\n", i, ctx->inputs[i]->h, 0, height); + return AVERROR(EINVAL); + } + width += ctx->inputs[i]->w; + } + } + + s->desc = av_pix_fmt_desc_get(outlink->format); + if (!s->desc) + return AVERROR_BUG; + s->nb_planes = s->desc->nb_components; + + outlink->w = width; + outlink->h = height; + outlink->time_base = time_base; + outlink->frame_rate= frame_rate; + + return 0; +} + +static int request_frame(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + StackContext *s = ctx->priv; + AVFrame *out; + int ret, i, p, offset[4] = { 0 }; + + for (i = 0; i < s->nb_inputs; i++) { + if (!s->frames[i]) { + ret = ff_request_frame(ctx->inputs[i]); + if (ret < 0) + return ret; + } + } + + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) + return AVERROR(ENOMEM); + out->pts = s->frames[0]->pts; + + for (i = 0; i < s->nb_inputs; i++) { + for (p = 0; p < s->nb_planes; p++) { + if (s->is_vertical) { + int h = (p == 1 || p == 2) ? FF_CEIL_RSHIFT(ctx->inputs[i]->h, s->desc->log2_chroma_h) : ctx->inputs[i]->h; + int w = (p == 1 || p == 2) ? FF_CEIL_RSHIFT(outlink->w, s->desc->log2_chroma_w) : outlink->w; + + av_image_copy_plane(out->data[p] + offset[p] * out->linesize[p], + out->linesize[p], + s->frames[i]->data[p], + s->frames[i]->linesize[p], + w, h); + offset[p] += h; + } else { + int h = (p == 1 || p == 2) ? FF_CEIL_RSHIFT(outlink->h, s->desc->log2_chroma_h) : outlink->h; + int w = (p == 1 || p == 2) ? FF_CEIL_RSHIFT(ctx->inputs[i]->w, s->desc->log2_chroma_w) : ctx->inputs[i]->w; + + av_image_copy_plane(out->data[p] + offset[p], + out->linesize[p], + s->frames[i]->data[p], + s->frames[i]->linesize[p], + w, h); + offset[p] += w; + } + } + } + + for (i = 0; i < s->nb_inputs; i++) + av_frame_free(&s->frames[i]); + + return ff_filter_frame(outlink, out); +} + +#define OFFSET(x) offsetof(StackContext, x) +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM +static const AVOption stack_options[] = { + { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS }, + { NULL }, +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + .request_frame = request_frame, + }, + { NULL } +}; + +#define hstack_options stack_options +AVFILTER_DEFINE_CLASS(hstack); + +AVFilter ff_vf_hstack = { + .name = "hstack", + .description = NULL_IF_CONFIG_SMALL("Stack video inputs horizontally."), + .priv_size = sizeof(StackContext), + .priv_class = &hstack_class, + .query_formats = query_formats, + .outputs = outputs, + .init = init, + .uninit = uninit, + .flags = AVFILTER_FLAG_DYNAMIC_INPUTS, +}; + +#define vstack_options stack_options +AVFILTER_DEFINE_CLASS(vstack); + +AVFilter ff_vf_vstack = { + .name = "vstack", + .description = NULL_IF_CONFIG_SMALL("Stack video inputs vertically."), + .priv_size = sizeof(StackContext), + .priv_class = &vstack_class, + .query_formats = query_formats, + .outputs = outputs, + .init = init, + .uninit = uninit, + .flags = AVFILTER_FLAG_DYNAMIC_INPUTS, +}; -- 1.7.11.2 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel