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.

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