vf_noise.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. * @file
  23. * noise generator
  24. */
  25. #include "libavutil/opt.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/lfg.h"
  28. #include "libavutil/parseutils.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. #include "vf_noise.h"
  34. #include "video.h"
  35. typedef struct ThreadData {
  36. AVFrame *in, *out;
  37. } ThreadData;
  38. #define OFFSET(x) offsetof(NoiseContext, x)
  39. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  40. #define NOISE_PARAMS(name, x, param) \
  41. {#name"_seed", "set component #"#x" noise seed", OFFSET(param.seed), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS}, \
  42. {#name"_strength", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
  43. {#name"s", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
  44. {#name"_flags", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
  45. {#name"f", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
  46. {"a", "averaged noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_AVERAGED}, 0, 0, FLAGS, #name"_flags"}, \
  47. {"p", "(semi)regular pattern", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_PATTERN}, 0, 0, FLAGS, #name"_flags"}, \
  48. {"t", "temporal noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_TEMPORAL}, 0, 0, FLAGS, #name"_flags"}, \
  49. {"u", "uniform noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_UNIFORM}, 0, 0, FLAGS, #name"_flags"},
  50. static const AVOption noise_options[] = {
  51. NOISE_PARAMS(all, 0, all)
  52. NOISE_PARAMS(c0, 0, param[0])
  53. NOISE_PARAMS(c1, 1, param[1])
  54. NOISE_PARAMS(c2, 2, param[2])
  55. NOISE_PARAMS(c3, 3, param[3])
  56. {NULL}
  57. };
  58. AVFILTER_DEFINE_CLASS(noise);
  59. static const int8_t patt[4] = { -1, 0, 1, 0 };
  60. #define RAND_N(range) ((int) ((double) range * av_lfg_get(lfg) / (UINT_MAX + 1.0)))
  61. static av_cold int init_noise(NoiseContext *n, int comp)
  62. {
  63. int8_t *noise = av_malloc(MAX_NOISE * sizeof(int8_t));
  64. FilterParams *fp = &n->param[comp];
  65. AVLFG *lfg = &n->param[comp].lfg;
  66. int strength = fp->strength;
  67. int flags = fp->flags;
  68. int i, j;
  69. if (!noise)
  70. return AVERROR(ENOMEM);
  71. av_lfg_init(&fp->lfg, fp->seed + comp*31415U);
  72. for (i = 0, j = 0; i < MAX_NOISE; i++, j++) {
  73. if (flags & NOISE_UNIFORM) {
  74. if (flags & NOISE_AVERAGED) {
  75. if (flags & NOISE_PATTERN) {
  76. noise[i] = (RAND_N(strength) - strength / 2) / 6
  77. + patt[j % 4] * strength * 0.25 / 3;
  78. } else {
  79. noise[i] = (RAND_N(strength) - strength / 2) / 3;
  80. }
  81. } else {
  82. if (flags & NOISE_PATTERN) {
  83. noise[i] = (RAND_N(strength) - strength / 2) / 2
  84. + patt[j % 4] * strength * 0.25;
  85. } else {
  86. noise[i] = RAND_N(strength) - strength / 2;
  87. }
  88. }
  89. } else {
  90. double x1, x2, w, y1;
  91. do {
  92. x1 = 2.0 * av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
  93. x2 = 2.0 * av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
  94. w = x1 * x1 + x2 * x2;
  95. } while (w >= 1.0);
  96. w = sqrt((-2.0 * log(w)) / w);
  97. y1 = x1 * w;
  98. y1 *= strength / sqrt(3.0);
  99. if (flags & NOISE_PATTERN) {
  100. y1 /= 2;
  101. y1 += patt[j % 4] * strength * 0.35;
  102. }
  103. y1 = av_clipf(y1, -128, 127);
  104. if (flags & NOISE_AVERAGED)
  105. y1 /= 3.0;
  106. noise[i] = (int)y1;
  107. }
  108. if (RAND_N(6) == 0)
  109. j--;
  110. }
  111. for (i = 0; i < MAX_RES; i++)
  112. for (j = 0; j < 3; j++)
  113. fp->prev_shift[i][j] = noise + (av_lfg_get(lfg) & (MAX_SHIFT - 1));
  114. fp->noise = noise;
  115. return 0;
  116. }
  117. static int query_formats(AVFilterContext *ctx)
  118. {
  119. AVFilterFormats *formats = NULL;
  120. int fmt, ret;
  121. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  122. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  123. if (desc->flags & AV_PIX_FMT_FLAG_PLANAR && !(desc->comp[0].depth & 7)
  124. && (ret = ff_add_format(&formats, fmt)) < 0)
  125. return ret;
  126. }
  127. return ff_set_common_formats(ctx, formats);
  128. }
  129. static int config_input(AVFilterLink *inlink)
  130. {
  131. NoiseContext *n = inlink->dst->priv;
  132. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  133. int ret;
  134. n->nb_planes = av_pix_fmt_count_planes(inlink->format);
  135. if ((ret = av_image_fill_linesizes(n->bytewidth, inlink->format, inlink->w)) < 0)
  136. return ret;
  137. n->height[1] = n->height[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  138. n->height[0] = n->height[3] = inlink->h;
  139. return 0;
  140. }
  141. void ff_line_noise_c(uint8_t *dst, const uint8_t *src, const int8_t *noise,
  142. int len, int shift)
  143. {
  144. int i;
  145. noise += shift;
  146. for (i = 0; i < len; i++) {
  147. int v = src[i] + noise[i];
  148. dst[i] = av_clip_uint8(v);
  149. }
  150. }
  151. void ff_line_noise_avg_c(uint8_t *dst, const uint8_t *src,
  152. int len, const int8_t * const *shift)
  153. {
  154. int i;
  155. const int8_t *src2 = (const int8_t*)src;
  156. for (i = 0; i < len; i++) {
  157. const int n = shift[0][i] + shift[1][i] + shift[2][i];
  158. dst[i] = src2[i] + ((n * src2[i]) >> 7);
  159. }
  160. }
  161. static void noise(uint8_t *dst, const uint8_t *src,
  162. int dst_linesize, int src_linesize,
  163. int width, int start, int end, NoiseContext *n, int comp)
  164. {
  165. FilterParams *p = &n->param[comp];
  166. int8_t *noise = p->noise;
  167. const int flags = p->flags;
  168. int y;
  169. if (!noise) {
  170. if (dst != src)
  171. av_image_copy_plane(dst, dst_linesize, src, src_linesize, width, end - start);
  172. return;
  173. }
  174. for (y = start; y < end; y++) {
  175. const int ix = y & (MAX_RES - 1);
  176. int x;
  177. for (x=0; x < width; x+= MAX_RES) {
  178. int w = FFMIN(width - x, MAX_RES);
  179. int shift = p->rand_shift[ix];
  180. if (flags & NOISE_AVERAGED) {
  181. n->line_noise_avg(dst + x, src + x, w, (const int8_t**)p->prev_shift[ix]);
  182. p->prev_shift[ix][shift & 3] = noise + shift;
  183. } else {
  184. n->line_noise(dst + x, src + x, noise, w, shift);
  185. }
  186. }
  187. dst += dst_linesize;
  188. src += src_linesize;
  189. }
  190. }
  191. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  192. {
  193. NoiseContext *s = ctx->priv;
  194. ThreadData *td = arg;
  195. int plane;
  196. for (plane = 0; plane < s->nb_planes; plane++) {
  197. const int height = s->height[plane];
  198. const int start = (height * jobnr ) / nb_jobs;
  199. const int end = (height * (jobnr+1)) / nb_jobs;
  200. noise(td->out->data[plane] + start * td->out->linesize[plane],
  201. td->in->data[plane] + start * td->in->linesize[plane],
  202. td->out->linesize[plane], td->in->linesize[plane],
  203. s->bytewidth[plane], start, end, s, plane);
  204. }
  205. return 0;
  206. }
  207. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  208. {
  209. AVFilterContext *ctx = inlink->dst;
  210. AVFilterLink *outlink = ctx->outputs[0];
  211. NoiseContext *n = ctx->priv;
  212. ThreadData td;
  213. AVFrame *out;
  214. int comp, i;
  215. if (av_frame_is_writable(inpicref)) {
  216. out = inpicref;
  217. } else {
  218. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  219. if (!out) {
  220. av_frame_free(&inpicref);
  221. return AVERROR(ENOMEM);
  222. }
  223. av_frame_copy_props(out, inpicref);
  224. }
  225. for (comp = 0; comp < 4; comp++) {
  226. FilterParams *fp = &n->param[comp];
  227. if ((!fp->rand_shift_init || (fp->flags & NOISE_TEMPORAL)) && fp->strength) {
  228. for (i = 0; i < MAX_RES; i++) {
  229. fp->rand_shift[i] = av_lfg_get(&fp->lfg) & (MAX_SHIFT - 1);
  230. }
  231. fp->rand_shift_init = 1;
  232. }
  233. }
  234. td.in = inpicref; td.out = out;
  235. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(n->height[0], ctx->graph->nb_threads));
  236. emms_c();
  237. if (inpicref != out)
  238. av_frame_free(&inpicref);
  239. return ff_filter_frame(outlink, out);
  240. }
  241. static av_cold int init(AVFilterContext *ctx)
  242. {
  243. NoiseContext *n = ctx->priv;
  244. int ret, i;
  245. for (i = 0; i < 4; i++) {
  246. if (n->all.seed >= 0)
  247. n->param[i].seed = n->all.seed;
  248. else
  249. n->param[i].seed = 123457;
  250. if (n->all.strength)
  251. n->param[i].strength = n->all.strength;
  252. if (n->all.flags)
  253. n->param[i].flags = n->all.flags;
  254. }
  255. for (i = 0; i < 4; i++) {
  256. if (n->param[i].strength && ((ret = init_noise(n, i)) < 0))
  257. return ret;
  258. }
  259. n->line_noise = ff_line_noise_c;
  260. n->line_noise_avg = ff_line_noise_avg_c;
  261. if (ARCH_X86)
  262. ff_noise_init_x86(n);
  263. return 0;
  264. }
  265. static av_cold void uninit(AVFilterContext *ctx)
  266. {
  267. NoiseContext *n = ctx->priv;
  268. int i;
  269. for (i = 0; i < 4; i++)
  270. av_freep(&n->param[i].noise);
  271. }
  272. static const AVFilterPad noise_inputs[] = {
  273. {
  274. .name = "default",
  275. .type = AVMEDIA_TYPE_VIDEO,
  276. .filter_frame = filter_frame,
  277. .config_props = config_input,
  278. },
  279. { NULL }
  280. };
  281. static const AVFilterPad noise_outputs[] = {
  282. {
  283. .name = "default",
  284. .type = AVMEDIA_TYPE_VIDEO,
  285. },
  286. { NULL }
  287. };
  288. AVFilter ff_vf_noise = {
  289. .name = "noise",
  290. .description = NULL_IF_CONFIG_SMALL("Add noise."),
  291. .priv_size = sizeof(NoiseContext),
  292. .init = init,
  293. .uninit = uninit,
  294. .query_formats = query_formats,
  295. .inputs = noise_inputs,
  296. .outputs = noise_outputs,
  297. .priv_class = &noise_class,
  298. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  299. };