vf_lagfun.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2019 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 LagfunContext {
  29. const AVClass *class;
  30. const AVPixFmtDescriptor *desc;
  31. float decay;
  32. int planes;
  33. int depth;
  34. int nb_planes;
  35. int linesize[4];
  36. int height[4];
  37. AVFrame *old;
  38. int (*lagfun)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  39. } LagfunContext;
  40. static int query_formats(AVFilterContext *ctx)
  41. {
  42. static const enum AVPixelFormat pixel_fmts[] = {
  43. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
  44. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
  45. AV_PIX_FMT_GRAY16,
  46. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  47. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  48. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  49. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  50. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  51. AV_PIX_FMT_YUVJ411P,
  52. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  53. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  54. AV_PIX_FMT_YUV440P10,
  55. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  56. AV_PIX_FMT_YUV440P12,
  57. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  58. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  59. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  60. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  61. AV_PIX_FMT_NONE
  62. };
  63. AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
  64. if (!formats)
  65. return AVERROR(ENOMEM);
  66. return ff_set_common_formats(ctx, formats);
  67. }
  68. typedef struct ThreadData {
  69. AVFrame *in, *out, *old;
  70. } ThreadData;
  71. static int lagfun_frame8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  72. {
  73. LagfunContext *s = ctx->priv;
  74. const float decay = s->decay;
  75. ThreadData *td = arg;
  76. AVFrame *in = td->in;
  77. AVFrame *out = td->out;
  78. AVFrame *old = td->old;
  79. for (int p = 0; p < s->nb_planes; p++) {
  80. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  81. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  82. const uint8_t *src = in->data[p] + slice_start * in->linesize[p];
  83. const uint8_t *osrc = old->data[p] + slice_start * old->linesize[p];
  84. uint8_t *dst = out->data[p] + slice_start * out->linesize[p];
  85. if (!((1 << p) & s->planes)) {
  86. av_image_copy_plane(dst, out->linesize[p],
  87. src, in->linesize[p],
  88. s->linesize[p], slice_end - slice_start);
  89. continue;
  90. }
  91. for (int y = slice_start; y < slice_end; y++) {
  92. for (int x = 0; x < s->linesize[p]; x++)
  93. dst[x] = FFMAX(src[x], osrc[x] * decay);
  94. src += in->linesize[p];
  95. osrc += old->linesize[p];
  96. dst += out->linesize[p];
  97. }
  98. }
  99. return 0;
  100. }
  101. static int lagfun_frame16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  102. {
  103. LagfunContext *s = ctx->priv;
  104. const float decay = s->decay;
  105. ThreadData *td = arg;
  106. AVFrame *in = td->in;
  107. AVFrame *out = td->out;
  108. AVFrame *old = td->old;
  109. for (int p = 0; p < s->nb_planes; p++) {
  110. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  111. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  112. const uint16_t *src = (const uint16_t *)in->data[p] + slice_start * in->linesize[p] / 2;
  113. const uint16_t *osrc = (const uint16_t *)old->data[p] + slice_start * old->linesize[p] / 2;
  114. uint16_t *dst = (uint16_t *)out->data[p] + slice_start * out->linesize[p] / 2;
  115. if (!((1 << p) & s->planes)) {
  116. av_image_copy_plane((uint8_t *)dst, out->linesize[p],
  117. (uint8_t *)src, in->linesize[p],
  118. s->linesize[p], slice_end - slice_start);
  119. continue;
  120. }
  121. for (int y = slice_start; y < slice_end; y++) {
  122. for (int x = 0; x < s->linesize[p]; x++)
  123. dst[x] = FFMAX(src[x], osrc[x] * decay);
  124. src += in->linesize[p] / 2;
  125. osrc += old->linesize[p] / 2;
  126. dst += out->linesize[p] / 2;
  127. }
  128. }
  129. return 0;
  130. }
  131. static int config_output(AVFilterLink *outlink)
  132. {
  133. AVFilterContext *ctx = outlink->src;
  134. LagfunContext *s = ctx->priv;
  135. AVFilterLink *inlink = ctx->inputs[0];
  136. int ret;
  137. s->desc = av_pix_fmt_desc_get(outlink->format);
  138. if (!s->desc)
  139. return AVERROR_BUG;
  140. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  141. s->depth = s->desc->comp[0].depth;
  142. s->lagfun = s->depth <= 8 ? lagfun_frame8 : lagfun_frame16;
  143. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  144. return ret;
  145. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  146. s->height[0] = s->height[3] = inlink->h;
  147. return 0;
  148. }
  149. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  150. {
  151. AVFilterContext *ctx = inlink->dst;
  152. AVFilterLink *outlink = ctx->outputs[0];
  153. LagfunContext *s = ctx->priv;
  154. ThreadData td;
  155. AVFrame *out;
  156. if (!s->old) {
  157. s->old = av_frame_clone(in);
  158. return ff_filter_frame(outlink, in);
  159. }
  160. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  161. if (!out) {
  162. av_frame_free(&in);
  163. return AVERROR(ENOMEM);
  164. }
  165. out->pts = in->pts;
  166. td.out = out;
  167. td.in = in;
  168. td.old = s->old;
  169. ctx->internal->execute(ctx, s->lagfun, &td, NULL, FFMIN(s->height[1], ff_filter_get_nb_threads(ctx)));
  170. av_frame_free(&s->old);
  171. av_frame_free(&in);
  172. s->old = av_frame_clone(out);
  173. return ff_filter_frame(outlink, out);
  174. }
  175. static av_cold void uninit(AVFilterContext *ctx)
  176. {
  177. LagfunContext *s = ctx->priv;
  178. av_frame_free(&s->old);
  179. }
  180. #define OFFSET(x) offsetof(LagfunContext, x)
  181. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  182. static const AVOption lagfun_options[] = {
  183. { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_FLOAT, {.dbl=.95}, 0, 1, FLAGS },
  184. { "planes", "set what planes to filter", OFFSET(planes), AV_OPT_TYPE_FLAGS, {.i64=15}, 0, 15, FLAGS },
  185. { NULL },
  186. };
  187. static const AVFilterPad inputs[] = {
  188. {
  189. .name = "default",
  190. .type = AVMEDIA_TYPE_VIDEO,
  191. .filter_frame = filter_frame,
  192. },
  193. { NULL }
  194. };
  195. static const AVFilterPad outputs[] = {
  196. {
  197. .name = "default",
  198. .type = AVMEDIA_TYPE_VIDEO,
  199. .config_props = config_output,
  200. },
  201. { NULL }
  202. };
  203. AVFILTER_DEFINE_CLASS(lagfun);
  204. AVFilter ff_vf_lagfun = {
  205. .name = "lagfun",
  206. .description = NULL_IF_CONFIG_SMALL("Slowly update darker pixels."),
  207. .priv_size = sizeof(LagfunContext),
  208. .priv_class = &lagfun_class,
  209. .query_formats = query_formats,
  210. .uninit = uninit,
  211. .outputs = outputs,
  212. .inputs = inputs,
  213. .flags = AVFILTER_FLAG_SLICE_THREADS,
  214. };