vf_amplify.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (c) 2018 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/imgutils.h"
  21. #include "libavutil/intreadwrite.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct AmplifyContext {
  29. const AVClass *class;
  30. const AVPixFmtDescriptor *desc;
  31. int radius;
  32. float factor;
  33. float threshold;
  34. float tolerance;
  35. int planes;
  36. int llimit;
  37. int hlimit;
  38. int nb_inputs;
  39. int nb_frames;
  40. int depth;
  41. int nb_planes;
  42. int linesize[4];
  43. int height[4];
  44. AVFrame **frames;
  45. } AmplifyContext;
  46. static int query_formats(AVFilterContext *ctx)
  47. {
  48. static const enum AVPixelFormat pixel_fmts[] = {
  49. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
  50. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
  51. AV_PIX_FMT_GRAY16,
  52. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  53. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  54. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  55. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  56. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  57. AV_PIX_FMT_YUVJ411P,
  58. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  59. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  60. AV_PIX_FMT_YUV440P10,
  61. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  62. AV_PIX_FMT_YUV440P12,
  63. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  64. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  65. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  66. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  67. AV_PIX_FMT_NONE
  68. };
  69. AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
  70. if (!formats)
  71. return AVERROR(ENOMEM);
  72. return ff_set_common_formats(ctx, formats);
  73. }
  74. static av_cold int init(AVFilterContext *ctx)
  75. {
  76. AmplifyContext *s = ctx->priv;
  77. s->nb_inputs = s->radius * 2 + 1;
  78. s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames));
  79. if (!s->frames)
  80. return AVERROR(ENOMEM);
  81. return 0;
  82. }
  83. typedef struct ThreadData {
  84. AVFrame **in, *out;
  85. } ThreadData;
  86. static int amplify_frame(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  87. {
  88. AmplifyContext *s = ctx->priv;
  89. ThreadData *td = arg;
  90. AVFrame **in = td->in;
  91. AVFrame *out = td->out;
  92. const int radius = s->radius;
  93. const int nb_inputs = s->nb_inputs;
  94. const float threshold = s->threshold;
  95. const float tolerance = s->tolerance;
  96. const float factor = s->factor;
  97. const int llimit = s->llimit;
  98. const int hlimit = s->hlimit;
  99. const int depth = s->depth;
  100. int i, p, x, y;
  101. if (s->depth <= 8) {
  102. for (p = 0; p < s->nb_planes; p++) {
  103. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  104. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  105. uint8_t *dst = out->data[p] + slice_start * out->linesize[p];
  106. if (!((1 << p) & s->planes)) {
  107. av_image_copy_plane(dst, out->linesize[p],
  108. in[radius]->data[p] + slice_start * in[radius]->linesize[p],
  109. in[radius]->linesize[p],
  110. s->linesize[p], slice_end - slice_start);
  111. continue;
  112. }
  113. for (y = slice_start; y < slice_end; y++) {
  114. for (x = 0; x < s->linesize[p]; x++) {
  115. int src = in[radius]->data[p][y * in[radius]->linesize[p] + x];
  116. float diff, avg;
  117. int sum = 0;
  118. for (i = 0; i < nb_inputs; i++) {
  119. sum += in[i]->data[p][y * in[i]->linesize[p] + x];
  120. }
  121. avg = sum / (float)nb_inputs;
  122. diff = src - avg;
  123. if (fabsf(diff) < threshold && fabsf(diff) > tolerance) {
  124. int amp;
  125. if (diff < 0) {
  126. amp = -FFMIN(FFABS(diff * factor), llimit);
  127. } else {
  128. amp = FFMIN(FFABS(diff * factor), hlimit);
  129. }
  130. dst[x] = av_clip_uint8(src + amp);
  131. } else {
  132. dst[x] = src;
  133. }
  134. }
  135. dst += out->linesize[p];
  136. }
  137. }
  138. } else {
  139. for (p = 0; p < s->nb_planes; p++) {
  140. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  141. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  142. uint16_t *dst = (uint16_t *)(out->data[p] + slice_start * out->linesize[p]);
  143. if (!((1 << p) & s->planes)) {
  144. av_image_copy_plane((uint8_t *)dst, out->linesize[p],
  145. in[radius]->data[p] + slice_start * in[radius]->linesize[p],
  146. in[radius]->linesize[p],
  147. s->linesize[p], slice_end - slice_start);
  148. continue;
  149. }
  150. for (y = slice_start; y < slice_end; y++) {
  151. for (x = 0; x < s->linesize[p] / 2; x++) {
  152. int src = AV_RN16(in[radius]->data[p] + y * in[radius]->linesize[p] + x * 2);
  153. float diff, avg;
  154. int sum = 0;
  155. for (i = 0; i < nb_inputs; i++) {
  156. sum += AV_RN16(in[i]->data[p] + y * in[i]->linesize[p] + x * 2);
  157. }
  158. avg = sum / (float)nb_inputs;
  159. diff = src - avg;
  160. if (fabsf(diff) < threshold && fabsf(diff) > tolerance) {
  161. int amp;
  162. if (diff < 0) {
  163. amp = -FFMIN(FFABS(diff * factor), llimit);
  164. } else {
  165. amp = FFMIN(FFABS(diff * factor), hlimit);
  166. }
  167. dst[x] = av_clip_uintp2_c(src + amp, depth);
  168. } else {
  169. dst[x] = src;
  170. }
  171. }
  172. dst += out->linesize[p] / 2;
  173. }
  174. }
  175. }
  176. return 0;
  177. }
  178. static int config_output(AVFilterLink *outlink)
  179. {
  180. AVFilterContext *ctx = outlink->src;
  181. AmplifyContext *s = ctx->priv;
  182. AVFilterLink *inlink = ctx->inputs[0];
  183. int ret;
  184. s->desc = av_pix_fmt_desc_get(outlink->format);
  185. if (!s->desc)
  186. return AVERROR_BUG;
  187. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  188. s->depth = s->desc->comp[0].depth;
  189. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  190. return ret;
  191. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  192. s->height[0] = s->height[3] = inlink->h;
  193. return 0;
  194. }
  195. static av_cold void uninit(AVFilterContext *ctx)
  196. {
  197. AmplifyContext *s = ctx->priv;
  198. int i;
  199. if (s->frames) {
  200. for (i = 0; i < s->nb_frames; i++)
  201. av_frame_free(&s->frames[i]);
  202. }
  203. av_freep(&s->frames);
  204. }
  205. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  206. {
  207. AVFilterContext *ctx = inlink->dst;
  208. AVFilterLink *outlink = ctx->outputs[0];
  209. AmplifyContext *s = ctx->priv;
  210. ThreadData td;
  211. AVFrame *out;
  212. if (s->nb_frames < s->nb_inputs) {
  213. s->frames[s->nb_frames] = in;
  214. s->nb_frames++;
  215. return 0;
  216. } else {
  217. av_frame_free(&s->frames[0]);
  218. memmove(&s->frames[0], &s->frames[1], sizeof(*s->frames) * (s->nb_inputs - 1));
  219. s->frames[s->nb_inputs - 1] = in;
  220. }
  221. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  222. if (!out)
  223. return AVERROR(ENOMEM);
  224. out->pts = s->frames[0]->pts;
  225. td.out = out;
  226. td.in = s->frames;
  227. ctx->internal->execute(ctx, amplify_frame, &td, NULL, FFMIN(s->height[1], ff_filter_get_nb_threads(ctx)));
  228. return ff_filter_frame(outlink, out);
  229. }
  230. #define OFFSET(x) offsetof(AmplifyContext, x)
  231. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  232. static const AVOption amplify_options[] = {
  233. { "radius", "set radius", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=2}, 1, 63, .flags = FLAGS },
  234. { "factor", "set factor", OFFSET(factor), AV_OPT_TYPE_FLOAT, {.dbl=2}, 0, UINT16_MAX, .flags = FLAGS },
  235. { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl=10}, 0, UINT16_MAX, .flags = FLAGS },
  236. { "tolerance", "set tolerance", OFFSET(tolerance), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, UINT16_MAX, .flags = FLAGS },
  237. { "low", "set low limit for amplification", OFFSET(llimit), AV_OPT_TYPE_INT, {.i64=UINT16_MAX}, 0, UINT16_MAX, .flags = FLAGS },
  238. { "high", "set high limit for amplification", OFFSET(hlimit), AV_OPT_TYPE_INT, {.i64=UINT16_MAX}, 0, UINT16_MAX, .flags = FLAGS },
  239. { "planes", "set what planes to filter", OFFSET(planes), AV_OPT_TYPE_FLAGS, {.i64=7}, 0, 15, FLAGS },
  240. { NULL },
  241. };
  242. static const AVFilterPad inputs[] = {
  243. {
  244. .name = "default",
  245. .type = AVMEDIA_TYPE_VIDEO,
  246. .filter_frame = filter_frame,
  247. },
  248. { NULL }
  249. };
  250. static const AVFilterPad outputs[] = {
  251. {
  252. .name = "default",
  253. .type = AVMEDIA_TYPE_VIDEO,
  254. .config_props = config_output,
  255. },
  256. { NULL }
  257. };
  258. AVFILTER_DEFINE_CLASS(amplify);
  259. AVFilter ff_vf_amplify = {
  260. .name = "amplify",
  261. .description = NULL_IF_CONFIG_SMALL("Amplify changes between successive video frames."),
  262. .priv_size = sizeof(AmplifyContext),
  263. .priv_class = &amplify_class,
  264. .query_formats = query_formats,
  265. .outputs = outputs,
  266. .inputs = inputs,
  267. .init = init,
  268. .uninit = uninit,
  269. .flags = AVFILTER_FLAG_SLICE_THREADS,
  270. };