[FFmpeg-cvslog] avfilter/vf_colorchannelmixer: add option to preserve lightness

Paul B Mahol git at videolan.org
Sat Jan 23 23:29:29 EET 2021


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Sat Jan 23 22:01:46 2021 +0100| [b2bbcede5e44e4c71d761ac28f3a5c1e653c3cb6] | committer: Paul B Mahol

avfilter/vf_colorchannelmixer: add option to preserve lightness

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=b2bbcede5e44e4c71d761ac28f3a5c1e653c3cb6
---

 doc/filters.texi                   |   3 +
 libavfilter/vf_colorchannelmixer.c | 378 ++++++++++++++++++++++++++++---------
 2 files changed, 297 insertions(+), 84 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index d75a1f5a78..62734e06e5 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -8065,6 +8065,9 @@ Adjust contribution of input red, green, blue and alpha channels for output alph
 Default is @code{1} for @var{aa}, and @code{0} for @var{ar}, @var{ag} and @var{ab}.
 
 Allowed ranges for options are @code{[-2.0, 2.0]}.
+
+ at item pl
+Preserve lightness when changing colors. Default is disabled.
 @end table
 
 @subsection Examples
diff --git a/libavfilter/vf_colorchannelmixer.c b/libavfilter/vf_colorchannelmixer.c
index 3bbe44d6ef..6085b36265 100644
--- a/libavfilter/vf_colorchannelmixer.c
+++ b/libavfilter/vf_colorchannelmixer.c
@@ -18,6 +18,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <float.h>
+
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
@@ -41,6 +43,8 @@ typedef struct ColorChannelMixerContext {
     double gr, gg, gb, ga;
     double br, bg, bb, ba;
     double ar, ag, ab, aa;
+    double sr, sg, sb;
+    int preserve_lightness;
 
     int *lut[4][4];
 
@@ -48,7 +52,7 @@ typedef struct ColorChannelMixerContext {
 
     uint8_t rgba_map[4];
 
-    int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
+    int (*filter_slice[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
 } ColorChannelMixerContext;
 
 #define OFFSET(x) offsetof(ColorChannelMixerContext, x)
@@ -71,6 +75,7 @@ static const AVOption colorchannelmixer_options[] = {
     { "ag", "set the green gain for the alpha channel", OFFSET(ag), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
     { "ab", "set the blue gain for the alpha channel",  OFFSET(ab), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
     { "aa", "set the alpha gain for the alpha channel", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
+    { "pl", "preserve lightness",       OFFSET(preserve_lightness), AV_OPT_TYPE_BOOL,   {.i64=0},  0, 1, FLAGS },
     { NULL }
 };
 
@@ -101,8 +106,15 @@ static int query_formats(AVFilterContext *ctx)
     return ff_set_common_formats(ctx, fmts_list);
 }
 
+static void preservel(float *r, float *g, float *b, float lin, float lout)
+{
+    *r *= lout / lin;
+    *g *= lout / lin;
+    *b *= lout / lin;
+}
+
 static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
-                                                     int have_alpha)
+                                                     int have_alpha, int pl)
 {
     ColorChannelMixerContext *s = ctx->priv;
     ThreadData *td = arg;
@@ -126,19 +138,42 @@ static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void
             const uint8_t gin = srcg[j];
             const uint8_t bin = srcb[j];
             const uint8_t ain = have_alpha ? srca[j] : 0;
+            int rout, gout, bout;
+            float lin;
+
+            if (pl)
+                lin = FFMAX3(rin, gin, bin) / 255.f + FFMIN3(rin, gin, bin) / 255.f;
+
+            rout = s->lut[R][R][rin] +
+                   s->lut[R][G][gin] +
+                   s->lut[R][B][bin] +
+                   (have_alpha == 1 ? s->lut[R][A][ain] : 0);
+            gout = s->lut[G][R][rin] +
+                   s->lut[G][G][gin] +
+                   s->lut[G][B][bin] +
+                   (have_alpha == 1 ? s->lut[G][A][ain] : 0);
+            bout = s->lut[B][R][rin] +
+                   s->lut[B][G][gin] +
+                   s->lut[B][B][bin] +
+                   (have_alpha == 1 ? s->lut[B][A][ain] : 0);
+
+            if (pl) {
+                float frout = rout / (255.f * s->sr);
+                float fgout = gout / (255.f * s->sg);
+                float fbout = bout / (255.f * s->sb);
+                float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
+
+                preservel(&frout, &fgout, &fbout, lin, lout);
+
+                rout = lrintf(frout * 255.f);
+                gout = lrintf(fgout * 255.f);
+                bout = lrintf(fbout * 255.f);
+            }
+
+            dstr[j] = av_clip_uint8(rout);
+            dstg[j] = av_clip_uint8(gout);
+            dstb[j] = av_clip_uint8(bout);
 
-            dstr[j] = av_clip_uint8(s->lut[R][R][rin] +
-                                    s->lut[R][G][gin] +
-                                    s->lut[R][B][bin] +
-                                    (have_alpha == 1 ? s->lut[R][A][ain] : 0));
-            dstg[j] = av_clip_uint8(s->lut[G][R][rin] +
-                                    s->lut[G][G][gin] +
-                                    s->lut[G][B][bin] +
-                                    (have_alpha == 1 ? s->lut[G][A][ain] : 0));
-            dstb[j] = av_clip_uint8(s->lut[B][R][rin] +
-                                    s->lut[B][G][gin] +
-                                    s->lut[B][B][bin] +
-                                    (have_alpha == 1 ? s->lut[B][A][ain] : 0));
             if (have_alpha == 1) {
                 dsta[j] = av_clip_uint8(s->lut[A][R][rin] +
                                         s->lut[A][G][gin] +
@@ -161,7 +196,7 @@ static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void
 }
 
 static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
-                                                       int have_alpha, int depth)
+                                                       int have_alpha, int depth, int pl)
 {
     ColorChannelMixerContext *s = ctx->priv;
     ThreadData *td = arg;
@@ -177,6 +212,8 @@ static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, voi
     uint16_t *dstb = (uint16_t *)(out->data[1] + slice_start * out->linesize[1]);
     uint16_t *dstr = (uint16_t *)(out->data[2] + slice_start * out->linesize[2]);
     uint16_t *dsta = (uint16_t *)(out->data[3] + slice_start * out->linesize[3]);
+    const float scale = 1.f / ((1 << depth) - 1);
+    const float factor = (1 << depth) - 1;
     int i, j;
 
     for (i = slice_start; i < slice_end; i++) {
@@ -185,19 +222,42 @@ static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, voi
             const uint16_t gin = srcg[j];
             const uint16_t bin = srcb[j];
             const uint16_t ain = have_alpha ? srca[j] : 0;
+            int rout, gout, bout;
+            float lin;
+
+            if (pl)
+                lin = FFMAX3(rin, gin, bin) * scale + FFMIN3(rin, gin, bin) * scale;
+
+            rout = s->lut[R][R][rin] +
+                   s->lut[R][G][gin] +
+                   s->lut[R][B][bin] +
+                   (have_alpha == 1 ? s->lut[R][A][ain] : 0);
+            gout = s->lut[G][R][rin] +
+                   s->lut[G][G][gin] +
+                   s->lut[G][B][bin] +
+                   (have_alpha == 1 ? s->lut[G][A][ain] : 0);
+            bout = s->lut[B][R][rin] +
+                   s->lut[B][G][gin] +
+                   s->lut[B][B][bin] +
+                   (have_alpha == 1 ? s->lut[B][A][ain] : 0);
+
+            if (pl) {
+                float frout = rout / (factor * s->sr);
+                float fgout = gout / (factor * s->sg);
+                float fbout = bout / (factor * s->sb);
+                float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
+
+                preservel(&frout, &fgout, &fbout, lin, lout);
+
+                rout = lrintf(frout * factor);
+                gout = lrintf(fgout * factor);
+                bout = lrintf(fbout * factor);
+            }
+
+            dstr[j] = av_clip_uintp2(rout, depth);
+            dstg[j] = av_clip_uintp2(gout, depth);
+            dstb[j] = av_clip_uintp2(bout, depth);
 
-            dstr[j] = av_clip_uintp2(s->lut[R][R][rin] +
-                                     s->lut[R][G][gin] +
-                                     s->lut[R][B][bin] +
-                                     (have_alpha == 1 ? s->lut[R][A][ain] : 0), depth);
-            dstg[j] = av_clip_uintp2(s->lut[G][R][rin] +
-                                     s->lut[G][G][gin] +
-                                     s->lut[G][B][bin] +
-                                     (have_alpha == 1 ? s->lut[G][A][ain] : 0), depth);
-            dstb[j] = av_clip_uintp2(s->lut[B][R][rin] +
-                                     s->lut[B][G][gin] +
-                                     s->lut[B][B][bin] +
-                                     (have_alpha == 1 ? s->lut[B][A][ain] : 0), depth);
             if (have_alpha == 1) {
                 dsta[j] = av_clip_uintp2(s->lut[A][R][rin] +
                                          s->lut[A][G][gin] +
@@ -221,56 +281,106 @@ static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, voi
 
 static int filter_slice_gbrp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 0);
+    return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 0, 0);
 }
 
 static int filter_slice_gbrap(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 1);
+    return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 1, 0);
+}
+
+static int filter_slice_gbrp_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 0, 1);
+}
+
+static int filter_slice_gbrap_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba_planar(ctx, arg, jobnr, nb_jobs, 1, 1);
 }
 
 static int filter_slice_gbrp9(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 9);
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 9, 0);
 }
 
 static int filter_slice_gbrp10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 10);
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 10, 0);
 }
 
 static int filter_slice_gbrap10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 10);
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 10, 0);
 }
 
 static int filter_slice_gbrp12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 12);
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 12, 0);
 }
 
 static int filter_slice_gbrap12(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 12);
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 12, 0);
 }
 
 static int filter_slice_gbrp14(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 14);
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 14, 0);
 }
 
 static int filter_slice_gbrp16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 16);
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 16, 0);
 }
 
 static int filter_slice_gbrap16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 16);
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 16, 0);
+}
+
+static int filter_slice_gbrp9_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 9, 1);
+}
+
+static int filter_slice_gbrp10_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 10, 1);
+}
+
+static int filter_slice_gbrap10_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 10, 1);
+}
+
+static int filter_slice_gbrp12_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 12, 1);
+}
+
+static int filter_slice_gbrap12_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 12, 1);
+}
+
+static int filter_slice_gbrp14_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 14, 1);
+}
+
+static int filter_slice_gbrp16_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 0, 16, 1);
+}
+
+static int filter_slice_gbrap16_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba16_planar(ctx, arg, jobnr, nb_jobs, 1, 16, 1);
 }
 
 static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
