2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2013 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
27 #include "libavutil/opt.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/parseutils.h"
30 #include "libavutil/pixdesc.h"
36 #define MAX_NOISE 4096
37 #define MAX_SHIFT 1024
38 #define MAX_RES (MAX_NOISE-MAX_SHIFT)
40 #define NOISE_UNIFORM 1
41 #define NOISE_TEMPORAL 2
42 #define NOISE_QUALITY 4
43 #define NOISE_AVERAGED 8
44 #define NOISE_PATTERN 16
52 int8_t *prev_shift[MAX_RES][3];
61 FilterParams param[4];
62 int rand_shift[MAX_RES];
66 #define OFFSET(x) offsetof(NoiseContext, x)
67 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
69 #define NOISE_PARAMS(name, x, param) \
70 {#name"_seed", "set component #"#x" noise seed", OFFSET(param.seed), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS}, \
71 {#name"_strength", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
72 {#name"s", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
73 {#name"_flags", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
74 {#name"f", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
75 {"a", "averaged noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_AVERAGED}, 0, 0, FLAGS, #name"_flags"}, \
76 {"p", "(semi)regular pattern", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_PATTERN}, 0, 0, FLAGS, #name"_flags"}, \
77 {"q", "high quality", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_QUALITY}, 0, 0, FLAGS, #name"_flags"}, \
78 {"t", "temporal noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_TEMPORAL}, 0, 0, FLAGS, #name"_flags"}, \
79 {"u", "uniform noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_UNIFORM}, 0, 0, FLAGS, #name"_flags"},
81 static const AVOption noise_options[] = {
82 NOISE_PARAMS(all, 0, all)
83 NOISE_PARAMS(c0, 0, param[0])
84 NOISE_PARAMS(c1, 1, param[1])
85 NOISE_PARAMS(c2, 2, param[2])
86 NOISE_PARAMS(c3, 3, param[3])
90 AVFILTER_DEFINE_CLASS(noise);
92 static const int8_t patt[4] = { -1, 0, 1, 0 };
94 #define RAND_N(range) ((int) ((double) range * rand() / (RAND_MAX + 1.0)))
95 static int init_noise(NoiseContext *n, int comp)
97 int8_t *noise = av_malloc(MAX_NOISE * sizeof(int8_t));
98 FilterParams *fp = &n->param[comp];
99 int strength = fp->strength;
100 int flags = fp->flags;
104 return AVERROR(ENOMEM);
108 for (i = 0, j = 0; i < MAX_NOISE; i++, j++) {
109 if (flags & NOISE_UNIFORM) {
110 if (flags & NOISE_AVERAGED) {
111 if (flags & NOISE_PATTERN) {
112 noise[i] = (RAND_N(strength) - strength / 2) / 6
113 + patt[j % 4] * strength * 0.25 / 3;
115 noise[i] = (RAND_N(strength) - strength / 2) / 3;
118 if (flags & NOISE_PATTERN) {
119 noise[i] = (RAND_N(strength) - strength / 2) / 2
120 + patt[j % 4] * strength * 0.25;
122 noise[i] = RAND_N(strength) - strength / 2;
126 double x1, x2, w, y1;
128 x1 = 2.0 * rand() / (float)RAND_MAX - 1.0;
129 x2 = 2.0 * rand() / (float)RAND_MAX - 1.0;
130 w = x1 * x1 + x2 * x2;
133 w = sqrt((-2.0 * log(w)) / w);
135 y1 *= strength / sqrt(3.0);
136 if (flags & NOISE_PATTERN) {
138 y1 += patt[j % 4] * strength * 0.35;
140 y1 = av_clipf(y1, -128, 127);
141 if (flags & NOISE_AVERAGED)
149 for (i = 0; i < MAX_RES; i++)
150 for (j = 0; j < 3; j++)
151 fp->prev_shift[i][j] = noise + (rand() & (MAX_SHIFT - 1));
153 if (!n->rand_shift_init) {
154 for (i = 0; i < MAX_RES; i++)
155 n->rand_shift[i] = rand() & (MAX_SHIFT - 1);
156 n->rand_shift_init = 1;
164 static av_cold int init(AVFilterContext *ctx, const char *args)
166 NoiseContext *n = ctx->priv;
169 n->class = &noise_class;
170 av_opt_set_defaults(n);
172 if ((ret = av_set_options_string(n, args, "=", ":")) < 0)
175 for (i = 0; i < 4; i++) {
176 if (n->all.seed >= 0)
177 n->param[i].seed = n->all.seed;
179 n->param[i].seed = 123457;
181 n->param[i].strength = n->all.strength;
183 n->param[i].flags = n->all.flags;
186 for (i = 0; i < 4; i++) {
187 if (n->param[i].strength && ((ret = init_noise(n, i)) < 0))
194 static int query_formats(AVFilterContext *ctx)
196 AVFilterFormats *formats = NULL;
199 for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
200 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
201 if (desc->flags & PIX_FMT_PLANAR && !((desc->comp[0].depth_minus1 + 1) & 7))
202 ff_add_format(&formats, fmt);
205 ff_set_common_formats(ctx, formats);
209 static int config_input(AVFilterLink *inlink)
211 NoiseContext *n = inlink->dst->priv;
212 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
215 for (i = 0; i < desc->nb_components; i++)
216 n->nb_planes = FFMAX(n->nb_planes, desc->comp[i].plane);
219 if ((ret = av_image_fill_linesizes(n->linesize, inlink->format, inlink->w)) < 0)
222 n->height[1] = n->height[2] = inlink->h >> desc->log2_chroma_h;
223 n->height[0] = n->height[3] = inlink->h;
228 static void line_noise(uint8_t *dst, const uint8_t *src, int8_t *noise,
234 for (i = 0; i < len; i++) {
235 int v = src[i] + noise[i];
237 dst[i] = av_clip_uint8(v);
241 static void line_noise_avg(uint8_t *dst, const uint8_t *src,
242 int len, int8_t **shift)
245 int8_t *src2 = (int8_t*)src;
247 for (i = 0; i < len; i++) {
248 const int n = shift[0][i] + shift[1][i] + shift[2][i];
249 dst[i] = src2[i] + ((n * src2[i]) >> 7);
253 static void noise(uint8_t *dst, const uint8_t *src,
254 int dst_linesize, int src_linesize,
255 int width, int height, NoiseContext *n, int comp)
257 int8_t *noise = n->param[comp].noise;
258 int flags = n->param[comp].flags;
263 for (y = 0; y < height; y++) {
264 memcpy(dst, src, width);
273 for (y = 0; y < height; y++) {
274 if (flags & NOISE_TEMPORAL)
275 shift = rand() & (MAX_SHIFT - 1);
277 shift = n->rand_shift[y];
279 if (!(flags & NOISE_QUALITY))
282 if (flags & NOISE_AVERAGED) {
283 line_noise_avg(dst, src, width, n->param[comp].prev_shift[y]);
284 n->param[comp].prev_shift[y][n->param[comp].shiftptr] = noise + shift;
286 line_noise(dst, src, noise, width, shift);
292 n->param[comp].shiftptr++;
293 if (n->param[comp].shiftptr == 3)
294 n->param[comp].shiftptr = 0;
297 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
299 NoiseContext *n = inlink->dst->priv;
300 AVFilterLink *outlink = inlink->dst->outputs[0];
301 AVFilterBufferRef *out;
304 if (inpicref->perms & AV_PERM_WRITE) {
307 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
309 avfilter_unref_bufferp(&inpicref);
310 return AVERROR(ENOMEM);
312 avfilter_copy_buffer_ref_props(out, inpicref);
315 for (i = 0; i < n->nb_planes; i++)
316 noise(out->data[i], inpicref->data[i], out->linesize[i],
317 inpicref->linesize[i], n->linesize[i], n->height[i], n, i);
319 ret = ff_filter_frame(outlink, out);
321 avfilter_unref_buffer(inpicref);
325 static av_cold void uninit(AVFilterContext *ctx)
327 NoiseContext *n = ctx->priv;
330 for (i = 0; i < 4; i++)
331 av_freep(&n->param[i].noise);
335 static const AVFilterPad noise_inputs[] = {
338 .type = AVMEDIA_TYPE_VIDEO,
339 .get_video_buffer = ff_null_get_video_buffer,
340 .filter_frame = filter_frame,
341 .config_props = config_input,
342 .min_perms = AV_PERM_READ,
347 static const AVFilterPad noise_outputs[] = {
350 .type = AVMEDIA_TYPE_VIDEO,
355 AVFilter avfilter_vf_noise = {
357 .description = NULL_IF_CONFIG_SMALL("Add noise."),
358 .priv_size = sizeof(NoiseContext),
361 .query_formats = query_formats,
362 .inputs = noise_inputs,
363 .outputs = noise_outputs,
364 .priv_class = &noise_class,