42 #define GREY_EDGE "greyedge"
44 #define SQRT3 1.73205080757
47 #define MAX_DIFF_ORD 2
48 #define MAX_META_DATA 4
55 #define INDEX_NORM INDEX_DX
92 #define OFFSET(x) offsetof(ColorConstancyContext, x)
93 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
95 #define GINDX(s, i) ( (i) - ((s) >> 2) )
112 int filtersize =
s->filtersize;
113 int difford =
s->difford;
114 double sigma =
s->sigma;
118 for (
i = 0;
i <= difford; ++
i) {
121 for (;
i >= 0; --
i) {
134 for (
i = 0;
i < filtersize; ++
i) {
135 s->gauss[0][
i] =
exp(- pow(
GINDX(filtersize,
i), 2.) / (2 * sigma * sigma)) / ( sqrt(2 *
M_PI) * sigma );
136 sum1 +=
s->gauss[0][
i];
138 for (
i = 0;
i < filtersize; ++
i) {
139 s->gauss[0][
i] /= sum1;
146 for (
i = 0;
i < filtersize; ++
i) {
147 s->gauss[1][
i] = - (
GINDX(filtersize,
i) / pow(sigma, 2)) *
s->gauss[0][
i];
148 sum1 +=
s->gauss[1][
i] *
GINDX(filtersize,
i);
151 for (
i = 0;
i < filtersize; ++
i) {
152 s->gauss[1][
i] /= sum1;
159 for (
i = 0;
i < filtersize; ++
i) {
160 s->gauss[2][
i] = ( pow(
GINDX(filtersize,
i), 2) / pow(sigma, 4) - 1/pow(sigma, 2) )
162 sum1 +=
s->gauss[2][
i];
166 for (
i = 0;
i < filtersize; ++
i) {
167 s->gauss[2][
i] -= sum1 / (filtersize);
168 sum2 += (0.5 *
GINDX(filtersize,
i) *
GINDX(filtersize,
i) *
s->gauss[2][
i]);
170 for (
i = 0;
i < filtersize ; ++
i) {
171 s->gauss[2][
i] /= sum2;
192 for (
b = 0;
b < nb_buff; ++
b) {
198 for (p = 0; p < nb_planes; ++p) {
216 int nb_buff =
s->difford + 1;
220 for (
b = 0;
b <= nb_buff; ++
b) {
223 if (!
td->data[
b][p]) {
232 #define CLAMP(x, mx) av_clip((x), 0, (mx-1))
233 #define INDX2D(r, c, w) ( (r) * (w) + (c) )
234 #define GAUSS(s, sr, sc, sls, sh, sw, g) ( (s)[ INDX2D(CLAMP((sr), (sh)), CLAMP((sc), (sw)), (sls)) ] * (g) )
258 const int filtersize =
s->filtersize;
259 const double *gauss =
s->gauss[ord];
262 for (plane = 0; plane <
NUM_PLANES; ++plane) {
263 const int height =
s->planeheight[plane];
264 const int width =
s->planewidth[plane];
265 const int in_linesize =
in->linesize[plane];
266 double *dst =
td->data[dst_index][plane];
273 slice_start = (
height * jobnr ) / nb_jobs;
279 for (
g = 0;
g < filtersize; ++
g) {
287 const double *
src =
td->data[src_index][plane];
288 slice_start = (
width * jobnr ) / nb_jobs;
294 for (
g = 0;
g < filtersize; ++
g) {
321 const int difford =
s->difford;
324 for (plane = 0; plane <
NUM_PLANES; ++plane) {
325 const int height =
s->planeheight[plane];
326 const int width =
s->planewidth[plane];
328 const int slice_start = (numpixels * jobnr ) / nb_jobs;
329 const int slice_end = (numpixels * (jobnr+1)) / nb_jobs;
337 norm[
i] = sqrt( pow(dx[
i], 2) + pow(dy[
i], 2));
342 norm[
i] = sqrt( pow(dx[
i], 2) + 4 * pow(dxy[
i], 2) + pow(dy[
i], 2) );
364 int src,
int dst,
int dim,
int nb_threads) {
384 int nb_threads =
s->nb_threads;
385 int height =
s->planeheight[1];
386 int width =
s->planewidth[1];
442 int minknorm =
s->minknorm;
446 for (plane = 0; plane <
NUM_PLANES; ++plane) {
447 const int height =
s->planeheight[plane];
448 const int width =
s->planewidth[plane];
449 const int in_linesize =
in->linesize[plane];
450 const int slice_start = (
height * jobnr) / nb_jobs;
452 const uint8_t *img_data =
in->data[plane];
462 * (img_data[
INDX2D(
r,
c, in_linesize)] < thresh) );
469 * (img_data[
INDX2D(
r,
c, in_linesize)] < thresh) );
490 int minknorm =
s->minknorm;
491 int difford =
s->difford;
492 double *white =
s->white;
493 int nb_jobs =
FFMIN3(
s->planeheight[1],
s->planewidth[1],
s->nb_threads);
508 for (plane = 0; plane <
NUM_PLANES; ++plane) {
510 for (job = 0; job < nb_jobs; ++job) {
515 for (plane = 0; plane <
NUM_PLANES; ++plane) {
517 for (job = 0; job < nb_jobs; ++job) {
520 white[plane] = pow(white[plane], 1./minknorm);
536 double abs_val = pow( pow(light[0], 2.0) + pow(light[1], 2.0) + pow(light[2], 2.0), 0.5);
542 for (plane = 0; plane <
NUM_PLANES; ++plane) {
546 for (plane = 0; plane <
NUM_PLANES; ++plane) {
547 light[plane] = (light[plane] / abs_val);
573 s->white[0],
s->white[1],
s->white[2]);
576 s->white[0],
s->white[1],
s->white[2]);
599 for (plane = 0; plane <
NUM_PLANES; ++plane) {
600 const int height =
s->planeheight[plane];
601 const int width =
s->planewidth[plane];
603 const int slice_start = (numpixels * jobnr) / nb_jobs;
604 const int slice_end = (numpixels * (jobnr+1)) / nb_jobs;
630 int nb_jobs =
FFMIN3(
s->planeheight[1],
s->planewidth[1],
s->nb_threads);
654 const double break_off_sigma = 3.0;
655 double sigma =
s->sigma;
658 if (!
floor(break_off_sigma * sigma + 0.5) &&
s->difford) {
663 s->filtersize = 2 *
floor(break_off_sigma * sigma + 0.5) + 1;
670 s->planewidth[0] =
s->planewidth[3] = inlink->
w;
672 s->planeheight[0] =
s->planeheight[3] = inlink->
h;
713 int difford =
s->difford;
716 for (
i = 0;
i <= difford; ++
i) {
739 #if CONFIG_GREYEDGE_FILTER
741 static const AVOption greyedge_options[] = {
754 .priv_class = &greyedge_class,
static const AVFilterPad inputs[]
static const AVFilterPad outputs[]
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Main libavfilter public API header.
#define flags(name, subs,...)
#define AV_CEIL_RSHIFT(a, b)
static __device__ float fabs(float a)
static __device__ float floor(float a)
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
static void direct(const float *in, const FFTComplex *ir, int len, float *out)
#define AVFILTER_DEFINE_CLASS(fname)
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static enum AVPixelFormat pix_fmts[]
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Describe the class of an AVClass context structure.
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVFilterContext * dst
dest filter
int format
agreed upon media format
A filter pad used for either input or output.
const char * name
Pad name.
const char * name
Filter name.
AVFormatInternal * internal
An opaque field for libavformat internal usage.
This structure describes decoded (raw) audio or video data.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Common struct for all algorithms contexts.
int minknorm
@minknorm = 0 : getMax instead
double * gauss[MAX_DIFF_ORD+1]
Used for passing data between threads.
double * data[MAX_DATA][NUM_PLANES]
int meta_data[MAX_META_DATA]
static const AVFilterPad colorconstancy_inputs[]
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.
#define GAUSS(s, sr, sc, sls, sh, sw, g)
static int diagonal_transformation(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Performs simple correction via diagonal transformation model.
static void normalize_light(double *light)
Normalizes estimated illumination since only illumination vector direction is required for color cons...
static int query_formats(AVFilterContext *ctx)
static int setup_derivative_buffers(AVFilterContext *ctx, ThreadData *td)
Allocates buffers used by grey edge for storing derivatives final and intermidiate results.
static int filter_grey_edge(AVFilterContext *ctx, AVFrame *in)
Main control function for grey edge algorithm.
static int slice_normalize(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Slice Frobius normalization of gaussian derivatives.
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static const AVFilterPad colorconstancy_outputs[]
static int get_derivative(AVFilterContext *ctx, ThreadData *td)
Main control function for calculating gaussian derivatives.
static int config_props(AVFilterLink *inlink)
static int set_gauss(AVFilterContext *ctx)
Sets gauss filters used for calculating gauss derivatives.
static void chromatic_adaptation(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
Main control function for correcting scene illumination based on estimated illumination.
static av_cold void uninit(AVFilterContext *ctx)
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.
static int illumination_estimation(AVFilterContext *ctx, AVFrame *in)
Redirects to corresponding algorithm estimation function and performs normalization after estimation.
static int slice_get_derivative(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Slice calculation of gaussian derivatives.
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.
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.