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.

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