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/mem.h"
30 
31 #include "avfilter.h"
32 #include "filters.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  float *tmpplab;
45  float *line_sum;
47 
48 static void apply_matrix(const float matrix[3][3], const float input[3], float output[3])
49 {
50  output[0] = matrix[0][0] * input[0] + matrix[0][1] * input[1] + matrix[0][2] * input[2];
51  output[1] = matrix[1][0] * input[0] + matrix[1][1] * input[1] + matrix[1][2] * input[2];
52  output[2] = matrix[2][0] * input[0] + matrix[2][1] * input[1] + matrix[2][2] * input[2];
53 }
54 
55 static const float lms2lab[3][3] = {
56  {0.5774, 0.5774, 0.5774},
57  {0.40825, 0.40825, -0.816458},
58  {0.707, -0.707, 0}
59 };
60 
61 static const float lab2lms[3][3] = {
62  {0.57735, 0.40825, 0.707},
63  {0.57735, 0.40825, -0.707},
64  {0.57735, -0.8165, 0}
65 };
66 
67 static const float rgb2lms[3][3] = {
68  {0.3811, 0.5783, 0.0402},
69  {0.1967, 0.7244, 0.0782},
70  {0.0241, 0.1288, 0.8444}
71 };
72 
73 static const float lms2rgb[3][3] = {
74  {4.4679, -3.5873, 0.1193},
75  {-1.2186, 2.3809, -0.1624},
76  {0.0497, -0.2439, 1.2045}
77 };
78 
79 /**
80  * Convert from Linear RGB to logspace LAB
81  *
82  * @param rgb Input array of rgb components
83  * @param lab output array of lab components
84  */
85 static void rgb2lab(const float rgb[3], float lab[3])
86 {
87  float lms[3];
88 
89  apply_matrix(rgb2lms, rgb, lms);
90  lms[0] = lms[0] > 0.f ? logf(lms[0]) : -1024.f;
91  lms[1] = lms[1] > 0.f ? logf(lms[1]) : -1024.f;
92  lms[2] = lms[2] > 0.f ? logf(lms[2]) : -1024.f;
93  apply_matrix(lms2lab, lms, lab);
94 }
95 
96 /**
97  * Convert from Logspace LAB to Linear RGB
98  *
99  * @param lab input array of lab components
100  * @param rgb output array of rgb components
101  */
102 static void lab2rgb(const float lab[3], float rgb[3])
103 {
104  float lms[3];
105 
106  apply_matrix(lab2lms, lab, lms);
107  lms[0] = expf(lms[0]);
108  lms[1] = expf(lms[1]);
109  lms[2] = expf(lms[2]);
110  apply_matrix(lms2rgb, lms, rgb);
111 }
112 
113 /**
114  * Convert a frame from linear RGB to logspace LAB, and accumulate channel totals for each row
115  * Convert from RGB -> lms using equation 4 in color transfer paper.
116  *
117  * @param ctx Filter context
118  * @param arg Thread data pointer
119  * @param jobnr job number
120  * @param nb_jobs number of jobs
121  */
122 static int convert_frame(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
123 {
124  GrayWorldContext *s = ctx->priv;
125  ThreadData *td = arg;
126  AVFrame *in = td->in;
127  AVFrame *out = td->out;
128  AVFilterLink *outlink = ctx->outputs[0];
129  const int slice_start = (out->height * jobnr) / nb_jobs;
130  const int slice_end = (out->height * (jobnr + 1)) / nb_jobs;
131  float rgb[3], lab[3];
132 
133  for (int i = slice_start; i < slice_end; i++) {
134  float *b_in_row = (float *)(in->data[1] + i * in->linesize[1]);
135  float *g_in_row = (float *)(in->data[0] + i * in->linesize[0]);
136  float *r_in_row = (float *)(in->data[2] + i * in->linesize[2]);
137  float *acur = s->tmpplab + i * outlink->w + outlink->w * outlink->h;
138  float *bcur = s->tmpplab + i * outlink->w + 2 * outlink->w * outlink->h;
139  float *lcur = s->tmpplab + i * outlink->w;
140 
141  s->line_sum[i] = 0.f;
142  s->line_sum[i + outlink->h] = 0.f;
143  s->line_count_pels[i] = 0;
144 
145  for (int j = 0; j < outlink->w; j++) {
146  rgb[0] = r_in_row[j];
147  rgb[1] = g_in_row[j];
148  rgb[2] = b_in_row[j];
149  rgb2lab(rgb, lab);
150  *(lcur++) = lab[0];
151  *(acur++) = lab[1];
152  *(bcur++) = lab[2];
153  s->line_sum[i] += lab[1];
154  s->line_sum[i + outlink->h] += lab[2];
155  s->line_count_pels[i]++;
156  }
157  }
158  return 0;
159 }
160 
161 /**
162  * Sum the channel totals and compute the mean for each channel
163  *
164  * @param s Frame context
165  * @param td thread data
166  */
168 {
169  float asum = 0.f, bsum = 0.f;
170  int pixels = 0;
171 
172  for (int y = 0; y < td->out->height; y++) {
173  asum += s->line_sum[y];
174  bsum += s->line_sum[y + td->out->height];
175  pixels += s->line_count_pels[y];
176  }
177 
178  td->a_avg = asum / pixels;
179  td->b_avg = bsum / pixels;
180 }
181 
182 /**
183  * Subtract the mean logspace AB values from each pixel.
184  *
185  * @param ctx Filter context
186  * @param arg Thread data pointer
187  * @param jobnr job number
188  * @param nb_jobs number of jobs
189  */
190 static int correct_frame(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
191 {
192  GrayWorldContext *s = ctx->priv;
193  ThreadData *td = arg;
194  AVFrame *out = td->out;
195  AVFilterLink *outlink = ctx->outputs[0];
196  const int slice_start = (out->height * jobnr) / nb_jobs;
197  const int slice_end = (out->height * (jobnr + 1)) / nb_jobs;
198  float rgb[3], lab[3];
199 
200  for (int i = slice_start; i < slice_end; i++) {
201  float *g_out_row = (float *)(out->data[0] + i * out->linesize[0]);
202  float *b_out_row = (float *)(out->data[1] + i * out->linesize[1]);
203  float *r_out_row = (float *)(out->data[2] + i * out->linesize[2]);
204  float *lcur = s->tmpplab + i * outlink->w;
205  float *acur = s->tmpplab + i * outlink->w + outlink->w * outlink->h;
206  float *bcur = s->tmpplab + i * outlink->w + 2 * outlink->w * outlink->h;
207 
208  for (int j = 0; j < outlink->w; j++) {
209  lab[0] = *lcur++;
210  lab[1] = *acur++;
211  lab[2] = *bcur++;
212 
213  // subtract the average for the color channels
214  lab[1] -= td->a_avg;
215  lab[2] -= td->b_avg;
216 
217  //convert back to linear rgb
218  lab2rgb(lab, rgb);
219  r_out_row[j] = rgb[0];
220  g_out_row[j] = rgb[1];
221  b_out_row[j] = rgb[2];
222  }
223  }
224  return 0;
225 }
226 
228 {
229  GrayWorldContext *s = inlink->dst->priv;
230 
231  FF_ALLOC_TYPED_ARRAY(s->tmpplab, inlink->h * inlink->w * 3);
232  FF_ALLOC_TYPED_ARRAY(s->line_count_pels, inlink->h);
233  FF_ALLOC_TYPED_ARRAY(s->line_sum, inlink->h * 2);
234  if (!s->tmpplab || !s->line_count_pels || !s->line_sum)
235  return AVERROR(ENOMEM);
236 
237  return 0;
238 }
239 
241 {
242  GrayWorldContext *s = ctx->priv;
243 
244  av_freep(&s->tmpplab);
245  av_freep(&s->line_count_pels);
246  av_freep(&s->line_sum);
247 }
248 
250 {
251  AVFilterContext *ctx = inlink->dst;
252  GrayWorldContext *s = ctx->priv;
253  AVFilterLink *outlink = ctx->outputs[0];
254  ThreadData td;
255  AVFrame *out;
256 
257  if (av_frame_is_writable(in)) {
258  out = in;
259  } else {
260  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
261  if (!out) {
262  av_frame_free(&in);
263  return AVERROR(ENOMEM);
264  }
266  }
267  /* input and output transfer will be linear */
268  if (in->color_trc == AVCOL_TRC_UNSPECIFIED) {
269  av_log(s, AV_LOG_WARNING, "Untagged transfer, assuming linear light.\n");
270  out->color_trc = AVCOL_TRC_LINEAR;
271  } else if (in->color_trc != AVCOL_TRC_LINEAR) {
272  av_log(s, AV_LOG_WARNING, "Gray world color correction works on linear light only.\n");
273  }
274 
275  td.in = in;
276  td.out = out;
277 
279  compute_correction(s, &td);
281 
282  if (in != out) {
283  av_image_copy_plane(out->data[3], out->linesize[3],
284  in->data[3], in->linesize[3], outlink->w * 4, outlink->h);
285  av_frame_free(&in);
286  }
287 
288  return ff_filter_frame(outlink, out);
289 }
290 
291 static const AVFilterPad grayworld_inputs[] = {
292  {
293  .name = "default",
294  .type = AVMEDIA_TYPE_VIDEO,
295  .filter_frame = filter_frame,
296  .config_props = config_input,
297  }
298 };
299 
301  .name = "grayworld",
302  .description = NULL_IF_CONFIG_SMALL("Adjust white balance using LAB gray world algorithm"),
303  .priv_size = sizeof(GrayWorldContext),
308  .uninit = uninit,
309 };
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:116
AVFrame::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: frame.h:672
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
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:122
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
out
FILE * out
Definition: movenc.c:55
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
AVCOL_TRC_LINEAR
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition: pixfmt.h:620
matrix
Definition: vc1dsp.c:43
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:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_grayworld.c:240
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:614
expf
#define expf(x)
Definition: libm.h:283
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_grayworld.c:249
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:526
video.h
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:155
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
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
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition: mpeg12dec.c:1719
FF_ALLOC_TYPED_ARRAY
#define FF_ALLOC_TYPED_ARRAY(p, nelem)
Definition: internal.h:77
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:167
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
lms2rgb
static const float lms2rgb[3][3]
Definition: vf_grayworld.c:73
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:37
GrayWorldContext::line_sum
float * line_sum
Definition: vf_grayworld.c:45
s
#define s(width, name)
Definition: cbs_vp9.c:198
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
lab2rgb
static void lab2rgb(const float lab[3], float rgb[3])
Convert from Logspace LAB to Linear RGB.
Definition: vf_grayworld.c:102
apply_matrix
static void apply_matrix(const float matrix[3][3], const float input[3], float output[3])
Definition: vf_grayworld.c:48
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
ThreadData::b_avg
float b_avg
Definition: vf_grayworld.c:39
arg
const char * arg
Definition: jacosubdec.c:67
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:713
ThreadData::a_avg
float a_avg
Definition: vf_grayworld.c:38
ff_vf_grayworld
const AVFilter ff_vf_grayworld
Definition: vf_grayworld.c:300
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:94
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_grayworld.c:227
AV_PIX_FMT_GBRPF32
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:532
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:649
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
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:182
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:841
ThreadData
Used for passing data between threads.
Definition: dsddec.c:71
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
FILTER_PIXFMTS
#define FILTER_PIXFMTS(...)
Definition: filters.h:248
slice_start
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition: dec.c:738
AVFilter
Filter definition.
Definition: avfilter.h:201
rgb2lms
static const float rgb2lms[3][3]
Definition: vf_grayworld.c:67
rgb2lab
static void rgb2lab(const float rgb[3], float lab[3])
Convert from Linear RGB to logspace LAB.
Definition: vf_grayworld.c:85
AVFrame::height
int height
Definition: frame.h:461
lms2lab
static const float lms2lab[3][3]
Definition: vf_grayworld.c:55
ff_filter_execute
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: avfilter.c:1667
avfilter.h
AV_PIX_FMT_GBRAPF32
#define AV_PIX_FMT_GBRAPF32
Definition: pixfmt.h:533
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
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:152
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
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:43
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:434
lab2lms
static const float lab2lms[3][3]
Definition: vf_grayworld.c:61
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:190
grayworld_inputs
static const AVFilterPad grayworld_inputs[]
Definition: vf_grayworld.c:291
GrayWorldContext::line_count_pels
int * line_count_pels
Definition: vf_grayworld.c:44