FFmpeg
Data Structures | Macros | Functions | Variables
vf_colorconstancy.c File Reference

Color Constancy filter. More...

#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 query_formats (AVFilterContext *ctx)
 
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

#define GREY_EDGE   "greyedge"

Definition at line 42 of file vf_colorconstancy.c.

#define SQRT3   1.73205080757

Definition at line 44 of file vf_colorconstancy.c.

Referenced by diagonal_transformation().

#define NUM_PLANES   3
#define MAX_DIFF_ORD   2

Definition at line 47 of file vf_colorconstancy.c.

#define MAX_META_DATA   4

Definition at line 48 of file vf_colorconstancy.c.

#define MAX_DATA   4

Definition at line 49 of file vf_colorconstancy.c.

#define INDEX_TEMP   0

Definition at line 51 of file vf_colorconstancy.c.

Referenced by get_derivative().

#define INDEX_DX   1

Definition at line 52 of file vf_colorconstancy.c.

Referenced by get_derivative(), and slice_normalize().

#define INDEX_DY   2

Definition at line 53 of file vf_colorconstancy.c.

Referenced by get_derivative(), and slice_normalize().

#define INDEX_DXY   3

Definition at line 54 of file vf_colorconstancy.c.

Referenced by get_derivative(), and slice_normalize().

#define INDEX_NORM   INDEX_DX

Definition at line 55 of file vf_colorconstancy.c.

Referenced by filter_slice_grey_edge(), get_derivative(), and slice_normalize().

#define INDEX_SRC   0

Definition at line 56 of file vf_colorconstancy.c.

Referenced by get_deriv(), and slice_get_derivative().

#define INDEX_DST   1
#define INDEX_ORD   2

Definition at line 58 of file vf_colorconstancy.c.

Referenced by get_deriv(), and slice_get_derivative().

#define INDEX_DIR   3

Definition at line 59 of file vf_colorconstancy.c.

Referenced by get_deriv(), and slice_get_derivative().

#define DIR_X   0

Definition at line 60 of file vf_colorconstancy.c.

Referenced by get_derivative(), and slice_get_derivative().

#define DIR_Y   1

Definition at line 61 of file vf_colorconstancy.c.

Referenced by get_derivative().

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

Definition at line 92 of file vf_colorconstancy.c.

Definition at line 93 of file vf_colorconstancy.c.

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

Definition at line 95 of file vf_colorconstancy.c.

Referenced by set_gauss(), and slice_get_derivative().

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

Definition at line 234 of file vf_colorconstancy.c.

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

Definition at line 235 of file vf_colorconstancy.c.

Referenced by filter_slice_grey_edge(), and slice_get_derivative().

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

Definition at line 236 of file vf_colorconstancy.c.

Referenced by slice_get_derivative().

Function Documentation

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().

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

Referenced by filter_grey_edge(), and 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 214 of file vf_colorconstancy.c.

Referenced by filter_grey_edge().

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

Referenced by get_deriv().

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

Referenced by filter_grey_edge().

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

Referenced by 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().

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().

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().

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().

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().

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().

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().

static int query_formats ( AVFilterContext ctx)
static

Definition at line 639 of file vf_colorconstancy.c.

static int config_props ( AVFilterLink inlink)
static

Definition at line 651 of file vf_colorconstancy.c.

static int filter_frame ( AVFilterLink inlink,
AVFrame in 
)
static

Definition at line 679 of file vf_colorconstancy.c.

static av_cold void uninit ( AVFilterContext ctx)
static

Definition at line 706 of file vf_colorconstancy.c.

Variable Documentation

const AVFilterPad colorconstancy_inputs[]
static
Initial value:
= {
{
.name = "default",
.config_props = config_props,
.filter_frame = filter_frame,
},
{ NULL }
}
#define NULL
Definition: coverity.c:32
static int config_props(AVFilterLink *inlink)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)

Definition at line 717 of file vf_colorconstancy.c.

const AVFilterPad colorconstancy_outputs[]
static
Initial value:
= {
{
.name = "default",
},
{ NULL }
}
#define NULL
Definition: coverity.c:32

Definition at line 727 of file vf_colorconstancy.c.