FFmpeg
Data Structures | Macros | Functions | Variables
vf_colorconstancy.c File Reference
#include "libavutil/imgutils.h"
#include "libavutil/opt.h"
#include "avfilter.h"
#include "internal.h"
#include "video.h"
#include <math.h>

Go to the source code of this file.

Data Structures

struct  ThreadData
 Used for passing data between threads. More...
 
struct  ColorConstancyContext
 Common struct for all algorithms contexts. More...
 

Macros

#define GREY_EDGE   "greyedge"
 
#define SQRT3   1.73205080757
 
#define NUM_PLANES   3
 
#define MAX_DIFF_ORD   2
 
#define MAX_META_DATA   4
 
#define MAX_DATA   4
 
#define INDEX_TEMP   0
 
#define INDEX_DX   1
 
#define INDEX_DY   2
 
#define INDEX_DXY   3
 
#define INDEX_NORM   INDEX_DX
 
#define INDEX_SRC   0
 
#define INDEX_DST   1
 
#define INDEX_ORD   2
 
#define INDEX_DIR   3
 
#define DIR_X   0
 
#define DIR_Y   1
 
#define OFFSET(x)   offsetof(ColorConstancyContext, x)
 
#define FLAGS   AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
#define GINDX(s, i)   ( (i) - ((s) >> 2) )
 
#define CLAMP(x, mx)   av_clip((x), 0, (mx-1))
 
#define INDX2D(r, c, w)   ( (r) * (w) + (c) )
 
#define GAUSS(s, sr, sc, sls, sh, sw, g)   ( (s)[ INDX2D(CLAMP((sr), (sh)), CLAMP((sc), (sw)), (sls)) ] * (g) )
 

Functions

static int set_gauss (AVFilterContext *ctx)
 Sets gauss filters used for calculating gauss derivatives. More...
 
static void cleanup_derivative_buffers (ThreadData *td, int nb_buff, int nb_planes)
 Frees up buffers used by grey edge for storing derivatives final and intermidiate results. More...
 
static int setup_derivative_buffers (AVFilterContext *ctx, ThreadData *td)
 Allocates buffers used by grey edge for storing derivatives final and intermidiate results. More...
 