-                                                     int have_alpha, int step)
+                                                     int have_alpha, int step, int pl)
 {
     ColorChannelMixerContext *s = ctx->priv;
     ThreadData *td = arg;
@@ -295,19 +405,42 @@ static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void
             const uint8_t gin = src[j + goffset];
             const uint8_t bin = src[j + boffset];
             const uint8_t ain = src[j + aoffset];
+            int rout, gout, bout;
+            float lin;
+
+            if (pl)
+                lin = FFMAX3(rin, gin, bin) / 255.f + FFMIN3(rin, gin, bin) / 255.f;
+
+            rout = s->lut[R][R][rin] +
+                   s->lut[R][G][gin] +
+                   s->lut[R][B][bin] +
+                   (have_alpha == 1 ? s->lut[R][A][ain] : 0);
+            gout = s->lut[G][R][rin] +
+                   s->lut[G][G][gin] +
+                   s->lut[G][B][bin] +
+                   (have_alpha == 1 ? s->lut[G][A][ain] : 0);
+            bout = s->lut[B][R][rin] +
+                   s->lut[B][G][gin] +
+                   s->lut[B][B][bin] +
+                   (have_alpha == 1 ? s->lut[B][A][ain] : 0);
+
+            if (pl) {
+                float frout = rout / (255.f * s->sr);
+                float fgout = gout / (255.f * s->sg);
+                float fbout = bout / (255.f * s->sb);
+                float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
+
+                preservel(&frout, &fgout, &fbout, lin, lout);
+
+                rout = lrintf(frout * 255.f);
+                gout = lrintf(fgout * 255.f);
+                bout = lrintf(fbout * 255.f);
+            }
+
+            dst[j + roffset] = av_clip_uint8(rout);
+            dst[j + goffset] = av_clip_uint8(gout);
+            dst[j + boffset] = av_clip_uint8(bout);
 
-            dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
-                                             s->lut[R][G][gin] +
-                                             s->lut[R][B][bin] +
-                                             (have_alpha == 1 ? s->lut[R][A][ain] : 0));
-            dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
-                                             s->lut[G][G][gin] +
-                                             s->lut[G][B][bin] +
-                                             (have_alpha == 1 ? s->lut[G][A][ain] : 0));
-            dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
-                                             s->lut[B][G][gin] +
-                                             s->lut[B][B][bin] +
-                                             (have_alpha == 1 ? s->lut[B][A][ain] : 0));
             if (have_alpha == 1) {
                 dst[j + aoffset] = av_clip_uint8(s->lut[A][R][rin] +
                                                  s->lut[A][G][gin] +
@@ -325,7 +458,7 @@ static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void
 }
 
 static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
-                                                       int have_alpha, int step)
+                                                       int have_alpha, int step, int pl)
 {
     ColorChannelMixerContext *s = ctx->priv;
     ThreadData *td = arg;
@@ -350,19 +483,42 @@ static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, voi
             const uint16_t gin = src[j + goffset];
             const uint16_t bin = src[j + boffset];
             const uint16_t ain = src[j + aoffset];
+            int rout, gout, bout;
+            float lin;
+
+            if (pl)
+                lin = FFMAX3(rin, gin, bin) / 65535.f + FFMIN3(rin, gin, bin) / 65535.f;
+
+            rout = s->lut[R][R][rin] +
+                   s->lut[R][G][gin] +
+                   s->lut[R][B][bin] +
+                   (have_alpha == 1 ? s->lut[R][A][ain] : 0);
+            gout = s->lut[G][R][rin] +
+                   s->lut[G][G][gin] +
+                   s->lut[G][B][bin] +
+                   (have_alpha == 1 ? s->lut[G][A][ain] : 0);
+            bout = s->lut[B][R][rin] +
+                   s->lut[B][G][gin] +
+                   s->lut[B][B][bin] +
+                   (have_alpha == 1 ? s->lut[B][A][ain] : 0);
+
+            if (pl) {
+                float frout = rout / (65535.f * s->sr);
+                float fgout = gout / (65535.f * s->sg);
+                float fbout = bout / (65535.f * s->sb);
+                float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
+
+                preservel(&frout, &fgout, &fbout, lin, lout);
+
+                rout = lrintf(frout * 65535.f);
+                gout = lrintf(fgout * 65535.f);
+                bout = lrintf(fbout * 65535.f);
+            }
+
+            dst[j + roffset] = av_clip_uint16(rout);
+            dst[j + goffset] = av_clip_uint16(gout);
+            dst[j + boffset] = av_clip_uint16(bout);
 
-            dst[j + roffset] = av_clip_uint16(s->lut[R][R][rin] +
-                                              s->lut[R][G][gin] +
-                                              s->lut[R][B][bin] +
-                                              (have_alpha == 1 ? s->lut[R][A][ain] : 0));
-            dst[j + goffset] = av_clip_uint16(s->lut[G][R][rin] +
-                                              s->lut[G][G][gin] +
-                                              s->lut[G][B][bin] +
-                                              (have_alpha == 1 ? s->lut[G][A][ain] : 0));
-            dst[j + boffset] = av_clip_uint16(s->lut[B][R][rin] +
-                                              s->lut[B][G][gin] +
-                                              s->lut[B][B][bin] +
-                                              (have_alpha == 1 ? s->lut[B][A][ain] : 0));
             if (have_alpha == 1) {
                 dst[j + aoffset] = av_clip_uint16(s->lut[A][R][rin] +
                                                   s->lut[A][G][gin] +
@@ -380,27 +536,52 @@ static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, voi
 
 static int filter_slice_rgba64(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 1, 4);
+    return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 0);
 }
 
 static int filter_slice_rgb48(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 0, 3);
+    return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 0);
+}
+
+static int filter_slice_rgba64_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 1);
+}
+
+static int filter_slice_rgb48_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba16_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 1);
 }
 
 static int filter_slice_rgba(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 1, 4);
