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.

234 lines
9.1KB

  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. float decay;
  31. int planes;
  32. int depth;
  33. int nb_planes;
  34. int linesize[4];
  35. int planewidth[4];
  36. int planeheight[4];
  37. float *old[4];
  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;
  70. } ThreadData;
  71. #define LAGFUN(name, type) \
  72. static int lagfun_frame##name(AVFilterContext *ctx, void *arg, \
  73. int jobnr, int nb_jobs) \
  74. { \
  75. LagfunContext *s = ctx->priv; \
  76. const float decay = s->decay; \
  77. ThreadData *td = arg; \
  78. AVFrame *in = td->in; \
  79. AVFrame *out = td->out; \
  80. \
  81. for (int p = 0; p < s->nb_planes; p++) { \
  82. const int slice_start = (s->planeheight[p] * jobnr) / nb_jobs; \
  83. const int slice_end = (s->planeheight[p] * (jobnr+1)) / nb_jobs; \
  84. const type *src = (const type *)in->data[p] + \
  85. slice_start * in->linesize[p] / sizeof(type); \
  86. float *osrc = s->old[p] + slice_start * s->planewidth[p]; \
  87. type *dst = (type *)out->data[p] + \
  88. slice_start * out->linesize[p] / sizeof(type); \
  89. \
  90. if (!((1 << p) & s->planes)) { \
  91. av_image_copy_plane((uint8_t *)dst, out->linesize[p], \
  92. (const uint8_t *)src, in->linesize[p], \
  93. s->linesize[p], slice_end - slice_start); \
  94. continue; \
  95. } \
  96. \
  97. for (int y = slice_start; y < slice_end; y++) { \
  98. for (int x = 0; x < s->planewidth[p]; x++) { \
  99. float v = FFMAX(src[x], osrc[x] * decay); \
  100. \
  101. osrc[x] = v; \
  102. if (ctx->is_disabled) { \
  103. dst[x] = src[x]; \
  104. } else { \
  105. dst[x] = lrintf(v); \
  106. } \
  107. } \
  108. \
  109. src += in->linesize[p] / sizeof(type); \
  110. osrc += s->planewidth[p]; \
  111. dst += out->linesize[p] / sizeof(type); \
  112. } \
  113. } \
  114. \
  115. return 0; \
  116. }
  117. LAGFUN(8, uint8_t)
  118. LAGFUN(16, uint16_t)
  119. static int config_output(AVFilterLink *outlink)
  120. {
  121. AVFilterContext *ctx = outlink->src;
  122. LagfunContext *s = ctx->priv;
  123. AVFilterLink *inlink = ctx->inputs[0];
  124. const AVPixFmtDescriptor *desc;
  125. int ret;
  126. desc = av_pix_fmt_desc_get(outlink->format);
  127. if (!desc)
  128. return AVERROR_BUG;
  129. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  130. s->depth = desc->comp[0].depth;
  131. s->lagfun = s->depth <= 8 ? lagfun_frame8 : lagfun_frame16;
  132. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  133. return ret;
  134. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  135. s->planewidth[0] = s->planewidth[3] = inlink->w;
  136. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  137. s->planeheight[0] = s->planeheight[3] = inlink->h;
  138. for (int p = 0; p < s->nb_planes; p++) {
  139. s->old[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof(*s->old[0]));
  140. if (!s->old[p])
  141. return AVERROR(ENOMEM);
  142. }
  143. return 0;
  144. }
  145. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  146. {
  147. AVFilterContext *ctx = inlink->dst;
  148. AVFilterLink *outlink = ctx->outputs[0];
  149. LagfunContext *s = ctx->priv;
  150. ThreadData td;
  151. AVFrame *out;
  152. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  153. if (!out) {
  154. av_frame_free(&in);
  155. return AVERROR(ENOMEM);
  156. }
  157. out->pts = in->pts;
  158. td.out = out;
  159. td.in = in;
  160. ctx->internal->execute(ctx, s->lagfun, &td, NULL, FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
  161. av_frame_free(&in);
  162. return ff_filter_frame(outlink, out);
  163. }
  164. static av_cold void uninit(AVFilterContext *ctx)
  165. {
  166. LagfunContext *s = ctx->priv;
  167. for (int p = 0; p < s->nb_planes; p++)
  168. av_freep(&s->old[p]);
  169. }
  170. #define OFFSET(x) offsetof(LagfunContext, x)
  171. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
  172. static const AVOption lagfun_options[] = {
  173. { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_FLOAT, {.dbl=.95}, 0, 1, FLAGS },
  174. { "planes", "set what planes to filter", OFFSET(planes), AV_OPT_TYPE_FLAGS, {.i64=15}, 0, 15, FLAGS },
  175. { NULL },
  176. };
  177. static const AVFilterPad inputs[] = {
  178. {
  179. .name = "default",
  180. .type = AVMEDIA_TYPE_VIDEO,
  181. .filter_frame = filter_frame,
  182. },
  183. { NULL }
  184. };
  185. static const AVFilterPad outputs[] = {
  186. {
  187. .name = "default",
  188. .type = AVMEDIA_TYPE_VIDEO,
  189. .config_props = config_output,
  190. },
  191. { NULL }
  192. };
  193. AVFILTER_DEFINE_CLASS(lagfun);
  194. AVFilter ff_vf_lagfun = {
  195. .name = "lagfun",
  196. .description = NULL_IF_CONFIG_SMALL("Slowly update darker pixels."),
  197. .priv_size = sizeof(LagfunContext),
  198. .priv_class = &lagfun_class,
  199. .query_formats = query_formats,
  200. .uninit = uninit,
  201. .outputs = outputs,
  202. .inputs = inputs,
  203. .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  204. .process_command = ff_filter_process_command,
  205. };