static int slice_get_derivative (AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 Slice calculation of gaussian derivatives. More...
 
static int slice_normalize (AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 Slice Frobius normalization of gaussian derivatives. More...
 
static void av_always_inline get_deriv (AVFilterContext *ctx, ThreadData *td, int ord, int dir, int src, int dst, int dim, int nb_threads)
 Utility function for setting up differentiation data/metadata. More...
 
static int get_derivative (AVFilterContext *ctx, ThreadData *td)
 Main control function for calculating gaussian derivatives. More...
 
static int filter_slice_grey_edge (AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 Slice function for grey edge algorithm that does partial summing/maximizing of gaussian derivatives. More...
 
static int filter_grey_edge (AVFilterContext *ctx, AVFrame *in)
 Main control function for grey edge algorithm. More...
 
static void normalize_light (double *light)
 Normalizes estimated illumination since only illumination vector direction is required for color constancy. More...
 
static int illumination_estimation (AVFilterContext *ctx, AVFrame *in)
 Redirects to corresponding algorithm estimation function and performs normalization after estimation. More...
 
static int diagonal_transformation (AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 Performs simple correction via diagonal transformation model. More...
 
static void chromatic_adaptation (AVFilterContext *ctx, AVFrame *in, AVFrame *out)
 Main control function for correcting scene illumination based on estimated illumination. More...
 
static int config_props (AVFilterLink *inlink)
 
static int filter_frame (AVFilterLink *inlink, AVFrame *in)
 
static av_cold void uninit (AVFilterContext *ctx)
 
 AVFILTER_DEFINE_CLASS (greyedge)
 

Variables

static const AVFilterPad colorconstancy_inputs []
 
static const AVOption greyedge_options []
 
const AVFilter ff_vf_greyedge
 

Detailed Description

Color Constancy filter

See also
http://colorconstancy.com/

[J]. van de Weijer, Th. Gevers, A. Gijsenij "Edge-Based Color Constancy".

Definition in file vf_colorconstancy.c.

Macro Definition Documentation

◆ GREY_EDGE

#define GREY_EDGE   "greyedge"

Definition at line 40 of file vf_colorconstancy.c.

◆ SQRT3

#define SQRT3   1.73205080757

Definition at line 42 of file vf_colorconstancy.c.

◆ NUM_PLANES

#define NUM_PLANES   3

Definition at line 44 of file vf_colorconstancy.c.

◆ MAX_DIFF_ORD

#define MAX_DIFF_ORD   2

Definition at line 45 of file vf_colorconstancy.c.

◆ MAX_META_DATA

#define MAX_META_DATA   4

Definition at line 46 of file vf_colorconstancy.c.

◆ MAX_DATA

#define MAX_DATA   4

Definition at line 47 of file vf_colorconstancy.c.

◆ INDEX_TEMP

#define INDEX_TEMP   0

Definition at line 49 of file vf_colorconstancy.c.

◆ INDEX_DX

#define INDEX_DX   1

Definition at line 50 of file vf_colorconstancy.c.

◆ INDEX_DY

#define INDEX_DY   2

Definition at line 51 of file vf_colorconstancy.c.

◆ INDEX_DXY

#define INDEX_DXY   3

Definition at line 52 of file vf_colorconstancy.c.

◆ INDEX_NORM

#define INDEX_NORM   INDEX_DX

Definition at line 53 of file vf_colorconstancy.c.

◆ INDEX_SRC

#define INDEX_SRC   0

Definition at line 54 of file vf_colorconstancy.c.

◆ INDEX_DST

#define INDEX_DST   1

Definition at line 55 of file vf_colorconstancy.c.

◆ INDEX_ORD

#define INDEX_ORD   2

Definition at line 56 of file vf_colorconstancy.c.

◆ INDEX_DIR

#define INDEX_DIR   3

Definition at line 57 of file vf_colorconstancy.c.

◆ DIR_X

#define DIR_X   0

Definition at line 58 of file vf_colorconstancy.c.

◆ DIR_Y

#define DIR_Y   1

Definition at line 59 of file vf_colorconstancy.c.

◆ OFFSET

#define OFFSET (   x)    offsetof(ColorConstancyContext, x)

Definition at line 90 of file vf_colorconstancy.c.

◆ FLAGS

Definition at line 91 of file vf_colorconstancy.c.

◆ GINDX

#define GINDX (   s,
  i 
)    ( (i) - ((s) >> 2) )

Definition at line 93 of file vf_colorconstancy.c.

◆ CLAMP

#define CLAMP (   x,
  mx 
)    av_clip((x), 0, (mx-1))

Definition at line 231 of file vf_colorconstancy.c.

◆ INDX2D

#define INDX2D (   r,
  c,
  w 
)    ( (r) * (w) + (c) )

Definition at line 232 of file vf_colorconstancy.c.

◆ GAUSS

#define GAUSS (   s,
  sr,
  sc,
  sls,
  sh,
  sw,
  g 
)    ( (s)[ INDX2D(CLAMP((sr), (sh)), CLAMP((sc), (sw)), (sls)) ] * (g) )

Definition at line 233 of file vf_colorconstancy.c.

Function Documentation

◆ set_gauss()

static int set_gauss ( AVFilterContext ctx)
static

Sets gauss filters used for calculating gauss derivatives.

Filter size depends on sigma which is a user option hence we calculate these filters each time. Also each higher order depends on lower ones. Sigma can be zero only at difford = 0, then we only convert data to double instead.

Parameters
ctxthe filter context.
Returns
0 in case of success, a negative value corresponding to an AVERROR code in case of failure.

Definition at line 107 of file vf_colorconstancy.c.

Referenced by config_props().

◆ cleanup_derivative_buffers()

