FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_multiply.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 101 0.0%
Functions: 0 7 0.0%
Branches: 0 28 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2022 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/imgutils.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "filters.h"
26 #include "video.h"
27 #include "framesync.h"
28
29 typedef struct MultiplyContext {
30 const AVClass *class;
31
32 float offset;
33 float scale;
34 int planes;
35
36 int linesize[4];
37 int nb_planes;
38
39 FFFrameSync fs;
40 } MultiplyContext;
41
42 #define OFFSET(x) offsetof(MultiplyContext, x)
43 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
44
45 typedef struct ThreadData {
46 AVFrame *src, *ref, *dst;
47 } ThreadData;
48
49 static const AVOption multiply_options[] = {
50 { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0., 9., FLAGS },
51 { "offset", "set offset", OFFSET(offset), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, -1., 1., FLAGS },
52 { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_FLAGS, {.i64=0xF}, 0., 0xF, FLAGS },
53 { NULL }
54 };
55
56 static const enum AVPixelFormat pix_fmts[] = {
57 AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32,
58 AV_PIX_FMT_NONE
59 };
60
61 static int config_input(AVFilterLink *inlink)
62 {
63 AVFilterContext *ctx = inlink->dst;
64 MultiplyContext *s = ctx->priv;
65 int ret;
66
67 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
68 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
69 return ret;
70
71 return 0;
72 }
73
74 static void multiply(const uint8_t *ssrc, const uint8_t *rref, uint8_t *ddst,
75 float scale, float offset, int w)
76 {
77 const float *src = (const float *)ssrc;
78 const float *ref = (const float *)rref;
79 float *dst = (float *)ddst;
80
81 for (int x = 0; x < w; x++) {
82 const float factor = (ref[x] + offset) * scale;
83
84 dst[x] = src[x] * factor;
85 }
86 }
87
88 static int multiply_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
89 {
90 MultiplyContext *s = ctx->priv;
91 const float offset = s->offset;
92 const float scale = s->scale;
93 ThreadData *td = arg;
94
95 for (int p = 0; p < s->nb_planes; p++) {
96 const ptrdiff_t src_linesize = td->src->linesize[p];
97 const ptrdiff_t ref_linesize = td->ref->linesize[p];
98 const ptrdiff_t dst_linesize = td->dst->linesize[p];
99 const int w = td->src->width;
100 const int h = td->src->height;
101 const int slice_start = (h * jobnr) / nb_jobs;
102 const int slice_end = (h * (jobnr+1)) / nb_jobs;
103 const uint8_t *src = td->src->data[p] + slice_start * src_linesize;
104 const uint8_t *ref = td->ref->data[p] + slice_start * ref_linesize;
105 uint8_t *dst = td->dst->data[p] + slice_start * dst_linesize;
106
107 if (!((1 << p) & s->planes)) {
108 av_image_copy_plane(dst, dst_linesize, ref, ref_linesize,
109 s->linesize[p], slice_end - slice_start);
110 continue;
111 }
112
113 for (int y = slice_start; y < slice_end; y++) {
114 multiply(src, ref, dst, scale, offset, w);
115
116 dst += dst_linesize;
117 src += src_linesize;
118 ref += ref_linesize;
119 }
120 }
121
122 return 0;
123 }
124
125 static int process_frame(FFFrameSync *fs)
126 {
127 AVFilterContext *ctx = fs->parent;
128 MultiplyContext *s = fs->opaque;
129 AVFilterLink *outlink = ctx->outputs[0];
130 AVFrame *out, *src, *ref;
131 int ret;
132
133 if ((ret = ff_framesync_get_frame(&s->fs, 0, &src, 0)) < 0 ||
134 (ret = ff_framesync_get_frame(&s->fs, 1, &ref, 0)) < 0)
135 return ret;
136
137 if (ctx->is_disabled) {
138 out = av_frame_clone(src);
139 if (!out)
140 return AVERROR(ENOMEM);
141 } else {
142 ThreadData td;
143
144 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
145 if (!out)
146 return AVERROR(ENOMEM);
147 av_frame_copy_props(out, src);
148
149 td.src = src;
150 td.ref = ref;
151 td.dst = out;
152
153 ff_filter_execute(ctx, multiply_slice, &td, NULL,
154 FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
155 }
156 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
157
158 return ff_filter_frame(outlink, out);
159 }
160
161 static int config_output(AVFilterLink *outlink)
162 {
163 AVFilterContext *ctx = outlink->src;
164 MultiplyContext *s = ctx->priv;
165 AVFilterLink *source = ctx->inputs[0];
166 AVFilterLink *ref = ctx->inputs[1];
167 FilterLink *il = ff_filter_link(source);
168 FilterLink *ol = ff_filter_link(outlink);
169 FFFrameSyncIn *in;
170 int ret;
171
172 if (source->w != ref->w || source->h != ref->h) {
173 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
174 "(size %dx%d) do not match the corresponding "
175 "second input link %s parameters (%dx%d)\n",
176 ctx->input_pads[0].name, source->w, source->h,
177 ctx->input_pads[1].name, ref->w, ref->h);
178 return AVERROR(EINVAL);
179 }
180
181 outlink->w = source->w;
182 outlink->h = source->h;
183 outlink->sample_aspect_ratio = source->sample_aspect_ratio;
184 ol->frame_rate = il->frame_rate;
185
186 if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
187 return ret;
188
189 in = s->fs.in;
190 in[0].time_base = source->time_base;
191 in[1].time_base = ref->time_base;
192 in[0].sync = 1;
193 in[0].before = EXT_STOP;
194 in[0].after = EXT_INFINITY;
195 in[1].sync = 1;
196 in[1].before = EXT_STOP;
197 in[1].after = EXT_INFINITY;
198 s->fs.opaque = s;
199 s->fs.on_event = process_frame;
200
201 ret = ff_framesync_configure(&s->fs);
202 outlink->time_base = s->fs.time_base;
203
204 return ret;
205 }
206
207 static int activate(AVFilterContext *ctx)
208 {
209 MultiplyContext *s = ctx->priv;
210 return ff_framesync_activate(&s->fs);
211 }
212
213 static av_cold void uninit(AVFilterContext *ctx)
214 {
215 MultiplyContext *s = ctx->priv;
216
217 ff_framesync_uninit(&s->fs);
218 }
219
220 static const AVFilterPad multiply_inputs[] = {
221 {
222 .name = "source",
223 .type = AVMEDIA_TYPE_VIDEO,
224 .config_props = config_input,
225 },
226 {
227 .name = "factor",
228 .type = AVMEDIA_TYPE_VIDEO,
229 },
230 };
231
232 static const AVFilterPad multiply_outputs[] = {
233 {
234 .name = "default",
235 .type = AVMEDIA_TYPE_VIDEO,
236 .config_props = config_output,
237 },
238 };
239
240 AVFILTER_DEFINE_CLASS(multiply);
241
242 const AVFilter ff_vf_multiply = {
243 .name = "multiply",
244 .description = NULL_IF_CONFIG_SMALL("Multiply first video stream with second video stream."),
245 .priv_class = &multiply_class,
246 .priv_size = sizeof(MultiplyContext),
247 .uninit = uninit,
248 .activate = activate,
249 FILTER_INPUTS(multiply_inputs),
250 FILTER_OUTPUTS(multiply_outputs),
251 FILTER_PIXFMTS_ARRAY(pix_fmts),
252 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
253 .process_command = ff_filter_process_command,
254 };
255