FFmpeg
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 "formats.h"
34 #include "internal.h"
35 #include "vf_noise.h"
36 #include "video.h"
37 
38 typedef struct ThreadData {
39  AVFrame *in, *out;
40 } ThreadData;
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, #name"_flags"}, \
50  {#name"f", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
51  {"a", "averaged noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_AVERAGED}, 0, 0, FLAGS, #name"_flags"}, \
52  {"p", "(semi)regular pattern", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_PATTERN}, 0, 0, FLAGS, #name"_flags"}, \
53  {"t", "temporal noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_TEMPORAL}, 0, 0, FLAGS, #name"_flags"}, \
54  {"u", "uniform noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_UNIFORM}, 0, 0, FLAGS, #name"_flags"},
55 
56 static const AVOption noise_options[] = {
57  NOISE_PARAMS(all, 0, all)
58  NOISE_PARAMS(c0, 0, param[0])
59  NOISE_PARAMS(c1, 1, param[1])
60  NOISE_PARAMS(c2, 2, param[2])
61  NOISE_PARAMS(c3, 3, param[3])
62  {NULL}
63 };
64 
66 
67 static 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 {
136  int fmt, ret;
137 
138  for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
140  if (desc->flags & AV_PIX_FMT_FLAG_PLANAR && !(desc->comp[0].depth & 7)
141  && (ret = ff_add_format(&formats, fmt)) < 0)
142  return ret;
143  }
144 
145  return ff_set_common_formats(ctx, formats);
146 }
147 
149 {
150  NoiseContext *n = inlink->dst->priv;
152  int ret;
153 
155 
156  if ((ret = av_image_fill_linesizes(n->bytewidth, inlink->format, inlink->w)) < 0)
157  return ret;
158 
159  n->height[1] = n->height[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
160  n->height[0] = n->height[3] = inlink->h;
161 
162  return 0;
163 }
164 
165 void ff_line_noise_c(uint8_t *dst, const uint8_t *src, const int8_t *noise,
166  int len, int shift)
167 {
168  int i;
169 
170  noise += shift;
171  for (i = 0; i < len; i++) {
172  int v = src[i] + noise[i];
173 
174  dst[i] = av_clip_uint8(v);
175  }
176 }
177 
179  int len, const int8_t * const *shift)
180 {
181  int i;
182  const int8_t *src2 = (const int8_t*)src;
183 
184  for (i = 0; i < len; i++) {
185  const int n = shift[0][i] + shift[1][i] + shift[2][i];
186  dst[i] = src2[i] + ((n * src2[i]) >> 7);
187  }
188 }
189 
190 static void noise(uint8_t *dst, const uint8_t *src,
191  int dst_linesize, int src_linesize,
192  int width, int start, int end, NoiseContext *n, int comp)
193 {
194  FilterParams *p = &n->param[comp];
195  int8_t *noise = p->noise;
196  const int flags = p->flags;
197  int y;
198 
199  if (!noise) {
200  if (dst != src)
201  av_image_copy_plane(dst, dst_linesize, src, src_linesize, width, end - start);
202  return;
203  }
204 
205  for (y = start; y < end; y++) {
206  const int ix = y & (MAX_RES - 1);
207  int x;
208  for (x=0; x < width; x+= MAX_RES) {
209  int w = FFMIN(width - x, MAX_RES);
210  int shift = p->rand_shift[ix];
211 
212  if (flags & NOISE_AVERAGED) {
213  n->line_noise_avg(dst + x, src + x, w, (const int8_t**)p->prev_shift[ix]);
214  p->prev_shift[ix][shift & 3] = noise + shift;
215  } else {
216  n->line_noise(dst + x, src + x, noise, w, shift);
217  }
218  }
219  dst += dst_linesize;
220  src += src_linesize;
221  }
222 }
223 
224 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
225 {
226  NoiseContext *s = ctx->priv;
227  ThreadData *td = arg;
228  int plane;
229 
230  for (plane = 0; plane < s->nb_planes; plane++) {
231  const int height = s->height[plane];
232  const int start = (height * jobnr ) / nb_jobs;
233  const int end = (height * (jobnr+1)) / nb_jobs;
234  noise(td->out->data[plane] + start * td->out->linesize[plane],
235  td->in->data[plane] + start * td->in->linesize[plane],
236  td->out->linesize[plane], td->in->linesize[plane],
237  s->bytewidth[plane], start, end, s, plane);
238  }
239  return 0;
240 }
241 
242 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
243 {
244  AVFilterContext *ctx = inlink->dst;
245  AVFilterLink *outlink = ctx->outputs[0];
246  NoiseContext *n = ctx->priv;
247  ThreadData td;
248  AVFrame *out;
249  int comp, i;
250 
251  if (av_frame_is_writable(inpicref)) {
252  out = inpicref;
253  } else {
254  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
255  if (!out) {
256  av_frame_free(&inpicref);
257  return AVERROR(ENOMEM);
258  }
259  av_frame_copy_props(out, inpicref);
260  }
261 
262  for (comp = 0; comp < 4; comp++) {
263  FilterParams *fp = &n->param[comp];
264 
265  if ((!fp->rand_shift_init || (fp->flags & NOISE_TEMPORAL)) && fp->strength) {
266 
267  for (i = 0; i < MAX_RES; i++) {
268  fp->rand_shift[i] = av_lfg_get(&fp->lfg) & (MAX_SHIFT - 1);
269  }
270  fp->rand_shift_init = 1;
271  }
272  }
273 
274  td.in = inpicref; td.out = out;
275  ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(n->height[0], ff_filter_get_nb_threads(ctx)));
276  emms_c();
277 
278  if (inpicref != out)
279  av_frame_free(&inpicref);
280  return ff_filter_frame(outlink, out);
281 }
282 
284 {
285  NoiseContext *n = ctx->priv;
286  int ret, i;
287 
288  for (i = 0; i < 4; i++) {
289  if (n->all.seed >= 0)
290  n->param[i].seed = n->all.seed;
291  else
292  n->param[i].seed = 123457;
293  if (n->all.strength)
294  n->param[i].strength = n->all.strength;
295  if (n->all.flags)
296  n->param[i].flags = n->all.flags;
297  }
298 
299  for (i = 0; i < 4; i++) {
300  if (n->param[i].strength && ((ret = init_noise(n, i)) < 0))
301  return ret;
302  }
303 
306 
307  if (ARCH_X86)
309 
310  return 0;
311 }
312 
314 {
315  NoiseContext *n = ctx->priv;
316  int i;
317 
318  for (i = 0; i < 4; i++)
319  av_freep(&n->param[i].noise);
320 }
321 
322 static const AVFilterPad noise_inputs[] = {
323  {
324  .name = "default",
325  .type = AVMEDIA_TYPE_VIDEO,
326  .filter_frame = filter_frame,
327  .config_props = config_input,
328  },
329  { NULL }
330 };
331 
332 static const AVFilterPad noise_outputs[] = {
333  {
334  .name = "default",
335  .type = AVMEDIA_TYPE_VIDEO,
336  },
337  { NULL }
338 };
339 
341  .name = "noise",
342  .description = NULL_IF_CONFIG_SMALL("Add noise."),
343  .priv_size = sizeof(NoiseContext),
344  .init = init,
345  .uninit = uninit,
347  .inputs = noise_inputs,
348  .outputs = noise_outputs,
349  .priv_class = &noise_class,
351 };
Definition: lfg.h:27
#define NULL
Definition: coverity.c:32
static const AVClass noise_class
Definition: noise_bsf.c:81
static const AVFilterPad noise_inputs[]
Definition: vf_noise.c:322
AVFrame * out
Definition: af_adeclick.c:485
static int shift(int a, int b)
Definition: sonic.c:82
FilterParams param[4]
Definition: vf_noise.h:54
void ff_line_noise_c(uint8_t *dst, const uint8_t *src, const int8_t *noise, int len, int shift)
Definition: vf_noise.c:165
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2498
This structure describes decoded (raw) audio or video data.
Definition: frame.h:268
AVOption.
Definition: opt.h:246
int height[4]
Definition: vf_noise.h:52
const char * fmt
Definition: avisynth_c.h:769
int nb_planes
Definition: vf_noise.h:50
misc image utilities
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2538
Main libavfilter public API header.
static int config_input(AVFilterLink *inlink)
Definition: vf_noise.c:148
const char * desc
Definition: nvenc.c:68
static const AVOption noise_options[]
Definition: vf_noise.c:56
#define NOISE_TEMPORAL
Definition: vf_noise.h:33
AVLFG lfg
Definition: vf_noise.h:40
static int query_formats(AVFilterContext *ctx)
Definition: vf_noise.c:133
FilterParams all
Definition: vf_noise.h:53
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99
AVFilter ff_vf_noise
Definition: vf_noise.c:340
int rand_shift_init
Definition: vf_noise.h:45
#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:125
const char * name
Pad name.
Definition: internal.h:60
static const AVFilterPad noise_outputs[]
Definition: vf_noise.c:332
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
AVFrame * in
Definition: af_afftdn.c:1082
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
#define MAX_SHIFT
Definition: vf_noise.h:29
AVFILTER_DEFINE_CLASS(noise)
AVOptions.
int strength
Definition: vf_noise.h:38
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
#define NOISE_PATTERN
Definition: vf_noise.h:35
static av_cold int init(AVFilterContext *ctx)
Definition: vf_noise.c:283
int height
Definition: vf_avgblur.c:61
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:190
int plane
Definition: vf_blend.c:58
static const uint64_t c1
Definition: murmur3.c:49
unsigned flags
Definition: vf_noise.h:39
void(* line_noise_avg)(uint8_t *dst, const uint8_t *src, int len, const int8_t *const *shift)
Definition: vf_noise.h:56
A filter pad used for either input or output.
Definition: internal.h:54
#define U(x)
Definition: vp56_arith.h:37
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:568
#define td
Definition: regdef.h:70
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:116
const char * arg
Definition: jacosubdec.c:66
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:337
#define MAX_NOISE
Definition: vf_noise.h:28
#define NOISE_PARAMS(name, x, param)
Definition: vf_noise.c:45
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
#define FFMIN(a, b)
Definition: common.h:96
AVFormatContext * ctx
Definition: movenc.c:48
int8_t * prev_shift[MAX_RES][3]
Definition: vf_noise.h:43
FilterParams * param
Definition: vf_blend.c:60
int8_t * noise
Definition: vf_noise.h:42
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:83
AVFrame * dst
Definition: vf_blend.c:56
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_noise.c:224
int bytewidth[4]
Definition: vf_noise.h:51
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:594
Used for passing data between threads.
Definition: af_adeclick.c:484
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:299
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
static const int8_t patt[4]
Definition: vf_noise.c:67
#define fp
Definition: regdef.h:44
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:47
filter data
Definition: mlp.h:74
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Filter definition.
Definition: avfilter.h:144
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
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
Definition: vf_noise.c:242
static av_cold int init_noise(NoiseContext *n, int comp)
Definition: vf_noise.c:70
const char * name
Filter name.
Definition: avfilter.h:148
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
#define MAX_RES
Definition: vf_noise.h:30
misc parsing utilities
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
#define flags(name, subs,...)
Definition: cbs_av1.c:606
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:282
int rand_shift[MAX_RES]
Definition: vf_noise.h:44
int
avfilter_execute_func * execute
Definition: internal.h:155
const uint8_t * src
Definition: vf_bm3d.c:56
void(* line_noise)(uint8_t *dst, const uint8_t *src, const int8_t *noise, int len, int shift)
Definition: vf_noise.h:55
int len
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
void ff_line_noise_avg_c(uint8_t *dst, const uint8_t *src, int len, const int8_t *const *shift)
Definition: vf_noise.c:178
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
formats
Definition: signature.h:48
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:338
AVFilterLink * inlink
Definition: vf_blend.c:57
internal API functions
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int depth
Number of bits in the component.
Definition: pixdesc.h:58
#define NOISE_AVERAGED
Definition: vf_noise.h:34
#define RAND_N(range)
Definition: vf_noise.c:69
av_cold void ff_noise_init_x86(NoiseContext *n)
Definition: vf_noise.c:129
int src_linesize
Definition: vf_bm3d.c:57
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:144
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:654
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define NOISE_UNIFORM
Definition: vf_noise.h:32
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_noise.c:313