FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_exposure.c
Date: 2024-04-19 07:31:02
Exec Total Coverage
Lines: 0 55 0.0%
Functions: 0 3 0.0%
Branches: 0 24 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 "avfilter.h"
25 #include "internal.h"
26 #include "video.h"
27
28 typedef struct ExposureContext {
29 const AVClass *class;
30
31 float exposure;
32 float black;
33
34 float scale;
35 int (*do_slice)(AVFilterContext *s, void *arg,
36 int jobnr, int nb_jobs);
37 } ExposureContext;
38
39 typedef struct ThreadData {
40 AVFrame *out, *in;
41 } ThreadData;
42
43 static int exposure_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
44 {
45 ExposureContext *s = ctx->priv;
46 ThreadData *td = arg;
47 const int width = td->out->width;
48 const int height = td->out->height;
49 const int slice_start = (height * jobnr) / nb_jobs;
50 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
51 const float black = s->black;
52 const float scale = s->scale;
53
54 for (int p = 0; p < 3; p++) {
55 const int slinesize = td->in->linesize[p] / 4;
56 const int dlinesize = td->out->linesize[p] / 4;
57 const float *src = (const float *)td->in->data[p] + slice_start * slinesize;
58 float *ptr = (float *)td->out->data[p] + slice_start * dlinesize;
59 for (int y = slice_start; y < slice_end; y++) {
60 for (int x = 0; x < width; x++)
61 ptr[x] = (src[x] - black) * scale;
62
63 ptr += dlinesize;
64 src += slinesize;
65 }
66 }
67
68 if (td->in->data[3] && td->in->linesize[3] && td->in != td->out) {
69 const int slinesize = td->in->linesize[3] / 4;
70 const int dlinesize = td->out->linesize[3] / 4;
71 const float *src = (const float *)td->in->data[3] + slice_start * slinesize;
72 float *ptr = (float *)td->out->data[3] + slice_start * dlinesize;
73 for (int y = slice_start; y < slice_end; y++) {
74 memcpy(ptr, src, width * sizeof(*ptr));
75 ptr += dlinesize;
76 src += slinesize;
77 }
78 }
79
80 return 0;
81 }
82
83 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
84 {
85 AVFilterContext *ctx = inlink->dst;
86 AVFilterLink *outlink = ctx->outputs[0];
87 ExposureContext *s = ctx->priv;
88 float diff = fabsf(exp2f(-s->exposure) - s->black);
89 ThreadData td;
90 AVFrame *out;
91
92 if (av_frame_is_writable(in)) {
93 out = in;
94 } else {
95 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
96 if (!out) {
97 av_frame_free(&in);
98 return AVERROR(ENOMEM);
99 }
100 av_frame_copy_props(out, in);
101 }
102
103 diff = diff > 0.f ? diff : 1.f / 1024.f;
104 s->scale = 1.f / diff;
105 td.out = out;
106 td.in = in;
107 ff_filter_execute(ctx, s->do_slice, &td, NULL,
108 FFMIN(out->height, ff_filter_get_nb_threads(ctx)));
109
110 if (out != in)
111 av_frame_free(&in);
112 return ff_filter_frame(outlink, out);
113 }
114
115 static av_cold int config_input(AVFilterLink *inlink)
116 {
117 AVFilterContext *ctx = inlink->dst;
118 ExposureContext *s = ctx->priv;
119
120 s->do_slice = exposure_slice;
121
122 return 0;
123 }
124
125 static const AVFilterPad exposure_inputs[] = {
126 {
127 .name = "default",
128 .type = AVMEDIA_TYPE_VIDEO,
129 .filter_frame = filter_frame,
130 .config_props = config_input,
131 },
132 };
133
134 #define OFFSET(x) offsetof(ExposureContext, x)
135 #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
136
137 static const AVOption exposure_options[] = {
138 { "exposure", "set the exposure correction", OFFSET(exposure), AV_OPT_TYPE_FLOAT, {.dbl=0}, -3, 3, VF },
139 { "black", "set the black level correction", OFFSET(black), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF },
140 { NULL }
141 };
142
143 AVFILTER_DEFINE_CLASS(exposure);
144
145 const AVFilter ff_vf_exposure = {
146 .name = "exposure",
147 .description = NULL_IF_CONFIG_SMALL("Adjust exposure of the video stream."),
148 .priv_size = sizeof(ExposureContext),
149 .priv_class = &exposure_class,
150 FILTER_INPUTS(exposure_inputs),
151 FILTER_OUTPUTS(ff_video_default_filterpad),
152 FILTER_PIXFMTS(AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32),
153 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
154 .process_command = ff_filter_process_command,
155 };
156