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.

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