FFmpeg
Loading...
Searching...
No Matches
vf_gainmap.c File Reference

Filter to compute an HDR gain map from a base and alternate rendition. More...

#include <float.h>
#include <math.h>
#include "libavutil/csp.h"
#include "libavutil/gain_map.h"
#include "libavutil/internal.h"
#include "libavutil/mastering_display_metadata.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "avfilter.h"
#include "colorspace.h"
#include "filters.h"
#include "formats.h"
#include "framesync.h"
#include "video.h"

Go to the source code of this file.

Data Structures

struct  GainMapEOTF
 
struct  GainMapContext
 
struct  ThreadData
 Used for passing data between threads. More...
 

Macros

#define SDR_DIFFUSE_WHITE   203.0
 
#define GAINMAP_LUT_SIZE   1025
 
#define OFFSET(x)
 
#define FLAGS   AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
 
#define GAIN(a, b)
 

Enumerations

enum  GainMapColorspace { GAINMAP_CSP_BASE , GAINMAP_CSP_ALT , GAINMAP_CSP_NB }
 
enum  GainMapMode { GAINMAP_RGB , GAINMAP_LUMA , GAINMAP_MAXRGB , GAINMAP_MODE_NB }
 
enum  GainMapMeasure {
  MEASURE_MIN = 1 << 0 , MEASURE_MAX = 1 << 1 , MEASURE_GAMMA = 1 << 2 , MEASURE_RANGE = MEASURE_MIN | MEASURE_MAX ,
  MEASURE_ALL = MEASURE_RANGE | MEASURE_GAMMA
}
 

Functions

 FRAMESYNC_DEFINE_CLASS (gainmap, GainMapContext, fs)
 
static AVRational quantq (double x, enum AVRounding rnd)
 
static void setup_range (AVFilterContext *ctx, int ch, AVRational min, AVRational max)
 
static void setup_gamma (AVFilterContext *ctx, int ch, AVRational gamma)
 
static av_cold int init (AVFilterContext *ctx)
 
