avfilter/vf_colorchannelmixer: add planar rgb support
authorPaul B Mahol <onemda@gmail.com>
Sat, 5 May 2018 15:25:13 +0000 (17:25 +0200)
committerPaul B Mahol <onemda@gmail.com>
Sat, 5 May 2018 15:37:50 +0000 (17:37 +0200)
Signed-off-by: Paul B Mahol <onemda@gmail.com>
libavfilter/vf_colorchannelmixer.c

index bbd7eaa..3a9cd37 100644 (file)
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
 #include "avfilter.h"
 #include "drawutils.h"
 #include "formats.h"
@@ -84,6 +85,12 @@ static int query_formats(AVFilterContext *ctx)
         AV_PIX_FMT_RGB0,   AV_PIX_FMT_BGR0,
         AV_PIX_FMT_RGB48,  AV_PIX_FMT_BGR48,
         AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
+        AV_PIX_FMT_GBRP,   AV_PIX_FMT_GBRAP,
+        AV_PIX_FMT_GBRP9,
+        AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
+        AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
+        AV_PIX_FMT_GBRP14,
+        AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
         AV_PIX_FMT_NONE
     };
 
@@ -93,6 +100,174 @@ static int query_formats(AVFilterContext *ctx)
     return ff_set_common_formats(ctx, fmts_list);
 }
 
+static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
+                                                     int have_alpha)
+{
+    ColorChannelMixerContext *s = ctx->priv;
+    ThreadData *td = arg;
+    AVFrame *in = td->in;
+    AVFrame *out = td->out;
+    const int slice_start = (out->height * jobnr) / nb_jobs;
+    const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
+    const uint8_t *srcg = in->data[0] + slice_start * in->linesize[0];
+    const uint8_t *srcb = in->data[1] + slice_start * in->linesize[1];
+    const uint8_t *srcr = in->data[2] + slice_start * in->linesize[2];
+    const uint8_t *srca = in->data[3] + slice_start * in->linesize[3];
+    uint8_t *dstg = out->data[0] + slice_start * out->linesize[0];
+    uint8_t *dstb = out->data[1] + slice_start * out->linesize[1];
+    uint8_t *dstr = out->data[2] + slice_start * out->linesize[2];
+    uint8_t *dsta = out->data[3] + slice_start * out->linesize[3];
+    int i, j;
+
+    for (i = slice_start; i < slice_end; i++) {
+        for (j = 0; j < out->width; j++) {
+            const uint8_t rin = srcr[j];
+            const uint8_t gin = srcg[j];
+            const uint8_t bin = srcb[j];
+            const uint8_t ain = srca[j];
+
+            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] +
+                                        s->lut[A][B][bin] +
+                                        s->lut[A][A][ain]);
+            }
+        }
+
+        srcg += in->linesize[0];
+        srcb += in->linesize[1];
+        srcr += in->linesize[2];
+        srca += in->linesize[3];
+        dstg += out->linesize[0];
+        dstb += out->linesize[1];
+        dstr += out->linesize[2];
+        dsta += out->linesize[3];
+    }
+
+    return 0;
+}
+
+static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
+                                                       int have_alpha, int depth)
+{
+    ColorChannelMixerContext *s = ctx->priv;
+    ThreadData *td = arg;
+    AVFrame *in = td->in;
+    AVFrame *out = td->out;
+    const int slice_start = (out->height * jobnr) / nb_jobs;
+    const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
+    const uint16_t *srcg = (const uint16_t *)(in->data[0] + slice_start * in->linesize[0]);
+    const uint16_t *srcb = (const uint16_t *)(in->data[1] + slice_start * in->linesize[1]);
+    const uint16_t *srcr = (const uint16_t *)(in->data[2] + slice_start * in->linesize[2]);
+    const uint16_t *srca = (const uint16_t *)(in->data[3] + slice_start * in->linesize[3]);
+    uint16_t *dstg = (uint16_t *)(out->data[0] + slice_start * out->linesize[0]);
+    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]);
+    int i, j;
+
+    for (i = slice_start; i < slice_end; i++) {
+        for (j = 0; j < out->width; j++) {
+            const uint16_t rin = srcr[j];
+            const uint16_t gin = srcg[j];
+            const uint16_t bin = srcb[j];
+            const uint16_t ain = srca[j];
+
+            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] +
+                                         s->lut[A][B][bin] +
+                                         s->lut[A][A][ain], depth);
+            }
+        }
+
+        srcg += in->linesize[0] / 2;
+        srcb += in->linesize[1] / 2;
+        srcr += in->linesize[2] / 2;
+        srca += in->linesize[3] / 2;
+        dstg += out->linesize[0] / 2;
+        dstb += out->linesize[1] / 2;
+        dstr += out->linesize[2] / 2;
+        dsta += out->linesize[3] / 2;
+    }
+
+    return 0;
+}
+
+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);
+}
+
+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);
+}
+
+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);
+}
+
+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);
+}
+
+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);
+}
+
+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);
+}
+
+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);
+}
+
+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);
+}
+
+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);
+}
+
+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);
+}
+
 static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs,
                                                      int have_alpha, int step)
 {
@@ -122,13 +297,16 @@ static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void
 
             dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
                                              s->lut[R][G][gin] +
-                                             s->lut[R][B][bin]);
+                                             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]);
+                                             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]);
+                                             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] +
@@ -174,20 +352,22 @@ static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, voi
 
             dst[j + roffset] = av_clip_uint16(s->lut[R][R][rin] +
                                               s->lut[R][G][gin] +
-                                              s->lut[R][B][bin]);
+                                              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]);
+                                              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]);
+                                              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] +
                                                   s->lut[A][B][bin] +
                                                   s->lut[A][A][ain]);
