• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013 Paul B Mahol
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 #include "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "avfilter.h"
25 #include "drawutils.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29 #include "preserve_color.h"
30 
31 #define R 0
32 #define G 1
33 #define B 2
34 #define A 3
35 
36 typedef struct Range {
37     double in_min, in_max;
38     double out_min, out_max;
39 } Range;
40 
41 typedef struct ColorLevelsContext {
42     const AVClass *class;
43     Range range[4];
44     int preserve_color;
45 
46     int nb_comp;
47     int depth;
48     int max;
49     int planar;
50     int bpp;
51     int step;
52     uint8_t rgba_map[4];
53     int linesize;
54 
55     int (*colorlevels_slice[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
56 } ColorLevelsContext;
57 
58 #define OFFSET(x) offsetof(ColorLevelsContext, x)
59 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
60 static const AVOption colorlevels_options[] = {
61     { "rimin", "set input red black point",    OFFSET(range[R].in_min),  AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
62     { "gimin", "set input green black point",  OFFSET(range[G].in_min),  AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
63     { "bimin", "set input blue black point",   OFFSET(range[B].in_min),  AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
64     { "aimin", "set input alpha black point",  OFFSET(range[A].in_min),  AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
65     { "rimax", "set input red white point",    OFFSET(range[R].in_max),  AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, FLAGS },
66     { "gimax", "set input green white point",  OFFSET(range[G].in_max),  AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, FLAGS },
67     { "bimax", "set input blue white point",   OFFSET(range[B].in_max),  AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, FLAGS },
68     { "aimax", "set input alpha white point",  OFFSET(range[A].in_max),  AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, FLAGS },
69     { "romin", "set output red black point",   OFFSET(range[R].out_min), AV_OPT_TYPE_DOUBLE, {.dbl=0},  0, 1, FLAGS },
70     { "gomin", "set output green black point", OFFSET(range[G].out_min), AV_OPT_TYPE_DOUBLE, {.dbl=0},  0, 1, FLAGS },
71     { "bomin", "set output blue black point",  OFFSET(range[B].out_min), AV_OPT_TYPE_DOUBLE, {.dbl=0},  0, 1, FLAGS },
72     { "aomin", "set output alpha black point", OFFSET(range[A].out_min), AV_OPT_TYPE_DOUBLE, {.dbl=0},  0, 1, FLAGS },
73     { "romax", "set output red white point",   OFFSET(range[R].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1},  0, 1, FLAGS },
74     { "gomax", "set output green white point", OFFSET(range[G].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1},  0, 1, FLAGS },
75     { "bomax", "set output blue white point",  OFFSET(range[B].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1},  0, 1, FLAGS },
76     { "aomax", "set output alpha white point", OFFSET(range[A].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1},  0, 1, FLAGS },
77     { "preserve", "set preserve color mode",   OFFSET(preserve_color),   AV_OPT_TYPE_INT,    {.i64=0},  0, NB_PRESERVE-1, FLAGS, "preserve" },
78     { "none",  "disabled",                     0,                        AV_OPT_TYPE_CONST,  {.i64=P_NONE}, 0, 0, FLAGS, "preserve" },
79     { "lum",   "luminance",                    0,                        AV_OPT_TYPE_CONST,  {.i64=P_LUM},  0, 0, FLAGS, "preserve" },
80     { "max",   "max",                          0,                        AV_OPT_TYPE_CONST,  {.i64=P_MAX},  0, 0, FLAGS, "preserve" },
81     { "avg",   "average",                      0,                        AV_OPT_TYPE_CONST,  {.i64=P_AVG},  0, 0, FLAGS, "preserve" },
82     { "sum",   "sum",                          0,                        AV_OPT_TYPE_CONST,  {.i64=P_SUM},  0, 0, FLAGS, "preserve" },
83     { "nrm",   "norm",                         0,                        AV_OPT_TYPE_CONST,  {.i64=P_NRM},  0, 0, FLAGS, "preserve" },
84     { "pwr",   "power",                        0,                        AV_OPT_TYPE_CONST,  {.i64=P_PWR},  0, 0, FLAGS, "preserve" },
85     { NULL }
86 };
87 
88 AVFILTER_DEFINE_CLASS(colorlevels);
89 
90 typedef struct ThreadData {
91     const uint8_t *srcrow[4];
92     uint8_t *dstrow[4];
93     int dst_linesize;
94     int src_linesize;
95 
96     float coeff[4];
97 
98     int h;
99 
100     int imin[4];
101     int omin[4];
102 } ThreadData;
103 
104 #define DO_COMMON(type, clip, preserve, planar)                                 \
105     const ThreadData *td = arg;                                                 \
106     const int linesize = s->linesize;                                           \
107     const int step = s->step;                                                   \
108     const int process_h = td->h;                                                \
109     const int slice_start = (process_h *  jobnr   ) / nb_jobs;                  \
110     const int slice_end   = (process_h * (jobnr+1)) / nb_jobs;                  \
111     const int src_linesize = td->src_linesize / sizeof(type);                   \
112     const int dst_linesize = td->dst_linesize / sizeof(type);                   \
113     const type *src_r = (const type *)(td->srcrow[R]) + src_linesize * slice_start; \
114     const type *src_g = (const type *)(td->srcrow[G]) + src_linesize * slice_start; \
115     const type *src_b = (const type *)(td->srcrow[B]) + src_linesize * slice_start; \
116     const type *src_a = (const type *)(td->srcrow[A]) + src_linesize * slice_start; \
117     type *dst_r = (type *)(td->dstrow[R]) + src_linesize * slice_start;         \
118     type *dst_g = (type *)(td->dstrow[G]) + src_linesize * slice_start;         \
119     type *dst_b = (type *)(td->dstrow[B]) + src_linesize * slice_start;         \
120     type *dst_a = (type *)(td->dstrow[A]) + src_linesize * slice_start;         \
121     const int imin_r = td->imin[R];                                             \
122     const int imin_g = td->imin[G];                                             \
123     const int imin_b = td->imin[B];                                             \
124     const int imin_a = td->imin[A];                                             \
125     const int omin_r = td->omin[R];                                             \
126     const int omin_g = td->omin[G];                                             \
127     const int omin_b = td->omin[B];                                             \
128     const int omin_a = td->omin[A];                                             \
129     const float coeff_r = td->coeff[R];                                         \
130     const float coeff_g = td->coeff[G];                                         \
131     const float coeff_b = td->coeff[B];                                         \
132     const float coeff_a = td->coeff[A];                                         \
133                                                                                 \
134     for (int y = slice_start; y < slice_end; y++) {                             \
135         for (int x = 0; x < linesize; x += step) {                              \
136             int ir, ig, ib, or, og, ob;                                         \
137             ir = src_r[x];                                                      \
138             ig = src_g[x];                                                      \
139             ib = src_b[x];                                                      \
140             if (preserve) {                                                     \
141                 float ratio, icolor, ocolor, max = s->max;                      \
142                                                                                 \
143                 or = (ir - imin_r) * coeff_r + omin_r;                          \
144                 og = (ig - imin_g) * coeff_g + omin_g;                          \
145                 ob = (ib - imin_b) * coeff_b + omin_b;                          \
146                                                                                 \
147                 preserve_color(s->preserve_color, ir, ig, ib, or, og, ob, max,  \
148                               &icolor, &ocolor);                                \
149                 if (ocolor > 0.f) {                                             \
150                     ratio = icolor / ocolor;                                    \
151                                                                                 \
152                     or *= ratio;                                                \
153                     og *= ratio;                                                \
154                     ob *= ratio;                                                \
155                 }                                                               \
156                                                                                 \
157                 dst_r[x] = clip(or, depth);                                     \
158                 dst_g[x] = clip(og, depth);                                     \
159                 dst_b[x] = clip(ob, depth);                                     \
160             } else {                                                            \
161                 dst_r[x] = clip((ir - imin_r) * coeff_r + omin_r, depth);       \
162                 dst_g[x] = clip((ig - imin_g) * coeff_g + omin_g, depth);       \
163                 dst_b[x] = clip((ib - imin_b) * coeff_b + omin_b, depth);       \
164             }                                                                   \
165         }                                                                       \
166                                                                                 \
167         for (int x = 0; x < linesize && s->nb_comp == 4; x += step)             \
168             dst_a[x] = clip((src_a[x] - imin_a) * coeff_a + omin_a, depth);     \
169                                                                                 \
170         src_r += src_linesize;                                                  \
171         src_g += src_linesize;                                                  \
172         src_b += src_linesize;                                                  \
173         src_a += src_linesize;                                                  \
174                                                                                 \
175         dst_r += dst_linesize;                                                  \
176         dst_g += dst_linesize;                                                  \
177         dst_b += dst_linesize;                                                  \
178         dst_a += dst_linesize;                                                  \
179     }
180 
181 #define CLIP8(x, depth) av_clip_uint8(x)
182 #define CLIP16(x, depth) av_clip_uint16(x)
183 
colorlevels_slice_8(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)184 static int colorlevels_slice_8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
185 {
186     ColorLevelsContext *s = ctx->priv;
187     DO_COMMON(uint8_t, CLIP8, 0, 0)
188     return 0;
189 }
190 
colorlevels_slice_16(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)191 static int colorlevels_slice_16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
192 {
193     ColorLevelsContext *s = ctx->priv;\
194     DO_COMMON(uint16_t, CLIP16, 0, 0)
195     return 0;
196 }
197 
colorlevels_preserve_slice_8(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)198 static int colorlevels_preserve_slice_8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
199 {
200     ColorLevelsContext *s = ctx->priv;
201     DO_COMMON(uint8_t, CLIP8, 1, 0)
202     return 0;
203 }
204 
colorlevels_preserve_slice_16(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)205 static int colorlevels_preserve_slice_16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
206 {
207     ColorLevelsContext *s = ctx->priv;
208     DO_COMMON(uint16_t, CLIP16, 1, 0)
209     return 0;
210 }
211 
colorlevels_slice_8_planar(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)212 static int colorlevels_slice_8_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
213 {
214     ColorLevelsContext *s = ctx->priv;
215     DO_COMMON(uint8_t, CLIP8, 0, 1)
216     return 0;
217 }
218 
colorlevels_slice_9_planar(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)219 static int colorlevels_slice_9_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
220 {
221     ColorLevelsContext *s = ctx->priv;
222     const int depth = 9;
223     DO_COMMON(uint16_t, av_clip_uintp2, 0, 1)
224     return 0;
225 }
226 
colorlevels_slice_10_planar(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)227 static int colorlevels_slice_10_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
228 {
229     ColorLevelsContext *s = ctx->priv;
230     const int depth = 10;
231     DO_COMMON(uint16_t, av_clip_uintp2, 0, 1)
232     return 0;
233 }
234 
colorlevels_slice_12_planar(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)235 static int colorlevels_slice_12_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
236 {
237     ColorLevelsContext *s = ctx->priv;
238     const int depth = 12;
239     DO_COMMON(uint16_t, av_clip_uintp2, 0, 1)
240     return 0;
241 }
242 
colorlevels_slice_14_planar(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)243 static int colorlevels_slice_14_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
244 {
245     ColorLevelsContext *s = ctx->priv;
246     const int depth = 14;
247     DO_COMMON(uint16_t, av_clip_uintp2, 0, 1)
248     return 0;
249 }
250 
colorlevels_slice_16_planar(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)251 static int colorlevels_slice_16_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
252 {
253     ColorLevelsContext *s = ctx->priv;
254     DO_COMMON(uint16_t, CLIP16, 0, 1)
255     return 0;
256 }
257 
colorlevels_preserve_slice_8_planar(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)258 static int colorlevels_preserve_slice_8_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
259 {
260     ColorLevelsContext *s = ctx->priv;
261     DO_COMMON(uint8_t, CLIP8, 1, 1)
262     return 0;
263 }
264 
colorlevels_preserve_slice_9_planar(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)265 static int colorlevels_preserve_slice_9_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
266 {
267     ColorLevelsContext *s = ctx->priv;
268     const int depth = 9;
269     DO_COMMON(uint16_t, av_clip_uintp2, 1, 1)
270     return 0;
271 }
272 
colorlevels_preserve_slice_10_planar(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)273 static int colorlevels_preserve_slice_10_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
274 {
275     ColorLevelsContext *s = ctx->priv;
276     const int depth = 10;
277     DO_COMMON(uint16_t, av_clip_uintp2, 1, 1)
278     return 0;
279 }
280 
colorlevels_preserve_slice_12_planar(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)281 static int colorlevels_preserve_slice_12_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
282 {
283     ColorLevelsContext *s = ctx->priv;
284     const int depth = 12;
285     DO_COMMON(uint16_t, av_clip_uintp2, 1, 1)
286     return 0;
287 }
288 
colorlevels_preserve_slice_14_planar(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)289 static int colorlevels_preserve_slice_14_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
290 {
291     ColorLevelsContext *s = ctx->priv;
292     const int depth = 14;
293     DO_COMMON(uint16_t, av_clip_uintp2, 1, 1)
294     return 0;
295 }
296 
colorlevels_preserve_slice_16_planar(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)297 static int colorlevels_preserve_slice_16_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
298 {
299     ColorLevelsContext *s = ctx->priv;
300     DO_COMMON(uint16_t, CLIP16, 1, 1)
301     return 0;
302 }
303 
config_input(AVFilterLink * inlink)304 static int config_input(AVFilterLink *inlink)
305 {
306     AVFilterContext *ctx = inlink->dst;
307     ColorLevelsContext *s = ctx->priv;
308     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
309 
310     s->nb_comp = desc->nb_components;
311     s->planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;
312     s->depth = desc->comp[0].depth;
313     s->max = (1 << s->depth) - 1;
314     s->bpp = (desc->comp[0].depth + 7) >> 3;
315     s->step = s->planar ? 1 : av_get_padded_bits_per_pixel(desc) >> (3 + (s->bpp == 2));
316     s->linesize = inlink->w * s->step;
317     ff_fill_rgba_map(s->rgba_map, inlink->format);
318 
319     if (!s->planar) {
320         s->colorlevels_slice[0] = colorlevels_slice_8;
321         s->colorlevels_slice[1] = colorlevels_preserve_slice_8;
322         if (s->bpp == 2) {
323             s->colorlevels_slice[0] = colorlevels_slice_16;
324             s->colorlevels_slice[1] = colorlevels_preserve_slice_16;
325         }
326     } else {
327         switch (s->depth) {
328         case 8:
329             s->colorlevels_slice[0] = colorlevels_slice_8_planar;
330             s->colorlevels_slice[1] = colorlevels_preserve_slice_8_planar;
331             break;
332         case 9:
333             s->colorlevels_slice[0] = colorlevels_slice_9_planar;
334             s->colorlevels_slice[1] = colorlevels_preserve_slice_9_planar;
335             break;
336         case 10:
337             s->colorlevels_slice[0] = colorlevels_slice_10_planar;
338             s->colorlevels_slice[1] = colorlevels_preserve_slice_10_planar;
339             break;
340         case 12:
341             s->colorlevels_slice[0] = colorlevels_slice_12_planar;
342             s->colorlevels_slice[1] = colorlevels_preserve_slice_12_planar;
343             break;
344         case 14:
345             s->colorlevels_slice[0] = colorlevels_slice_14_planar;
346             s->colorlevels_slice[1] = colorlevels_preserve_slice_14_planar;
347             break;
348         case 16:
349             s->colorlevels_slice[0] = colorlevels_slice_16_planar;
350             s->colorlevels_slice[1] = colorlevels_preserve_slice_16_planar;
351             break;
352         }
353     }
354 
355     return 0;
356 }
357 
filter_frame(AVFilterLink * inlink,AVFrame * in)358 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
359 {
360     AVFilterContext *ctx = inlink->dst;
361     ColorLevelsContext *s = ctx->priv;
362     AVFilterLink *outlink = ctx->outputs[0];
363     const int step = s->step;
364     ThreadData td;
365     AVFrame *out;
366 
367     if (av_frame_is_writable(in)) {
368         out = in;
369     } else {
370         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
371         if (!out) {
372             av_frame_free(&in);
373             return AVERROR(ENOMEM);
374         }
375         av_frame_copy_props(out, in);
376     }
377 
378     td.h             = inlink->h;
379     td.dst_linesize  = out->linesize[0];
380     td.src_linesize  = in->linesize[0];
381     if (s->planar) {
382         td.srcrow[R] = in->data[2];
383         td.dstrow[R] = out->data[2];
384         td.srcrow[G] = in->data[0];
385         td.dstrow[G] = out->data[0];
386         td.srcrow[B] = in->data[1];
387         td.dstrow[B] = out->data[1];
388         td.srcrow[A] = in->data[3];
389         td.dstrow[A] = out->data[3];
390     } else {
391         td.srcrow[R] = in->data[0]  + s->rgba_map[R] * s->bpp;
392         td.dstrow[R] = out->data[0] + s->rgba_map[R] * s->bpp;
393         td.srcrow[G] = in->data[0]  + s->rgba_map[G] * s->bpp;
394         td.dstrow[G] = out->data[0] + s->rgba_map[G] * s->bpp;
395         td.srcrow[B] = in->data[0]  + s->rgba_map[B] * s->bpp;
396         td.dstrow[B] = out->data[0] + s->rgba_map[B] * s->bpp;
397         td.srcrow[A] = in->data[0]  + s->rgba_map[A] * s->bpp;
398         td.dstrow[A] = out->data[0] + s->rgba_map[A] * s->bpp;
399     }
400 
401     switch (s->bpp) {
402     case 1:
403         for (int i = 0; i < s->nb_comp; i++) {
404             Range *r = &s->range[i];
405             const uint8_t offset = s->rgba_map[i];
406             const uint8_t *srcrow = in->data[0];
407             int imin = lrint(r->in_min  * UINT8_MAX);
408             int imax = lrint(r->in_max  * UINT8_MAX);
409             int omin = lrint(r->out_min * UINT8_MAX);
410             int omax = lrint(r->out_max * UINT8_MAX);
411             float coeff;
412 
413             if (imin < 0) {
414                 imin = UINT8_MAX;
415                 for (int y = 0; y < inlink->h; y++) {
416                     const uint8_t *src = srcrow;
417 
418                     for (int x = 0; x < s->linesize; x += step)
419                         imin = FFMIN(imin, src[x + offset]);
420                     srcrow += in->linesize[0];
421                 }
422             }
423             if (imax < 0) {
424                 srcrow = in->data[0];
425                 imax = 0;
426                 for (int y = 0; y < inlink->h; y++) {
427                     const uint8_t *src = srcrow;
428 
429                     for (int x = 0; x < s->linesize; x += step)
430                         imax = FFMAX(imax, src[x + offset]);
431                     srcrow += in->linesize[0];
432                 }
433             }
434 
435             coeff = (omax - omin) / (double)(imax - imin);
436 
437             td.coeff[i] = coeff;
438             td.imin[i]  = imin;
439             td.omin[i]  = omin;
440         }
441         break;
442     case 2:
443         for (int i = 0; i < s->nb_comp; i++) {
444             Range *r = &s->range[i];
445             const uint8_t offset = s->rgba_map[i];
446             const uint8_t *srcrow = in->data[0];
447             int imin = lrint(r->in_min  * UINT16_MAX);
448             int imax = lrint(r->in_max  * UINT16_MAX);
449             int omin = lrint(r->out_min * UINT16_MAX);
450             int omax = lrint(r->out_max * UINT16_MAX);
451             float coeff;
452 
453             if (imin < 0) {
454                 imin = UINT16_MAX;
455                 for (int y = 0; y < inlink->h; y++) {
456                     const uint16_t *src = (const uint16_t *)srcrow;
457 
458                     for (int x = 0; x < s->linesize; x += step)
459                         imin = FFMIN(imin, src[x + offset]);
460                     srcrow += in->linesize[0];
461                 }
462             }
463             if (imax < 0) {
464                 srcrow = in->data[0];
465                 imax = 0;
466                 for (int y = 0; y < inlink->h; y++) {
467                     const uint16_t *src = (const uint16_t *)srcrow;
468 
469                     for (int x = 0; x < s->linesize; x += step)
470                         imax = FFMAX(imax, src[x + offset]);
471                     srcrow += in->linesize[0];
472                 }
473             }
474 
475             coeff = (omax - omin) / (double)(imax - imin);
476 
477             td.coeff[i] = coeff;
478             td.imin[i]  = imin;
479             td.omin[i]  = omin;
480         }
481         break;
482     }
483 
484     ff_filter_execute(ctx, s->colorlevels_slice[s->preserve_color > 0], &td, NULL,
485                       FFMIN(inlink->h, ff_filter_get_nb_threads(ctx)));
486 
487     if (in != out)
488         av_frame_free(&in);
489     return ff_filter_frame(outlink, out);
490 }
491 
492 static const AVFilterPad colorlevels_inputs[] = {
493     {
494         .name         = "default",
495         .type         = AVMEDIA_TYPE_VIDEO,
496         .filter_frame = filter_frame,
497         .config_props = config_input,
498     },
499 };
500 
501 static const AVFilterPad colorlevels_outputs[] = {
502     {
503         .name = "default",
504         .type = AVMEDIA_TYPE_VIDEO,
505     },
506 };
507 
508 const AVFilter ff_vf_colorlevels = {
509     .name          = "colorlevels",
510     .description   = NULL_IF_CONFIG_SMALL("Adjust the color levels."),
511     .priv_size     = sizeof(ColorLevelsContext),
512     .priv_class    = &colorlevels_class,
513     FILTER_INPUTS(colorlevels_inputs),
514     FILTER_OUTPUTS(colorlevels_outputs),
515     FILTER_PIXFMTS(AV_PIX_FMT_0RGB,   AV_PIX_FMT_0BGR,
516                    AV_PIX_FMT_ARGB,   AV_PIX_FMT_ABGR,
517                    AV_PIX_FMT_RGB0,   AV_PIX_FMT_BGR0,
518                    AV_PIX_FMT_RGB24,  AV_PIX_FMT_BGR24,
519                    AV_PIX_FMT_RGB48,  AV_PIX_FMT_BGR48,
520                    AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
521                    AV_PIX_FMT_RGBA,   AV_PIX_FMT_BGRA,
522                    AV_PIX_FMT_GBRP,   AV_PIX_FMT_GBRAP,
523                    AV_PIX_FMT_GBRP9,
524                    AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
525                    AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
526                    AV_PIX_FMT_GBRP14,
527                    AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16),
528     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
529     .process_command = ff_filter_process_command,
530 };
531