FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * 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 "video.h"
36 
37 #define MAX_NOISE 4096
38 #define MAX_SHIFT 1024
39 #define MAX_RES (MAX_NOISE-MAX_SHIFT)
40 
41 #define NOISE_UNIFORM 1
42 #define NOISE_TEMPORAL 2
43 #define NOISE_QUALITY 4
44 #define NOISE_AVERAGED 8
45 #define NOISE_PATTERN 16
46 
47 typedef struct {
48  int strength;
49  unsigned flags;
50  int shiftptr;
52  int seed;
53  int8_t *noise;
54  int8_t *prev_shift[MAX_RES][3];
55 } FilterParams;
56 
57 typedef struct {
58  const AVClass *class;
59  int nb_planes;
60  int linesize[4];
61  int height[4];
63  FilterParams param[4];
64  int rand_shift[MAX_RES];
66 } NoiseContext;
67 
68 #define OFFSET(x) offsetof(NoiseContext, x)
69 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
70 
71 #define NOISE_PARAMS(name, x, param) \
72  {#name"_seed", "set component #"#x" noise seed", OFFSET(param.seed), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS}, \
73  {#name"_strength", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
74  {#name"s", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
75  {#name"_flags", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
76  {#name"f", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
77  {"a", "averaged noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_AVERAGED}, 0, 0, FLAGS, #name"_flags"}, \
78  {"p", "(semi)regular pattern", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_PATTERN}, 0, 0, FLAGS, #name"_flags"}, \
79  {"q", "high quality", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_QUALITY}, 0, 0, FLAGS, #name"_flags"}, \
80  {"t", "temporal noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_TEMPORAL}, 0, 0, FLAGS, #name"_flags"}, \
81  {"u", "uniform noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_UNIFORM}, 0, 0, FLAGS, #name"_flags"},
82 
83 static const AVOption noise_options[] = {
84  NOISE_PARAMS(all, 0, all)
85  NOISE_PARAMS(c0, 0, param[0])
86  NOISE_PARAMS(c1, 1, param[1])
87  NOISE_PARAMS(c2, 2, param[2])
88  NOISE_PARAMS(c3, 3, param[3])
89  {NULL}
90 };
91 
93 
94 static const int8_t patt[4] = { -1, 0, 1, 0 };
95 
96 #define RAND_N(range) ((int) ((double) range * av_lfg_get(lfg) / (UINT_MAX + 1.0)))
97 static int init_noise(NoiseContext *n, int comp)
98 {
99  int8_t *noise = av_malloc(MAX_NOISE * sizeof(int8_t));
100  FilterParams *fp = &n->param[comp];
101  AVLFG *lfg = &n->param[comp].lfg;
102  int strength = fp->strength;
103  int flags = fp->flags;
104  int i, j;
105 
106  if (!noise)
107  return AVERROR(ENOMEM);
108 
109  av_lfg_init(&fp->lfg, fp->seed);
110 
111  for (i = 0, j = 0; i < MAX_NOISE; i++, j++) {
112  if (flags & NOISE_UNIFORM) {
113  if (flags & NOISE_AVERAGED) {
114  if (flags & NOISE_PATTERN) {
115  noise[i] = (RAND_N(strength) - strength / 2) / 6
116  + patt[j % 4] * strength * 0.25 / 3;
117  } else {
118  noise[i] = (RAND_N(strength) - strength / 2) / 3;
119  }
120  } else {
121  if (flags & NOISE_PATTERN) {
122  noise[i] = (RAND_N(strength) - strength / 2) / 2
123  + patt[j % 4] * strength * 0.25;
124  } else {
125  noise[i] = RAND_N(strength) - strength / 2;
126  }
127  }
128  } else {
129  double x1, x2, w, y1;
130  do {
131  x1 = 2.0 * av_lfg_get(lfg) / (float)RAND_MAX - 1.0;
132  x2 = 2.0 * av_lfg_get(lfg) / (float)RAND_MAX - 1.0;
133  w = x1 * x1 + x2 * x2;
134  } while (w >= 1.0);
135 
136  w = sqrt((-2.0 * log(w)) / w);
137  y1 = x1 * w;
138  y1 *= strength / sqrt(3.0);
139  if (flags & NOISE_PATTERN) {
140  y1 /= 2;
141  y1 += patt[j % 4] * strength * 0.35;
142  }
143  y1 = av_clipf(y1, -128, 127);
144  if (flags & NOISE_AVERAGED)
145  y1 /= 3.0;
146  noise[i] = (int)y1;
147  }
148  if (RAND_N(6) == 0)
149  j--;
150  }
151 
152  for (i = 0; i < MAX_RES; i++)
153  for (j = 0; j < 3; j++)
154  fp->prev_shift[i][j] = noise + (av_lfg_get(lfg) & (MAX_SHIFT - 1));
155 
156  if (!n->rand_shift_init) {
157  for (i = 0; i < MAX_RES; i++)
158  n->rand_shift[i] = av_lfg_get(lfg) & (MAX_SHIFT - 1);
159  n->rand_shift_init = 1;
160  }
161 
162  fp->noise = noise;
163  fp->shiftptr = 0;
164  return 0;
165 }
166 
167 static av_cold int init(AVFilterContext *ctx, const char *args)
168 {
169  NoiseContext *n = ctx->priv;
170  int ret, i;
171 
172  n->class = &noise_class;
174 
175  if ((ret = av_set_options_string(n, args, "=", ":")) < 0)
176  return ret;
177 
178  for (i = 0; i < 4; i++) {
179  if (n->all.seed >= 0)
180  n->param[i].seed = n->all.seed;
181  else
182  n->param[i].seed = 123457;
183  if (n->all.strength)
184  n->param[i].strength = n->all.strength;
185  if (n->all.flags)
186  n->param[i].flags = n->all.flags;
187  }
188 
189  for (i = 0; i < 4; i++) {
190  if (n->param[i].strength && ((ret = init_noise(n, i)) < 0))
191  return ret;
192  }
193 
194  return 0;
195 }
196 
198 {
200  int fmt;
201 
202  for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
203  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
204  if (desc->flags & PIX_FMT_PLANAR && !((desc->comp[0].depth_minus1 + 1) & 7))
205  ff_add_format(&formats, fmt);
206  }
207 
208  ff_set_common_formats(ctx, formats);
209  return 0;
210 }
211 
212 static int config_input(AVFilterLink *inlink)
213 {
214  NoiseContext *n = inlink->dst->priv;
215  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
216  int i, ret;
217 
218  for (i = 0; i < desc->nb_components; i++)
219  n->nb_planes = FFMAX(n->nb_planes, desc->comp[i].plane);
220  n->nb_planes++;
221 
222  if ((ret = av_image_fill_linesizes(n->linesize, inlink->format, inlink->w)) < 0)
223  return ret;
224 
225  n->height[1] = n->height[2] = inlink->h >> desc->log2_chroma_h;
226  n->height[0] = n->height[3] = inlink->h;
227 
228  return 0;
229 }
230 
231 static void line_noise(uint8_t *dst, const uint8_t *src, int8_t *noise,
232  int len, int shift)
233 {
234  int i;
235 
236  noise += shift;
237  for (i = 0; i < len; i++) {
238  int v = src[i] + noise[i];
239 
240  dst[i] = av_clip_uint8(v);
241  }
242 }
243 
244 static void line_noise_avg(uint8_t *dst, const uint8_t *src,
245  int len, int8_t **shift)
246 {
247  int i;
248  int8_t *src2 = (int8_t*)src;
249 
250  for (i = 0; i < len; i++) {
251  const int n = shift[0][i] + shift[1][i] + shift[2][i];
252  dst[i] = src2[i] + ((n * src2[i]) >> 7);
253  }
254 }
255 
256 static void noise(uint8_t *dst, const uint8_t *src,
257  int dst_linesize, int src_linesize,
258  int width, int height, NoiseContext *n, int comp)
259 {
260  int8_t *noise = n->param[comp].noise;
261  int flags = n->param[comp].flags;
262  AVLFG *lfg = &n->param[comp].lfg;
263  int shift, y;
264 
265  if (!noise) {
266  if (dst != src) {
267  for (y = 0; y < height; y++) {
268  memcpy(dst, src, width);
269  dst += dst_linesize;
270  src += src_linesize;
271  }
272  }
273 
274  return;
275  }
276 
277  for (y = 0; y < height; y++) {
278  if (flags & NOISE_TEMPORAL)
279  shift = av_lfg_get(lfg) & (MAX_SHIFT - 1);
280  else
281  shift = n->rand_shift[y];
282 
283  if (!(flags & NOISE_QUALITY))
284  shift &= ~7;
285 
286  if (flags & NOISE_AVERAGED) {
287  line_noise_avg(dst, src, width, n->param[comp].prev_shift[y]);
288  n->param[comp].prev_shift[y][n->param[comp].shiftptr] = noise + shift;
289  } else {
290  line_noise(dst, src, noise, width, shift);
291  }
292  dst += dst_linesize;
293  src += src_linesize;
294  }
295 
296  n->param[comp].shiftptr++;
297  if (n->param[comp].shiftptr == 3)
298  n->param[comp].shiftptr = 0;
299 }
300 
301 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
302 {
303  NoiseContext *n = inlink->dst->priv;
304  AVFilterLink *outlink = inlink->dst->outputs[0];
306  int ret, i;
307 
308  if (inpicref->perms & AV_PERM_WRITE) {
309  out = inpicref;
310  } else {
311  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
312  if (!out) {
313  avfilter_unref_bufferp(&inpicref);
314  return AVERROR(ENOMEM);
315  }
316  avfilter_copy_buffer_ref_props(out, inpicref);
317  }
318 
319  for (i = 0; i < n->nb_planes; i++)
320  noise(out->data[i], inpicref->data[i], out->linesize[i],
321  inpicref->linesize[i], n->linesize[i], n->height[i], n, i);
322 
323  ret = ff_filter_frame(outlink, out);
324  if (inpicref != out)
325  avfilter_unref_buffer(inpicref);
326  return ret;
327 }
328 
329 static av_cold void uninit(AVFilterContext *ctx)
330 {
331  NoiseContext *n = ctx->priv;
332  int i;
333 
334  for (i = 0; i < 4; i++)
335  av_freep(&n->param[i].noise);
336  av_opt_free(n);
337 }
338 
339 static const AVFilterPad noise_inputs[] = {
340  {
341  .name = "default",
342  .type = AVMEDIA_TYPE_VIDEO,
343  .get_video_buffer = ff_null_get_video_buffer,
344  .filter_frame = filter_frame,
345  .config_props = config_input,
346  .min_perms = AV_PERM_READ,
347  },
348  { NULL }
349 };
350 
351 static const AVFilterPad noise_outputs[] = {
352  {
353  .name = "default",
354  .type = AVMEDIA_TYPE_VIDEO,
355  },
356  { NULL }
357 };
358 
360  .name = "noise",
361  .description = NULL_IF_CONFIG_SMALL("Add noise."),
362  .priv_size = sizeof(NoiseContext),
363  .init = init,
364  .uninit = uninit,
366  .inputs = noise_inputs,
367  .outputs = noise_outputs,
368  .priv_class = &noise_class,
369 };