FFmpeg  4.4.8
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/opt.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/lfg.h"
30 #include "libavutil/parseutils.h"
31 #include "libavutil/pixdesc.h"
32 #include "avfilter.h"
33 #include "filters.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "vf_noise.h"
37 #include "video.h"
38 
39 typedef struct ThreadData {
40  AVFrame *in, *out;
41 } ThreadData;
42 
43 #define OFFSET(x) offsetof(NoiseContext, x)
44 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
45 
46 #define NOISE_PARAMS(name, x, param) \
47  {#name"_seed", "set component #"#x" noise seed", OFFSET(param.seed), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS}, \
48  {#name"_strength", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
49  {#name"s", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
50  {#name"_flags", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
51  {#name"f", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
52  {"a", "averaged noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_AVERAGED}, 0, 0, FLAGS, #name"_flags"}, \
53  {"p", "(semi)regular pattern", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_PATTERN}, 0, 0, FLAGS, #name"_flags"}, \
54  {"t", "temporal noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_TEMPORAL}, 0, 0, FLAGS, #name"_flags"}, \
55  {"u", "uniform noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_UNIFORM}, 0, 0, FLAGS, #name"_flags"},
56 
57 static const AVOption noise_options[] = {
58  NOISE_PARAMS(all, 0, all)
59  NOISE_PARAMS(c0, 0, param[0])
60  NOISE_PARAMS(c1, 1, param[1])
61  NOISE_PARAMS(c2, 2, param[2])
62  NOISE_PARAMS(c3, 3, param[3])
63  {NULL}
64 };
65 
67 
68 static const int8_t patt[4] = { -1, 0, 1, 0 };
69 
70 #define RAND_N(range) ((int) ((double) range * av_lfg_get(lfg) / (UINT_MAX + 1.0)))
71 static av_cold int init_noise(NoiseContext *n, int comp)
72 {
73  int8_t *noise = av_malloc(MAX_NOISE * sizeof(int8_t));
74  FilterParams *fp = &n->param[comp];
75  AVLFG *lfg = &n->param[comp].lfg;
76  int strength = fp->strength;
77  int flags = fp->flags;
78  int i, j;
79 
80  if (!noise)
81  return AVERROR(ENOMEM);
82 
83  av_lfg_init(&fp->lfg, fp->seed + comp*31415U);
84 
85  for (i = 0, j = 0; i < MAX_NOISE; i++, j++) {
86  if (flags & NOISE_UNIFORM) {
87  if (flags & NOISE_AVERAGED) {
88  if (flags & NOISE_PATTERN) {
89  noise[i] = (RAND_N(strength) - strength / 2) / 6
90  + patt[j % 4] * strength * 0.25 / 3;
91  } else {
92  noise[i] = (RAND_N(strength) - strength / 2) / 3;
93  }
94  } else {
95  if (flags & NOISE_PATTERN) {
96  noise[i] = (RAND_N(strength) - strength / 2) / 2
97  + patt[j % 4] * strength * 0.25;
98  } else {
99  noise[i] = RAND_N(strength) - strength / 2;
100  }
101  }
102  } else {
103  double x1, x2, w, y1;
104  do {
105  x1 = 2.0 * av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
106  x2 = 2.0 * av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
107  w = x1 * x1 + x2 * x2;
108  } while (w >= 1.0);
109 
110  w = sqrt((-2.0 * log(w)) / w);
111  y1 = x1 * w;
112  y1 *= strength / sqrt(3.0);
113  if (flags & NOISE_PATTERN) {
114  y1 /= 2;
115  y1 += patt[j % 4] * strength * 0.35;
116  }
117  y1 = av_clipf(y1, -128, 127);
118  if (flags & NOISE_AVERAGED)
119  y1 /= 3.0;
120  noise[i] = (int)y1;
121  }
122  if (RAND_N(6) == 0)
123  j--;
124  }
125 
126  for (i = 0; i < MAX_RES; i++)
127  for (j = 0; j < 3; j++)
128  fp->prev_shift[i][j] = noise + (av_lfg_get(lfg) & (MAX_SHIFT - 1));
129 
130  fp->noise = noise;
131  return 0;
132 }
133 
135 {
137  int fmt, ret;
138 
139  for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
141  if (desc->flags & AV_PIX_FMT_FLAG_PLANAR && !(desc->comp[0].depth & 7)
142  && (ret = ff_add_format(&formats, fmt)) < 0)
143  return ret;
144  }
145 
147 }
148 
149 static int config_input(AVFilterLink *inlink)
150 {
151  NoiseContext *n = inlink->dst->priv;
153  int ret;
154 
156 
157  if ((ret = av_image_fill_linesizes(n->bytewidth, inlink->format, inlink->w)) < 0)
158  return ret;
159 
160  n->height[1] = n->height[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
161  n->height[0] = n->height[3] = inlink->h;
162 
163  return 0;
164 }
165 
166 void ff_line_noise_c(uint8_t *dst, const uint8_t *src, const int8_t *noise,
167  int len, int shift)
168 {
169  int i;
170 
171  noise += shift;
172  for (i = 0; i < len; i++) {
173  int v = src[i] + noise[i];
174 
175  dst[i] = av_clip_uint8(v);
176  }
177 }
178 
180  int len, const int8_t * const *shift)
181 {
182  int i;
183  const int8_t *src2 = (const int8_t*)src;
184 
185  for (i = 0; i < len; i++) {
186  const int n = shift[0][i] + shift[1][i] + shift[2][i];
187  dst[i] = src2[i] + ((n * src2[i]) >> 7);
188  }
189 }
190 
191 static void noise(uint8_t *dst, const uint8_t *src,
192  int dst_linesize, int src_linesize,
193  int width, int start, int end, NoiseContext *n, int comp)
194 {
195  FilterParams *p = &n->param[comp];
196  int8_t *noise = p->noise;
197  const int flags = p->flags;
198  int y;
199 
200  if (!noise) {
201  if (dst != src)
202  av_image_copy_plane(dst, dst_linesize, src, src_linesize, width, end - start);
203  return;
204  }
205 
206  for (y = start; y < end; y++) {
207  const int ix = y & (MAX_RES - 1);
208  int x;
209  for (x=0; x < width; x+= MAX_RES) {
210  int w = FFMIN(width - x, MAX_RES);
211  int shift = p->rand_shift[ix];
212 
213  if (flags & NOISE_AVERAGED) {
214  n->line_noise_avg(dst + x, src + x, w, (const int8_t**)p->prev_shift[ix]);
215  p->prev_shift[ix][shift & 3] = noise + shift;
216  } else {
217  n->line_noise(dst + x, src + x, noise, w, shift);
218  }
219  }
220  dst += dst_linesize;
221  src += src_linesize;
222  }
223 }
224 
225 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
226 {
227  NoiseContext *s = ctx->priv;
228  ThreadData *td = arg;
229  int plane;
230 
231  for (plane = 0; plane < s->nb_planes; plane++) {
232  const int height = s->height[plane];
233  const int start = ff_slice_pos(height, jobnr, nb_jobs);
234  const int end = ff_slice_pos(height, jobnr + 1, nb_jobs);
235  noise(td->out->data[plane] + start * td->out->linesize[plane],
236  td->in->data[plane] + start * td->in->linesize[plane],
237  td->out->linesize[plane], td->in->linesize[plane],
238  s->bytewidth[plane], start, end, s, plane);
239  }
240  return 0;
241 }
242 
243 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
244 {
245  AVFilterContext *ctx = inlink->dst;
246  AVFilterLink *outlink = ctx->outputs[0];
247  NoiseContext *n = ctx->priv;
248  ThreadData td;
249  AVFrame *out;
250  int comp, i;
251 
252  if (av_frame_is_writable(inpicref)) {
253  out = inpicref;
254  } else {
255  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
256  if (!out) {
257  av_frame_free(&inpicref);
258  return AVERROR(ENOMEM);
259  }
260  av_frame_copy_props(out, inpicref);
261  }
262 
263  for (comp = 0; comp < 4; comp++) {
264  FilterParams *fp = &n->param[comp];
265 
266  if ((!fp->rand_shift_init || (fp->flags & NOISE_TEMPORAL)) && fp->strength) {
267 
268  for (i = 0; i < MAX_RES; i++) {
269  fp->rand_shift[i] = av_lfg_get(&fp->lfg) & (MAX_SHIFT - 1);
270  }
271  fp->rand_shift_init = 1;
272  }
273  }
274 
275  td.in = inpicref; td.out = out;
277  emms_c();
278 
279  if (inpicref != out)
280  av_frame_free(&inpicref);
281  return ff_filter_frame(outlink, out);
282 }
283 
285 {
286  NoiseContext *n = ctx->priv;
287  int ret, i;
288 
289  for (i = 0; i < 4; i++) {
290  if (n->all.seed >= 0)
291  n->param[i].seed = n->all.seed;
292  else
293  n->param[i].seed = 123457;
294  if (n->all.strength)
295  n->param[i].strength = n->all.strength;
296  if (n->all.flags)
297  n->param[i].flags = n->all.flags;
298  }
299 
300  for (i = 0; i < 4; i++) {
301  if (n->param[i].strength && ((ret = init_noise(n, i)) < 0))
302  return ret;
303  }
304 
307 
308  if (ARCH_X86)
310 
311  return 0;
312 }
313 
315 {
316  NoiseContext *n = ctx->priv;
317  int i;
318 
319  for (i = 0; i < 4; i++)
320  av_freep(&n->param[i].noise);
321 }
322 
323 static const AVFilterPad noise_inputs[] = {
324  {
325  .name = "default",
326  .type = AVMEDIA_TYPE_VIDEO,
327  .filter_frame = filter_frame,
328  .config_props = config_input,
329  },
330  { NULL }
331 };
332 
333 static const AVFilterPad noise_outputs[] = {
334  {
335  .name = "default",
336  .type = AVMEDIA_TYPE_VIDEO,
337  },
338  { NULL }
339 };
340 
342  .name = "noise",
343  .description = NULL_IF_CONFIG_SMALL("Add noise."),
344  .priv_size = sizeof(NoiseContext),
345  .init = init,
346  .uninit = uninit,
348  .inputs = noise_inputs,
350  .priv_class = &noise_class,
352 };
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define av_cold
Definition: attributes.h:88
uint8_t
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
Main libavfilter public API header.
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFMIN(a, b)
Definition: common.h:105
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define av_clip_uint8
Definition: common.h:128
#define av_clipf
Definition: common.h:170
#define ARCH_X86
Definition: config.h:39
#define NULL
Definition: coverity.c:32
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:85
int
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:271
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:332
#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:126
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
#define AVERROR(e)
Definition: error.h:43
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:594
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:658
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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:373
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
int i
Definition: input.c:407
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
const char * arg
Definition: jacosubdec.c:66
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define emms_c()
Definition: internal.h:54
const char * desc
Definition: libsvtav1.c:79
uint8_t w
Definition: llviddspenc.c:39
static const uint64_t c2
Definition: murmur3.c:52
static const uint64_t c1
Definition: murmur3.c:51
static const AVClass noise_class
Definition: noise_bsf.c:83
AVOptions.
misc parsing utilities
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:144
#define td
Definition: regdef.h:70
#define fp
Definition: regdef.h:44
formats
Definition: signature.h:48
static int shift(int a, int b)
Definition: sonic.c:82
An instance of a filter.
Definition: avfilter.h:341
void * priv
private data for use by the filter
Definition: avfilter.h:356
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
filter data
Definition: mlp.h:74
int8_t * noise
Definition: vf_noise.h:42
int8_t * prev_shift[MAX_RES][3]
Definition: vf_noise.h:43
int rand_shift[MAX_RES]
Definition: vf_noise.h:44
int strength
Definition: vf_noise.h:38
AVLFG lfg
Definition: vf_noise.h:40
unsigned flags
Definition: vf_noise.h:39
FilterParams all
Definition: vf_noise.h:53
int height[4]
Definition: vf_noise.h:52
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:56
FilterParams param[4]
Definition: vf_noise.h:54
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:55
Used for passing data between threads.
Definition: dsddec.c:67
AVFrame * out
Definition: af_adeclick.c:502
AVFrame * in
Definition: af_adenorm.c:223
#define av_freep(p)
#define av_malloc(s)
#define src
Definition: vp8dsp.c:255
FILE * out
Definition: movenc.c:54
AVFormatContext * ctx
Definition: movenc.c:48
#define height
#define width
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_noise.c:225
#define NOISE_PARAMS(name, x, param)
Definition: vf_noise.c:46
AVFILTER_DEFINE_CLASS(noise)
static const AVOption noise_options[]
Definition: vf_noise.c:57
AVFilter ff_vf_noise
Definition: vf_noise.c:341
static av_cold int init_noise(NoiseContext *n, int comp)
Definition: vf_noise.c:71
static int query_formats(AVFilterContext *ctx)
Definition: vf_noise.c:134
static int config_input(AVFilterLink *inlink)
Definition: vf_noise.c:149
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
Definition: vf_noise.c:243
#define RAND_N(range)
Definition: vf_noise.c:70
static const AVFilterPad noise_inputs[]
Definition: vf_noise.c:323
static av_cold int init(AVFilterContext *ctx)
Definition: vf_noise.c:284
static const AVFilterPad noise_outputs[]
Definition: vf_noise.c:333
static const int8_t patt[4]
Definition: vf_noise.c:68
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_noise.c:314
void ff_line_noise_avg_c(uint8_t *dst, const uint8_t *src, int len, const int8_t *const *shift)
Definition: vf_noise.c:179
void ff_line_noise_c(uint8_t *dst, const uint8_t *src, const int8_t *noise, int len, int shift)
Definition: vf_noise.c:166
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:191
#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
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:104
int len
av_cold void ff_noise_init_x86(NoiseContext *n)
Definition: vf_noise.c:129