FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_exposure.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 0 31 0.0%
Functions: 0 3 0.0%
Branches: 0 10 0.0%

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