+    return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 0);
 }
 
 static int filter_slice_rgb24(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 0, 3);
+    return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 0);
 }
 
 static int filter_slice_rgb0(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, -1, 4);
+    return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, -1, 4, 0);
+}
+
+static int filter_slice_rgba_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 1, 4, 1);
+}
+
+static int filter_slice_rgb24_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, 0, 3, 1);
+}
+
+static int filter_slice_rgb0_pl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    return filter_slice_rgba_packed(ctx, arg, jobnr, nb_jobs, -1, 4, 1);
 }
 
 static int config_output(AVFilterLink *outlink)
@@ -424,6 +605,19 @@ static int config_output(AVFilterLink *outlink)
                 s->lut[i][j] = buffer;
     }
 
+    s->sr = s->rr + s->rg + s->rb + s->ra;
+    s->sg = s->gr + s->gg + s->gb + s->ga;
+    s->sb = s->br + s->bg + s->bb + s->ba;
+
+    if (fabs(s->sr) <= DBL_EPSILON)
+        s->sr = 1.;
+
+    if (fabs(s->sg) <= DBL_EPSILON)
+        s->sg = 1.;
+
+    if (fabs(s->sb) <= DBL_EPSILON)
+        s->sb = 1.;
+
     for (i = 0; i < size; i++) {
         s->lut[R][R][i] = lrint(i * s->rr);
         s->lut[R][G][i] = lrint(i * s->rg);
@@ -449,57 +643,72 @@ static int config_output(AVFilterLink *outlink)
     switch (outlink->format) {
     case AV_PIX_FMT_BGR24:
     case AV_PIX_FMT_RGB24:
-        s->filter_slice = filter_slice_rgb24;
+        s->filter_slice[0] = filter_slice_rgb24;
+        s->filter_slice[1] = filter_slice_rgb24_pl;
         break;
     case AV_PIX_FMT_0BGR:
     case AV_PIX_FMT_0RGB:
     case AV_PIX_FMT_BGR0:
     case AV_PIX_FMT_RGB0:
-        s->filter_slice = filter_slice_rgb0;
+        s->filter_slice[0] = filter_slice_rgb0;
+        s->filter_slice[1] = filter_slice_rgb0_pl;
         break;
     case AV_PIX_FMT_ABGR:
     case AV_PIX_FMT_ARGB:
     case AV_PIX_FMT_BGRA:
     case AV_PIX_FMT_RGBA:
-        s->filter_slice = filter_slice_rgba;
+        s->filter_slice[0] = filter_slice_rgba;
+        s->filter_slice[1] = filter_slice_rgba_pl;
         break;
     case AV_PIX_FMT_BGR48:
     case AV_PIX_FMT_RGB48:
-        s->filter_slice = filter_slice_rgb48;
+        s->filter_slice[0] = filter_slice_rgb48;
+        s->filter_slice[1] = filter_slice_rgb48_pl;
         break;
     case AV_PIX_FMT_BGRA64:
     case AV_PIX_FMT_RGBA64:
-        s->filter_slice = filter_slice_rgba64;
+        s->filter_slice[0] = filter_slice_rgba64;
+        s->filter_slice[1] = filter_slice_rgba64_pl;
         break;
     case AV_PIX_FMT_GBRP:
-        s->filter_slice = filter_slice_gbrp;
+        s->filter_slice[0] = filter_slice_gbrp;
+        s->filter_slice[1] = filter_slice_gbrp_pl;
         break;
     case AV_PIX_FMT_GBRAP:
-        s->filter_slice = filter_slice_gbrap;
+        s->filter_slice[0] = filter_slice_gbrap;
+        s->filter_slice[1] = filter_slice_gbrap_pl;
         break;
     case AV_PIX_FMT_GBRP9:
-        s->filter_slice = filter_slice_gbrp9;
+        s->filter_slice[0] = filter_slice_gbrp9;
+        s->filter_slice[1] = filter_slice_gbrp9_pl;
         break;
     case AV_PIX_FMT_GBRP10:
-        s->filter_slice = filter_slice_gbrp10;
+        s->filter_slice[0] = filter_slice_gbrp10;
+        s->filter_slice[1] = filter_slice_gbrp10_pl;
         break;
     case AV_PIX_FMT_GBRAP10:
-        s->filter_slice = filter_slice_gbrap10;
+        s->filter_slice[0] = filter_slice_gbrap10;
+        s->filter_slice[1] = filter_slice_gbrap10_pl;
         break;
     case AV_PIX_FMT_GBRP12:
-        s->filter_slice = filter_slice_gbrp12;
+        s->filter_slice[0] = filter_slice_gbrp12;
+        s->filter_slice[1] = filter_slice_gbrp12_pl;
         break;
     case AV_PIX_FMT_GBRAP12:
-        s->filter_slice = filter_slice_gbrap12;
+        s->filter_slice[0] = filter_slice_gbrap12;
+        s->filter_slice[1] = filter_slice_gbrap12_pl;
         break;
     case AV_PIX_FMT_GBRP14:
-        s->filter_slice = filter_slice_gbrp14;
+        s->filter_slice[0] = filter_slice_gbrp14;
+        s->filter_slice[1] = filter_slice_gbrp14_pl;
         break;
     case AV_PIX_FMT_GBRP16:
-        s->filter_slice = filter_slice_gbrp16;
+        s->filter_slice[0] = filter_slice_gbrp16;
+        s->filter_slice[1] = filter_slice_gbrp16_pl;
         break;
     case AV_PIX_FMT_GBRAP16:
-        s->filter_slice = filter_slice_gbrap16;
+        s->filter_slice[0] = filter_slice_gbrap16;
+        s->filter_slice[1] = filter_slice_gbrap16_pl;
         break;
     }
 
@@ -511,6 +720,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     AVFilterContext *ctx = inlink->dst;
     ColorChannelMixerContext *s = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
+    const int pl = s->preserve_lightness;
     ThreadData td;
     AVFrame *out;
 
@@ -527,7 +737,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 
     td.in = in;
     td.out = out;
-    ctx->internal->execute(ctx, s->filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
+    ctx->internal->execute(ctx, s->filter_slice[pl], &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
 
     if (in != out)
         av_frame_free(&in);



More information about the ffmpeg-cvslog mailing list