You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

360 lines
11KB

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