FFmpeg
Data Structures | Macros | Functions | Variables
vf_colorconstancy.c File Reference
#include "libavutil/imgutils.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "avfilter.h"
#include "formats.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)
 

Variables

static const AVFilterPad colorconstancy_inputs []
 
static const AVFilterPad colorconstancy_outputs []
 

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 42 of file vf_colorconstancy.c.

◆ SQRT3

#define SQRT3   1.73205080757

Definition at line 44 of file vf_colorconstancy.c.

◆ NUM_PLANES

#define NUM_PLANES   3

Definition at line 46 of file vf_colorconstancy.c.

◆ MAX_DIFF_ORD

#define MAX_DIFF_ORD   2

Definition at line 47 of file vf_colorconstancy.c.

◆ MAX_META_DATA

#define MAX_META_DATA   4

Definition at line 48 of file vf_colorconstancy.c.

◆ MAX_DATA

#define MAX_DATA   4

Definition at line 49 of file vf_colorconstancy.c.

◆ INDEX_TEMP

#define INDEX_TEMP   0

Definition at line 51 of file vf_colorconstancy.c.

◆ INDEX_DX

#define INDEX_DX   1

Definition at line 52 of file vf_colorconstancy.c.

◆ INDEX_DY

#define INDEX_DY   2

Definition at line 53 of file vf_colorconstancy.c.

◆ INDEX_DXY

#define INDEX_DXY   3

Definition at line 54 of file vf_colorconstancy.c.

◆ INDEX_NORM

#define INDEX_NORM   INDEX_DX

Definition at line 55 of file vf_colorconstancy.c.

◆ INDEX_SRC

#define INDEX_SRC   0

Definition at line 56 of file vf_colorconstancy.c.

◆ INDEX_DST

#define INDEX_DST   1

Definition at line 57 of file vf_colorconstancy.c.

◆ INDEX_ORD

#define INDEX_ORD   2

Definition at line 58 of file vf_colorconstancy.c.

◆ INDEX_DIR

#define INDEX_DIR   3

Definition at line 59 of file vf_colorconstancy.c.

◆ DIR_X

#define DIR_X   0

Definition at line 60 of file vf_colorconstancy.c.

◆ DIR_Y

#define DIR_Y   1

Definition at line 61 of file vf_colorconstancy.c.

◆ OFFSET

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

Definition at line 92 of file vf_colorconstancy.c.

◆ FLAGS

Definition at line 93 of file vf_colorconstancy.c.

◆ GINDX

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

Definition at line 95 of file vf_colorconstancy.c.

◆ CLAMP

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

Definition at line 233 of file vf_colorconstancy.c.

◆ INDX2D

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

Definition at line 234 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 235 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 109 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 188 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 213 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 250 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 318 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 364 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 383 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 439 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 488 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 536 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 567 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 593 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 628 of file vf_colorconstancy.c.

Referenced by filter_frame().

◆ config_props()

static int config_props ( AVFilterLink inlink)
static

Definition at line 639 of file vf_colorconstancy.c.

◆ filter_frame()

static int filter_frame ( AVFilterLink inlink,
AVFrame in 
)
static

Definition at line 667 of file vf_colorconstancy.c.

◆ uninit()

static av_cold void uninit ( AVFilterContext ctx)
static

Definition at line 700 of file vf_colorconstancy.c.

Variable Documentation

◆ colorconstancy_inputs

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

Definition at line 711 of file vf_colorconstancy.c.

◆ colorconstancy_outputs

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

Definition at line 720 of file vf_colorconstancy.c.

filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_colorconstancy.c:667
config_props
static int config_props(AVFilterLink *inlink)
Definition: vf_colorconstancy.c:639
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201