History

avfilter/vf_colorchannelmixer: add option to preserve lightness

Paul B Mahol committed 5 years ago

Commit b2bbced1 parent 257a83b2 files · +297 −84Browse files

Showing the changes to libavfilter (1 of 2 files). Show all

Unified · Split

libavfilter/vf_colorchannelmixer.c+294−84
@@ -18,6 +18,8 @@ · 17 unmodified lines
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */19 */
20 20
21#include <float.h>
22
21#include "libavutil/opt.h"23#include "libavutil/opt.h"
22#include "libavutil/pixdesc.h"24#include "libavutil/pixdesc.h"
23#include "avfilter.h"25#include "avfilter.h"
@@ -41,6 +43,8 @@ typedef struct ColorChannelMixerContext { · 17 unmodified lines
41 double gr, gg, gb, ga;43 double gr, gg, gb, ga;
42 double br, bg, bb, ba;44 double br, bg, bb, ba;
43 double ar, ag, ab, aa;45 double ar, ag, ab, aa;
46 double sr, sg, sb;
47 int preserve_lightness;
44 48
45 int *lut[4][4];49 int *lut[4][4];
46 50
@@ -48,7 +52,7 @@ typedef struct ColorChannelMixerContext { · 1 unmodified line
48 52
49 uint8_t rgba_map[4];53 uint8_t rgba_map[4];
50 54
51 int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);55 int (*filter_slice[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
52} ColorChannelMixerContext;56} ColorChannelMixerContext;
53 57
54#define OFFSET(x) offsetof(ColorChannelMixerContext, x)58#define OFFSET(x) offsetof(ColorChannelMixerContext, x)
@@ -71,6 +75,7 @@ static const AVOption colorchannelmixer_options[] = { · 16 unmodified lines
71 { "ag", "set the green gain for the alpha channel", OFFSET(ag), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },75 { "ag", "set the green gain for the alpha channel", OFFSET(ag), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
72 { "ab", "set the blue gain for the alpha channel", OFFSET(ab), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },76 { "ab", "set the blue gain for the alpha channel", OFFSET(ab), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
73 { "aa", "set the alpha gain for the alpha channel", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },77 { "aa", "set the alpha gain for the alpha channel", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
78 { "pl", "preserve lightness", OFFSET(preserve_lightness), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
74 { NULL }79 { NULL }
75};80};
76 81
@@ -101,8 +106,15 @@ static int query_formats(AVFilterContext *ctx) · 24 unmodified lines
101 return ff_set_common_formats(ctx, fmts_list);106 return ff_set_common_formats(ctx, fmts_list);
102}107}
103 108
109static void preservel(float *r, float *g, float *b, float lin, float lout)
110{
111 *r *= lout / lin;
112 *g *= lout / lin;
113 *b *= lout / lin;
114}
115
104static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,116static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
105 int have_alpha)117 int have_alpha, int pl)
106{118{
107 ColorChannelMixerContext *s = ctx->priv;119 ColorChannelMixerContext *s = ctx->priv;
108 ThreadData *td = arg;120 ThreadData *td = arg;
@@ -126,19 +138,42 @@ static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void · 17 unmodified lines
126 const uint8_t gin = srcg[j];138 const uint8_t gin = srcg[j];
127 const uint8_t bin = srcb[j];139 const uint8_t bin = srcb[j];
128 const uint8_t ain = have_alpha ? srca[j] : 0;140 const uint8_t ain = have_alpha ? srca[j] : 0;
141 int rout, gout, bout;
142 float lin;
143
144 if (pl)
145 lin = FFMAX3(rin, gin, bin) / 255.f + FFMIN3(rin, gin, bin) / 255.f;
146
147 rout = s->lut[R][R][rin] +
148 s->lut[R][G][gin] +
149 s->lut[R][B][bin] +
150 (have_alpha == 1 ? s->lut[R][A][ain] : 0);
151 gout = s->lut[G][R][rin] +
152 s->lut[G][G][gin] +
153 s->lut[G][B][bin] +
154 (have_alpha == 1 ? s->lut[G][A][ain] : 0);
155 bout = s->lut[B][R][rin] +
156 s->lut[B][G][gin] +
157 s->lut[B][B][bin] +
158 (have_alpha == 1 ? s->lut[B][A][ain] : 0);
159
160 if (pl) {
161 float frout = rout / (255.f * s->sr);
162 float fgout = gout / (255.f * s->sg);
163 float fbout = bout / (255.f * s->sb);
164 float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
165
166 preservel(&frout, &fgout, &fbout, lin, lout);
167
168 rout = lrintf(frout * 255.f);
169 gout = lrintf(fgout * 255.f);
170 bout = lrintf(fbout * 255.f);
171 }
172
173 dstr[j] = av_clip_uint8(rout);
174 dstg[j] = av_clip_uint8(gout);
175 dstb[j] = av_clip_uint8(bout);
129 176
130 dstr[j] = av_clip_uint8(s->lut[R][R][rin] +
131 s->lut[R][G][gin] +
132 s->lut[R][B][bin] +
133 (have_alpha == 1 ? s->lut[R][A][ain] : 0));
134 dstg[j] = av_clip_uint8(s->lut[G][R][rin] +
135 s->lut[G][G][gin] +
136 s->lut[G][B][bin] +
137 (have_alpha == 1 ? s->lut[G][A][ain] : 0));
138 dstb[j] = av_clip_uint8(s->lut[B][R][rin] +
139 s->lut[B][G][gin] +
140 s->lut[B][B][bin] +
141 (have_alpha == 1 ? s->lut[B][A][ain] : 0));
142 if (have_alpha == 1) {177 if (have_alpha == 1) {
143 dsta[j] = av_clip_uint8(s->lut[A][R][rin] +178 dsta[j] = av_clip_uint8(s->lut[A][R][rin] +
144 s->lut[A][G][gin] +179 s->lut[A][G][gin] +
@@ -161,7 +196,7 @@ static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void · 16 unmodified lines
161}196}
162 197
163static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,198static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
164 int have_alpha, int depth)199 int have_alpha, int depth, int pl)
165{200{
166 ColorChannelMixerContext *s = ctx->priv;201 ColorChannelMixerContext *s = ctx->priv;
167 ThreadData *td = arg;202 ThreadData *td = arg;
@@ -177,6 +212,8 @@ static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, voi · 9 unmodified lines
177 uint16_t *dstb = (uint16_t *)(out->data[1] + slice_start * out->linesize[1]);212 uint16_t *dstb = (uint16_t *)(out->data[1] + slice_start * out->linesize[1]);
178 uint16_t *dstr = (uint16_t *)(out->data[2] + slice_start * out->linesize[2]);213 uint16_t *dstr = (uint16_t *)(out->data[2] + slice_start * out->linesize[2]);
179 uint16_t *dsta = (uint16_t *)(out->data[3] + slice_start * out->linesize[3]);214 uint16_t *dsta = (uint16_t *)(out->data[3] + slice_start * out->linesize[3]);
215 const float scale = 1.f / ((1 << depth) - 1);
216 const float factor = (1 << depth) - 1;
180 int i, j;217 int i, j;
181 218
182 for (i = slice_start; i < slice_end; i++) {219 for (i = slice_start; i < slice_end; i++) {
@@ -185,19 +222,42 @@ static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, voi · 2 unmodified lines
185 const uint16_t gin = srcg[j];222 const uint16_t gin = srcg[j];
186 const uint16_t bin = srcb[j];223 const uint16_t bin = srcb[j];
187 const uint16_t ain = have_alpha ? srca[j] : 0;224 const uint16_t ain = have_alpha ? srca[j] : 0;
225 int rout, gout, bout;
226 float lin;
227
228 if (pl)
229 lin = FFMAX3(rin, gin, bin) * scale + FFMIN3(rin, gin, bin) * scale;
230
231 rout = s->lut[R][R][rin] +
232 s->lut[R][G][gin] +
233 s->lut[R][B][bin] +
234 (have_alpha == 1 ? s->lut[R][A][ain] : 0);
235 gout = s->lut[G][R][rin] +
236 s->lut[G][G][gin] +
237 s->lut[G][B][bin] +
238 (have_alpha == 1 ? s->lut[G][A][ain] : 0);
239 bout = s->lut[B][R][rin] +
240 s->lut[B][G][gin] +
241 s->lut[B][B][bin] +
242 (have_alpha == 1 ? s->lut[B][A][ain] : 0);
243
244 if (pl) {
245 float frout = rout / (factor * s->sr);
246 float fgout = gout / (factor * s->sg);
247 float fbout = bout / (factor * s->sb);
248 float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
249
250 preservel(&frout, &fgout, &fbout, lin, lout);
251
252 rout = lrintf(frout * factor);
253 gout = lrintf(fgout * factor);
254 bout = lrintf(fbout * factor);
255 }
256
257 dstr[j] = av_clip_uintp2(rout, depth);
258 dstg[j] = av_clip_uintp2(gout, depth);
259 dstb[j] = av_clip_uintp2(bout, depth);
188 260
189 dstr[j] = av_clip_uintp2(s->lut[R][R][rin] +
190 s->lut[R][G][gin] +
191 s->lut[R][B][bin] +
192 (have_alpha == 1 ? s->lut[R][A][ain] : 0), depth);
193 dstg[j] = av_clip_uintp2(s->lut[G][R][rin] +
194 s->lut[G][G][gin] +
195 s->lut[G][B][bin] +
196 (have_alpha == 1 ? s->lut[G][A][ain] : 0), depth);
197 dstb[j] = av_clip_uintp2(s->lut[B][R][rin] +
198 s->lut[B][G][gin] +
199 s->lut[B][B][bin] +
200 (have_alpha == 1 ? s->lut[B][A][ain] : 0), depth);
201 if (have_alpha == 1) {261 if (have_alpha == 1) {
202 dsta[j] = av_clip_uintp2(s->lut[A][R][rin] +262 dsta[j] = av_clip_uintp2(s->lut[A][R][rin] +
203 s->lut[A][G][gin] +263 s->lut[A][G][gin] +
@@ -221,56 +281,106 @@ static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, voi · 17 unmodified lines
221 281
222static int filter_slice_gbrp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)282static int filter_slice_gbrp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
223{283{
224 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 0);284 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 0, 0);
225}285}
226 286
227static int filter_slice_gbrap(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)287static int filter_slice_gbrap(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
228{288{
229 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 1);289 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 1, 0);
290}
291
292static int filter_slice_gbrp_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
293{
294 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 0, 1);
295}
296
297static int filter_slice_gbrap_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
298{
299 return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 1, 1);
230}300}
231 301
232static int filter_slice_gbrp9(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)302static int filter_slice_gbrp9(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
233{303{
234 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 9);304 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 9, 0);
235}305}
236 306
237static int filter_slice_gbrp10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)307static int filter_slice_gbrp10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
238{308{
239 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 10);309 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 10, 0);
240}310}
241 311
242static int filter_slice_gbrap10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)312static int filter_slice_gbrap10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
243{313{
244 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 10);314 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 10, 0);
245}315}
246 316
247static int filter_slice_gbrp12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)317static int filter_slice_gbrp12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
248{318{
249 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 12);319 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 12, 0);
250}320}
251 321
252static int filter_slice_gbrap12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)322static int filter_slice_gbrap12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
253{323{
254 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 12);324 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 12, 0);
255}325}
256 326
257static int filter_slice_gbrp14(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)327static int filter_slice_gbrp14(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
258{328{
259 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 14);329 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 14, 0);
260}330}
261 331
262static int filter_slice_gbrp16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)332static int filter_slice_gbrp16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
263{333{
264 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 16);334 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 16, 0);
265}335}
266 336
267static int filter_slice_gbrap16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)337static int filter_slice_gbrap16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
268{338{
269 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 16);339 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 16, 0);
340}
341
342static int filter_slice_gbrp9_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
343{
344 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 9, 1);
345}
346
347static int filter_slice_gbrp10_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
348{
349 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 10, 1);
350}
351
352static int filter_slice_gbrap10_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
353{
354 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 10, 1);
355}
356
357static int filter_slice_gbrp12_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
358{
359 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 12, 1);
360}
361
362static int filter_slice_gbrap12_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
363{
364 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 12, 1);
365}
366
367static int filter_slice_gbrp14_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
368{
369 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 14, 1);
370}
371
372static int filter_slice_gbrp16_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
373{
374 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 16, 1);
375}
376
377static int filter_slice_gbrap16_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
378{
379 return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 16, 1);
270}380}
271 381
272static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,382static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
273 int have_alpha, int step)383 int have_alpha, int step, int pl)
274{384{
275 ColorChannelMixerContext *s = ctx->priv;385 ColorChannelMixerContext *s = ctx->priv;
276 ThreadData *td = arg;386 ThreadData *td = arg;
@@ -295,19 +405,42 @@ static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void · 18 unmodified lines
295 const uint8_t gin = src[j + goffset];405 const uint8_t gin = src[j + goffset];
296 const uint8_t bin = src[j + boffset];406 const uint8_t bin = src[j + boffset];
297 const uint8_t ain = src[j + aoffset];407 const uint8_t ain = src[j + aoffset];
408 int rout, gout, bout;
409 float lin;
410
411 if (pl)
412 lin = FFMAX3(rin, gin, bin) / 255.f + FFMIN3(rin, gin, bin) / 255.f;
413
414 rout = s->lut[R][R][rin] +
415 s->lut[R][G][gin] +
416 s->lut[R][B][bin] +
417 (have_alpha == 1 ? s->lut[R][A][ain] : 0);
418 gout = s->lut[G][R][rin] +
419 s->lut[G][G][gin] +
420 s->lut[G][B][bin] +
421 (have_alpha == 1 ? s->lut[G][A][ain] : 0);
422 bout = s->lut[B][R][rin] +
423 s->lut[B][G][gin] +
424 s->lut[B][B][bin] +
425 (have_alpha == 1 ? s->lut[B][A][ain] : 0);
426
427 if (pl) {
428 float frout = rout / (255.f * s->sr);
429 float fgout = gout / (255.f * s->sg);
430 float fbout = bout / (255.f * s->sb);
431 float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
432
433 preservel(&frout, &fgout, &fbout, lin, lout);
434
435 rout = lrintf(frout * 255.f);
436 gout = lrintf(fgout * 255.f);
437 bout = lrintf(fbout * 255.f);
438 }
439
440 dst[j + roffset] = av_clip_uint8(rout);
441 dst[j + goffset] = av_clip_uint8(gout);
442 dst[j + boffset] = av_clip_uint8(bout);
298 443
299 dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
300 s->lut[R][G][gin] +
301 s->lut[R][B][bin] +
302 (have_alpha == 1 ? s->lut[R][A][ain] : 0));
303 dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
304 s->lut[G][G][gin] +
305 s->lut[G][B][bin] +
306 (have_alpha == 1 ? s->lut[G][A][ain] : 0));
307 dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
308 s->lut[B][G][gin] +
309 s->lut[B][B][bin] +
310 (have_alpha == 1 ? s->lut[B][A][ain] : 0));
311 if (have_alpha == 1) {444 if (have_alpha == 1) {
312 dst[j + aoffset] = av_clip_uint8(s->lut[A][R][rin] +445 dst[j + aoffset] = av_clip_uint8(s->lut[A][R][rin] +
313 s->lut[A][G][gin] +446 s->lut[A][G][gin] +
@@ -325,7 +458,7 @@ static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void · 11 unmodified lines
325}458}
326 459
327static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,460static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
328 int have_alpha, int step)461 int have_alpha, int step, int pl)
329{462{
330 ColorChannelMixerContext *s = ctx->priv;463 ColorChannelMixerContext *s = ctx->priv;
331 ThreadData *td = arg;464 ThreadData *td = arg;
@@ -350,19 +483,42 @@ static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, voi · 18 unmodified lines
350 const uint16_t gin = src[j + goffset];483 const uint16_t gin = src[j + goffset];
351 const uint16_t bin = src[j + boffset];484 const uint16_t bin = src[j + boffset];
352 const uint16_t ain = src[j + aoffset];485 const uint16_t ain = src[j + aoffset];
486 int rout, gout, bout;
487 float lin;
488
489 if (pl)
490 lin = FFMAX3(rin, gin, bin) / 65535.f + FFMIN3(rin, gin, bin) / 65535.f;
491
492 rout = s->lut[R][R][rin] +
493 s->lut[R][G][gin] +
494 s->lut[R][B][bin] +
495 (have_alpha == 1 ? s->lut[R][A][ain] : 0);
496 gout = s->lut[G][R][rin] +
497 s->lut[G][G][gin] +
498 s->lut[G][B][bin] +
499 (have_alpha == 1 ? s->lut[G][A][ain] : 0);
500 bout = s->lut[B][R][rin] +
501 s->lut[B][G][gin] +
502 s->lut[B][B][bin] +
503 (have_alpha == 1 ? s->lut[B][A][ain] : 0);
504
505 if (pl) {
506 float frout = rout / (65535.f * s->sr);
507 float fgout = gout / (65535.f * s->sg);
508 float fbout = bout / (65535.f * s->sb);
509 float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
510
511 preservel(&frout, &fgout, &fbout, lin, lout);
512
513 rout = lrintf(frout * 65535.f);
514 gout = lrintf(fgout * 65535.f);
515 bout = lrintf(fbout * 65535.f);
516 }
517
518 dst[j + roffset] = av_clip_uint16(rout);
519 dst[j + goffset] = av_clip_uint16(gout);
520 dst[j + boffset] = av_clip_uint16(bout);
353 521
354 dst[j + roffset] = av_clip_uint16(s->lut[R][R][rin] +
355 s->lut[R][G][gin] +
356 s->lut[R][B][bin] +
357 (have_alpha == 1 ? s->lut[R][A][ain] : 0));
358 dst[j + goffset] = av_clip_uint16(s->lut[G][R][rin] +
359 s->lut[G][G][gin] +
360 s->lut[G][B][bin] +
361 (have_alpha == 1 ? s->lut[G][A][ain] : 0));
362 dst[j + boffset] = av_clip_uint16(s->lut[B][R][rin] +
363 s->lut[B][G][gin] +
364 s->lut[B][B][bin] +
365 (have_alpha == 1 ? s->lut[B][A][ain] : 0));
366 if (have_alpha == 1) {522 if (have_alpha == 1) {
367 dst[j + aoffset] = av_clip_uint16(s->lut[A][R][rin] +523 dst[j + aoffset] = av_clip_uint16(s->lut[A][R][rin] +
368 s->lut[A][G][gin] +524 s->lut[A][G][gin] +
@@ -380,27 +536,52 @@ static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, voi · 11 unmodified lines
380 536
381static int filter_slice_rgba64(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)537static int filter_slice_rgba64(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
382{538{
383 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 1, 4);539 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 0);
384}540}
385 541
386static int filter_slice_rgb48(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)542static int filter_slice_rgb48(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
387{543{
388 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 0, 3);544 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 0);
545}
546
547static int filter_slice_rgba64_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
548{
549 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 1);
550}
551
552static int filter_slice_rgb48_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
553{
554 return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 1);
389}555}
390 556
391static int filter_slice_rgba(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)557static int filter_slice_rgba(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
392{558{
393 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 1, 4);559 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 0);
394}560}
395 561
396static int filter_slice_rgb24(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)562static int filter_slice_rgb24(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
397{563{
398 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 0, 3);564 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 0);
399}565}
400 566
401static int filter_slice_rgb0(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)567static int filter_slice_rgb0(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
402{568{
403 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, -1, 4);569 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, -1, 4, 0);
570}
571
572static int filter_slice_rgba_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
573{
574 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 1);
575}
576
577static int filter_slice_rgb24_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
578{
579 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 1);
580}
581
582static int filter_slice_rgb0_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
583{
584 return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, -1, 4, 1);
404}585}
405 586
406static int config_output(AVFilterLink *outlink)587static int config_output(AVFilterLink *outlink)
@@ -424,6 +605,19 @@ static int config_output(AVFilterLink *outlink) · 17 unmodified lines
424 s->lut[i][j] = buffer;605 s->lut[i][j] = buffer;
425 }606 }
426 607
608 s->sr = s->rr + s->rg + s->rb + s->ra;
609 s->sg = s->gr + s->gg + s->gb + s->ga;
610 s->sb = s->br + s->bg + s->bb + s->ba;
611
612 if (fabs(s->sr) <= DBL_EPSILON)
613 s->sr = 1.;
614
615 if (fabs(s->sg) <= DBL_EPSILON)
616 s->sg = 1.;
617
618 if (fabs(s->sb) <= DBL_EPSILON)
619 s->sb = 1.;
620
427 for (i = 0; i < size; i++) {621 for (i = 0; i < size; i++) {
428 s->lut[R][R][i] = lrint(i * s->rr);622 s->lut[R][R][i] = lrint(i * s->rr);
429 s->lut[R][G][i] = lrint(i * s->rg);623 s->lut[R][G][i] = lrint(i * s->rg);
@@ -449,57 +643,72 @@ static int config_output(AVFilterLink *outlink) · 19 unmodified lines
449 switch (outlink->format) {643 switch (outlink->format) {
450 case AV_PIX_FMT_BGR24:644 case AV_PIX_FMT_BGR24:
451 case AV_PIX_FMT_RGB24:645 case AV_PIX_FMT_RGB24:
452 s->filter_slice = filter_slice_rgb24;646 s->filter_slice[0] = filter_slice_rgb24;
647 s->filter_slice[1] = filter_slice_rgb24_pl;
453 break;648 break;
454 case AV_PIX_FMT_0BGR:649 case AV_PIX_FMT_0BGR:
455 case AV_PIX_FMT_0RGB:650 case AV_PIX_FMT_0RGB:
456 case AV_PIX_FMT_BGR0:651 case AV_PIX_FMT_BGR0:
457 case AV_PIX_FMT_RGB0:652 case AV_PIX_FMT_RGB0:
458 s->filter_slice = filter_slice_rgb0;653 s->filter_slice[0] = filter_slice_rgb0;
654 s->filter_slice[1] = filter_slice_rgb0_pl;
459 break;655 break;
460 case AV_PIX_FMT_ABGR:656 case AV_PIX_FMT_ABGR:
461 case AV_PIX_FMT_ARGB:657 case AV_PIX_FMT_ARGB:
462 case AV_PIX_FMT_BGRA:658 case AV_PIX_FMT_BGRA:
463 case AV_PIX_FMT_RGBA:659 case AV_PIX_FMT_RGBA:
464 s->filter_slice = filter_slice_rgba;660 s->filter_slice[0] = filter_slice_rgba;
661 s->filter_slice[1] = filter_slice_rgba_pl;
465 break;662 break;
466 case AV_PIX_FMT_BGR48:663 case AV_PIX_FMT_BGR48:
467 case AV_PIX_FMT_RGB48:664 case AV_PIX_FMT_RGB48:
468 s->filter_slice = filter_slice_rgb48;665 s->filter_slice[0] = filter_slice_rgb48;
666 s->filter_slice[1] = filter_slice_rgb48_pl;
469 break;667 break;
470 case AV_PIX_FMT_BGRA64:668 case AV_PIX_FMT_BGRA64:
471 case AV_PIX_FMT_RGBA64:669 case AV_PIX_FMT_RGBA64:
472 s->filter_slice = filter_slice_rgba64;670 s->filter_slice[0] = filter_slice_rgba64;
671 s->filter_slice[1] = filter_slice_rgba64_pl;
473 break;672 break;
474 case AV_PIX_FMT_GBRP:673 case AV_PIX_FMT_GBRP:
475 s->filter_slice = filter_slice_gbrp;674 s->filter_slice[0] = filter_slice_gbrp;
675 s->filter_slice[1] = filter_slice_gbrp_pl;
476 break;676 break;
477 case AV_PIX_FMT_GBRAP:677 case AV_PIX_FMT_GBRAP:
478 s->filter_slice = filter_slice_gbrap;678 s->filter_slice[0] = filter_slice_gbrap;
679 s->filter_slice[1] = filter_slice_gbrap_pl;
479 break;680 break;
480 case AV_PIX_FMT_GBRP9:681 case AV_PIX_FMT_GBRP9:
481 s->filter_slice = filter_slice_gbrp9;682 s->filter_slice[0] = filter_slice_gbrp9;
683 s->filter_slice[1] = filter_slice_gbrp9_pl;
482 break;684 break;
483 case AV_PIX_FMT_GBRP10:685 case AV_PIX_FMT_GBRP10:
484 s->filter_slice = filter_slice_gbrp10;686 s->filter_slice[0] = filter_slice_gbrp10;
687 s->filter_slice[1] = filter_slice_gbrp10_pl;
485 break;688 break;
486 case AV_PIX_FMT_GBRAP10:689 case AV_PIX_FMT_GBRAP10:
487 s->filter_slice = filter_slice_gbrap10;690 s->filter_slice[0] = filter_slice_gbrap10;
691 s->filter_slice[1] = filter_slice_gbrap10_pl;
488 break;692 break;
489 case AV_PIX_FMT_GBRP12:693 case AV_PIX_FMT_GBRP12:
490 s->filter_slice = filter_slice_gbrp12;694 s->filter_slice[0] = filter_slice_gbrp12;
695 s->filter_slice[1] = filter_slice_gbrp12_pl;
491 break;696 break;
492 case AV_PIX_FMT_GBRAP12:697 case AV_PIX_FMT_GBRAP12:
493 s->filter_slice = filter_slice_gbrap12;698 s->filter_slice[0] = filter_slice_gbrap12;
699 s->filter_slice[1] = filter_slice_gbrap12_pl;
494 break;700 break;
495 case AV_PIX_FMT_GBRP14:701 case AV_PIX_FMT_GBRP14:
496 s->filter_slice = filter_slice_gbrp14;702 s->filter_slice[0] = filter_slice_gbrp14;
703 s->filter_slice[1] = filter_slice_gbrp14_pl;
497 break;704 break;
498 case AV_PIX_FMT_GBRP16:705 case AV_PIX_FMT_GBRP16:
499 s->filter_slice = filter_slice_gbrp16;706 s->filter_slice[0] = filter_slice_gbrp16;
707 s->filter_slice[1] = filter_slice_gbrp16_pl;
500 break;708 break;
501 case AV_PIX_FMT_GBRAP16:709 case AV_PIX_FMT_GBRAP16:
502 s->filter_slice = filter_slice_gbrap16;710 s->filter_slice[0] = filter_slice_gbrap16;
711 s->filter_slice[1] = filter_slice_gbrap16_pl;
503 break;712 break;
504 }713 }
505 714
@@ -511,6 +720,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 5 unmodified lines
511 AVFilterContext *ctx = inlink->dst;720 AVFilterContext *ctx = inlink->dst;
512 ColorChannelMixerContext *s = ctx->priv;721 ColorChannelMixerContext *s = ctx->priv;
513 AVFilterLink *outlink = ctx->outputs[0];722 AVFilterLink *outlink = ctx->outputs[0];
723 const int pl = s->preserve_lightness;
514 ThreadData td;724 ThreadData td;
515 AVFrame *out;725 AVFrame *out;
516 726
@@ -527,7 +737,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 10 unmodified lines
527 737
528 td.in = in;738 td.in = in;
529 td.out = out;739 td.out = out;
530 ctx->internal->execute(ctx, s->filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));740 ctx->internal->execute(ctx, s->filter_slice[pl], &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
531 741
532 if (in != out)742 if (in != out)
533 av_frame_free(&in);743 av_frame_free(&in);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t