FFmpeg
Loading...
Searching...
No Matches
vf_multiply.c
Go to the documentation of this file.
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
29typedef struct MultiplyContext {
30 const AVClass *class;
31
32 float offset;
33 float scale;
34 int planes;
35
36 int linesize[4];
38
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
45typedef struct ThreadData {
46 AVFrame *src, *ref, *dst;
48
49static 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
60
61static 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
74static 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
88static 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 = ff_slice_pos(h, jobnr, nb_jobs);
102 const int slice_end = ff_slice_pos(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++) {
115
116 dst += dst_linesize;
117 src += src_linesize;
118 ref += ref_linesize;
119 }
120 }
121
122 return 0;
123}
124
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) {
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);
148
149 td.src = src;
150 td.ref = ref;
151 td.dst = out;
152
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
161static 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
208{
209 MultiplyContext *s = ctx->priv;
210 return ff_framesync_activate(&s->fs);
211}
212
214{
215 MultiplyContext *s = ctx->priv;
216
218}
219
220static 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
233 {
234 .name = "default",
235 .type = AVMEDIA_TYPE_VIDEO,
236 .config_props = config_output,
237 },
238};
239
241
243 .p.name = "multiply",
244 .p.description = NULL_IF_CONFIG_SMALL("Multiply first video stream with second video stream."),
245 .p.priv_class = &multiply_class,
247 .priv_size = sizeof(MultiplyContext),
248 .uninit = uninit,
253 .process_command = ff_filter_process_command,
254};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_multiply
static SoftFloat_IEEE754 multiply(SoftFloat_IEEE754 a, SoftFloat_IEEE754 b)
multiply two softfloats and handle the rounding off
Definition alsdec.c:1406
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define fs(width, name, subs,...)
Definition cbs_vp9.c:200
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
static av_always_inline int process_frame(AVTextFormatContext *tfc, InputFile *ifile, AVFrame *frame, const AVPacket *pkt, int *packet_new)
Definition ffprobe.c:1596
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition framesync.c:137
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition framesync.c:352
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition framesync.c:269
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition framesync.c:301
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition framesync.c:86
@ EXT_INFINITY
Extend the frame to infinity.
Definition framesync.h:75
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition opt.h:254
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:204
#define AVERROR(e)
Definition error.h:45
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition imgutils.c:374
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition imgutils.c:89
misc image utilities
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
unsigned offset
Definition libaomenc.c:763
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:763
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
static const int factor[16]
Definition vf_pp7.c:98
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
static const struct @257111027162314367033347246032313251342043035002 planes[]
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition mpeg12dec.c:1697
AVOptions.
@ EXT_STOP
Completely stop all streams with this one.
Definition packetsync.h:62
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
#define AV_PIX_FMT_GBRPF32
Definition pixfmt.h:584
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
#define AV_PIX_FMT_GBRAPF32
Definition pixfmt.h:585
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
AVOption.
Definition opt.h:428
Input stream structure.
Definition framesync.h:102
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
Definition framesync.h:112
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
Definition framesync.h:107
AVRational time_base
Time base for the incoming frames.
Definition framesync.h:117
unsigned sync
Synchronization level: frames on input at the highest sync level will generate output frame events.
Definition framesync.h:160
Frame sync structure.
Definition framesync.h:168
FFFrameSync fs
Definition vf_multiply.c:39
Used for passing data between threads.
Definition dsddec.c:71
const uint8_t * ref
Definition vf_bm3d.c:56
const uint8_t * src
Definition vf_bm3d.c:54
AVFrame * dst
Definition vf_blend.c:59
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static int ref[MAX_W *MAX_W]
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static int config_input(AVFilterLink *inlink)
Definition vf_multiply.c:61
static int multiply_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_multiply.c:88
static void multiply(const uint8_t *ssrc, const uint8_t *rref, uint8_t *ddst, float scale, float offset, int w)
Definition vf_multiply.c:74
static const AVFilterPad multiply_inputs[]
static int activate(AVFilterContext *ctx)
static const AVFilterPad multiply_outputs[]
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
Definition vf_multiply.c:42
static int config_output(AVFilterLink *outlink)
static const AVOption multiply_options[]
Definition vf_multiply.c:49
static int process_frame(FFFrameSync *fs)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844