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.

468 lines
15KB

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