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.

145 lines
4.2KB

  1. /*
  2. * Copyright (c) 2021 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 <float.h>
  21. #include "libavutil/opt.h"
  22. #include "libavutil/imgutils.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. typedef struct ExposureContext {
  28. const AVClass *class;
  29. float exposure;
  30. float black;
  31. float scale;
  32. int (*do_slice)(AVFilterContext *s, void *arg,
  33. int jobnr, int nb_jobs);
  34. } ExposureContext;
  35. static int exposure_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  36. {
  37. ExposureContext *s = ctx->priv;
  38. AVFrame *frame = arg;
  39. const int width = frame->width;
  40. const int height = frame->height;
  41. const int slice_start = (height * jobnr) / nb_jobs;
  42. const int slice_end = (height * (jobnr + 1)) / nb_jobs;
  43. const float black = s->black;
  44. const float scale = s->scale;
  45. for (int p = 0; p < 3; p++) {
  46. const int linesize = frame->linesize[p] / 4;
  47. float *ptr = (float *)frame->data[p] + slice_start * linesize;
  48. for (int y = slice_start; y < slice_end; y++) {
  49. for (int x = 0; x < width; x++)
  50. ptr[x] = (ptr[x] - black) * scale;
  51. ptr += linesize;
  52. }
  53. }
  54. return 0;
  55. }
  56. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  57. {
  58. AVFilterContext *ctx = inlink->dst;
  59. ExposureContext *s = ctx->priv;
  60. s->scale = 1.f / (exp2f(-s->exposure) - s->black);
  61. ctx->internal->execute(ctx, s->do_slice, frame, NULL,
  62. FFMIN(frame->height, ff_filter_get_nb_threads(ctx)));
  63. return ff_filter_frame(ctx->outputs[0], frame);
  64. }
  65. static av_cold int query_formats(AVFilterContext *ctx)
  66. {
  67. static const enum AVPixelFormat pixel_fmts[] = {
  68. AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32,
  69. AV_PIX_FMT_NONE
  70. };
  71. AVFilterFormats *formats = NULL;
  72. formats = ff_make_format_list(pixel_fmts);
  73. if (!formats)
  74. return AVERROR(ENOMEM);
  75. return ff_set_common_formats(ctx, formats);
  76. }
  77. static av_cold int config_input(AVFilterLink *inlink)
  78. {
  79. AVFilterContext *ctx = inlink->dst;
  80. ExposureContext *s = ctx->priv;
  81. s->do_slice = exposure_slice;
  82. return 0;
  83. }
  84. static const AVFilterPad exposure_inputs[] = {
  85. {
  86. .name = "default",
  87. .type = AVMEDIA_TYPE_VIDEO,
  88. .needs_writable = 1,
  89. .filter_frame = filter_frame,
  90. .config_props = config_input,
  91. },
  92. { NULL }
  93. };
  94. static const AVFilterPad exposure_outputs[] = {
  95. {
  96. .name = "default",
  97. .type = AVMEDIA_TYPE_VIDEO,
  98. },
  99. { NULL }
  100. };
  101. #define OFFSET(x) offsetof(ExposureContext, x)
  102. #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  103. static const AVOption exposure_options[] = {
  104. { "exposure", "set the exposure correction", OFFSET(exposure), AV_OPT_TYPE_FLOAT, {.dbl=0}, -3, 3, VF },
  105. { "black", "set the black level correction", OFFSET(black), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF },
  106. { NULL }
  107. };
  108. AVFILTER_DEFINE_CLASS(exposure);
  109. AVFilter ff_vf_exposure = {
  110. .name = "exposure",
  111. .description = NULL_IF_CONFIG_SMALL("Adjust exposure of the video stream."),
  112. .priv_size = sizeof(ExposureContext),
  113. .priv_class = &exposure_class,
  114. .query_formats = query_formats,
  115. .inputs = exposure_inputs,
  116. .outputs = exposure_outputs,
  117. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  118. .process_command = ff_filter_process_command,
  119. };