FFmpeg
Loading...
Searching...
No Matches
vf_tonemap.c
Go to the documentation of this file.
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/intfloat.h"
34#include "libavutil/opt.h"
35#include "libavutil/pixdesc.h"
36
37#include "avfilter.h"
38#include "colorspace.h"
39#include "filters.h"
40#include "video.h"
41
52
53typedef struct TonemapContext {
54 const AVClass *class;
55
56 /* enum TonemapAlgorithm */
58 double param;
59 double desat;
60 double peak;
61
64
66{
67 TonemapContext *s = ctx->priv;
68
69 switch(s->tonemap) {
70 case TONEMAP_GAMMA:
71 if (isnan(s->param))
72 s->param = 1.8f;
73 break;
75 if (!isnan(s->param))
76 s->param = (1.0f - s->param) / s->param;
77 break;
78 case TONEMAP_MOBIUS:
79 if (isnan(s->param))
80 s->param = 0.3f;
81 break;
82 }
83
84 if (isnan(s->param))
85 s->param = 1.0f;
86
87 return 0;
88}
89
90static float hable(float in)
91{
92 float a = 0.15f, b = 0.50f, c = 0.10f, d = 0.20f, e = 0.02f, f = 0.30f;
93 return (in * (in * a + b * c) + d * e) / (in * (in * a + b) + d * f) - e / f;
94}
95
96static float mobius(float in, float j, double peak)
97{
98 float a, b;
99
100 if (in <= j)
101 return in;
102
103 a = -j * j * (peak - 1.0f) / (j * j - 2.0f * j + peak);
104 b = (j * j - 2.0f * j * peak + peak) / FFMAX(peak - 1.0f, 1e-6);
105
106 return (b * b + 2.0f * b * j + j * j) / (b - a) * (in + a) / (in + b);
107}
108
109#define MIX(x,y,a) (x) * (1 - (a)) + (y) * (a)
110static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in,
111 const AVPixFmtDescriptor *desc, int x, int y, double peak)
112{
113 int map[3] = { desc->comp[0].plane, desc->comp[1].plane, desc->comp[2].plane };
114 const float *r_in = (const float *)(in->data[map[0]] + x * desc->comp[map[0]].step + y * in->linesize[map[0]]);
115 const float *g_in = (const float *)(in->data[map[1]] + x * desc->comp[map[1]].step + y * in->linesize[map[1]]);
116 const float *b_in = (const float *)(in->data[map[2]] + x * desc->comp[map[2]].step + y * in->linesize[map[2]]);
117 float *r_out = (float *)(out->data[map[0]] + x * desc->comp[map[0]].step + y * out->linesize[map[0]]);
118 float *g_out = (float *)(out->data[map[1]] + x * desc->comp[map[1]].step + y * out->linesize[map[1]]);
119 float *b_out = (float *)(out->data[map[2]] + x * desc->comp[map[2]].step + y * out->linesize[map[2]]);
120 float sig, sig_orig;
121
122 /* load values */
123 *r_out = *r_in;
124 *g_out = *g_in;
125 *b_out = *b_in;
126
127 /* desaturate to prevent unnatural colors */
128 if (s->desat > 0) {
129 float luma = av_q2d(s->coeffs->cr) * *r_in + av_q2d(s->coeffs->cg) * *g_in + av_q2d(s->coeffs->cb) * *b_in;
130 float overbright = FFMAX(luma - s->desat, 1e-6) / FFMAX(luma, 1e-6);
131 *r_out = MIX(*r_in, luma, overbright);
132 *g_out = MIX(*g_in, luma, overbright);
133 *b_out = MIX(*b_in, luma, overbright);
134 }
135
136 /* pick the brightest component, reducing the value range as necessary
137 * to keep the entire signal in range and preventing discoloration due to
138 * out-of-bounds clipping */
139 sig = FFMAX(FFMAX3(*r_out, *g_out, *b_out), 1e-6);
140 sig_orig = sig;
141
142 switch(s->tonemap) {
143 default:
144 case TONEMAP_NONE:
145 // do nothing
146 break;
147 case TONEMAP_LINEAR:
148 sig = sig * s->param / peak;
149 break;
150 case TONEMAP_GAMMA:
151 sig = sig > 0.05f ? pow(sig / peak, 1.0f / s->param)
152 : sig * pow(0.05f / peak, 1.0f / s->param) / 0.05f;
153 break;
154 case TONEMAP_CLIP:
155 sig = av_clipf(sig * s->param, 0, 1.0f);
156 break;
157 case TONEMAP_HABLE:
158 sig = hable(sig) / hable(peak);
159 break;
160 case TONEMAP_REINHARD:
161 sig = sig / (sig + s->param) * (peak + s->param) / peak;
162 break;
163 case TONEMAP_MOBIUS:
164 sig = mobius(sig, s->param, peak);
165 break;
166 }
167
168 /* apply the computed scale factor to the color,
169 * linearly to prevent discoloration */
170 *r_out *= sig / sig_orig;
171 *g_out *= sig / sig_orig;
172 *b_out *= sig / sig_orig;
173}
174
175typedef struct ThreadData {
176 AVFrame *in, *out;
178 double peak;
179} ThreadData;
180
181static int tonemap_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
182{
183 TonemapContext *s = ctx->priv;
184 ThreadData *td = arg;
185 AVFrame *in = td->in;
186 AVFrame *out = td->out;
187 const AVPixFmtDescriptor *desc = td->desc;
188 const int slice_start = ff_slice_pos(in->height, jobnr, nb_jobs);
189 const int slice_end = ff_slice_pos(in->height, jobnr + 1, nb_jobs);
190 double peak = td->peak;
191
192 for (int y = slice_start; y < slice_end; y++)
193 for (int x = 0; x < out->width; x++)
194 tonemap(s, out, in, desc, x, y, peak);
195
196 return 0;
197}
198
199static int filter_frame(AVFilterLink *link, AVFrame *in)
200{
201 AVFilterContext *ctx = link->dst;
202 TonemapContext *s = ctx->priv;
203 AVFilterLink *outlink = ctx->outputs[0];
204 ThreadData td;
205 AVFrame *out;
207 const AVPixFmtDescriptor *odesc = av_pix_fmt_desc_get(outlink->format);
208 int ret, x, y;
209 double peak = s->peak;
210
211 if (!desc || !odesc) {
212 av_frame_free(&in);
213 return AVERROR_BUG;
214 }
215
216 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
217 if (!out) {
218 av_frame_free(&in);
219 return AVERROR(ENOMEM);
220 }
221
222 ret = av_frame_copy_props(out, in);
223 if (ret < 0) {
224 av_frame_free(&in);
226 return ret;
227 }
228
229 /* input and output transfer will be linear */
230 if (in->color_trc == AVCOL_TRC_UNSPECIFIED) {
231 av_log(ctx, AV_LOG_WARNING, "Untagged transfer, assuming linear light\n");
232 out->color_trc = AVCOL_TRC_LINEAR;
233 } else if (in->color_trc != AVCOL_TRC_LINEAR)
234 av_log(ctx, AV_LOG_WARNING, "Tonemapping works on linear light only\n");
235
236 /* read peak from side data if not passed in */
237 if (!peak) {
238 peak = ff_determine_signal_peak(in);
239 av_log(ctx, AV_LOG_DEBUG, "Computed signal peak: %f\n", peak);
240 }
241
242 /* load original color space even if pixel format is RGB to compute overbrights */
244 if (s->desat > 0 && (in->colorspace == AVCOL_SPC_UNSPECIFIED || !s->coeffs)) {
246 av_log(ctx, AV_LOG_WARNING, "Missing color space information, ");
247 else if (!s->coeffs)
248 av_log(ctx, AV_LOG_WARNING, "Unsupported color space '%s', ",
250 av_log(ctx, AV_LOG_WARNING, "desaturation is disabled\n");
251 s->desat = 0;
252 }
253
254 /* do the tone map */
255 td.out = out;
256 td.in = in;
257 td.desc = desc;
258 td.peak = peak;
261
262 /* copy/generate alpha if needed */
263 if (desc->flags & AV_PIX_FMT_FLAG_ALPHA && odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
264 av_image_copy_plane(out->data[3], out->linesize[3],
265 in->data[3], in->linesize[3],
266 out->linesize[3], outlink->h);
267 } else if (odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
268 for (y = 0; y < out->height; y++) {
269 for (x = 0; x < out->width; x++) {
270 AV_WN32(out->data[3] + x * odesc->comp[3].step + y * out->linesize[3],
271 av_float2int(1.0f));
272 }
273 }
274 }
275
276 av_frame_free(&in);
277
279
280 return ff_filter_frame(outlink, out);
281}
282
283#define OFFSET(x) offsetof(TonemapContext, x)
284#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
285static const AVOption tonemap_options[] = {
286 { "tonemap", "tonemap algorithm selection", OFFSET(tonemap), AV_OPT_TYPE_INT, {.i64 = TONEMAP_NONE}, TONEMAP_NONE, TONEMAP_MAX - 1, FLAGS, .unit = "tonemap" },
287 { "none", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_NONE}, 0, 0, FLAGS, .unit = "tonemap" },
288 { "linear", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_LINEAR}, 0, 0, FLAGS, .unit = "tonemap" },
289 { "gamma", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_GAMMA}, 0, 0, FLAGS, .unit = "tonemap" },
290 { "clip", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_CLIP}, 0, 0, FLAGS, .unit = "tonemap" },
291 { "reinhard", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_REINHARD}, 0, 0, FLAGS, .unit = "tonemap" },
292 { "hable", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_HABLE}, 0, 0, FLAGS, .unit = "tonemap" },
293 { "mobius", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_MOBIUS}, 0, 0, FLAGS, .unit = "tonemap" },
294 { "param", "tonemap parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl = NAN}, DBL_MIN, DBL_MAX, FLAGS },
295 { "desat", "desaturation strength", OFFSET(desat), AV_OPT_TYPE_DOUBLE, {.dbl = 2}, 0, DBL_MAX, FLAGS },
296 { "peak", "signal peak override", OFFSET(peak), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, DBL_MAX, FLAGS },
297 { NULL }
298};
299
301
302static const AVFilterPad tonemap_inputs[] = {
303 {
304 .name = "default",
305 .type = AVMEDIA_TYPE_VIDEO,
306 .filter_frame = filter_frame,
307 },
308};
309
311 .p.name = "tonemap",
312 .p.description = NULL_IF_CONFIG_SMALL("Conversion to/from different dynamic ranges."),
313 .p.priv_class = &tonemap_class,
315 .init = init,
316 .priv_size = sizeof(TonemapContext),
320};
const FFFilter ff_vf_tonemap
Definition vf_tonemap.c:310
static FILE * out
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define av_clipf
Definition common.h:145
#define NULL
Definition coverity.c:32
Colorspace value utility functions for libavutil.
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
const struct AVLumaCoefficients * av_csp_luma_coeffs_from_avcsp(enum AVColorSpace csp)
Retrieves the Luma coefficients necessary to construct a conversion matrix from an enum constant desc...
Definition csp.c:58
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition imgutils.c:374
int a
const VDPAUPixFmtMap * map
misc image utilities
#define b
Definition input.c:43
static av_always_inline uint32_t av_float2int(float f)
Reinterpret a float as a 32-bit integer.
Definition intfloat.h:50
#define AV_WN32(p, v)
const char * arg
Definition jacosubdec.c:65
double ff_determine_signal_peak(AVFrame *in)
Definition colorspace.c:153
void ff_update_hdr_metadata(AVFrame *in, double peak)
Definition colorspace.c:178
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:763
#define FILTER_PIXFMTS(...)
Definition filters.h:250
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define isnan(x)
Definition libm.h:342
const char * desc
Definition libsvtav1.c:83
#define FFMAX3(a, b, c)
Definition macros.h:48
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define NAN
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition mpeg12dec.c:1697
AVOptions.
const char * av_color_space_name(enum AVColorSpace space)
Definition pixdesc.c:3860
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition pixdesc.h:147
#define AV_PIX_FMT_GBRPF32
Definition pixfmt.h:584
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition pixfmt.h:681
@ AVCOL_TRC_UNSPECIFIED
Definition pixfmt.h:675
#define AV_PIX_FMT_GBRAPF32
Definition pixfmt.h:585
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:709
static volatile sig_atomic_t sig
Definition signal.c:48
Describe the class of an AVClass context structure.
Definition log.h:76
int step
Number of elements between 2 horizontally consecutive pixels.
Definition pixdesc.h:40
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int height
Definition frame.h:544
enum AVColorSpace colorspace
YUV colorspace type.
Definition frame.h:734
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
enum AVColorTransferCharacteristic color_trc
Definition frame.h:727
Struct containing luma coefficients to be used for RGB to YUV/YCoCg, or similar calculations.
Definition csp.h:48
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
uint64_t flags
Combination of AV_PIX_FMT_FLAG_... flags.
Definition pixdesc.h:94
Used for passing data between threads.
Definition dsddec.c:71
const AVPixFmtDescriptor * desc
Definition vf_tonemap.c:177
AVFrame * out
double peak
Definition vf_tonemap.c:178
const AVLumaCoefficients * coeffs
Definition vf_tonemap.c:62
#define av_log(a,...)
TonemapAlgorithm
Definition vf_tonemap.c:42
@ TONEMAP_HABLE
Definition vf_tonemap.c:48
@ TONEMAP_LINEAR
Definition vf_tonemap.c:44
@ TONEMAP_MAX
Definition vf_tonemap.c:50
@ TONEMAP_MOBIUS
Definition vf_tonemap.c:49
@ TONEMAP_REINHARD
Definition vf_tonemap.c:47
@ TONEMAP_NONE
Definition vf_tonemap.c:43
@ TONEMAP_CLIP
Definition vf_tonemap.c:46
@ TONEMAP_GAMMA
Definition vf_tonemap.c:45
static int filter_frame(AVFilterLink *link, AVFrame *in)
Definition vf_tonemap.c:199
static float hable(float in)
Definition vf_tonemap.c:90
#define MIX(x, y, a)
Definition vf_tonemap.c:109
static const AVOption tonemap_options[]
Definition vf_tonemap.c:285
static int tonemap_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_tonemap.c:181
#define OFFSET(x)
Definition vf_tonemap.c:283
static const AVFilterPad tonemap_inputs[]
Definition vf_tonemap.c:302
static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in, const AVPixFmtDescriptor *desc, int x, int y, double peak)
Definition vf_tonemap.c:110
static float mobius(float in, float j, double peak)
Definition vf_tonemap.c:96
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89
static double c[64]
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844