History

avfilter: add scroll video filter

Paul B Mahol committed 7 years ago

Commit a7463591 parent 5161b276 files · +222 −1Browse files

Showing the changes to libavfilter (4 of 6 files). Show all

Unified · Split

4 files
  1. libavfilter/Makefile +1 −0
  2. libavfilter/allfilters.c +1 −0
  3. libavfilter/version.h +1 −1
  4. libavfilter/vf_scroll.c +198 −0
libavfilter/Makefile+1−0
@@ -356,6 +356,7 @@ OBJS-$(CONFIG_SCALE_NPP_FILTER) += vf_scale_npp.o scale.o · 355 unmodified lines
356356 OBJS-$(CONFIG_SCALE_QSV_FILTER) += vf_scale_qsv.o
357357 OBJS-$(CONFIG_SCALE_VAAPI_FILTER) += vf_scale_vaapi.o scale.o vaapi_vpp.o
358358 OBJS-$(CONFIG_SCALE2REF_FILTER) += vf_scale.o scale.o
359+OBJS-$(CONFIG_SCROLL_FILTER) += vf_scroll.o
359360 OBJS-$(CONFIG_SELECT_FILTER) += f_select.o
360361 OBJS-$(CONFIG_SELECTIVECOLOR_FILTER) += vf_selectivecolor.o
361362 OBJS-$(CONFIG_SENDCMD_FILTER) += f_sendcmd.o
libavfilter/allfilters.c+1−0
@@ -338,6 +338,7 @@ extern AVFilter ff_vf_scale_npp; · 337 unmodified lines
338338 extern AVFilter ff_vf_scale_qsv;
339339 extern AVFilter ff_vf_scale_vaapi;
340340 extern AVFilter ff_vf_scale2ref;
341+extern AVFilter ff_vf_scroll;
341342 extern AVFilter ff_vf_select;
342343 extern AVFilter ff_vf_selectivecolor;
343344 extern AVFilter ff_vf_sendcmd;
libavfilter/version.h+1−1
@@ -30,7 +30,7 @@ · 29 unmodified lines
3030 #include "libavutil/version.h"
3131
3232 #define LIBAVFILTER_VERSION_MAJOR 7
33−#define LIBAVFILTER_VERSION_MINOR 59
33+#define LIBAVFILTER_VERSION_MINOR 60
3434 #define LIBAVFILTER_VERSION_MICRO 100
3535
3636
libavfilter/vf_scroll.c+198−0
@@ -0,0 +1,198 @@
1+/*
2+ * Copyright (c) 2019 Paul B Mahol
3+ *
4+ * This file is part of FFmpeg.
5+ *
6+ * FFmpeg is free software; you can redistribute it and/or
7+ * modify it under the terms of the GNU Lesser General Public
8+ * License as published by the Free Software Foundation; either
9+ * version 2.1 of the License, or (at your option) any later version.
10+ *
11+ * FFmpeg is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+ * Lesser General Public License for more details.
15+ *
16+ * You should have received a copy of the GNU Lesser General Public
17+ * License along with FFmpeg; if not, write to the Free Software
18+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19+ */
20+
21+#include "libavutil/common.h"
22+#include "libavutil/opt.h"
23+#include "libavutil/pixdesc.h"
24+#include "avfilter.h"
25+#include "formats.h"
26+#include "internal.h"
27+
28+typedef struct ScrollContext {
29+ const AVClass *class;
30+
31+ float h_speed, v_speed;
32+ float h_pos, v_pos;
33+ float h_ipos, v_ipos;
34+
35+ const AVPixFmtDescriptor *desc;
36+ int nb_planes;
37+ int bytes;
38+
39+ int planewidth[4];
40+ int planeheight[4];
41+} ScrollContext;
42+
43+static int query_formats(AVFilterContext *ctx)
44+{
45+ static const enum AVPixelFormat pix_fmts[] = {
46+ AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
47+ AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
48+ AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
49+ AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
50+ AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
51+ AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
52+ AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
53+ AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
54+ AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
55+ AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
56+ AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
57+ AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
58+ AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
59+ AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
60+ AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
61+ AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
62+ AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
63+ AV_PIX_FMT_NONE
64+ };
65+
66+ AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
67+ if (!fmts_list)
68+ return AVERROR(ENOMEM);
69+ return ff_set_common_formats(ctx, fmts_list);
70+}
71+
72+static void scroll(ScrollContext *s, AVFrame *in, AVFrame *out)
73+{
74+ int h_pos, v_pos;
75+ int pos_h[4], pos_v[4];
76+
77+ s->h_pos = fmodf(s->h_pos, in->width);
78+ s->v_pos = fmodf(s->v_pos, in->height);
79+
80+ h_pos = s->h_pos;
81+ v_pos = s->v_pos;
82+
83+ if (h_pos < 0)
84+ h_pos += in->width;
85+ if (v_pos < 0)
86+ v_pos += in->height;
87+
88+ pos_v[1] = pos_v[2] = AV_CEIL_RSHIFT(v_pos, s->desc->log2_chroma_h);
89+ pos_v[0] = pos_v[3] = v_pos;
90+ pos_h[1] = pos_h[2] = AV_CEIL_RSHIFT(h_pos, s->desc->log2_chroma_w) * s->bytes;
91+ pos_h[0] = pos_h[3] = h_pos * s->bytes;
92+
93+ for (int p = 0; p < s->nb_planes; p++) {
94+ const uint8_t *src = in->data[p];
95+ uint8_t *dst = out->data[p];
96+ const int h = s->planeheight[p];
97+ const int w = s->planewidth[p] * s->bytes;
98+
99+ for (int y = 0; y < h; y++) {
100+ int yy = (y + pos_v[p]) % h;
101+ const uint8_t *ssrc = src + yy * in->linesize[p];
102+
103+ if (pos_h[p] < w)
104+ memcpy(dst, ssrc + pos_h[p], w - pos_h[p]);
105+ if (pos_h[p] > 0)
106+ memcpy(dst + w - pos_h[p], ssrc, pos_h[p]);
107+
108+ dst += out->linesize[p];
109+ }
110+ }
111+
112+ s->h_pos += s->h_speed * in->width;
113+ s->v_pos += s->v_speed * in->height;
114+}
115+
116+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
117+{
118+ AVFilterContext *ctx = inlink->dst;
119+ ScrollContext *s = ctx->priv;
120+ AVFilterLink *outlink = ctx->outputs[0];
121+ AVFrame *out;
122+
123+ out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
124+ if (!out) {
125+ av_frame_free(&in);
126+ return AVERROR(ENOMEM);
127+ }
128+ av_frame_copy_props(out, in);
129+
130+ scroll(s, in, out);
131+
132+ av_frame_free(&in);
133+ return ff_filter_frame(outlink, out);
134+}
135+
136+static int config_input(AVFilterLink *inlink)
137+{
138+ AVFilterContext *ctx = inlink->dst;
139+ ScrollContext *s = ctx->priv;
140+
141+ s->desc = av_pix_fmt_desc_get(inlink->format);
142+ s->nb_planes = s->desc->nb_components;
143+ s->bytes = (s->desc->comp[0].depth + 7) >> 3;
144+
145+ s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
146+ s->planeheight[0] = s->planeheight[3] = inlink->h;
147+ s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
148+ s->planewidth[0] = s->planewidth[3] = inlink->w;
149+
150+ s->h_pos = (1.f - s->h_ipos) * inlink->w;
151+ s->v_pos = (1.f - s->v_ipos) * inlink->h;
152+
153+ return 0;
154+}
155+
156+#define OFFSET(x) offsetof(ScrollContext, x)
157+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
158+
159+static const AVOption scroll_options[] = {
160+ { "horizontal", "set the horizontal scrolling speed", OFFSET(h_speed), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -1., 1., FLAGS },
161+ { "h", "set the horizontal scrolling speed", OFFSET(h_speed), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -1., 1., FLAGS },
162+ { "vertical", "set the vertical scrolling speed", OFFSET(v_speed), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -1., 1., FLAGS },
163+ { "v", "set the vertical scrolling speed", OFFSET(v_speed), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -1., 1., FLAGS },
164+ { "hpos", "set initial horizontal position", OFFSET(h_ipos), AV_OPT_TYPE_FLOAT, {.dbl=0.}, 0, 1., FLAGS },
165+ { "vpos", "set initial vertical position", OFFSET(v_ipos), AV_OPT_TYPE_FLOAT, {.dbl=0.}, 0, 1., FLAGS },
166+ { NULL }
167+};
168+
169+AVFILTER_DEFINE_CLASS(scroll);
170+
171+static const AVFilterPad scroll_inputs[] = {
172+ {
173+ .name = "default",
174+ .type = AVMEDIA_TYPE_VIDEO,
175+ .config_props = config_input,
176+ .filter_frame = filter_frame,
177+ },
178+ { NULL }
179+};
180+
181+static const AVFilterPad scroll_outputs[] = {
182+ {
183+ .name = "default",
184+ .type = AVMEDIA_TYPE_VIDEO,
185+ },
186+ { NULL }
187+};
188+
189+AVFilter ff_vf_scroll = {
190+ .name = "scroll",
191+ .description = NULL_IF_CONFIG_SMALL("Scroll input video."),
192+ .priv_size = sizeof(ScrollContext),
193+ .priv_class = &scroll_class,
194+ .query_formats = query_formats,
195+ .inputs = scroll_inputs,
196+ .outputs = scroll_outputs,
197+ .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
198+};

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t