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 int pos_h[4], pos_v[4];
36
37 const AVPixFmtDescriptor *desc;
38 int nb_planes;
39 int bytes;
40
41 int planewidth[4];
42 int planeheight[4];
43 } ScrollContext;
44
query_formats(AVFilterContext * ctx)45 static int query_formats(AVFilterContext *ctx)
46 {
47 static const enum AVPixelFormat pix_fmts[] = {
48 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
49 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
50 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
51 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
52 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
53 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
54 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
55 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
56 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
57 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
58 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
59 AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
60 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
61 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
62 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
63 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
64 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
65 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
66 AV_PIX_FMT_NONE
67 };
68
69 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
70 if (!fmts_list)
71 return AVERROR(ENOMEM);
72 return ff_set_common_formats(ctx, fmts_list);
73 }
74
75 typedef struct ThreadData {
76 AVFrame *in, *out;
77 } ThreadData;
78
scroll_slice(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)79 static int scroll_slice(AVFilterContext *ctx, void *arg, int jobnr,
80 int nb_jobs)
81 {
82 ScrollContext *s = ctx->priv;
83 ThreadData *td = arg;
84 AVFrame *in = td->in;
85 AVFrame *out = td->out;
86
87 for (int p = 0; p < s->nb_planes; p++) {
88 const uint8_t *src = in->data[p];
89 const int h = s->planeheight[p];
90 const int w = s->planewidth[p] * s->bytes;
91 const int slice_start = (h * jobnr) / nb_jobs;
92 const int slice_end = (h * (jobnr+1)) / nb_jobs;
93 uint8_t *dst = out->data[p] + slice_start * out->linesize[p];
94
95 for (int y = slice_start; y < slice_end; y++) {
96 int yy = (y + s->pos_v[p]) % h;
97 const uint8_t *ssrc = src + yy * in->linesize[p];
98
99 if (s->pos_h[p] < w)
100 memcpy(dst, ssrc + s->pos_h[p], w - s->pos_h[p]);
101 if (s->pos_h[p] > 0)
102 memcpy(dst + w - s->pos_h[p], ssrc, s->pos_h[p]);
103
104 dst += out->linesize[p];
105 }
106 }
107
108 return 0;
109 }
110
scroll(AVFilterContext * ctx,AVFrame * in,AVFrame * out)111 static void scroll(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
112 {
113 ScrollContext *s = ctx->priv;
114 ThreadData td;
115 int h_pos, v_pos;
116
117 s->h_pos = fmodf(s->h_pos, in->width);
118 s->v_pos = fmodf(s->v_pos, in->height);
119
120 h_pos = s->h_pos;
121 v_pos = s->v_pos;
122
123 if (h_pos < 0)
124 h_pos += in->width;
125 if (v_pos < 0)
126 v_pos += in->height;
127
128 s->pos_v[1] = s->pos_v[2] = AV_CEIL_RSHIFT(v_pos, s->desc->log2_chroma_h);
129 s->pos_v[0] = s->pos_v[3] = v_pos;
130 s->pos_h[1] = s->pos_h[2] = AV_CEIL_RSHIFT(h_pos, s->desc->log2_chroma_w) * s->bytes;
131 s->pos_h[0] = s->pos_h[3] = h_pos * s->bytes;
132
133 td.in = in; td.out = out;
134 ctx->internal->execute(ctx, scroll_slice, &td, NULL,
135 FFMIN(out->height, ff_filter_get_nb_threads(ctx)));
136
137 s->h_pos += s->h_speed * in->width;
138 s->v_pos += s->v_speed * in->height;
139 }
140
filter_frame(AVFilterLink * inlink,AVFrame * in)141 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
142 {
143 AVFilterContext *ctx = inlink->dst;
144 AVFilterLink *outlink = ctx->outputs[0];
145 AVFrame *out;
146
147 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
148 if (!out) {
149 av_frame_free(&in);
150 return AVERROR(ENOMEM);
151 }
152 av_frame_copy_props(out, in);
153
154 scroll(ctx, in, out);
155
156 av_frame_free(&in);
157 return ff_filter_frame(outlink, out);
158 }
159
config_input(AVFilterLink * inlink)160 static int config_input(AVFilterLink *inlink)
161 {
162 AVFilterContext *ctx = inlink->dst;
163 ScrollContext *s = ctx->priv;
164
165 s->desc = av_pix_fmt_desc_get(inlink->format);
166 s->nb_planes = s->desc->nb_components;
167 s->bytes = (s->desc->comp[0].depth + 7) >> 3;
168
169 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
170 s->planeheight[0] = s->planeheight[3] = inlink->h;
171 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
172 s->planewidth[0] = s->planewidth[3] = inlink->w;
173
174 s->h_pos = (1.f - s->h_ipos) * inlink->w;
175 s->v_pos = (1.f - s->v_ipos) * inlink->h;
176
177 return 0;
178 }
179
180 #define OFFSET(x) offsetof(ScrollContext, x)
181 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
182 #define VFT AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
183
184 static const AVOption scroll_options[] = {
185 { "horizontal", "set the horizontal scrolling speed", OFFSET(h_speed), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -1., 1., VFT },
186 { "h", "set the horizontal scrolling speed", OFFSET(h_speed), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -1., 1., VFT },
187 { "vertical", "set the vertical scrolling speed", OFFSET(v_speed), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -1., 1., VFT },
188 { "v", "set the vertical scrolling speed", OFFSET(v_speed), AV_OPT_TYPE_FLOAT, {.dbl=0.}, -1., 1., VFT },
189 { "hpos", "set initial horizontal position", OFFSET(h_ipos), AV_OPT_TYPE_FLOAT, {.dbl=0.}, 0, 1., FLAGS },
190 { "vpos", "set initial vertical position", OFFSET(v_ipos), AV_OPT_TYPE_FLOAT, {.dbl=0.}, 0, 1., FLAGS },
191 { NULL }
192 };
193
194 AVFILTER_DEFINE_CLASS(scroll);
195
196 static const AVFilterPad scroll_inputs[] = {
197 {
198 .name = "default",
199 .type = AVMEDIA_TYPE_VIDEO,
200 .config_props = config_input,
201 .filter_frame = filter_frame,
202 },
203 { NULL }
204 };
205
206 static const AVFilterPad scroll_outputs[] = {
207 {
208 .name = "default",
209 .type = AVMEDIA_TYPE_VIDEO,
210 },
211 { NULL }
212 };
213
214 AVFilter ff_vf_scroll = {
215 .name = "scroll",
216 .description = NULL_IF_CONFIG_SMALL("Scroll input video."),
217 .priv_size = sizeof(ScrollContext),
218 .priv_class = &scroll_class,
219 .query_formats = query_formats,
220 .inputs = scroll_inputs,
221 .outputs = scroll_outputs,
222 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
223 .process_command = ff_filter_process_command,
224 };
225