History

avfilter: add colorspectrum source video filter

Paul B Mahol committed 5 years ago

Commit d401b1c1 parent 08b47166 files · +111 −3Browse files

Showing the changes to libavfilter (4 of 6 files). Show all

Unified · Split

4 files
  1. libavfilter/Makefile +1 −0
  2. libavfilter/allfilters.c +1 −0
  3. libavfilter/version.h +2 −2
  4. libavfilter/vsrc_testsrc.c +92 −0
libavfilter/Makefile+1−0
@@ -522,6 +522,7 @@ OBJS-$(CONFIG_ALLRGB_FILTER) += vsrc_testsrc.o · 521 unmodified lines
522522 OBJS-$(CONFIG_ALLYUV_FILTER) += vsrc_testsrc.o
523523 OBJS-$(CONFIG_CELLAUTO_FILTER) += vsrc_cellauto.o
524524 OBJS-$(CONFIG_COLOR_FILTER) += vsrc_testsrc.o
525+OBJS-$(CONFIG_COLORSPECTRUM_FILTER) += vsrc_testsrc.o
525526 OBJS-$(CONFIG_COREIMAGESRC_FILTER) += vf_coreimage.o
526527 OBJS-$(CONFIG_FREI0R_SRC_FILTER) += vf_frei0r.o
527528 OBJS-$(CONFIG_GRADIENTS_FILTER) += vsrc_gradients.o
libavfilter/allfilters.c+1−0
@@ -498,6 +498,7 @@ extern const AVFilter ff_vsrc_allrgb; · 497 unmodified lines
498498 extern const AVFilter ff_vsrc_allyuv;
499499 extern const AVFilter ff_vsrc_cellauto;
500500 extern const AVFilter ff_vsrc_color;
501+extern const AVFilter ff_vsrc_colorspectrum;
501502 extern const AVFilter ff_vsrc_coreimagesrc;
502503 extern const AVFilter ff_vsrc_frei0r_src;
503504 extern const AVFilter ff_vsrc_gradients;
libavfilter/version.h+2−2
@@ -30,8 +30,8 @@ · 29 unmodified lines
3030 #include "libavutil/version.h"
3131
3232 #define LIBAVFILTER_VERSION_MAJOR 8
33−#define LIBAVFILTER_VERSION_MINOR 16
34−#define LIBAVFILTER_VERSION_MICRO 102
33+#define LIBAVFILTER_VERSION_MINOR 17
34+#define LIBAVFILTER_VERSION_MICRO 100
3535
3636
3737 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
libavfilter/vsrc_testsrc.c+92−0
@@ -70,6 +70,9 @@ typedef struct TestSourceContext { · 69 unmodified lines
7070 /* only used by testsrc2 */
7171 int alpha;
7272
73+ /* only used by colorspectrum */
74+ int type;
75+
7376 /* only used by color */
7477 FFDrawContext draw;
7578 FFDrawColor color;
@@ -1791,3 +1794,92 @@ const AVFilter ff_vsrc_allrgb = { · 1715 unmodified lines
17911794 };
17921795
17931796 #endif /* CONFIG_ALLRGB_FILTER */
1797+
1798+#if CONFIG_COLORSPECTRUM_FILTER
1799+
1800+static const AVOption colorspectrum_options[] = {
1801+ COMMON_OPTIONS
1802+ { "type", "set the color spectrum type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "type" },
1803+ { "black","fade to black", 0, AV_OPT_TYPE_CONST,{.i64=0},0, 0, FLAGS, "type" },
1804+ { "white","fade to white", 0, AV_OPT_TYPE_CONST,{.i64=1},0, 0, FLAGS, "type" },
1805+ { "all", "white to black", 0, AV_OPT_TYPE_CONST,{.i64=2},0, 0, FLAGS, "type" },
1806+ { NULL }
1807+};
1808+
1809+AVFILTER_DEFINE_CLASS(colorspectrum);
1810+
1811+static inline float mix(float a, float b, float mix)
1812+{
1813+ return a * mix + b * (1.f - mix);
1814+}
1815+
1816+static void hsb2rgb(const float *c, float *rgb)
1817+{
1818+ rgb[0] = av_clipf(fabsf(fmodf(c[0] * 6.f + 0.f, 6.f) - 3.f) - 1.f, 0.f, 1.f);
1819+ rgb[1] = av_clipf(fabsf(fmodf(c[0] * 6.f + 4.f, 6.f) - 3.f) - 1.f, 0.f, 1.f);
1820+ rgb[2] = av_clipf(fabsf(fmodf(c[0] * 6.f + 2.f, 6.f) - 3.f) - 1.f, 0.f, 1.f);
1821+ rgb[0] = mix(c[3], (rgb[0] * rgb[0] * (3.f - 2.f * rgb[0])), c[1]) * c[2];
1822+ rgb[1] = mix(c[3], (rgb[1] * rgb[1] * (3.f - 2.f * rgb[1])), c[1]) * c[2];
1823+ rgb[2] = mix(c[3], (rgb[2] * rgb[2] * (3.f - 2.f * rgb[2])), c[1]) * c[2];
1824+}
1825+
1826+static void colorspectrum_fill_picture(AVFilterContext *ctx, AVFrame *frame)
1827+{
1828+ TestSourceContext *test = ctx->priv;
1829+ const float w = frame->width - 1.f;
1830+ const float h = frame->height - 1.f;
1831+ float c[4];
1832+
1833+ for (int y = 0; y < frame->height; y++) {
1834+ float *r = (float *)(frame->data[2] + y * frame->linesize[2]);
1835+ float *g = (float *)(frame->data[0] + y * frame->linesize[0]);
1836+ float *b = (float *)(frame->data[1] + y * frame->linesize[1]);
1837+ const float yh = y / h;
1838+
1839+ c[1] = test->type == 2 ? yh > 0.5f ? 2.f * (yh - 0.5f) : 1.f - 2.f * yh : test->type == 1 ? 1.f - yh : yh;
1840+ c[2] = 1.f;
1841+ c[3] = test->type == 1 ? 1.f : test->type == 2 ? (yh > 0.5f ? 0.f : 1.f): 0.f;
1842+ for (int x = 0; x < frame->width; x++) {
1843+ float rgb[3];
1844+
1845+ c[0] = x / w;
1846+ hsb2rgb(c, rgb);
1847+
1848+ r[x] = rgb[0];
1849+ g[x] = rgb[1];
1850+ b[x] = rgb[2];
1851+ }
1852+ }
1853+}
1854+
1855+static av_cold int colorspectrum_init(AVFilterContext *ctx)
1856+{
1857+ TestSourceContext *test = ctx->priv;
1858+
1859+ test->draw_once = 1;
1860+ test->fill_picture_fn = colorspectrum_fill_picture;
1861+ return init(ctx);
1862+}
1863+
1864+static const AVFilterPad avfilter_vsrc_colorspectrum_outputs[] = {
1865+ {
1866+ .name = "default",
1867+ .type = AVMEDIA_TYPE_VIDEO,
1868+ .config_props = config_props,
1869+ },
1870+};
1871+
1872+const AVFilter ff_vsrc_colorspectrum = {
1873+ .name = "colorspectrum",
1874+ .description = NULL_IF_CONFIG_SMALL("Generate colors spectrum."),
1875+ .priv_size = sizeof(TestSourceContext),
1876+ .priv_class = &colorspectrum_class,
1877+ .init = colorspectrum_init,
1878+ .uninit = uninit,
1879+ .activate = activate,
1880+ .inputs = NULL,
1881+ FILTER_OUTPUTS(avfilter_vsrc_colorspectrum_outputs),
1882+ FILTER_SINGLE_PIXFMT(AV_PIX_FMT_GBRPF32),
1883+};
1884+
1885+#endif /* CONFIG_COLORSPECTRUM_FILTER */

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