static void cleanup_derivative_buffers ( ThreadData td,
int  nb_buff,
int  nb_planes 
)
static

Frees up buffers used by grey edge for storing derivatives final and intermidiate results.

Number of buffers and number of planes for last buffer are given so it can be safely called at allocation failure instances.

Parameters
tdholds the buffers.
nb_buffnumber of buffers to be freed.
nb_planesnumber of planes for last buffer to be freed.

Definition at line 186 of file vf_colorconstancy.c.

Referenced by filter_grey_edge(), and setup_derivative_buffers().

◆ setup_derivative_buffers()

static int setup_derivative_buffers ( AVFilterContext ctx,
ThreadData td 
)
static

Allocates buffers used by grey edge for storing derivatives final and intermidiate results.

Parameters
ctxthe filter context.
tdholds the buffers.
Returns
0 in case of success, a negative value corresponding to an AVERROR code in case of failure.

Definition at line 211 of file vf_colorconstancy.c.

Referenced by filter_grey_edge().

◆ slice_get_derivative()

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

Slice calculation of gaussian derivatives.

Applies 1-D gaussian derivative filter either horizontally or vertically according to meta data given in thread data. When convoluting horizontally source is always the in frame withing thread data while when convoluting vertically source is a buffer.

Parameters
ctxthe filter context.
argdata to be passed between threads.
jobnrcurrent job nubmer.
nb_jobstotal number of jobs.
Returns
0.

Applying gauss horizontally along each row

Applying gauss vertically along each column

Definition at line 248 of file vf_colorconstancy.c.

Referenced by get_deriv().

◆ slice_normalize()

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

Slice Frobius normalization of gaussian derivatives.

Only called for difford values of 1 or 2.

Parameters
ctxthe filter context.
argdata to be passed between threads.
jobnrcurrent job nubmer.
nb_jobstotal number of jobs.
Returns
0.

Definition at line 316 of file vf_colorconstancy.c.

Referenced by filter_grey_edge().

◆ get_deriv()

static void av_always_inline get_deriv ( AVFilterContext ctx,
ThreadData td,
int  ord,
int  dir,
int  src,
int  dst,
int  dim,
int  nb_threads 
)
static

Utility function for setting up differentiation data/metadata.

Parameters
ctxthe filter context.
tdto be used for passing data between threads.
ordord of differentiation.
dirdirection of differentiation.
srcindex of source used for differentiation.
dstindex destination used for saving differentiation result.
dimmaximum dimension in current direction.
nb_threadsnumber of threads to use.

Definition at line 362 of file vf_colorconstancy.c.

Referenced by get_derivative().

◆ get_derivative()

static int get_derivative ( AVFilterContext ctx,
ThreadData td 
)
static

Main control function for calculating gaussian derivatives.

Parameters
ctxthe filter context.
tdholds the buffers used for storing results.
Returns
0 in case of success, a negative value corresponding to an AVERROR code in case of failure.

Definition at line 381 of file vf_colorconstancy.c.

Referenced by filter_grey_edge().

◆ filter_slice_grey_edge()

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

Slice function for grey edge algorithm that does partial summing/maximizing of gaussian derivatives.

Parameters
ctxthe filter context.
argdata to be passed between threads.
jobnrcurrent job nubmer.
nb_jobstotal number of jobs.
Returns
0.

Definition at line 437 of file vf_colorconstancy.c.

Referenced by filter_grey_edge().

◆ filter_grey_edge()

static int filter_grey_edge ( AVFilterContext ctx,
AVFrame in 
)
static

Main control function for grey edge algorithm.

Parameters
ctxthe filter context.
inframe to perfrom grey edge on.
Returns
0 in case of success, a negative value corresponding to an AVERROR code in case of failure.

Definition at line 486 of file vf_colorconstancy.c.

Referenced by illumination_estimation().

◆ normalize_light()

static void normalize_light ( double light)
static

Normalizes estimated illumination since only illumination vector direction is required for color constancy.