-            } else if (have_alpha == -1 && in != out)
-                dst[j + aoffset] = 0;
+            }
         }
 
         srcrow += in->linesize[0];
@@ -226,21 +406,13 @@ static int config_output(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
     ColorChannelMixerContext *s = ctx->priv;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
+    const int depth = desc->comp[0].depth;
     int i, j, size, *buffer;
 
     ff_fill_rgba_map(s->rgba_map, outlink->format);
 
-    switch (outlink->format) {
-    case AV_PIX_FMT_RGB48:
-    case AV_PIX_FMT_BGR48:
-    case AV_PIX_FMT_RGBA64:
-    case AV_PIX_FMT_BGRA64:
-        size = 65536;
-        break;
-    default:
-        size = 256;
-    }
-
+    size = 1 << depth;
     s->buffer = buffer = av_malloc(16 * size * sizeof(*s->buffer));
     if (!s->buffer)
         return AVERROR(ENOMEM);
@@ -296,6 +468,36 @@ static int config_output(AVFilterLink *outlink)
     case AV_PIX_FMT_RGBA64:
         s->filter_slice = filter_slice_rgba64;
         break;
+    case AV_PIX_FMT_GBRP:
+        s->filter_slice = filter_slice_gbrp;
+        break;
+    case AV_PIX_FMT_GBRAP:
+        s->filter_slice = filter_slice_gbrap;
+        break;
+    case AV_PIX_FMT_GBRP9:
+        s->filter_slice = filter_slice_gbrp9;
+        break;
+    case AV_PIX_FMT_GBRP10:
+        s->filter_slice = filter_slice_gbrp10;
+        break;
+    case AV_PIX_FMT_GBRAP10:
+        s->filter_slice = filter_slice_gbrap10;
+        break;
+    case AV_PIX_FMT_GBRP12:
+        s->filter_slice = filter_slice_gbrp12;
+        break;
+    case AV_PIX_FMT_GBRAP12:
+        s->filter_slice = filter_slice_gbrap12;
+        break;
+    case AV_PIX_FMT_GBRP14:
+        s->filter_slice = filter_slice_gbrp14;
+        break;
+    case AV_PIX_FMT_GBRP16:
+        s->filter_slice = filter_slice_gbrp16;
+        break;
+    case AV_PIX_FMT_GBRAP16:
+        s->filter_slice = filter_slice_gbrap16;
+        break;
     }
 
     return 0;