vf_noise.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 "libavutil/x86/asm.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. #define MAX_NOISE 5120
  36. #define MAX_SHIFT 1024
  37. #define MAX_RES (MAX_NOISE-MAX_SHIFT)
  38. #define NOISE_UNIFORM 1
  39. #define NOISE_TEMPORAL 2
  40. #define NOISE_AVERAGED 8
  41. #define NOISE_PATTERN 16
  42. typedef struct {
  43. int strength;
  44. unsigned flags;
  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 bytewidth[4];
  54. int height[4];
  55. FilterParams all;
  56. FilterParams param[4];
  57. int rand_shift[MAX_RES];
  58. int rand_shift_init;
  59. void (*line_noise)(uint8_t *dst, const uint8_t *src, const int8_t *noise, int len, int shift);
  60. void (*line_noise_avg)(uint8_t *dst, const uint8_t *src, int len, const int8_t * const *shift);
  61. } NoiseContext;
  62. typedef struct ThreadData {
  63. AVFrame *in, *out;
  64. } ThreadData;
  65. #define OFFSET(x) offsetof(NoiseContext, x)
  66. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  67. #define NOISE_PARAMS(name, x, param) \
  68. {#name"_seed", "set component #"#x" noise seed", OFFSET(param.seed), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS}, \
  69. {#name"_strength", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
  70. {#name"s", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
  71. {#name"_flags", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
  72. {#name"f", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
  73. {"a", "averaged noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_AVERAGED}, 0, 0, FLAGS, #name"_flags"}, \
  74. {"p", "(semi)regular pattern", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_PATTERN}, 0, 0, FLAGS, #name"_flags"}, \
  75. {"t", "temporal noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_TEMPORAL}, 0, 0, FLAGS, #name"_flags"}, \
  76. {"u", "uniform noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_UNIFORM}, 0, 0, FLAGS, #name"_flags"},
  77. static const AVOption noise_options[] = {
  78. NOISE_PARAMS(all, 0, all)
  79. NOISE_PARAMS(c0, 0, param[0])
  80. NOISE_PARAMS(c1, 1, param[1])
  81. NOISE_PARAMS(c2, 2, param[2])
  82. NOISE_PARAMS(c3, 3, param[3])
  83. {NULL}
  84. };
  85. AVFILTER_DEFINE_CLASS(noise);
  86. static const int8_t patt[4] = { -1, 0, 1, 0 };
  87. #define RAND_N(range) ((int) ((double) range * av_lfg_get(lfg) / (UINT_MAX + 1.0)))
  88. static av_cold int init_noise(NoiseContext *n, int comp)
  89. {
  90. int8_t *noise = av_malloc(MAX_NOISE * sizeof(int8_t));
  91. FilterParams *fp = &n->param[comp];
  92. AVLFG *lfg = &n->param[comp].lfg;
  93. int strength = fp->strength;
  94. int flags = fp->flags;
  95. int i, j;
  96. if (!noise)
  97. return AVERROR(ENOMEM);
  98. av_lfg_init(&fp->lfg, fp->seed);
  99. for (i = 0, j = 0; i < MAX_NOISE; i++, j++) {
  100. if (flags & NOISE_UNIFORM) {
  101. if (flags & NOISE_AVERAGED) {
  102. if (flags & NOISE_PATTERN) {
  103. noise[i] = (RAND_N(strength) - strength / 2) / 6
  104. + patt[j % 4] * strength * 0.25 / 3;
  105. } else {
  106. noise[i] = (RAND_N(strength) - strength / 2) / 3;
  107. }
  108. } else {
  109. if (flags & NOISE_PATTERN) {
  110. noise[i] = (RAND_N(strength) - strength / 2) / 2
  111. + patt[j % 4] * strength * 0.25;
  112. } else {
  113. noise[i] = RAND_N(strength) - strength / 2;
  114. }
  115. }
  116. } else {
  117. double x1, x2, w, y1;
  118. do {
  119. x1 = 2.0 * av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
  120. x2 = 2.0 * av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
  121. w = x1 * x1 + x2 * x2;
  122. } while (w >= 1.0);
  123. w = sqrt((-2.0 * log(w)) / w);
  124. y1 = x1 * w;
  125. y1 *= strength / sqrt(3.0);
  126. if (flags & NOISE_PATTERN) {
  127. y1 /= 2;
  128. y1 += patt[j % 4] * strength * 0.35;
  129. }
  130. y1 = av_clipf(y1, -128, 127);
  131. if (flags & NOISE_AVERAGED)
  132. y1 /= 3.0;
  133. noise[i] = (int)y1;
  134. }
  135. if (RAND_N(6) == 0)
  136. j--;
  137. }
  138. for (i = 0; i < MAX_RES; i++)
  139. for (j = 0; j < 3; j++)
  140. fp->prev_shift[i][j] = noise + (av_lfg_get(lfg) & (MAX_SHIFT - 1));
  141. if (!n->rand_shift_init) {
  142. for (i = 0; i < MAX_RES; i++)
  143. n->rand_shift[i] = av_lfg_get(lfg) & (MAX_SHIFT - 1);
  144. n->rand_shift_init = 1;
  145. }
  146. fp->noise = noise;
  147. return 0;
  148. }
  149. static int query_formats(AVFilterContext *ctx)
  150. {
  151. AVFilterFormats *formats = NULL;
  152. int fmt;
  153. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  154. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  155. if (desc->flags & AV_PIX_FMT_FLAG_PLANAR && !((desc->comp[0].depth_minus1 + 1) & 7))
  156. ff_add_format(&formats, fmt);
  157. }
  158. ff_set_common_formats(ctx, formats);
  159. return 0;
  160. }
  161. static int config_input(AVFilterLink *inlink)
  162. {
  163. NoiseContext *n = inlink->dst->priv;
  164. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  165. int ret;
  166. n->nb_planes = av_pix_fmt_count_planes(inlink->format);
  167. if ((ret = av_image_fill_linesizes(n->bytewidth, inlink->format, inlink->w)) < 0)
  168. return ret;
  169. n->height[1] = n->height[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  170. n->height[0] = n->height[3] = inlink->h;
  171. return 0;
  172. }
  173. static inline void line_noise_c(uint8_t *dst, const uint8_t *src, const int8_t *noise,
  174. int len, int shift)
  175. {
  176. int i;
  177. noise += shift;
  178. for (i = 0; i < len; i++) {
  179. int v = src[i] + noise[i];
  180. dst[i] = av_clip_uint8(v);
  181. }
  182. }
  183. #define ASMALIGN(ZEROBITS) ".p2align " #ZEROBITS "\n\t"
  184. static void line_noise_mmx(uint8_t *dst, const uint8_t *src,
  185. const int8_t *noise, int len, int shift)
  186. {
  187. #if HAVE_MMX_INLINE
  188. x86_reg mmx_len= len&(~7);
  189. noise+=shift;
  190. __asm__ volatile(
  191. "mov %3, %%"REG_a" \n\t"
  192. "pcmpeqb %%mm7, %%mm7 \n\t"
  193. "psllw $15, %%mm7 \n\t"
  194. "packsswb %%mm7, %%mm7 \n\t"
  195. ASMALIGN(4)
  196. "1: \n\t"
  197. "movq (%0, %%"REG_a"), %%mm0 \n\t"
  198. "movq (%1, %%"REG_a"), %%mm1 \n\t"
  199. "pxor %%mm7, %%mm0 \n\t"
  200. "paddsb %%mm1, %%mm0 \n\t"
  201. "pxor %%mm7, %%mm0 \n\t"
  202. "movq %%mm0, (%2, %%"REG_a") \n\t"
  203. "add $8, %%"REG_a" \n\t"
  204. " js 1b \n\t"
  205. :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
  206. : "%"REG_a
  207. );
  208. if (mmx_len!=len)
  209. line_noise_c(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
  210. #endif
  211. }
  212. static void line_noise_mmxext(uint8_t *dst, const uint8_t *src,
  213. const int8_t *noise, int len, int shift)
  214. {
  215. #if HAVE_MMXEXT_INLINE
  216. x86_reg mmx_len= len&(~7);
  217. noise+=shift;
  218. __asm__ volatile(
  219. "mov %3, %%"REG_a" \n\t"
  220. "pcmpeqb %%mm7, %%mm7 \n\t"
  221. "psllw $15, %%mm7 \n\t"
  222. "packsswb %%mm7, %%mm7 \n\t"
  223. ASMALIGN(4)
  224. "1: \n\t"
  225. "movq (%0, %%"REG_a"), %%mm0 \n\t"
  226. "movq (%1, %%"REG_a"), %%mm1 \n\t"
  227. "pxor %%mm7, %%mm0 \n\t"
  228. "paddsb %%mm1, %%mm0 \n\t"
  229. "pxor %%mm7, %%mm0 \n\t"
  230. "movntq %%mm0, (%2, %%"REG_a") \n\t"
  231. "add $8, %%"REG_a" \n\t"
  232. " js 1b \n\t"
  233. :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
  234. : "%"REG_a
  235. );
  236. if (mmx_len != len)
  237. line_noise_c(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
  238. #endif
  239. }
  240. static inline void line_noise_avg_c(uint8_t *dst, const uint8_t *src,
  241. int len, const int8_t * const *shift)
  242. {
  243. int i;
  244. const int8_t *src2 = (const int8_t*)src;
  245. for (i = 0; i < len; i++) {
  246. const int n = shift[0][i] + shift[1][i] + shift[2][i];
  247. dst[i] = src2[i] + ((n * src2[i]) >> 7);
  248. }
  249. }
  250. static inline void line_noise_avg_mmx(uint8_t *dst, const uint8_t *src,
  251. int len, const int8_t * const *shift)
  252. {
  253. #if HAVE_MMX_INLINE && HAVE_6REGS
  254. x86_reg mmx_len= len&(~7);
  255. __asm__ volatile(
  256. "mov %5, %%"REG_a" \n\t"
  257. ASMALIGN(4)
  258. "1: \n\t"
  259. "movq (%1, %%"REG_a"), %%mm1 \n\t"
  260. "movq (%0, %%"REG_a"), %%mm0 \n\t"
  261. "paddb (%2, %%"REG_a"), %%mm1 \n\t"
  262. "paddb (%3, %%"REG_a"), %%mm1 \n\t"
  263. "movq %%mm0, %%mm2 \n\t"
  264. "movq %%mm1, %%mm3 \n\t"
  265. "punpcklbw %%mm0, %%mm0 \n\t"
  266. "punpckhbw %%mm2, %%mm2 \n\t"
  267. "punpcklbw %%mm1, %%mm1 \n\t"
  268. "punpckhbw %%mm3, %%mm3 \n\t"
  269. "pmulhw %%mm0, %%mm1 \n\t"
  270. "pmulhw %%mm2, %%mm3 \n\t"
  271. "paddw %%mm1, %%mm1 \n\t"
  272. "paddw %%mm3, %%mm3 \n\t"
  273. "paddw %%mm0, %%mm1 \n\t"
  274. "paddw %%mm2, %%mm3 \n\t"
  275. "psrlw $8, %%mm1 \n\t"
  276. "psrlw $8, %%mm3 \n\t"
  277. "packuswb %%mm3, %%mm1 \n\t"
  278. "movq %%mm1, (%4, %%"REG_a") \n\t"
  279. "add $8, %%"REG_a" \n\t"
  280. " js 1b \n\t"
  281. :: "r" (src+mmx_len), "r" (shift[0]+mmx_len), "r" (shift[1]+mmx_len), "r" (shift[2]+mmx_len),
  282. "r" (dst+mmx_len), "g" (-mmx_len)
  283. : "%"REG_a
  284. );
  285. if (mmx_len != len){
  286. const int8_t *shift2[3]={shift[0]+mmx_len, shift[1]+mmx_len, shift[2]+mmx_len};
  287. line_noise_avg_c(dst+mmx_len, src+mmx_len, len-mmx_len, shift2);
  288. }
  289. #endif
  290. }
  291. static void noise(uint8_t *dst, const uint8_t *src,
  292. int dst_linesize, int src_linesize,
  293. int width, int start, int end, NoiseContext *n, int comp)
  294. {
  295. FilterParams *p = &n->param[comp];
  296. int8_t *noise = p->noise;
  297. const int flags = p->flags;
  298. AVLFG *lfg = &p->lfg;
  299. int shift, y;
  300. if (!noise) {
  301. if (dst != src)
  302. av_image_copy_plane(dst, dst_linesize, src, src_linesize, width, end - start);
  303. return;
  304. }
  305. for (y = start; y < end; y++) {
  306. const int ix = y & (MAX_RES - 1);
  307. if (flags & NOISE_TEMPORAL)
  308. shift = av_lfg_get(lfg) & (MAX_SHIFT - 1);
  309. else
  310. shift = n->rand_shift[ix];
  311. if (flags & NOISE_AVERAGED) {
  312. n->line_noise_avg(dst, src, width, (const int8_t**)p->prev_shift[ix]);
  313. p->prev_shift[ix][shift & 3] = noise + shift;
  314. } else {
  315. n->line_noise(dst, src, noise, width, shift);
  316. }
  317. dst += dst_linesize;
  318. src += src_linesize;
  319. }
  320. }
  321. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  322. {
  323. NoiseContext *s = ctx->priv;
  324. ThreadData *td = arg;
  325. int plane;
  326. for (plane = 0; plane < s->nb_planes; plane++) {
  327. const int height = s->height[plane];
  328. const int start = (height * jobnr ) / nb_jobs;
  329. const int end = (height * (jobnr+1)) / nb_jobs;
  330. noise(td->out->data[plane] + start * td->out->linesize[plane],
  331. td->in->data[plane] + start * td->in->linesize[plane],
  332. td->out->linesize[plane], td->in->linesize[plane],
  333. s->bytewidth[plane], start, end, s, plane);
  334. }
  335. return 0;
  336. }
  337. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  338. {
  339. AVFilterContext *ctx = inlink->dst;
  340. AVFilterLink *outlink = ctx->outputs[0];
  341. NoiseContext *n = ctx->priv;
  342. ThreadData td;
  343. AVFrame *out;
  344. if (av_frame_is_writable(inpicref)) {
  345. out = inpicref;
  346. } else {
  347. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  348. if (!out) {
  349. av_frame_free(&inpicref);
  350. return AVERROR(ENOMEM);
  351. }
  352. av_frame_copy_props(out, inpicref);
  353. }
  354. td.in = inpicref; td.out = out;
  355. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(n->height[0], ctx->graph->nb_threads));
  356. emms_c();
  357. if (inpicref != out)
  358. av_frame_free(&inpicref);
  359. return ff_filter_frame(outlink, out);
  360. }
  361. static av_cold int init(AVFilterContext *ctx)
  362. {
  363. NoiseContext *n = ctx->priv;
  364. int ret, i;
  365. int cpu_flags = av_get_cpu_flags();
  366. for (i = 0; i < 4; i++) {
  367. if (n->all.seed >= 0)
  368. n->param[i].seed = n->all.seed;
  369. else
  370. n->param[i].seed = 123457;
  371. if (n->all.strength)
  372. n->param[i].strength = n->all.strength;
  373. if (n->all.flags)
  374. n->param[i].flags = n->all.flags;
  375. }
  376. for (i = 0; i < 4; i++) {
  377. if (n->param[i].strength && ((ret = init_noise(n, i)) < 0))
  378. return ret;
  379. }
  380. n->line_noise = line_noise_c;
  381. n->line_noise_avg = line_noise_avg_c;
  382. if (HAVE_MMX_INLINE &&
  383. cpu_flags & AV_CPU_FLAG_MMX) {
  384. n->line_noise = line_noise_mmx;
  385. #if HAVE_6REGS
  386. n->line_noise_avg = line_noise_avg_mmx;
  387. #endif
  388. }
  389. if (HAVE_MMXEXT_INLINE &&
  390. cpu_flags & AV_CPU_FLAG_MMXEXT)
  391. n->line_noise = line_noise_mmxext;
  392. return 0;
  393. }
  394. static av_cold void uninit(AVFilterContext *ctx)
  395. {
  396. NoiseContext *n = ctx->priv;
  397. int i;
  398. for (i = 0; i < 4; i++)
  399. av_freep(&n->param[i].noise);
  400. }
  401. static const AVFilterPad noise_inputs[] = {
  402. {
  403. .name = "default",
  404. .type = AVMEDIA_TYPE_VIDEO,
  405. .filter_frame = filter_frame,
  406. .config_props = config_input,
  407. },
  408. { NULL }
  409. };
  410. static const AVFilterPad noise_outputs[] = {
  411. {
  412. .name = "default",
  413. .type = AVMEDIA_TYPE_VIDEO,
  414. },
  415. { NULL }
  416. };
  417. AVFilter ff_vf_noise = {
  418. .name = "noise",
  419. .description = NULL_IF_CONFIG_SMALL("Add noise."),
  420. .priv_size = sizeof(NoiseContext),
  421. .init = init,
  422. .uninit = uninit,
  423. .query_formats = query_formats,
  424. .inputs = noise_inputs,
  425. .outputs = noise_outputs,
  426. .priv_class = &noise_class,
  427. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  428. };