Parameters
lightthe estimated illumination to be normalized in place

Definition at line 534 of file vf_colorconstancy.c.

Referenced by illumination_estimation().

◆ illumination_estimation()

static int illumination_estimation ( AVFilterContext ctx,
AVFrame in 
)
static

Redirects to corresponding algorithm estimation function and performs normalization after estimation.

Parameters
ctxthe filter context.
inframe to perfrom estimation on.
Returns
0 in case of success, a negative value corresponding to an AVERROR code in case of failure.

Definition at line 565 of file vf_colorconstancy.c.

Referenced by filter_frame().

◆ diagonal_transformation()

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

Performs simple correction via diagonal transformation model.

Parameters
ctxthe filter context.
argdata to be passed between threads.
jobnrcurrent job nubmer.
nb_jobstotal number of jobs.
Returns
0.

Definition at line 591 of file vf_colorconstancy.c.

Referenced by chromatic_adaptation().

◆ chromatic_adaptation()

static void chromatic_adaptation ( AVFilterContext ctx,
AVFrame in,
AVFrame out 
)
static

Main control function for correcting scene illumination based on estimated illumination.

Parameters
ctxthe filter context.
inholds frame to correct
outholds corrected frame

Definition at line 626 of file vf_colorconstancy.c.

Referenced by filter_frame().

◆ config_props()

static int config_props ( AVFilterLink inlink)
static

Definition at line 637 of file vf_colorconstancy.c.

◆ filter_frame()

static int filter_frame ( AVFilterLink inlink,
AVFrame in 
)
static

Definition at line 665 of file vf_colorconstancy.c.

◆ uninit()

static av_cold void uninit ( AVFilterContext ctx)
static

Definition at line 698 of file vf_colorconstancy.c.

◆ AVFILTER_DEFINE_CLASS()

AVFILTER_DEFINE_CLASS ( greyedge  )

Variable Documentation

◆ colorconstancy_inputs

const AVFilterPad colorconstancy_inputs[]
static
Initial value:
= {
{
.name = "default",
.config_props = config_props,
.filter_frame = filter_frame,
},
}

Definition at line 709 of file vf_colorconstancy.c.

◆ greyedge_options

const AVOption greyedge_options[]
static
Initial value:
= {
{ "difford", "set differentiation order", OFFSET(difford), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, FLAGS },
{ "minknorm", "set Minkowski norm", OFFSET(minknorm), AV_OPT_TYPE_INT, {.i64=1}, 0, 20, FLAGS },
{ "sigma", "set sigma", OFFSET(sigma), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, 1024.0, FLAGS },
{ NULL }
}

Definition at line 718 of file vf_colorconstancy.c.

◆ ff_vf_greyedge

const AVFilter ff_vf_greyedge
Initial value:
= {
.name = GREY_EDGE,
.description = NULL_IF_CONFIG_SMALL("Estimates scene illumination by grey edge assumption."),
.priv_size = sizeof(ColorConstancyContext),
.priv_class = &greyedge_class,
}

Definition at line 727 of file vf_colorconstancy.c.

FLAGS
#define FLAGS
Definition: vf_colorconstancy.c:91
ColorConstancyContext
Common struct for all algorithms contexts.
Definition: vf_colorconstancy.c:73
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_colorconstancy.c:698
OFFSET
#define OFFSET(x)
Definition: vf_colorconstancy.c:90
GREY_EDGE
#define GREY_EDGE
Definition: vf_colorconstancy.c:40
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_colorconstancy.c:665
ff_video_default_filterpad
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
config_props
static int config_props(AVFilterLink *inlink)
Definition: vf_colorconstancy.c:637
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:237
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
NULL
#define NULL
Definition: coverity.c:32
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:147
FILTER_SINGLE_PIXFMT
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition: internal.h:172
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
colorconstancy_inputs
static const AVFilterPad colorconstancy_inputs[]
Definition: vf_colorconstancy.c:709
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:165
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183