FFmpeg
Loading...
Searching...
No Matches
vf_noise.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2013 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * noise generator
25 */
26
27#include "libavutil/mem.h"
28#include "libavutil/opt.h"
29#include "libavutil/imgutils.h"
30#include "libavutil/lfg.h"
31#include "libavutil/pixdesc.h"
32#include "avfilter.h"
33#include "filters.h"
34#include "formats.h"
35#include "vf_noise.h"
36#include "video.h"
37
38typedef struct ThreadData {
39 AVFrame *in, *out;
41
42#define OFFSET(x) offsetof(NoiseContext, x)
43#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
44
45#define NOISE_PARAMS(name, x, param) \
46 {#name"_seed", "set component #"#x" noise seed", OFFSET(param.seed), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS}, \
47 {#name"_strength", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
48 {#name"s", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
49 {#name"_flags", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, .unit = "flags"}, \
50 {#name"f", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, .unit = "flags"}, \
51
52static const AVOption noise_options[] = {
53 NOISE_PARAMS(all, 0, all)
54 NOISE_PARAMS(c0, 0, param[0])
55 NOISE_PARAMS(c1, 1, param[1])
56 NOISE_PARAMS(c2, 2, param[2])
57 NOISE_PARAMS(c3, 3, param[3])
58 {"a", "averaged noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_AVERAGED}, 0, 0, FLAGS, .unit = "flags"},
59 {"p", "(semi)regular pattern", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_PATTERN}, 0, 0, FLAGS, .unit = "flags"},
60 {"t", "temporal noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_TEMPORAL}, 0, 0, FLAGS, .unit = "flags"},
61 {"u", "uniform noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_UNIFORM}, 0, 0, FLAGS, .unit = "flags"},
62 {NULL}
63};
64
66
67static const int8_t patt[4] = { -1, 0, 1, 0 };
68
69#define RAND_N(range) ((int) ((double) range * av_lfg_get(lfg) / (UINT_MAX + 1.0)))
71{
72 int8_t *noise = av_malloc(MAX_NOISE * sizeof(int8_t));
73 FilterParams *fp = &n->param[comp];
74 AVLFG *lfg = &n->param[comp].lfg;
75 int strength = fp->strength;
76 int flags = fp->flags;
77 int i, j;
78
79 if (!noise)
80 return AVERROR(ENOMEM);
81
82 av_lfg_init(&fp->lfg, fp->seed + comp*31415U);
83
84 for (i = 0, j = 0; i < MAX_NOISE; i++, j++) {
85 if (flags & NOISE_UNIFORM) {
86 if (flags & NOISE_AVERAGED) {
87 if (flags & NOISE_PATTERN) {
88 noise[i] = (RAND_N(strength) - strength / 2) / 6
89 + patt[j % 4] * strength * 0.25 / 3;
90 } else {
91 noise[i] = (RAND_N(strength) - strength / 2) / 3;
92 }
93 } else {
94 if (flags & NOISE_PATTERN) {
95 noise[i] = (RAND_N(strength) - strength / 2) / 2
96 + patt[j % 4] * strength * 0.25;
97 } else {
98 noise[i] = RAND_N(strength) - strength / 2;
99 }
100 }
101 } else {
102 double x1, x2, w, y1;
103 do {
104 x1 = 2.0 * av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
105 x2 = 2.0 * av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
106 w = x1 * x1 + x2 * x2;
107 } while (w >= 1.0);
108
109 w = sqrt((-2.0 * log(w)) / w);
110 y1 = x1 * w;
111 y1 *= strength / sqrt(3.0);
112 if (flags & NOISE_PATTERN) {
113 y1 /= 2;
114 y1 += patt[j % 4] * strength * 0.35;
115 }
116 y1 = av_clipf(y1, -128, 127);
117 if (flags & NOISE_AVERAGED)
118 y1 /= 3.0;
119 noise[i] = (int)y1;
120 }
121 if (RAND_N(6) == 0)
122 j--;
123 }
124
125 for (i = 0; i < MAX_RES; i++)
126 for (j = 0; j < 3; j++)
127 fp->prev_shift[i][j] = noise + (av_lfg_get(lfg) & (MAX_SHIFT - 1));
128
129 fp->noise = noise;
130 return 0;
131}
132
134 AVFilterFormatsConfig **cfg_in,
135 AVFilterFormatsConfig **cfg_out)
136{
138 int fmt, ret;
139
140 for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
142 if (desc->flags & AV_PIX_FMT_FLAG_PLANAR && !(desc->comp[0].depth & 7)
143 && (ret = ff_add_format(&formats, fmt)) < 0)
144 return ret;
145 }
146
147 return ff_set_common_formats2(ctx, cfg_in, cfg_out, formats);
148}
149
150static int config_input(AVFilterLink *inlink)
151{
152 NoiseContext *n = inlink->dst->priv;
154 int ret;
155
157
158 if ((ret = av_image_fill_linesizes(n->bytewidth, inlink->format, inlink->w)) < 0)
159 return ret;
160
161 n->height[1] = n->height[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
162 n->height[0] = n->height[3] = inlink->h;
163
164 return 0;
165}
166
167void ff_line_noise_c(uint8_t *dst, const uint8_t *src, const int8_t *noise,
168 int len, int shift)
169{
170 int i;
171
172 noise += shift;
173 for (i = 0; i < len; i++) {
174 int v = src[i] + noise[i];
175
176 dst[i] = av_clip_uint8(v);
177 }
178}
179
180void ff_line_noise_avg_c(uint8_t *dst, const uint8_t *src,
181 int len, const int8_t * const *shift)
182{
183 int i;
184 const int8_t *src2 = (const int8_t*)src;
185
186 for (i = 0; i < len; i++) {
187 const int n = shift[0][i] + shift[1][i] + shift[2][i];
188 dst[i] = src2[i] + ((n * src2[i]) >> 7);
189 }
190}
191
192static void noise(uint8_t *dst, const uint8_t *src,
193 int dst_linesize, int src_linesize,
194 int width, int start, int end, NoiseContext *n, int comp)
195{
196 FilterParams *p = &n->param[comp];
197 int8_t *noise = p->noise;
198 const int flags = p->flags;
199 int y;
200
201 if (!noise) {
202 if (dst != src)
203 av_image_copy_plane(dst, dst_linesize, src, src_linesize, width, end - start);
204 return;
205 }
206
207 for (y = start; y < end; y++) {
208 const int ix = y & (MAX_RES - 1);
209 int x;
210 for (x=0; x < width; x+= MAX_RES) {
211 int w = FFMIN(width - x, MAX_RES);
212 int shift = p->rand_shift[ix];
213
214 if (flags & NOISE_AVERAGED) {
215 n->line_noise_avg(dst + x, src + x, w, p->prev_shift[ix]);
216 p->prev_shift[ix][shift % 3] = noise + shift;
217 } else {
218 n->line_noise(dst + x, src + x, noise, w, shift);
219 }
220 }
221 dst += dst_linesize;
222 src += src_linesize;
223 }
224}
225
226static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
227{
228 NoiseContext *s = ctx->priv;
229 ThreadData *td = arg;
230 int plane;
231
232 for (plane = 0; plane < s->nb_planes; plane++) {
233 const int height = s->height[plane];
234 const int start = ff_slice_pos(height, jobnr, nb_jobs);
235 const int end = ff_slice_pos(height, jobnr + 1, nb_jobs);
236 noise(td->out->data[plane] + start * td->out->linesize[plane],
237 td->in->data[plane] + start * td->in->linesize[plane],
238 td->out->linesize[plane], td->in->linesize[plane],
239 s->bytewidth[plane], start, end, s, plane);
240 }
241 return 0;
242}
243
244static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
245{
246 AVFilterContext *ctx = inlink->dst;
247 AVFilterLink *outlink = ctx->outputs[0];
248 NoiseContext *n = ctx->priv;
249 ThreadData td;
250 AVFrame *out;
251 int comp, i;
252
253 if (av_frame_is_writable(inpicref)) {
254 out = inpicref;
255 } else {
256 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
257 if (!out) {
258 av_frame_free(&inpicref);
259 return AVERROR(ENOMEM);
260 }
261 av_frame_copy_props(out, inpicref);
262 }
263
264 for (comp = 0; comp < 4; comp++) {
265 FilterParams *fp = &n->param[comp];
266
267 if ((!fp->rand_shift_init || (fp->flags & NOISE_TEMPORAL)) && fp->strength) {
268
269 for (i = 0; i < MAX_RES; i++) {
270 fp->rand_shift[i] = av_lfg_get(&fp->lfg) & (MAX_SHIFT - 1);
271 }
272 fp->rand_shift_init = 1;
273 if (fp->flags & NOISE_AVERAGED && outlink->h > MAX_RES)
275 }
276 }
277
278 td.in = inpicref; td.out = out;
282
283 if (inpicref != out)
284 av_frame_free(&inpicref);
285 return ff_filter_frame(outlink, out);
286}
287
289{
290 NoiseContext *n = ctx->priv;
291 int ret, i;
292
293 for (i = 0; i < 4; i++) {
294 if (n->all.seed >= 0)
295 n->param[i].seed = n->all.seed;
296 else
297 n->param[i].seed = 123457;
298 if (n->all.strength)
299 n->param[i].strength = n->all.strength;
300 if (n->all.flags)
301 n->param[i].flags = n->all.flags;
302 }
303
304 for (i = 0; i < 4; i++) {
305 if (n->param[i].strength && ((ret = init_noise(n, i)) < 0))
306 return ret;
307 }
308
311
312#if ARCH_X86
314#endif
315
316 return 0;
317}
318
320{
321 NoiseContext *n = ctx->priv;
322 int i;
323
324 for (i = 0; i < 4; i++)
325 av_freep(&n->param[i].noise);
326}
327
328static const AVFilterPad noise_inputs[] = {
329 {
330 .name = "default",
331 .type = AVMEDIA_TYPE_VIDEO,
332 .filter_frame = filter_frame,
333 .config_props = config_input,
334 },
335};
336
338 .p.name = "noise",
339 .p.description = NULL_IF_CONFIG_SMALL("Add noise."),
340 .p.priv_class = &noise_class,
342 .priv_size = sizeof(NoiseContext),
343 .init = init,
344 .uninit = uninit,
348};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_noise
Definition vf_noise.c:337
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
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 flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define av_clip_uint8
Definition common.h:106
#define av_clipf
Definition common.h:145
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition eamad.c:79
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition formats.c:1137
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition formats.c:571
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ 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
const pixel * src2
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition lfg.c:32
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition lfg.h:53
static int shift(int a, int b)
Definition bonk.c:261
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 AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#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
const char * desc
Definition libsvtav1.c:83
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
static const uint64_t c2
Definition murmur3.c:53
static const uint64_t c1
Definition murmur3.c:52
static int noise(AVBSFContext *ctx, AVPacket *pkt)
Definition noise.c:124
static const AVClass noise_class
Definition noise.c:219
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition pixdesc.h:132
formats
Definition signature.h:47
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
Lists of formats / etc.
Definition avfilter.h:120
A list of supported formats for one end of a filter link.
Definition formats.h:64
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
Context structure for the Lagged Fibonacci PRNG.
Definition lfg.h:33
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
filter data
Definition mlp.h:86
int8_t * noise
Definition vf_noise.h:42
int rand_shift[MAX_RES]
Definition vf_noise.h:44
AVLFG lfg
Definition vf_noise.h:40
int rand_shift_init
Definition vf_noise.h:45
const int8_t * prev_shift[MAX_RES][3]
Definition vf_noise.h:43
unsigned flags
Definition vf_noise.h:39
int height[4]
Definition vf_noise.h:52
struct NoiseContext::@321016275026027314223166305311350163157177202306 all
int slice_threading_impossible
Definition vf_noise.h:53
int nb_planes
Definition vf_noise.h:50
void(* line_noise_avg)(uint8_t *dst, const uint8_t *src, int len, const int8_t *const *shift)
Definition vf_noise.h:60
unsigned flags
Definition vf_noise.h:56
FilterParams param[4]
Definition vf_noise.h:58
int bytewidth[4]
Definition vf_noise.h:51
void(* line_noise)(uint8_t *dst, const uint8_t *src, const int8_t *noise, int len, int shift)
Definition vf_noise.h:59
Used for passing data between threads.
Definition dsddec.c:71
AVFrame * out
#define av_freep(p)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_noise.c:226
#define NOISE_PARAMS(name, x, param)
Definition vf_noise.c:45
static const AVOption noise_options[]
Definition vf_noise.c:52
static av_cold int init_noise(NoiseContext *n, int comp)
Definition vf_noise.c:70
static int config_input(AVFilterLink *inlink)
Definition vf_noise.c:150
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
Definition vf_noise.c:244
#define RAND_N(range)
Definition vf_noise.c:69
static const AVFilterPad noise_inputs[]
Definition vf_noise.c:328
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vf_noise.c:133
static const int8_t patt[4]
Definition vf_noise.c:67
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_noise.c:319
void ff_line_noise_avg_c(uint8_t *dst, const uint8_t *src, int len, const int8_t *const *shift)
Definition vf_noise.c:180
void ff_line_noise_c(uint8_t *dst, const uint8_t *src, const int8_t *noise, int len, int shift)
Definition vf_noise.c:167
static void noise(uint8_t *dst, const uint8_t *src, int dst_linesize, int src_linesize, int width, int start, int end, NoiseContext *n, int comp)
Definition vf_noise.c:192
#define NOISE_UNIFORM
Definition vf_noise.h:32
#define NOISE_AVERAGED
Definition vf_noise.h:34
#define NOISE_PATTERN
Definition vf_noise.h:35
#define NOISE_TEMPORAL
Definition vf_noise.h:33
#define MAX_SHIFT
Definition vf_noise.h:29
#define MAX_NOISE
Definition vf_noise.h:28
#define MAX_RES
Definition vf_noise.h:30
void ff_noise_init_x86(NoiseContext *n)
Definition vf_noise.c:107
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
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
int len