From 17d5ddf4c2e18dbe3e563efb72e4d09b9e8ae72c Mon Sep 17 00:00:00 2001
From: Paul B Mahol <onemda@gmail.com>
Date: Sun, 13 Aug 2017 19:16:42 +0200
Subject: [PATCH] avfilter: add chromatestsrc source filter

---
 doc/filters.texi           |  9 ++++-
 libavfilter/Makefile       |  1 +
 libavfilter/allfilters.c   |  1 +
 libavfilter/vsrc_testsrc.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 108 insertions(+), 1 deletion(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index eedc7b5896..8a377b4fff 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -16423,6 +16423,7 @@ ffplay -f lavfi life=s=300x200:mold=10:r=60:ratio=0.1:death_color=#C83232:life_c
 
 @anchor{allrgb}
 @anchor{allyuv}
+@anchor{chromatestsrc}
 @anchor{color}
 @anchor{haldclutsrc}
 @anchor{nullsrc}
@@ -16432,12 +16433,18 @@ ffplay -f lavfi life=s=300x200:mold=10:r=60:ratio=0.1:death_color=#C83232:life_c
 @anchor{testsrc}
 @anchor{testsrc2}
 @anchor{yuvtestsrc}
-@section allrgb, allyuv, color, haldclutsrc, nullsrc, rgbtestsrc, smptebars, smptehdbars, testsrc, testsrc2, yuvtestsrc
+@section allrgb, allyuv, chromatestsrc, color, haldclutsrc, nullsrc, rgbtestsrc, smptebars, smptehdbars, testsrc, testsrc2, yuvtestsrc
 
 The @code{allrgb} source returns frames of size 4096x4096 of all rgb colors.
 
 The @code{allyuv} source returns frames of size 4096x4096 of all yuv colors.
 
+The @code{chromatestsrc} source provides a test pattern for detecting chroma
+subsampling of display. If all quadrants are of similar color than chroma
+subsampling is 4:2:0. If only top right quadrant is darker subsampling is
+4:4:4. If bottom left is brigthest subsampling is 4:2:2. If bottom right
+is brightest subsampling is 4:4:0.
+
 The @code{color} source provides an uniformly colored input.
 
 The @code{haldclutsrc} source provides an identity Hald CLUT. See also
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 06b915fc91..e324679b9b 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -338,6 +338,7 @@ OBJS-$(CONFIG_ZSCALE_FILTER)                 += vf_zscale.o
 OBJS-$(CONFIG_ALLRGB_FILTER)                 += vsrc_testsrc.o
 OBJS-$(CONFIG_ALLYUV_FILTER)                 += vsrc_testsrc.o
 OBJS-$(CONFIG_CELLAUTO_FILTER)               += vsrc_cellauto.o
+OBJS-$(CONFIG_CHROMATESTSRC_FILTER)          += vsrc_testsrc.o
 OBJS-$(CONFIG_COLOR_FILTER)                  += vsrc_testsrc.o
 OBJS-$(CONFIG_COREIMAGESRC_FILTER)           += vf_coreimage.o
 OBJS-$(CONFIG_FREI0R_SRC_FILTER)             += vf_frei0r.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index e58c0d5c82..14a78d0844 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -349,6 +349,7 @@ static void register_all(void)
     REGISTER_FILTER(ALLRGB,         allrgb,         vsrc);
     REGISTER_FILTER(ALLYUV,         allyuv,         vsrc);
     REGISTER_FILTER(CELLAUTO,       cellauto,       vsrc);
+    REGISTER_FILTER(CHROMATESTSRC,  chromatestsrc,  vsrc);
     REGISTER_FILTER(COLOR,          color,          vsrc);
     REGISTER_FILTER(COREIMAGESRC,   coreimagesrc,   vsrc);
     REGISTER_FILTER(FREI0R,         frei0r_src,     vsrc);
diff --git a/libavfilter/vsrc_testsrc.c b/libavfilter/vsrc_testsrc.c
index fe0d50aa41..2be71f9b68 100644
--- a/libavfilter/vsrc_testsrc.c
+++ b/libavfilter/vsrc_testsrc.c
@@ -1705,3 +1705,101 @@ AVFilter ff_vsrc_allrgb = {
 };
 
 #endif /* CONFIG_ALLRGB_FILTER */
+
+#if CONFIG_CHROMATESTSRC_FILTER
+
+#define chromatestsrc_options options
+AVFILTER_DEFINE_CLASS(chromatestsrc);
+
+static void chromatestsrc_fill_picture(AVFilterContext *ctx, AVFrame *frame)
+{
+    const int gs = frame->linesize[0];
+    const int bs = frame->linesize[1];
+    const int rs = frame->linesize[2];
+    uint8_t *gptr = frame->data[0];
+    uint8_t *bptr = frame->data[1];
+    uint8_t *rptr = frame->data[2];
+    int x, y;
+
+    for (y = 0; y < frame->height / 2; y++) {
+        for (x = 0; x < frame->width / 2; x++) {
+            gptr[x] = 0;
+            bptr[x] = (x & 1) == (y & 1) ? 0 : 255;
+            rptr[x] = (x & 1) != (y & 1) ? 0 : 255;
+        }
+
+        for (; x < frame->width; x++) {
+            gptr[x] = 0;
+            bptr[x] = 128;
+            rptr[x] = 128;
+        }
+
+        gptr += gs;
+        bptr += bs;
+        rptr += rs;
+    }
+
+    for (; y < frame->height; y++) {
+        for (x = 0; x < frame->width / 2; x++) {
+            gptr[x] = 0;
+            bptr[x] = (y & 1) ? 0 : 255;
+            rptr[x] = (y & 1) ? 255 : 0;
+        }
+
+        for (; x < frame->width; x++) {
+            gptr[x] = 0;
+            bptr[x] = (x & 1) ? 0 : 255;
+            rptr[x] = (x & 1) ? 255 : 0;
+        }
+
+        gptr += gs;
+        bptr += bs;
+        rptr += rs;
+    }
+}
+
+static av_cold int chromatestsrc_init(AVFilterContext *ctx)
+{
+    TestSourceContext *test = ctx->priv;
+
+    test->draw_once = 1;
+    test->fill_picture_fn = chromatestsrc_fill_picture;
+    return init(ctx);
+}
+
+static int chromatestsrc_query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_GBRP,
+        AV_PIX_FMT_NONE
+    };
+
+    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
+    if (!fmts_list)
+        return AVERROR(ENOMEM);
+    return ff_set_common_formats(ctx, fmts_list);
+}
+
+static const AVFilterPad avfilter_vsrc_chromatestsrc_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .request_frame = request_frame,
+        .config_props  = config_props,
+    },
+    { NULL }
+};
+
+AVFilter ff_vsrc_chromatestsrc = {
+    .name          = "chromatestsrc",
+    .description   = NULL_IF_CONFIG_SMALL("Generate chroma subsampling test."),
+    .priv_size     = sizeof(TestSourceContext),
+    .priv_class    = &chromatestsrc_class,
+    .init          = chromatestsrc_init,
+    .uninit        = uninit,
+    .query_formats = chromatestsrc_query_formats,
+    .inputs        = NULL,
+    .outputs       = avfilter_vsrc_chromatestsrc_outputs,
+};
+
+#endif /* CONFIG_CHROMATESTSRC_FILTER */
-- 
2.13.3