static int query_formats (const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
 
static double frame_luminance (const AVFrame *frame)
 
static void update_eotf (GainMapEOTF *tf, enum AVColorTransferCharacteristic trc, av_csp_eotf_function eotf, double Lw)
 
static const char * unknown_if_null (const char *s)
 
static int setup_colorspace (AVFilterContext *ctx, const AVFrame *base, const AVFrame *alt)
 
static av_always_inline float quantize (float gain, float scale, float offset, float gamma)
 
static av_always_inline void get_pixel (const GainMapContext *s, float dst[3], const float *restrict const src[3], int x, int alt)
 
static av_always_inline int slice_internal (AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs, int quant)
 
static int slice_gain (AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 
static int slice_gain_quant (AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 
static int slice_quant (AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 
static void choose_quant_params (AVFilterContext *ctx, const AVFrame *out, int nb_jobs)
 
static int process_frame (FFFrameSync *fs)
 
static int config_output (AVFilterLink *outlink)
 
static int activate (AVFilterContext *ctx)
 
static av_cold void uninit (AVFilterContext *ctx)
 

Variables

static const AVOption gainmap_options []
 
static const int gbr_order [3] = { 2, 0, 1 }
 
static const AVFilterPad gainmap_inputs []
 
static const AVFilterPad gainmap_outputs []
 
const FFFilter ff_vf_gainmap
 

Detailed Description

Filter to compute an HDR gain map from a base and alternate rendition.

Definition in file vf_gainmap.c.

Macro Definition Documentation

◆ SDR_DIFFUSE_WHITE

#define SDR_DIFFUSE_WHITE   203.0

Definition at line 42 of file vf_gainmap.c.

Referenced by frame_luminance(), get_pixel(), setup_colorspace(), and update_eotf().

◆ GAINMAP_LUT_SIZE

#define GAINMAP_LUT_SIZE   1025

Definition at line 70 of file vf_gainmap.c.

Referenced by get_pixel(), and update_eotf().

◆ OFFSET

#define OFFSET ( x)
Value:

Definition at line 121 of file vf_gainmap.c.

◆ FLAGS

Definition at line 122 of file vf_gainmap.c.

◆ GAIN

#define GAIN ( a,
b )
Value:
(sign * log2f(((a) + alt_off) / ((b) + base_off)))
#define log2f(x)
Definition math.h:27
int a
#define b
Definition input.c:43

Referenced by slice_internal().

Enumeration Type Documentation

◆ GainMapColorspace

Enumerator
GAINMAP_CSP_BASE 

apply the map in the base rendition's space

GAINMAP_CSP_ALT 

apply it in the alternate rendition's space

GAINMAP_CSP_NB 

Definition at line 44 of file vf_gainmap.c.

◆ GainMapMode

Enumerator
GAINMAP_RGB 

one channel per component

GAINMAP_LUMA 

single channel, from the luminance

GAINMAP_MAXRGB 

single channel, from max(R,G,B)

GAINMAP_MODE_NB 

Definition at line 50 of file vf_gainmap.c.

◆ GainMapMeasure

Enumerator
MEASURE_MIN 
MEASURE_MAX 
MEASURE_GAMMA 
MEASURE_RANGE 
MEASURE_ALL 

Definition at line 57 of file vf_gainmap.c.

Function Documentation

◆ FRAMESYNC_DEFINE_CLASS()

FRAMESYNC_DEFINE_CLASS ( gainmap ,
GainMapContext ,
fs  )

◆ quantq()

static AVRational quantq ( double x,
enum AVRounding rnd )
static

Definition at line 145 of file vf_gainmap.c.

Referenced by choose_quant_params(), and setup_colorspace().

◆ setup_range()

static void setup_range ( AVFilterContext * ctx,
int ch,
AVRational min,
AVRational max )
static

Definition at line 161 of file vf_gainmap.c.

Referenced by choose_quant_params(), init(), and setup_colorspace().

◆ setup_gamma()

static void setup_gamma ( AVFilterContext * ctx,
int ch,
AVRational gamma )
static

Definition at line 180 of file vf_gainmap.c.

Referenced by choose_quant_params(), init(), and setup_colorspace().

◆ init()

static av_cold int init ( AVFilterContext * ctx)
static

Definition at line 189 of file vf_gainmap.c.

◆ query_formats()

static int query_formats ( const AVFilterContext * ctx,
AVFilterFormatsConfig ** cfg_in,
AVFilterFormatsConfig ** cfg_out )
static

Definition at line 231 of file vf_gainmap.c.

◆ frame_luminance()

static double frame_luminance ( const AVFrame * frame)
static

Definition at line 250 of file vf_gainmap.c.

Referenced by setup_colorspace().

◆ update_eotf()

static void update_eotf ( GainMapEOTF * tf,
enum AVColorTransferCharacteristic trc,
av_csp_eotf_function eotf,
double Lw )
static

Definition at line 268 of file vf_gainmap.c.

Referenced by setup_colorspace().

◆ unknown_if_null()

static const char * unknown_if_null ( const char * s)
static

Definition at line 309 of file vf_gainmap.c.

Referenced by setup_colorspace().

◆ setup_colorspace()

static int setup_colorspace ( AVFilterContext * ctx,
const AVFrame * base,
const AVFrame * alt )
static

Definition at line 315 of file vf_gainmap.c.

Referenced by process_frame().

◆ quantize()

static av_always_inline float quantize ( float gain,
float scale,
float offset,
float gamma )
static

Definition at line 392 of file vf_gainmap.c.

Referenced by slice_internal(), and slice_quant().

◆ get_pixel()

static av_always_inline void get_pixel ( const GainMapContext * s,
float dst[3],
const float *restrict const src[3],
int x,
int alt )
static

Definition at line 399 of file vf_gainmap.c.

Referenced by slice_internal().

◆ slice_internal()

static av_always_inline int slice_internal ( AVFilterContext * ctx,
void * arg,
int jobnr,
int nb_jobs,
int quant )
static

Definition at line 439 of file vf_gainmap.c.

Referenced by slice_gain(), and slice_gain_quant().

◆ slice_gain()

static int slice_gain ( AVFilterContext * ctx,
void * arg,
int jobnr,
int nb_jobs )
static

Definition at line 529 of file vf_gainmap.c.

Referenced by process_frame().

◆ slice_gain_quant()

static int slice_gain_quant ( AVFilterContext * ctx,
void * arg,
int jobnr,
int nb_jobs )
static

Definition at line 534 of file vf_gainmap.c.

Referenced by process_frame().

◆ slice_quant()

static int slice_quant ( AVFilterContext * ctx,
void * arg,
int jobnr,
int nb_jobs )
static

Definition at line 539 of file vf_gainmap.c.

Referenced by process_frame().

◆ choose_quant_params()

static void choose_quant_params ( AVFilterContext * ctx,
const AVFrame * out,
int nb_jobs )
static

Choose gamma such that mean_quant ^ gamma = 0.5; clamp to a sane value range of [1/8, 8] to prevent degenerate encodings. A gamma of >8 would collapse half the encoding space into a single 8-bit value, at which point we're losing more then gaining, and a value of <1/8 limits the amount by which a single high outlier pixel can determine the overall encoding gamma.

We also clamp the mean to [0.01, 0.99] to prevent numerical explosion in the case that the mean is very close to 0 or 1. This is a looser bound than the final gamma clamp, so the exact values chosen do not matter as much.

Definition at line 570 of file vf_gainmap.c.

Referenced by process_frame().

◆ process_frame()

static int process_frame ( FFFrameSync * fs)
static

Definition at line 619 of file vf_gainmap.c.

◆ config_output()

static int config_output ( AVFilterLink * outlink)
static

Definition at line 692 of file vf_gainmap.c.

◆ activate()

static int activate ( AVFilterContext * ctx)
static

Definition at line 727 of file vf_gainmap.c.

◆ uninit()

static av_cold void uninit ( AVFilterContext * ctx)
static

Definition at line 733 of file vf_gainmap.c.

Variable Documentation

◆ gainmap_options

const AVOption gainmap_options[]
static
Initial value:
= {
{ "mode", "quantity the gain is computed over", OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = GAINMAP_RGB }, 0, GAINMAP_MODE_NB - 1, FLAGS, .unit = "mode" },
{ "rgb", "one gain channel per component", 0, AV_OPT_TYPE_CONST, { .i64 = GAINMAP_RGB }, 0, 0, FLAGS, .unit = "mode" },
{ "luma", "single gain channel, from luminance", 0, AV_OPT_TYPE_CONST, { .i64 = GAINMAP_LUMA }, 0, 0, FLAGS, .unit = "mode" },
{ "maxrgb", "single gain channel, from max(R,G,B)", 0, AV_OPT_TYPE_CONST, { .i64 = GAINMAP_MAXRGB }, 0, 0, FLAGS, .unit = "mode" },
{ "colorspace", "rendition whose colour space the map is applied in", OFFSET(colorspace), AV_OPT_TYPE_INT, { .i64 = GAINMAP_CSP_BASE }, 0, GAINMAP_CSP_NB - 1, FLAGS, .unit = "colorspace" },
{ "base", "the base rendition's colour space", 0, AV_OPT_TYPE_CONST, { .i64 = GAINMAP_CSP_BASE }, 0, 0, FLAGS, .unit = "colorspace" },
{ "alternate", "the alternate rendition's colour space", 0, AV_OPT_TYPE_CONST, { .i64 = GAINMAP_CSP_ALT }, 0, 0, FLAGS, .unit = "colorspace" },
{ "min", "override lower bound on the encoded gain, in log2 space", OFFSET(gain_min), AV_OPT_TYPE_RATIONAL, { .dbl = NAN }, -32.0, 32.0, FLAGS },
{ "max", "override upper bound on the encoded gain, in log2 space", OFFSET(gain_max), AV_OPT_TYPE_RATIONAL, { .dbl = NAN }, -32.0, 32.0, FLAGS },
{ "gamma", "override encoding gamma of the stored map (default: measured)", OFFSET(gamma), AV_OPT_TYPE_RATIONAL, { .dbl = NAN }, 0.0001, 100.0, FLAGS },
{ "base_offset", "constant added to the base rendition", OFFSET(base_offset), AV_OPT_TYPE_RATIONAL, { .dbl = 1.0 / 64.0 }, 1e-6, 1.0, FLAGS },
{ "alt_offset", "constant added to the alternate rendition", OFFSET(alt_offset), AV_OPT_TYPE_RATIONAL, { .dbl = 1.0 / 64.0 }, 1e-6, 1.0, FLAGS },
{ "base_nits", "override input luminance of the base rendition", OFFSET(base_nits), AV_OPT_TYPE_RATIONAL, { .dbl = NAN }, 1.0, 10000.0, FLAGS },
{ "alt_nits", "override input luminance of the alternate rendition", OFFSET(alt_nits), AV_OPT_TYPE_RATIONAL, { .dbl = NAN }, 1.0, 10000.0, FLAGS },
{ NULL }
}
#define FLAGS
Definition cmdutils.c:596
#define NULL
Definition coverity.c:32
#define OFFSET(x)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_RATIONAL
Underlying C type is AVRational.
Definition opt.h:279
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define NAN
Definition swscale.c:71
@ GAINMAP_CSP_ALT
apply it in the alternate rendition's space
Definition vf_gainmap.c:46
@ GAINMAP_CSP_NB
Definition vf_gainmap.c:47
@ GAINMAP_CSP_BASE
apply the map in the base rendition's space
Definition vf_gainmap.c:45
@ GAINMAP_RGB
one channel per component
Definition vf_gainmap.c:51
@ GAINMAP_MODE_NB
Definition vf_gainmap.c:54
@ GAINMAP_LUMA
single channel, from the luminance
Definition vf_gainmap.c:52
@ GAINMAP_MAXRGB
single channel, from max(R,G,B)
Definition vf_gainmap.c:53

Definition at line 124 of file vf_gainmap.c.

◆ gbr_order

const int gbr_order[3] = { 2, 0, 1 }
static

Definition at line 436 of file vf_gainmap.c.

◆ gainmap_inputs

const AVFilterPad gainmap_inputs[]
static
Initial value:
= {
{
.name = "base",
},
{
.name = "alternate",
},
}
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200

Definition at line 742 of file vf_gainmap.c.

◆ gainmap_outputs

const AVFilterPad gainmap_outputs[]
static
Initial value:
= {
{
.name = "default",
.config_props = config_output,
},
}
static int config_output(AVBitStreamFilterLink *outlink)

Definition at line 753 of file vf_gainmap.c.

◆ ff_vf_gainmap

const FFFilter ff_vf_gainmap
Initial value:
= {
.p.name = "gainmap",
.p.description = NULL_IF_CONFIG_SMALL("Generate a gain map from a base/alternate pair."),
.p.priv_class = &gainmap_class,
.preinit = gainmap_framesync_preinit,
.priv_size = sizeof(GainMapContext),
.init = init,
}
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:172
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
static const AVFilterPad gainmap_outputs[]
Definition vf_gainmap.c:753
static const AVFilterPad gainmap_inputs[]
Definition vf_gainmap.c:742

Definition at line 761 of file vf_gainmap.c.