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.

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