2 * Copyright (c) 2017 Vittorio Giovara <vittorio.giovara@gmail.com>
4 * This file is part of FFmpeg.
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.
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.
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
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"
37 #include "colorspace.h"
42 enum TonemapAlgorithm {
53 static const struct LumaCoefficients luma_coefficients[AVCOL_SPC_NB] = {
54 [AVCOL_SPC_FCC] = { 0.30, 0.59, 0.11 },
55 [AVCOL_SPC_BT470BG] = { 0.299, 0.587, 0.114 },
56 [AVCOL_SPC_SMPTE170M] = { 0.299, 0.587, 0.114 },
57 [AVCOL_SPC_BT709] = { 0.2126, 0.7152, 0.0722 },
58 [AVCOL_SPC_SMPTE240M] = { 0.212, 0.701, 0.087 },
59 [AVCOL_SPC_BT2020_NCL] = { 0.2627, 0.6780, 0.0593 },
60 [AVCOL_SPC_BT2020_CL] = { 0.2627, 0.6780, 0.0593 },
63 typedef struct TonemapContext {
66 enum TonemapAlgorithm tonemap;
71 const struct LumaCoefficients *coeffs;
74 static const enum AVPixelFormat pix_fmts[] = {
80 static int query_formats(AVFilterContext *ctx)
82 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
85 static av_cold int init(AVFilterContext *ctx)
87 TonemapContext *s = ctx->priv;
94 case TONEMAP_REINHARD:
96 s->param = (1.0f - s->param) / s->param;
110 static float hable(float in)
112 float a = 0.15f, b = 0.50f, c = 0.10f, d = 0.20f, e = 0.02f, f = 0.30f;
113 return (in * (in * a + b * c) + d * e) / (in * (in * a + b) + d * f) - e / f;
116 static float mobius(float in, float j, double peak)
123 a = -j * j * (peak - 1.0f) / (j * j - 2.0f * j + peak);
124 b = (j * j - 2.0f * j * peak + peak) / FFMAX(peak - 1.0f, 1e-6);
126 return (b * b + 2.0f * b * j + j * j) / (b - a) * (in + a) / (in + b);
129 #define MIX(x,y,a) (x) * (1 - (a)) + (y) * (a)
130 static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in,
131 const AVPixFmtDescriptor *desc, int x, int y, double peak)
133 const float *r_in = (const float *)(in->data[0] + x * desc->comp[0].step + y * in->linesize[0]);
134 const float *b_in = (const float *)(in->data[1] + x * desc->comp[1].step + y * in->linesize[1]);
135 const float *g_in = (const float *)(in->data[2] + x * desc->comp[2].step + y * in->linesize[2]);
136 float *r_out = (float *)(out->data[0] + x * desc->comp[0].step + y * out->linesize[0]);
137 float *b_out = (float *)(out->data[1] + x * desc->comp[1].step + y * out->linesize[1]);
138 float *g_out = (float *)(out->data[2] + x * desc->comp[2].step + y * out->linesize[2]);
146 /* desaturate to prevent unnatural colors */
148 float luma = s->coeffs->cr * *r_in + s->coeffs->cg * *g_in + s->coeffs->cb * *b_in;
149 float overbright = FFMAX(luma - s->desat, 1e-6) / FFMAX(luma, 1e-6);
150 *r_out = MIX(*r_in, luma, overbright);
151 *g_out = MIX(*g_in, luma, overbright);
152 *b_out = MIX(*b_in, luma, overbright);
155 /* pick the brightest component, reducing the value range as necessary
156 * to keep the entire signal in range and preventing discoloration due to
157 * out-of-bounds clipping */
158 sig = FFMAX(FFMAX3(*r_out, *g_out, *b_out), 1e-6);
167 sig = sig * s->param / peak;
170 sig = sig > 0.05f ? pow(sig / peak, 1.0f / s->param)
171 : sig * pow(0.05f / peak, 1.0f / s->param) / 0.05f;
174 sig = av_clipf(sig * s->param, 0, 1.0f);
177 sig = hable(sig) / hable(peak);
179 case TONEMAP_REINHARD:
180 sig = sig / (sig + s->param) * (peak + s->param) / peak;
183 sig = mobius(sig, s->param, peak);
187 /* apply the computed scale factor to the color,
188 * linearly to prevent discoloration */
189 *r_out *= sig / sig_orig;
190 *g_out *= sig / sig_orig;
191 *b_out *= sig / sig_orig;
194 static int filter_frame(AVFilterLink *link, AVFrame *in)
196 TonemapContext *s = link->dst->priv;
197 AVFilterLink *outlink = link->dst->outputs[0];
199 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
200 const AVPixFmtDescriptor *odesc = av_pix_fmt_desc_get(outlink->format);
202 double peak = s->peak;
204 if (!desc || !odesc) {
209 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
212 return AVERROR(ENOMEM);
215 ret = av_frame_copy_props(out, in);
222 /* input and output transfer will be linear */
223 if (in->color_trc == AVCOL_TRC_UNSPECIFIED) {
224 av_log(s, AV_LOG_WARNING, "Untagged transfer, assuming linear light\n");
225 out->color_trc = AVCOL_TRC_LINEAR;
226 } else if (in->color_trc != AVCOL_TRC_LINEAR)
227 av_log(s, AV_LOG_WARNING, "Tonemapping works on linear light only\n");
229 /* read peak from side data if not passed in */
231 peak = ff_determine_signal_peak(in);
232 av_log(s, AV_LOG_DEBUG, "Computed signal peak: %f\n", peak);
235 /* load original color space even if pixel format is RGB to compute overbrights */
236 s->coeffs = &luma_coefficients[in->colorspace];
237 if (s->desat > 0 && (in->colorspace == AVCOL_SPC_UNSPECIFIED || !s->coeffs)) {
238 if (in->colorspace == AVCOL_SPC_UNSPECIFIED)
239 av_log(s, AV_LOG_WARNING, "Missing color space information, ");
241 av_log(s, AV_LOG_WARNING, "Unsupported color space '%s', ",
242 av_color_space_name(in->colorspace));
243 av_log(s, AV_LOG_WARNING, "desaturation is disabled\n");
247 /* do the tone map */
248 for (y = 0; y < out->height; y++)
249 for (x = 0; x < out->width; x++)
250 tonemap(s, out, in, desc, x, y, peak);
252 /* copy/generate alpha if needed */
253 if (desc->flags & AV_PIX_FMT_FLAG_ALPHA && odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
254 av_image_copy_plane(out->data[3], out->linesize[3],
255 in->data[3], in->linesize[3],
256 out->linesize[3], outlink->h);
257 } else if (odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
258 for (y = 0; y < out->height; y++) {
259 for (x = 0; x < out->width; x++) {
260 AV_WN32(out->data[3] + x * odesc->comp[3].step + y * out->linesize[3],
268 ff_update_hdr_metadata(out, peak);
270 return ff_filter_frame(outlink, out);
273 #define OFFSET(x) offsetof(TonemapContext, x)
274 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
275 static const AVOption tonemap_options[] = {
276 { "tonemap", "tonemap algorithm selection", OFFSET(tonemap), AV_OPT_TYPE_INT, {.i64 = TONEMAP_NONE}, TONEMAP_NONE, TONEMAP_MAX - 1, FLAGS, "tonemap" },
277 { "none", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_NONE}, 0, 0, FLAGS, "tonemap" },
278 { "linear", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_LINEAR}, 0, 0, FLAGS, "tonemap" },
279 { "gamma", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_GAMMA}, 0, 0, FLAGS, "tonemap" },
280 { "clip", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_CLIP}, 0, 0, FLAGS, "tonemap" },
281 { "reinhard", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_REINHARD}, 0, 0, FLAGS, "tonemap" },
282 { "hable", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_HABLE}, 0, 0, FLAGS, "tonemap" },
283 { "mobius", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_MOBIUS}, 0, 0, FLAGS, "tonemap" },
284 { "param", "tonemap parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl = NAN}, DBL_MIN, DBL_MAX, FLAGS },
285 { "desat", "desaturation strength", OFFSET(desat), AV_OPT_TYPE_DOUBLE, {.dbl = 2}, 0, DBL_MAX, FLAGS },
286 { "peak", "signal peak override", OFFSET(peak), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, DBL_MAX, FLAGS },
290 AVFILTER_DEFINE_CLASS(tonemap);
292 static const AVFilterPad tonemap_inputs[] = {
295 .type = AVMEDIA_TYPE_VIDEO,
296 .filter_frame = filter_frame,
301 static const AVFilterPad tonemap_outputs[] = {
304 .type = AVMEDIA_TYPE_VIDEO,
309 AVFilter ff_vf_tonemap = {
311 .description = NULL_IF_CONFIG_SMALL("Conversion to/from different dynamic ranges."),
313 .query_formats = query_formats,
314 .priv_size = sizeof(TonemapContext),
315 .priv_class = &tonemap_class,
316 .inputs = tonemap_inputs,
317 .outputs = tonemap_outputs,