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/lfg.h"
30 #include "libavutil/parseutils.h"
31 #include "libavutil/pixdesc.h"
37 #define MAX_NOISE 4096
38 #define MAX_SHIFT 1024
39 #define MAX_RES (MAX_NOISE-MAX_SHIFT)
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
54 int8_t *prev_shift[MAX_RES][3];
63 FilterParams param[4];
64 int rand_shift[MAX_RES];
68 #define OFFSET(x) offsetof(NoiseContext, x)
69 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
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"},
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])
92 AVFILTER_DEFINE_CLASS(noise);
94 static const int8_t patt[4] = { -1, 0, 1, 0 };
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)
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;
107 return AVERROR(ENOMEM);
109 av_lfg_init(&fp->lfg, fp->seed);
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;
118 noise[i] = (RAND_N(strength) - strength / 2) / 3;
121 if (flags & NOISE_PATTERN) {
122 noise[i] = (RAND_N(strength) - strength / 2) / 2
123 + patt[j % 4] * strength * 0.25;
125 noise[i] = RAND_N(strength) - strength / 2;
129 double x1, x2, w, y1;
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;
136 w = sqrt((-2.0 * log(w)) / w);
138 y1 *= strength / sqrt(3.0);
139 if (flags & NOISE_PATTERN) {
141 y1 += patt[j % 4] * strength * 0.35;
143 y1 = av_clipf(y1, -128, 127);
144 if (flags & NOISE_AVERAGED)
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));
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;
167 static av_cold int init(AVFilterContext *ctx, const char *args)
169 NoiseContext *n = ctx->priv;
172 n->class = &noise_class;
173 av_opt_set_defaults(n);
175 if ((ret = av_set_options_string(n, args, "=", ":")) < 0)
178 for (i = 0; i < 4; i++) {
179 if (n->all.seed >= 0)
180 n->param[i].seed = n->all.seed;
182 n->param[i].seed = 123457;
184 n->param[i].strength = n->all.strength;
186 n->param[i].flags = n->all.flags;
189 for (i = 0; i < 4; i++) {
190 if (n->param[i].strength && ((ret = init_noise(n, i)) < 0))
197 static int query_formats(AVFilterContext *ctx)
199 AVFilterFormats *formats = NULL;
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);
208 ff_set_common_formats(ctx, formats);
212 static int config_input(AVFilterLink *inlink)
214 NoiseContext *n = inlink->dst->priv;
215 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
218 for (i = 0; i < desc->nb_components; i++)
219 n->nb_planes = FFMAX(n->nb_planes, desc->comp[i].plane);
222 if ((ret = av_image_fill_linesizes(n->linesize, inlink->format, inlink->w)) < 0)
225 n->height[1] = n->height[2] = inlink->h >> desc->log2_chroma_h;
226 n->height[0] = n->height[3] = inlink->h;
231 static void line_noise(uint8_t *dst, const uint8_t *src, int8_t *noise,
237 for (i = 0; i < len; i++) {
238 int v = src[i] + noise[i];
240 dst[i] = av_clip_uint8(v);
244 static void line_noise_avg(uint8_t *dst, const uint8_t *src,
245 int len, int8_t **shift)
248 int8_t *src2 = (int8_t*)src;
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);
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)
260 int8_t *noise = n->param[comp].noise;
261 int flags = n->param[comp].flags;
262 AVLFG *lfg = &n->param[comp].lfg;
267 for (y = 0; y < height; y++) {
268 memcpy(dst, src, width);
277 for (y = 0; y < height; y++) {
278 if (flags & NOISE_TEMPORAL)
279 shift = av_lfg_get(lfg) & (MAX_SHIFT - 1);
281 shift = n->rand_shift[y];
283 if (!(flags & NOISE_QUALITY))
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;
290 line_noise(dst, src, noise, width, shift);
296 n->param[comp].shiftptr++;
297 if (n->param[comp].shiftptr == 3)
298 n->param[comp].shiftptr = 0;
301 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
303 NoiseContext *n = inlink->dst->priv;
304 AVFilterLink *outlink = inlink->dst->outputs[0];
305 AVFilterBufferRef *out;
308 if (inpicref->perms & AV_PERM_WRITE) {
311 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
313 avfilter_unref_bufferp(&inpicref);
314 return AVERROR(ENOMEM);
316 avfilter_copy_buffer_ref_props(out, inpicref);
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);
323 ret = ff_filter_frame(outlink, out);
325 avfilter_unref_buffer(inpicref);
329 static av_cold void uninit(AVFilterContext *ctx)
331 NoiseContext *n = ctx->priv;
334 for (i = 0; i < 4; i++)
335 av_freep(&n->param[i].noise);
339 static const AVFilterPad noise_inputs[] = {
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,
351 static const AVFilterPad noise_outputs[] = {
354 .type = AVMEDIA_TYPE_VIDEO,
359 AVFilter avfilter_vf_noise = {
361 .description = NULL_IF_CONFIG_SMALL("Add noise."),
362 .priv_size = sizeof(NoiseContext),
365 .query_formats = query_formats,
366 .inputs = noise_inputs,
367 .outputs = noise_outputs,
368 .priv_class = &noise_class,