FFmpeg
vf_grayworld.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Paul Buxton
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21  /**
22  * @file
23  * Color correction filter based on
24  * https://www.researchgate.net/publication/275213614_A_New_Color_Correction_Method_for_Underwater_Imaging
25  *
26  */
27 
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
30 
31 #include "avfilter.h"
32 #include "internal.h"
33 #include "video.h"
34 
35 typedef struct ThreadData {
36  AVFrame *in, *out;
37  float l_avg;
38  float a_avg;
39  float b_avg;
40 } ThreadData;
41 
42 typedef struct GrayWorldContext {
43  const AVClass *class;
44  float *tmpplab;
46  float *line_sum;
48 
49 #define OFFSET(x) offsetof(GrayWorldContext, x)
50 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
51 static const AVOption grayworld_options[] = {
52  { NULL }
53 };
54 
55 AVFILTER_DEFINE_CLASS(grayworld);
56 
57 static void apply_matrix(const float matrix[3][3], const float input[3], float output[3])
58 {
59  output[0] = matrix[0][0] * input[0] + matrix[0][1] * input[1] + matrix[0][2] * input[2];
60  output[1] = matrix[1][0] * input[0] + matrix[1][1] * input[1] + matrix[1][2] * input[2];
61  output[2] = matrix[2][0] * input[0] + matrix[2][1] * input[1] + matrix[2][2] * input[2];
62 }
63 
64 static const float lms2lab[3][3] = {
65  {0.5774, 0.5774, 0.5774},
66  {0.40825, 0.40825, -0.816458},
67  {0.707, -0.707, 0}
68 };
69 
70 static const float lab2lms[3][3] = {
71  {0.57735, 0.40825, 0.707},
72  {0.57735, 0.40825, -0.707},
73  {0.57735, -0.8165, 0}
74 };
75 
76 static const float rgb2lms[3][3] = {
77  {0.3811, 0.5783, 0.0402},
78  {0.1967, 0.7244, 0.0782},
79  {0.0241, 0.1288, 0.8444}
80 };
81 
82 static const float lms2rgb[3][3] = {
83  {4.4679, -3.5873, 0.1193},
84  {-1.2186, 2.3809, -0.1624},
85  {0.0497, -0.2439, 1.2045}
86 };
87 
88 /**
89  * Convert from Linear RGB to logspace LAB
90  *
91  * @param rgb Input array of rgb components
92  * @param lab output array of lab components
93  */
94 static void rgb2lab(const float rgb[3], float lab[3])
95 {
96  float lms[3];
97 
98  apply_matrix(rgb2lms, rgb, lms);
99  lms[0] = lms[0] > 0.f ? logf(lms[0]) : -1024.f;
100  lms[1] = lms[1] > 0.f ? logf(lms[1]) : -1024.f;
101  lms[2] = lms[2] > 0.f ? logf(lms[2]) : -1024.f;
102  apply_matrix(lms2lab, lms, lab);
103 }
104 
105 /**
106  * Convert from Logspace LAB to Linear RGB
107  *
108  * @param lab input array of lab components
109  * @param rgb output array of rgb components
110  */
111 static void lab2rgb(const float lab[3], float rgb[3])
112 {
113  float lms[3];
114 
115  apply_matrix(lab2lms, lab, lms);
116  lms[0] = expf(lms[0]);
117  lms[1] = expf(lms[1]);
118  lms[2] = expf(lms[2]);
119  apply_matrix(lms2rgb, lms, rgb);
120 }
121 
122 /**
123  * Convert a frame from linear RGB to logspace LAB, and accumulate channel totals for each row
124  * Convert from RGB -> lms using equation 4 in color transfer paper.
125  *
126  * @param ctx Filter context
127  * @param arg Thread data pointer
128  * @param jobnr job number
129  * @param nb_jobs number of jobs
130  */
131 static int convert_frame(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
132 {
133  GrayWorldContext *s = ctx->priv;
134  ThreadData *td = arg;
135  AVFrame *in = td->in;
136  AVFrame *out = td->out;
137  AVFilterLink *outlink = ctx->outputs[0];
138  const int slice_start = (out->height * jobnr) / nb_jobs;
139  const int slice_end = (out->height * (jobnr + 1)) / nb_jobs;
140  float rgb[3], lab[3];
141 
142  for (int i = slice_start; i < slice_end; i++) {
143  float *b_in_row = (float *)(in->data[1] + i * in->linesize[1]);
144  float *g_in_row = (float *)(in->data[0] + i * in->linesize[0]);
145  float *r_in_row = (float *)(in->data[2] + i * in->linesize[2]);
146  float *acur = s->tmpplab + i * outlink->w + outlink->w * outlink->h;
147  float *bcur = s->tmpplab + i * outlink->w + 2 * outlink->w * outlink->h;
148  float *lcur = s->tmpplab + i * outlink->w;
149 
150  s->line_sum[i] = 0.f;
151  s->line_sum[i + outlink->h] = 0.f;
152  s->line_count_pels[i] = 0;
153 
154  for (int j = 0; j < outlink->w; j++) {
155  rgb[0] = r_in_row[j];
156  rgb[1] = g_in_row[j];
157  rgb[2] = b_in_row[j];
158  rgb2lab(rgb, lab);
159  *(lcur++) = lab[0];
160  *(acur++) = lab[1];
161  *(bcur++) = lab[2];
162  s->line_sum[i] += lab[1];
163  s->line_sum[i + outlink->h] += lab[2];
164  s->line_count_pels[i]++;
165  }
166  }
167  return 0;
168 }
169 
170 /**
171  * Sum the channel totals and compute the mean for each channel
172  *
173  * @param s Frame context
174  * @param td thread data
175  */
177 {
178  float asum = 0.f, bsum = 0.f;
179  int pixels = 0;
180 
181  for (int y = 0; y < td->out->height; y++) {
182  asum += s->line_sum[y];
183  bsum += s->line_sum[y + td->out->height];
184  pixels += s->line_count_pels[y];
185  }
186 
187  td->a_avg = asum / pixels;
188  td->b_avg = bsum / pixels;
189 }
190 
191 /**
192  * Subtract the mean logspace AB values from each pixel.
193  *
194  * @param ctx Filter context
195  * @param arg Thread data pointer
196  * @param jobnr job number
197  * @param nb_jobs number of jobs
198  */
199 static int correct_frame(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
200 {
201  GrayWorldContext *s = ctx->priv;
202  ThreadData *td = arg;
203  AVFrame *out = td->out;
204  AVFilterLink *outlink = ctx->outputs[0];
205  const int slice_start = (out->height * jobnr) / nb_jobs;
206  const int slice_end = (out->height * (jobnr + 1)) / nb_jobs;
207  float rgb[3], lab[3];
208 
209  for (int i = slice_start; i < slice_end; i++) {
210  float *g_out_row = (float *)(out->data[0] + i * out->linesize[0]);
211  float *b_out_row = (float *)(out->data[1] + i * out->linesize[1]);
212  float *r_out_row = (float *)(out->data[2] + i * out->linesize[2]);
213  float *lcur = s->tmpplab + i * outlink->w;
214  float *acur = s->tmpplab + i * outlink->w + outlink->w * outlink->h;
215  float *bcur = s->tmpplab + i * outlink->w + 2 * outlink->w * outlink->h;
216 
217  for (int j = 0; j < outlink->w; j++) {
218  lab[0] = *lcur++;
219  lab[1] = *acur++;
220  lab[2] = *bcur++;
221 
222  // subtract the average for the color channels
223  lab[1] -= td->a_avg;
224  lab[2] -= td->b_avg;
225 
226  //convert back to linear rgb
227  lab2rgb(lab, rgb);
228  r_out_row[j] = rgb[0];
229  g_out_row[j] = rgb[1];
230  b_out_row[j] = rgb[2];
231  }
232  }
233  return 0;
234 }
235 
237 {
238  GrayWorldContext *s = inlink->dst->priv;
239 
240  FF_ALLOC_TYPED_ARRAY(s->tmpplab, inlink->h * inlink->w * 3);
241  FF_ALLOC_TYPED_ARRAY(s->line_count_pels, inlink->h);
242  FF_ALLOC_TYPED_ARRAY(s->line_sum, inlink->h * 2);
243  if (!s->tmpplab || !s->line_count_pels || !s->line_sum)
244  return AVERROR(ENOMEM);
245 
246  return 0;
247 }
248 
250 {
251  GrayWorldContext *s = ctx->priv;
252 
253  av_freep(&s->tmpplab);
254  av_freep(&s->line_count_pels);
255  av_freep(&s->line_sum);
256 }
257 
259 {
260  AVFilterContext *ctx = inlink->dst;
261  GrayWorldContext *s = ctx->priv;
262  AVFilterLink *outlink = ctx->outputs[0];
263  ThreadData td;
264  AVFrame *out;
265 
266  if (av_frame_is_writable(in)) {
267  out = in;
268  } else {
269  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
270  if (!out) {
271  av_frame_free(&in);
272  return AVERROR(ENOMEM);
273  }
275  }
276  /* input and output transfer will be linear */
277  if (in->color_trc == AVCOL_TRC_UNSPECIFIED) {
278  av_log(s, AV_LOG_WARNING, "Untagged transfer, assuming linear light.\n");
279  out->color_trc = AVCOL_TRC_LINEAR;
280  } else if (in->color_trc != AVCOL_TRC_LINEAR) {
281  av_log(s, AV_LOG_WARNING, "Gray world color correction works on linear light only.\n");
282  }
283 
284  td.in = in;
285  td.out = out;
286 
290 
291  if (in != out) {
292  av_image_copy_plane(out->data[3], out->linesize[3],
293  in->data[3], in->linesize[3], outlink->w * 4, outlink->h);
294  av_frame_free(&in);
295  }
296 
297  return ff_filter_frame(outlink, out);
298 }
299 
300 static const AVFilterPad grayworld_inputs[] = {
301  {
302  .name = "default",
303  .type = AVMEDIA_TYPE_VIDEO,
304  .filter_frame = filter_frame,
305  .config_props = config_input,
306  }
307 };
308 
310  .name = "grayworld",
311  .description = NULL_IF_CONFIG_SMALL("Adjust white balance using LAB gray world algorithm"),
312  .priv_size = sizeof(GrayWorldContext),
313  .priv_class = &grayworld_class,
318  .uninit = uninit,
319 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:108
AVFrame::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: frame.h:660
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
td
#define td
Definition: regdef.h:70
convert_frame
static int convert_frame(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Convert a frame from linear RGB to logspace LAB, and accumulate channel totals for each row Convert f...
Definition: vf_grayworld.c:131
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
AVCOL_TRC_LINEAR
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition: pixfmt.h:579
matrix
Definition: vc1dsp.c:42
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_grayworld.c:249
AVOption
AVOption.
Definition: opt.h:251
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:573
expf
#define expf(x)
Definition: libm.h:283
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_grayworld.c:258
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:526
video.h
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:153
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
av_image_copy_plane
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:374
rgb
Definition: rpzaenc.c:60
FF_ALLOC_TYPED_ARRAY
#define FF_ALLOC_TYPED_ARRAY(p, nelem)
Definition: internal.h:87
compute_correction
static void compute_correction(GrayWorldContext *s, ThreadData *td)
Sum the channel totals and compute the mean for each channel.
Definition: vf_grayworld.c:176
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
lms2rgb
static const float lms2rgb[3][3]
Definition: vf_grayworld.c:82
av_cold
#define av_cold
Definition: attributes.h:90
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:36
GrayWorldContext::line_sum
float * line_sum
Definition: vf_grayworld.c:46
s
#define s(width, name)
Definition: cbs_vp9.c:198
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:1979
ctx
AVFormatContext * ctx
Definition: movenc.c:48
lab2rgb
static void lab2rgb(const float lab[3], float rgb[3])
Convert from Logspace LAB to Linear RGB.
Definition: vf_grayworld.c:111
apply_matrix
static void apply_matrix(const float matrix[3][3], const float input[3], float output[3])
Definition: vf_grayworld.c:57
ThreadData::b_avg
float b_avg
Definition: vf_grayworld.c:39
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
arg
const char * arg
Definition: jacosubdec.c:67
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:736
ThreadData::a_avg
float a_avg
Definition: vf_grayworld.c:38
ff_vf_grayworld
const AVFilter ff_vf_grayworld
Definition: vf_grayworld.c:309
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
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_grayworld.c:236
FILTER_PIXFMTS
#define FILTER_PIXFMTS(...)
Definition: internal.h:178
AV_PIX_FMT_GBRPF32
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:498
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:666
GrayWorldContext
Definition: vf_grayworld.c:42
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
internal.h
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
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(grayworld)
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:786
ThreadData
Used for passing data between threads.
Definition: dsddec.c:69
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
AVFilter
Filter definition.
Definition: avfilter.h:166
rgb2lms
static const float rgb2lms[3][3]
Definition: vf_grayworld.c:76
rgb2lab
static void rgb2lab(const float rgb[3], float lab[3])
Convert from Linear RGB to logspace LAB.
Definition: vf_grayworld.c:94
lms2lab
static const float lms2lab[3][3]
Definition: vf_grayworld.c:64
avfilter.h
AV_PIX_FMT_GBRAPF32
#define AV_PIX_FMT_GBRAPF32
Definition: pixfmt.h:499
AVFilterContext
An instance of a filter.
Definition: avfilter.h:397
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:193
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
ThreadData::l_avg
float l_avg
Definition: vf_grayworld.c:37
GrayWorldContext::tmpplab
float * tmpplab
Definition: vf_grayworld.c:44
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
lab2lms
static const float lab2lms[3][3]
Definition: vf_grayworld.c:70
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
correct_frame
static int correct_frame(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Subtract the mean logspace AB values from each pixel.
Definition: vf_grayworld.c:199
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:144
grayworld_inputs
static const AVFilterPad grayworld_inputs[]
Definition: vf_grayworld.c:300
grayworld_options
static const AVOption grayworld_options[]
Definition: vf_grayworld.c:51
GrayWorldContext::line_count_pels
int * line_count_pels
Definition: vf_grayworld.c:45