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"
32 #include "libavutil/x86/asm.h"
38 #define MAX_NOISE 4096
39 #define MAX_SHIFT 1024
40 #define MAX_RES (MAX_NOISE-MAX_SHIFT)
42 #define NOISE_UNIFORM 1
43 #define NOISE_TEMPORAL 2
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];
66 void (*line_noise)(uint8_t *dst, const uint8_t *src, int8_t *noise, int len, int shift);
67 void (*line_noise_avg)(uint8_t *dst, const uint8_t *src, int len, int8_t **shift);
70 #define OFFSET(x) offsetof(NoiseContext, x)
71 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
73 #define NOISE_PARAMS(name, x, param) \
74 {#name"_seed", "set component #"#x" noise seed", OFFSET(param.seed), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS}, \
75 {#name"_strength", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
76 {#name"s", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
77 {#name"_flags", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
78 {#name"f", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
79 {"a", "averaged noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_AVERAGED}, 0, 0, FLAGS, #name"_flags"}, \
80 {"p", "(semi)regular pattern", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_PATTERN}, 0, 0, FLAGS, #name"_flags"}, \
81 {"t", "temporal noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_TEMPORAL}, 0, 0, FLAGS, #name"_flags"}, \
82 {"u", "uniform noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_UNIFORM}, 0, 0, FLAGS, #name"_flags"},
84 static const AVOption noise_options[] = {
85 NOISE_PARAMS(all, 0, all)
86 NOISE_PARAMS(c0, 0, param[0])
87 NOISE_PARAMS(c1, 1, param[1])
88 NOISE_PARAMS(c2, 2, param[2])
89 NOISE_PARAMS(c3, 3, param[3])
93 AVFILTER_DEFINE_CLASS(noise);
95 static const int8_t patt[4] = { -1, 0, 1, 0 };
97 #define RAND_N(range) ((int) ((double) range * av_lfg_get(lfg) / (UINT_MAX + 1.0)))
98 static int init_noise(NoiseContext *n, int comp)
100 int8_t *noise = av_malloc(MAX_NOISE * sizeof(int8_t));
101 FilterParams *fp = &n->param[comp];
102 AVLFG *lfg = &n->param[comp].lfg;
103 int strength = fp->strength;
104 int flags = fp->flags;
108 return AVERROR(ENOMEM);
110 av_lfg_init(&fp->lfg, fp->seed);
112 for (i = 0, j = 0; i < MAX_NOISE; i++, j++) {
113 if (flags & NOISE_UNIFORM) {
114 if (flags & NOISE_AVERAGED) {
115 if (flags & NOISE_PATTERN) {
116 noise[i] = (RAND_N(strength) - strength / 2) / 6
117 + patt[j % 4] * strength * 0.25 / 3;
119 noise[i] = (RAND_N(strength) - strength / 2) / 3;
122 if (flags & NOISE_PATTERN) {
123 noise[i] = (RAND_N(strength) - strength / 2) / 2
124 + patt[j % 4] * strength * 0.25;
126 noise[i] = RAND_N(strength) - strength / 2;
130 double x1, x2, w, y1;
132 x1 = 2.0 * av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
133 x2 = 2.0 * av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
134 w = x1 * x1 + x2 * x2;
137 w = sqrt((-2.0 * log(w)) / w);
139 y1 *= strength / sqrt(3.0);
140 if (flags & NOISE_PATTERN) {
142 y1 += patt[j % 4] * strength * 0.35;
144 y1 = av_clipf(y1, -128, 127);
145 if (flags & NOISE_AVERAGED)
153 for (i = 0; i < MAX_RES; i++)
154 for (j = 0; j < 3; j++)
155 fp->prev_shift[i][j] = noise + (av_lfg_get(lfg) & (MAX_SHIFT - 1));
157 if (!n->rand_shift_init) {
158 for (i = 0; i < MAX_RES; i++)
159 n->rand_shift[i] = av_lfg_get(lfg) & (MAX_SHIFT - 1);
160 n->rand_shift_init = 1;
168 static int query_formats(AVFilterContext *ctx)
170 AVFilterFormats *formats = NULL;
173 for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
174 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
175 if (desc->flags & AV_PIX_FMT_FLAG_PLANAR && !((desc->comp[0].depth_minus1 + 1) & 7))
176 ff_add_format(&formats, fmt);
179 ff_set_common_formats(ctx, formats);
183 static int config_input(AVFilterLink *inlink)
185 NoiseContext *n = inlink->dst->priv;
186 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
189 n->nb_planes = av_pix_fmt_count_planes(inlink->format);
191 if ((ret = av_image_fill_linesizes(n->linesize, inlink->format, inlink->w)) < 0)
194 n->height[1] = n->height[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
195 n->height[0] = n->height[3] = inlink->h;
200 static inline void line_noise_c(uint8_t *dst, const uint8_t *src, int8_t *noise,
206 for (i = 0; i < len; i++) {
207 int v = src[i] + noise[i];
209 dst[i] = av_clip_uint8(v);
213 #define ASMALIGN(ZEROBITS) ".p2align " #ZEROBITS "\n\t"
215 static void line_noise_mmx(uint8_t *dst, const uint8_t *src,
216 int8_t *noise, int len, int shift)
219 x86_reg mmx_len= len&(~7);
223 "mov %3, %%"REG_a" \n\t"
224 "pcmpeqb %%mm7, %%mm7 \n\t"
225 "psllw $15, %%mm7 \n\t"
226 "packsswb %%mm7, %%mm7 \n\t"
229 "movq (%0, %%"REG_a"), %%mm0 \n\t"
230 "movq (%1, %%"REG_a"), %%mm1 \n\t"
231 "pxor %%mm7, %%mm0 \n\t"
232 "paddsb %%mm1, %%mm0 \n\t"
233 "pxor %%mm7, %%mm0 \n\t"
234 "movq %%mm0, (%2, %%"REG_a") \n\t"
235 "add $8, %%"REG_a" \n\t"
237 :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
241 line_noise_c(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
245 static void line_noise_mmxext(uint8_t *dst, const uint8_t *src,
246 int8_t *noise, int len, int shift)
248 #if HAVE_MMXEXT_INLINE
249 x86_reg mmx_len= len&(~7);
253 "mov %3, %%"REG_a" \n\t"
254 "pcmpeqb %%mm7, %%mm7 \n\t"
255 "psllw $15, %%mm7 \n\t"
256 "packsswb %%mm7, %%mm7 \n\t"
259 "movq (%0, %%"REG_a"), %%mm0 \n\t"
260 "movq (%1, %%"REG_a"), %%mm1 \n\t"
261 "pxor %%mm7, %%mm0 \n\t"
262 "paddsb %%mm1, %%mm0 \n\t"
263 "pxor %%mm7, %%mm0 \n\t"
264 "movntq %%mm0, (%2, %%"REG_a") \n\t"
265 "add $8, %%"REG_a" \n\t"
267 :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
271 line_noise_c(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
275 static inline void line_noise_avg_c(uint8_t *dst, const uint8_t *src,
276 int len, int8_t **shift)
279 int8_t *src2 = (int8_t*)src;
281 for (i = 0; i < len; i++) {
282 const int n = shift[0][i] + shift[1][i] + shift[2][i];
283 dst[i] = src2[i] + ((n * src2[i]) >> 7);
287 static inline void line_noise_avg_mmx(uint8_t *dst, const uint8_t *src,
288 int len, int8_t **shift)
291 x86_reg mmx_len= len&(~7);
294 "mov %5, %%"REG_a" \n\t"
297 "movq (%1, %%"REG_a"), %%mm1 \n\t"
298 "movq (%0, %%"REG_a"), %%mm0 \n\t"
299 "paddb (%2, %%"REG_a"), %%mm1 \n\t"
300 "paddb (%3, %%"REG_a"), %%mm1 \n\t"
301 "movq %%mm0, %%mm2 \n\t"
302 "movq %%mm1, %%mm3 \n\t"
303 "punpcklbw %%mm0, %%mm0 \n\t"
304 "punpckhbw %%mm2, %%mm2 \n\t"
305 "punpcklbw %%mm1, %%mm1 \n\t"
306 "punpckhbw %%mm3, %%mm3 \n\t"
307 "pmulhw %%mm0, %%mm1 \n\t"
308 "pmulhw %%mm2, %%mm3 \n\t"
309 "paddw %%mm1, %%mm1 \n\t"
310 "paddw %%mm3, %%mm3 \n\t"
311 "paddw %%mm0, %%mm1 \n\t"
312 "paddw %%mm2, %%mm3 \n\t"
313 "psrlw $8, %%mm1 \n\t"
314 "psrlw $8, %%mm3 \n\t"
315 "packuswb %%mm3, %%mm1 \n\t"
316 "movq %%mm1, (%4, %%"REG_a") \n\t"
317 "add $8, %%"REG_a" \n\t"
319 :: "r" (src+mmx_len), "r" (shift[0]+mmx_len), "r" (shift[1]+mmx_len), "r" (shift[2]+mmx_len),
320 "r" (dst+mmx_len), "g" (-mmx_len)
325 int8_t *shift2[3]={shift[0]+mmx_len, shift[1]+mmx_len, shift[2]+mmx_len};
326 line_noise_avg_c(dst+mmx_len, src+mmx_len, len-mmx_len, shift2);
331 static void noise(uint8_t *dst, const uint8_t *src,
332 int dst_linesize, int src_linesize,
333 int width, int height, NoiseContext *n, int comp)
335 int8_t *noise = n->param[comp].noise;
336 int flags = n->param[comp].flags;
337 AVLFG *lfg = &n->param[comp].lfg;
342 av_image_copy_plane(dst, dst_linesize, src, src_linesize, width, height);
346 for (y = 0; y < height; y++) {
347 if (flags & NOISE_TEMPORAL)
348 shift = av_lfg_get(lfg) & (MAX_SHIFT - 1);
350 shift = n->rand_shift[y];
352 if (flags & NOISE_AVERAGED) {
353 n->line_noise_avg(dst, src, width, n->param[comp].prev_shift[y]);
354 n->param[comp].prev_shift[y][n->param[comp].shiftptr] = noise + shift;
356 n->line_noise(dst, src, noise, width, shift);
362 n->param[comp].shiftptr++;
363 if (n->param[comp].shiftptr == 3)
364 n->param[comp].shiftptr = 0;
367 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
369 NoiseContext *n = inlink->dst->priv;
370 AVFilterLink *outlink = inlink->dst->outputs[0];
374 if (av_frame_is_writable(inpicref)) {
377 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
379 av_frame_free(&inpicref);
380 return AVERROR(ENOMEM);
382 av_frame_copy_props(out, inpicref);
385 for (i = 0; i < n->nb_planes; i++)
386 noise(out->data[i], inpicref->data[i], out->linesize[i],
387 inpicref->linesize[i], n->linesize[i], n->height[i], n, i);
391 av_frame_free(&inpicref);
392 return ff_filter_frame(outlink, out);
395 static av_cold int init(AVFilterContext *ctx)
397 NoiseContext *n = ctx->priv;
399 int cpu_flags = av_get_cpu_flags();
401 for (i = 0; i < 4; i++) {
402 if (n->all.seed >= 0)
403 n->param[i].seed = n->all.seed;
405 n->param[i].seed = 123457;
407 n->param[i].strength = n->all.strength;
409 n->param[i].flags = n->all.flags;
412 for (i = 0; i < 4; i++) {
413 if (n->param[i].strength && ((ret = init_noise(n, i)) < 0))
417 if (HAVE_MMX_INLINE &&
418 cpu_flags & AV_CPU_FLAG_MMX) {
419 n->line_noise = line_noise_mmx;
420 n->line_noise_avg = line_noise_avg_mmx;
422 if (HAVE_MMXEXT_INLINE &&
423 cpu_flags & AV_CPU_FLAG_MMXEXT)
424 n->line_noise = line_noise_mmxext;
429 static av_cold void uninit(AVFilterContext *ctx)
431 NoiseContext *n = ctx->priv;
434 for (i = 0; i < 4; i++)
435 av_freep(&n->param[i].noise);
438 static const AVFilterPad noise_inputs[] = {
441 .type = AVMEDIA_TYPE_VIDEO,
442 .filter_frame = filter_frame,
443 .config_props = config_input,
448 static const AVFilterPad noise_outputs[] = {
451 .type = AVMEDIA_TYPE_VIDEO,
456 AVFilter avfilter_vf_noise = {
458 .description = NULL_IF_CONFIG_SMALL("Add noise."),
459 .priv_size = sizeof(NoiseContext),
462 .query_formats = query_formats,
463 .inputs = noise_inputs,
464 .outputs = noise_outputs,
465 .priv_class = &noise_class,
466 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,