FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_tonemap.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 138 0.0%
Functions: 0 6 0.0%
Branches: 0 85 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2017 Vittorio Giovara <vittorio.giovara@gmail.com>
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 /**
22 * @file
23 * tonemap algorithms
24 */
25
26 #include <float.h>
27 #include <stdio.h>
28
29 #include "libavutil/csp.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35
36 #include "avfilter.h"
37 #include "colorspace.h"
38 #include "filters.h"
39 #include "video.h"
40
41 enum TonemapAlgorithm {
42 TONEMAP_NONE,
43 TONEMAP_LINEAR,
44 TONEMAP_GAMMA,
45 TONEMAP_CLIP,
46 TONEMAP_REINHARD,
47 TONEMAP_HABLE,
48 TONEMAP_MOBIUS,
49 TONEMAP_MAX,
50 };
51
52 typedef struct TonemapContext {
53 const AVClass *class;
54
55 enum TonemapAlgorithm tonemap;
56 double param;
57 double desat;
58 double peak;
59
60 const AVLumaCoefficients *coeffs;
61 } TonemapContext;
62
63 static av_cold int init(AVFilterContext *ctx)
64 {
65 TonemapContext *s = ctx->priv;
66
67 switch(s->tonemap) {
68 case TONEMAP_GAMMA:
69 if (isnan(s->param))
70 s->param = 1.8f;
71 break;
72 case TONEMAP_REINHARD:
73 if (!isnan(s->param))
74 s->param = (1.0f - s->param) / s->param;
75 break;
76 case TONEMAP_MOBIUS:
77 if (isnan(s->param))
78 s->param = 0.3f;
79 break;
80 }
81
82 if (isnan(s->param))
83 s->param = 1.0f;
84
85 return 0;
86 }
87
88 static float hable(float in)
89 {
90 float a = 0.15f, b = 0.50f, c = 0.10f, d = 0.20f, e = 0.02f, f = 0.30f;
91 return (in * (in * a + b * c) + d * e) / (in * (in * a + b) + d * f) - e / f;
92 }
93
94 static float mobius(float in, float j, double peak)
95 {
96 float a, b;
97
98 if (in <= j)
99 return in;
100
101 a = -j * j * (peak - 1.0f) / (j * j - 2.0f * j + peak);
102 b = (j * j - 2.0f * j * peak + peak) / FFMAX(peak - 1.0f, 1e-6);
103
104 return (b * b + 2.0f * b * j + j * j) / (b - a) * (in + a) / (in + b);
105 }
106
107 #define MIX(x,y,a) (x) * (1 - (a)) + (y) * (a)
108 static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in,
109 const AVPixFmtDescriptor *desc, int x, int y, double peak)
110 {
111 int map[3] = { desc->comp[0].plane, desc->comp[1].plane, desc->comp[2].plane };
112 const float *r_in = (const float *)(in->data[map[0]] + x * desc->comp[map[0]].step + y * in->linesize[map[0]]);
113 const float *g_in = (const float *)(in->data[map[1]] + x * desc->comp[map[1]].step + y * in->linesize[map[1]]);
114 const float *b_in = (const float *)(in->data[map[2]] + x * desc->comp[map[2]].step + y * in->linesize[map[2]]);
115 float *r_out = (float *)(out->data[map[0]] + x * desc->comp[map[0]].step + y * out->linesize[map[0]]);
116 float *g_out = (float *)(out->data[map[1]] + x * desc->comp[map[1]].step + y * out->linesize[map[1]]);
117 float *b_out = (float *)(out->data[map[2]] + x * desc->comp[map[2]].step + y * out->linesize[map[2]]);
118 float sig, sig_orig;
119
120 /* load values */
121 *r_out = *r_in;
122 *g_out = *g_in;
123 *b_out = *b_in;
124
125 /* desaturate to prevent unnatural colors */
126 if (s->desat > 0) {
127 float luma = av_q2d(s->coeffs->cr) * *r_in + av_q2d(s->coeffs->cg) * *g_in + av_q2d(s->coeffs->cb) * *b_in;
128 float overbright = FFMAX(luma - s->desat, 1e-6) / FFMAX(luma, 1e-6);
129 *r_out = MIX(*r_in, luma, overbright);
130 *g_out = MIX(*g_in, luma, overbright);
131 *b_out = MIX(*b_in, luma, overbright);
132 }
133
134 /* pick the brightest component, reducing the value range as necessary
135 * to keep the entire signal in range and preventing discoloration due to
136 * out-of-bounds clipping */
137 sig = FFMAX(FFMAX3(*r_out, *g_out, *b_out), 1e-6);
138 sig_orig = sig;
139
140 switch(s->tonemap) {
141 default:
142 case TONEMAP_NONE:
143 // do nothing
144 break;
145 case TONEMAP_LINEAR:
146 sig = sig * s->param / peak;
147 break;
148 case TONEMAP_GAMMA:
149 sig = sig > 0.05f ? pow(sig / peak, 1.0f / s->param)
150 : sig * pow(0.05f / peak, 1.0f / s->param) / 0.05f;
151 break;
152 case TONEMAP_CLIP:
153 sig = av_clipf(sig * s->param, 0, 1.0f);
154 break;
155 case TONEMAP_HABLE:
156 sig = hable(sig) / hable(peak);
157 break;
158 case TONEMAP_REINHARD:
159 sig = sig / (sig + s->param) * (peak + s->param) / peak;
160 break;
161 case TONEMAP_MOBIUS:
162 sig = mobius(sig, s->param, peak);
163 break;
164 }
165
166 /* apply the computed scale factor to the color,
167 * linearly to prevent discoloration */
168 *r_out *= sig / sig_orig;
169 *g_out *= sig / sig_orig;
170 *b_out *= sig / sig_orig;
171 }
172
173 typedef struct ThreadData {
174 AVFrame *in, *out;
175 const AVPixFmtDescriptor *desc;
176 double peak;
177 } ThreadData;
178
179 static int tonemap_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
180 {
181 TonemapContext *s = ctx->priv;
182 ThreadData *td = arg;
183 AVFrame *in = td->in;
184 AVFrame *out = td->out;
185 const AVPixFmtDescriptor *desc = td->desc;
186 const int slice_start = (in->height * jobnr) / nb_jobs;
187 const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
188 double peak = td->peak;
189
190 for (int y = slice_start; y < slice_end; y++)
191 for (int x = 0; x < out->width; x++)
192 tonemap(s, out, in, desc, x, y, peak);
193
194 return 0;
195 }
196
197 static int filter_frame(AVFilterLink *link, AVFrame *in)
198 {
199 AVFilterContext *ctx = link->dst;
200 TonemapContext *s = ctx->priv;
201 AVFilterLink *outlink = ctx->outputs[0];
202 ThreadData td;
203 AVFrame *out;
204 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
205 const AVPixFmtDescriptor *odesc = av_pix_fmt_desc_get(outlink->format);
206 int ret, x, y;
207 double peak = s->peak;
208
209 if (!desc || !odesc) {
210 av_frame_free(&in);
211 return AVERROR_BUG;
212 }
213
214 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
215 if (!out) {
216 av_frame_free(&in);
217 return AVERROR(ENOMEM);
218 }
219
220 ret = av_frame_copy_props(out, in);
221 if (ret < 0) {
222 av_frame_free(&in);
223 av_frame_free(&out);
224 return ret;
225 }
226
227 /* input and output transfer will be linear */
228 if (in->color_trc == AVCOL_TRC_UNSPECIFIED) {
229 av_log(s, AV_LOG_WARNING, "Untagged transfer, assuming linear light\n");
230 out->color_trc = AVCOL_TRC_LINEAR;
231 } else if (in->color_trc != AVCOL_TRC_LINEAR)
232 av_log(s, AV_LOG_WARNING, "Tonemapping works on linear light only\n");
233
234 /* read peak from side data if not passed in */
235 if (!peak) {
236 peak = ff_determine_signal_peak(in);
237 av_log(s, AV_LOG_DEBUG, "Computed signal peak: %f\n", peak);
238 }
239
240 /* load original color space even if pixel format is RGB to compute overbrights */
241 s->coeffs = av_csp_luma_coeffs_from_avcsp(in->colorspace);
242 if (s->desat > 0 && (in->colorspace == AVCOL_SPC_UNSPECIFIED || !s->coeffs)) {
243 if (in->colorspace == AVCOL_SPC_UNSPECIFIED)
244 av_log(s, AV_LOG_WARNING, "Missing color space information, ");
245 else if (!s->coeffs)
246 av_log(s, AV_LOG_WARNING, "Unsupported color space '%s', ",
247 av_color_space_name(in->colorspace));
248 av_log(s, AV_LOG_WARNING, "desaturation is disabled\n");
249 s->desat = 0;
250 }
251
252 /* do the tone map */
253 td.out = out;
254 td.in = in;
255 td.desc = desc;
256 td.peak = peak;
257 ff_filter_execute(ctx, tonemap_slice, &td, NULL,
258 FFMIN(in->height, ff_filter_get_nb_threads(ctx)));
259
260 /* copy/generate alpha if needed */
261 if (desc->flags & AV_PIX_FMT_FLAG_ALPHA && odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
262 av_image_copy_plane(out->data[3], out->linesize[3],
263 in->data[3], in->linesize[3],
264 out->linesize[3], outlink->h);
265 } else if (odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
266 for (y = 0; y < out->height; y++) {
267 for (x = 0; x < out->width; x++) {
268 AV_WN32(out->data[3] + x * odesc->comp[3].step + y * out->linesize[3],
269 av_float2int(1.0f));
270 }
271 }
272 }
273
274 av_frame_free(&in);
275
276 ff_update_hdr_metadata(out, peak);
277
278 return ff_filter_frame(outlink, out);
279 }
280
281 #define OFFSET(x) offsetof(TonemapContext, x)
282 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
283 static const AVOption tonemap_options[] = {
284 { "tonemap", "tonemap algorithm selection", OFFSET(tonemap), AV_OPT_TYPE_INT, {.i64 = TONEMAP_NONE}, TONEMAP_NONE, TONEMAP_MAX - 1, FLAGS, .unit = "tonemap" },
285 { "none", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_NONE}, 0, 0, FLAGS, .unit = "tonemap" },
286 { "linear", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_LINEAR}, 0, 0, FLAGS, .unit = "tonemap" },
287 { "gamma", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_GAMMA}, 0, 0, FLAGS, .unit = "tonemap" },
288 { "clip", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_CLIP}, 0, 0, FLAGS, .unit = "tonemap" },
289 { "reinhard", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_REINHARD}, 0, 0, FLAGS, .unit = "tonemap" },
290 { "hable", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_HABLE}, 0, 0, FLAGS, .unit = "tonemap" },
291 { "mobius", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_MOBIUS}, 0, 0, FLAGS, .unit = "tonemap" },
292 { "param", "tonemap parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl = NAN}, DBL_MIN, DBL_MAX, FLAGS },
293 { "desat", "desaturation strength", OFFSET(desat), AV_OPT_TYPE_DOUBLE, {.dbl = 2}, 0, DBL_MAX, FLAGS },
294 { "peak", "signal peak override", OFFSET(peak), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, DBL_MAX, FLAGS },
295 { NULL }
296 };
297
298 AVFILTER_DEFINE_CLASS(tonemap);
299
300 static const AVFilterPad tonemap_inputs[] = {
301 {
302 .name = "default",
303 .type = AVMEDIA_TYPE_VIDEO,
304 .filter_frame = filter_frame,
305 },
306 };
307
308 const AVFilter ff_vf_tonemap = {
309 .name = "tonemap",
310 .description = NULL_IF_CONFIG_SMALL("Conversion to/from different dynamic ranges."),
311 .init = init,
312 .priv_size = sizeof(TonemapContext),
313 .priv_class = &tonemap_class,
314 FILTER_INPUTS(tonemap_inputs),
315 FILTER_OUTPUTS(ff_video_default_filterpad),
316 FILTER_PIXFMTS(AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32),
317 .flags = AVFILTER_FLAG_SLICE_THREADS,
318 };
319