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.

498 lines
16KB

  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. fp->noise = noise;
  142. return 0;
  143. }
  144. static int query_formats(AVFilterContext *ctx)
  145. {
  146. AVFilterFormats *formats = NULL;
  147. int fmt;
  148. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  149. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  150. if (desc->flags & AV_PIX_FMT_FLAG_PLANAR && !((desc->comp[0].depth_minus1 + 1) & 7))
  151. ff_add_format(&formats, fmt);
  152. }
  153. ff_set_common_formats(ctx, formats);
  154. return 0;
  155. }
  156. static int config_input(AVFilterLink *inlink)
  157. {
  158. NoiseContext *n = inlink->dst->priv;
  159. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  160. int ret;
  161. n->nb_planes = av_pix_fmt_count_planes(inlink->format);
  162. if ((ret = av_image_fill_linesizes(n->bytewidth, inlink->format, inlink->w)) < 0)
  163. return ret;
  164. n->height[1] = n->height[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  165. n->height[0] = n->height[3] = inlink->h;
  166. return 0;
  167. }
  168. static inline void line_noise_c(uint8_t *dst, const uint8_t *src, const int8_t *noise,
  169. int len, int shift)
  170. {
  171. int i;
  172. noise += shift;
  173. for (i = 0; i < len; i++) {
  174. int v = src[i] + noise[i];
  175. dst[i] = av_clip_uint8(v);
  176. }
  177. }
  178. #define ASMALIGN(ZEROBITS) ".p2align " #ZEROBITS "\n\t"
  179. static void line_noise_mmx(uint8_t *dst, const uint8_t *src,
  180. const int8_t *noise, int len, int shift)
  181. {
  182. #if HAVE_MMX_INLINE
  183. x86_reg mmx_len= len&(~7);
  184. noise+=shift;
  185. __asm__ volatile(
  186. "mov %3, %%"REG_a" \n\t"
  187. "pcmpeqb %%mm7, %%mm7 \n\t"
  188. "psllw $15, %%mm7 \n\t"
  189. "packsswb %%mm7, %%mm7 \n\t"
  190. ASMALIGN(4)
  191. "1: \n\t"
  192. "movq (%0, %%"REG_a"), %%mm0 \n\t"
  193. "movq (%1, %%"REG_a"), %%mm1 \n\t"
  194. "pxor %%mm7, %%mm0 \n\t"
  195. "paddsb %%mm1, %%mm0 \n\t"
  196. "pxor %%mm7, %%mm0 \n\t"
  197. "movq %%mm0, (%2, %%"REG_a") \n\t"
  198. "add $8, %%"REG_a" \n\t"
  199. " js 1b \n\t"
  200. :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
  201. : "%"REG_a
  202. );
  203. if (mmx_len!=len)
  204. line_noise_c(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
  205. #endif
  206. }
  207. static void line_noise_mmxext(uint8_t *dst, const uint8_t *src,
  208. const int8_t *noise, int len, int shift)
  209. {
  210. #if HAVE_MMXEXT_INLINE
  211. x86_reg mmx_len= len&(~7);
  212. noise+=shift;
  213. __asm__ volatile(
  214. "mov %3, %%"REG_a" \n\t"
  215. "pcmpeqb %%mm7, %%mm7 \n\t"
  216. "psllw $15, %%mm7 \n\t"
  217. "packsswb %%mm7, %%mm7 \n\t"
  218. ASMALIGN(4)
  219. "1: \n\t"
  220. "movq (%0, %%"REG_a"), %%mm0 \n\t"
  221. "movq (%1, %%"REG_a"), %%mm1 \n\t"
  222. "pxor %%mm7, %%mm0 \n\t"
  223. "paddsb %%mm1, %%mm0 \n\t"
  224. "pxor %%mm7, %%mm0 \n\t"
  225. "movntq %%mm0, (%2, %%"REG_a") \n\t"
  226. "add $8, %%"REG_a" \n\t"
  227. " js 1b \n\t"
  228. :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
  229. : "%"REG_a
  230. );
  231. if (mmx_len != len)
  232. line_noise_c(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
  233. #endif
  234. }
  235. static inline void line_noise_avg_c(uint8_t *dst, const uint8_t *src,
  236. int len, const int8_t * const *shift)
  237. {
  238. int i;
  239. const int8_t *src2 = (const int8_t*)src;
  240. for (i = 0; i < len; i++) {
  241. const int n = shift[0][i] + shift[1][i] + shift[2][i];
  242. dst[i] = src2[i] + ((n * src2[i]) >> 7);
  243. }
  244. }
  245. static inline void line_noise_avg_mmx(uint8_t *dst, const uint8_t *src,
  246. int len, const int8_t * const *shift)
  247. {
  248. #if HAVE_MMX_INLINE && HAVE_6REGS
  249. x86_reg mmx_len= len&(~7);
  250. __asm__ volatile(
  251. "mov %5, %%"REG_a" \n\t"
  252. ASMALIGN(4)
  253. "1: \n\t"
  254. "movq (%1, %%"REG_a"), %%mm1 \n\t"
  255. "movq (%0, %%"REG_a"), %%mm0 \n\t"
  256. "paddb (%2, %%"REG_a"), %%mm1 \n\t"
  257. "paddb (%3, %%"REG_a"), %%mm1 \n\t"
  258. "movq %%mm0, %%mm2 \n\t"
  259. "movq %%mm1, %%mm3 \n\t"
  260. "punpcklbw %%mm0, %%mm0 \n\t"
  261. "punpckhbw %%mm2, %%mm2 \n\t"
  262. "punpcklbw %%mm1, %%mm1 \n\t"
  263. "punpckhbw %%mm3, %%mm3 \n\t"
  264. "pmulhw %%mm0, %%mm1 \n\t"
  265. "pmulhw %%mm2, %%mm3 \n\t"
  266. "paddw %%mm1, %%mm1 \n\t"
  267. "paddw %%mm3, %%mm3 \n\t"
  268. "paddw %%mm0, %%mm1 \n\t"
  269. "paddw %%mm2, %%mm3 \n\t"
  270. "psrlw $8, %%mm1 \n\t"
  271. "psrlw $8, %%mm3 \n\t"
  272. "packuswb %%mm3, %%mm1 \n\t"
  273. "movq %%mm1, (%4, %%"REG_a") \n\t"
  274. "add $8, %%"REG_a" \n\t"
  275. " js 1b \n\t"
  276. :: "r" (src+mmx_len), "r" (shift[0]+mmx_len), "r" (shift[1]+mmx_len), "r" (shift[2]+mmx_len),
  277. "r" (dst+mmx_len), "g" (-mmx_len)
  278. : "%"REG_a
  279. );
  280. if (mmx_len != len){
  281. const int8_t *shift2[3]={shift[0]+mmx_len, shift[1]+mmx_len, shift[2]+mmx_len};
  282. line_noise_avg_c(dst+mmx_len, src+mmx_len, len-mmx_len, shift2);
  283. }
  284. #endif
  285. }
  286. static void noise(uint8_t *dst, const uint8_t *src,
  287. int dst_linesize, int src_linesize,
  288. int width, int start, int end, NoiseContext *n, int comp)
  289. {
  290. FilterParams *p = &n->param[comp];
  291. int8_t *noise = p->noise;
  292. const int flags = p->flags;
  293. int y;
  294. if (!noise) {
  295. if (dst != src)
  296. av_image_copy_plane(dst, dst_linesize, src, src_linesize, width, end - start);
  297. return;
  298. }
  299. for (y = start; y < end; y++) {
  300. const int ix = y & (MAX_RES - 1);
  301. int x;
  302. for (x=0; x < width; x+= MAX_RES) {
  303. int w = FFMIN(width - x, MAX_RES);
  304. int shift = n->rand_shift[ix];
  305. if (flags & NOISE_AVERAGED) {
  306. n->line_noise_avg(dst + x, src + x, w, (const int8_t**)p->prev_shift[ix]);
  307. p->prev_shift[ix][shift & 3] = noise + shift;
  308. } else {
  309. n->line_noise(dst + x, src + x, noise, w, shift);
  310. }
  311. }
  312. dst += dst_linesize;
  313. src += src_linesize;
  314. }
  315. }
  316. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  317. {
  318. NoiseContext *s = ctx->priv;
  319. ThreadData *td = arg;
  320. int plane;
  321. for (plane = 0; plane < s->nb_planes; plane++) {
  322. const int height = s->height[plane];
  323. const int start = (height * jobnr ) / nb_jobs;
  324. const int end = (height * (jobnr+1)) / nb_jobs;
  325. noise(td->out->data[plane] + start * td->out->linesize[plane],
  326. td->in->data[plane] + start * td->in->linesize[plane],
  327. td->out->linesize[plane], td->in->linesize[plane],
  328. s->bytewidth[plane], start, end, s, plane);
  329. }
  330. return 0;
  331. }
  332. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  333. {
  334. AVFilterContext *ctx = inlink->dst;
  335. AVFilterLink *outlink = ctx->outputs[0];
  336. NoiseContext *n = ctx->priv;
  337. ThreadData td;
  338. AVFrame *out;
  339. int comp, i;
  340. if (av_frame_is_writable(inpicref)) {
  341. out = inpicref;
  342. } else {
  343. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  344. if (!out) {
  345. av_frame_free(&inpicref);
  346. return AVERROR(ENOMEM);
  347. }
  348. av_frame_copy_props(out, inpicref);
  349. }
  350. for (comp = 0; comp < 4; comp++) {
  351. FilterParams *fp = &n->param[comp];
  352. if ((!n->rand_shift_init || (fp->flags & NOISE_TEMPORAL)) && fp->strength) {
  353. for (i = 0; i < MAX_RES; i++) {
  354. n->rand_shift[i] = av_lfg_get(&fp->lfg) & (MAX_SHIFT - 1);
  355. }
  356. n->rand_shift_init = 1;
  357. }
  358. }
  359. td.in = inpicref; td.out = out;
  360. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(n->height[0], ctx->graph->nb_threads));
  361. emms_c();
  362. if (inpicref != out)
  363. av_frame_free(&inpicref);
  364. return ff_filter_frame(outlink, out);
  365. }
  366. static av_cold int init(AVFilterContext *ctx)
  367. {
  368. NoiseContext *n = ctx->priv;
  369. int ret, i;
  370. int cpu_flags = av_get_cpu_flags();
  371. for (i = 0; i < 4; i++) {
  372. if (n->all.seed >= 0)
  373. n->param[i].seed = n->all.seed;
  374. else
  375. n->param[i].seed = 123457;
  376. if (n->all.strength)
  377. n->param[i].strength = n->all.strength;
  378. if (n->all.flags)
  379. n->param[i].flags = n->all.flags;
  380. }
  381. for (i = 0; i < 4; i++) {
  382. if (n->param[i].strength && ((ret = init_noise(n, i)) < 0))
  383. return ret;
  384. }
  385. n->line_noise = line_noise_c;
  386. n->line_noise_avg = line_noise_avg_c;
  387. if (HAVE_MMX_INLINE &&
  388. cpu_flags & AV_CPU_FLAG_MMX) {
  389. n->line_noise = line_noise_mmx;
  390. #if HAVE_6REGS
  391. n->line_noise_avg = line_noise_avg_mmx;
  392. #endif
  393. }
  394. if (HAVE_MMXEXT_INLINE &&
  395. cpu_flags & AV_CPU_FLAG_MMXEXT)
  396. n->line_noise = line_noise_mmxext;
  397. return 0;
  398. }
  399. static av_cold void uninit(AVFilterContext *ctx)
  400. {
  401. NoiseContext *n = ctx->priv;
  402. int i;
  403. for (i = 0; i < 4; i++)
  404. av_freep(&n->param[i].noise);
  405. }
  406. static const AVFilterPad noise_inputs[] = {
  407. {
  408. .name = "default",
  409. .type = AVMEDIA_TYPE_VIDEO,
  410. .filter_frame = filter_frame,
  411. .config_props = config_input,
  412. },
  413. { NULL }
  414. };
  415. static const AVFilterPad noise_outputs[] = {
  416. {
  417. .name = "default",
  418. .type = AVMEDIA_TYPE_VIDEO,
  419. },
  420. { NULL }
  421. };
  422. AVFilter ff_vf_noise = {
  423. .name = "noise",
  424. .description = NULL_IF_CONFIG_SMALL("Add noise."),
  425. .priv_size = sizeof(NoiseContext),
  426. .init = init,
  427. .uninit = uninit,
  428. .query_formats = query_formats,
  429. .inputs = noise_inputs,
  430. .outputs = noise_outputs,
  431. .priv_class = &noise_class,
  432. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  433. };