[FFmpeg-devel] [PATCH] avfilter/showcqt: add fontcolor option

Muhammad Faiz mfcc64 at gmail.com
Mon Aug 18 01:54:55 CEST 2014


This fontcolor option uses arithmetic expression, not color value,
so color names aren't available.
Thank's
-------------- next part --------------

---
 doc/filters.texi          | 20 +++++++++++
 libavfilter/avf_showcqt.c | 84 ++++++++++++++++++++++++++++++++++-------------
 2 files changed, 81 insertions(+), 23 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 0ca1d6f..958b48e 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -10610,6 +10610,26 @@ Default value is @code{3.0}.
 @item fontfile
 Specify font file for use with freetype. If not specified, use embedded font.
 
+ at item fontcolor
+Specify font color expression. This is arithmetic expression that should return
+integer value 0xRRGGBB. The expression can contain variables:
+ at table @option
+ at item frequency, freq, f
+the frequency where transform is evaluated
+ at item timeclamp, tc
+value of timeclamp option
+ at end table
+and functions:
+ at table @option
+ at item midi(f)
+midi number of frequency f
+ at item r(x), g(x), b(x)
+red, green, and blue value of intensity x
+ at end table
+Default value is @code{st(0, (midi(f)-59.5)/12);
+st(1, if(between(ld(0),0,1), 0.5-0.5*cos(2*PI*ld(0)), 0));
+r(1-ld(1)) + b(ld(1)}
+
 @item fullhd
 If set to 1 (the default), the video size is 1920x1080 (full HD),
 if set to 0, the video size is 960x540. Use this option to make CPU usage lower.
diff --git a/libavfilter/avf_showcqt.c b/libavfilter/avf_showcqt.c
index 012362b..d349c3d 100644
--- a/libavfilter/avf_showcqt.c
+++ b/libavfilter/avf_showcqt.c
@@ -54,6 +54,9 @@
 #define TLENGTH_DEFAULT "384/f*tc/(384/f+tc)"
 #define VOLUME_MIN 1e-10
 #define VOLUME_MAX 100.0
+#define FONTCOLOR_DEFAULT "st(0, (midi(f)-59.5)/12);" \
+    "st(1, if(between(ld(0),0,1), 0.5-0.5*cos(2*PI*ld(0)), 0));" \
+    "r(1-ld(1)) + b(ld(1))"
 
 typedef struct {
     FFTSample value;
@@ -73,7 +76,7 @@ typedef struct {
     uint8_t *font_alpha;
     char *fontfile;     /* using freetype */
     int coeffs_len[VIDEO_WIDTH];
-    uint8_t font_color[VIDEO_WIDTH];
+    uint8_t fontcolor_value[VIDEO_WIDTH*3];  /* result of fontcolor option */
     int64_t frame_count;
     int spectogram_count;
     int spectogram_index;
@@ -82,6 +85,7 @@ typedef struct {
     int remaining_fill;
     char *tlength;
     char *volume;
+    char *fontcolor;
     double timeclamp;   /* lower timeclamp, time-accurate, higher timeclamp, freq-accurate (at low freq)*/
     float coeffclamp;   /* lower coeffclamp, more precise, higher coeffclamp, faster */
     int fullhd;         /* if true, output video is at full HD resolution, otherwise it will be halved */
@@ -103,6 +107,7 @@ static const AVOption showcqt_options[] = {
     { "fps", "set video fps", OFFSET(fps), AV_OPT_TYPE_INT, { .i64 = 25 }, 10, 100, FLAGS },
     { "count", "set number of transform per frame", OFFSET(count), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, 30, FLAGS },
     { "fontfile", "set font file", OFFSET(fontfile), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "fontcolor", "set font color", OFFSET(fontcolor), AV_OPT_TYPE_STRING, { .str = FONTCOLOR_DEFAULT }, CHAR_MIN, CHAR_MAX, FLAGS },
     { NULL }
 };
 
@@ -275,6 +280,29 @@ static double c_weighting(void *p, double f)
     return ret;
 }
 
+static double midi(void *p, double f)
+{
+    return log2(f/440.0) * 12.0 + 69.0;
+}
+
+static double r_func(void *p, double x)
+{
+    x = FFMAX(0.0, FFMIN(1.0, x));
+    return (int)(x*255.0+0.5) << 16;
+}
+
+static double g_func(void *p, double x)
+{
+    x = FFMAX(0.0, FFMIN(1.0, x));
+    return (int)(x*255.0+0.5) << 8;
+}
+
+static double b_func(void *p, double x)
+{
+    x = FFMAX(0.0, FFMIN(1.0, x));
+    return (int)(x*255.0+0.5);
+}
+
 static inline int qsort_sparsecoeff(const SparseCoeff *a, const SparseCoeff *b)
 {
     if (fabsf(a->value) >= fabsf(b->value))
@@ -288,10 +316,13 @@ static int config_output(AVFilterLink *outlink)
     AVFilterContext *ctx = outlink->src;
     AVFilterLink *inlink = ctx->inputs[0];
     ShowCQTContext *s = ctx->priv;
-    AVExpr *tlength_expr, *volume_expr;
+    AVExpr *tlength_expr, *volume_expr, *fontcolor_expr;
+    uint8_t *fontcolor_value = s->fontcolor_value;
     static const char * const expr_vars[] = { "timeclamp", "tc", "frequency", "freq", "f", NULL };
     static const char * const expr_func_names[] = { "a_weighting", "b_weighting", "c_weighting", NULL };
+    static const char * const expr_fontcolor_func_names[] = { "midi", "r", "g", "b", NULL };
     static double (* const expr_funcs[])(void *, double) = { a_weighting, b_weighting, c_weighting, NULL };
+    static double (* const expr_fontcolor_funcs[])(void *, double) = { midi, r_func, g_func, b_func, NULL };
     int fft_len, k, x, y, ret;
     int num_coeffs = 0;
     int rate = inlink->sample_rate;
@@ -319,17 +350,6 @@ static int config_output(AVFilterLink *outlink)
     if (!s->fft_data || !s->coeff_sort || !s->fft_result_left || !s->fft_result_right || !s->fft_context)
         return AVERROR(ENOMEM);
 
-    /* initializing font */
-    for (x = 0; x < video_width; x++) {
-        if (x >= (12*3+8)*8*video_scale && x < (12*4+8)*8*video_scale) {
-            float fx = (x-(12*3+8)*8*video_scale) * (2.0f/(192.0f*video_scale));
-            float sv = sinf(M_PI*fx);
-            s->font_color[x] = sv*sv*255.0f + 0.5f;
-        } else {
-            s->font_color[x] = 0;
-        }
-    }
-
 #if CONFIG_LIBFREETYPE
     load_freetype_font(ctx);
 #else
@@ -349,6 +369,13 @@ static int config_output(AVFilterLink *outlink)
         av_expr_free(tlength_expr);
         return ret;
     }
+    ret = av_expr_parse(&fontcolor_expr, s->fontcolor, expr_vars, expr_fontcolor_func_names,
+                        expr_fontcolor_funcs, NULL, NULL, 0, ctx);
+    if (ret < 0) {
+        av_expr_free(volume_expr);
+        av_expr_free(tlength_expr);
+        return ret;
+    }
     for (k = 0; k < VIDEO_WIDTH; k++) {
         int hlen = fft_len >> 1;
         float total = 0;
@@ -390,6 +417,14 @@ static int config_output(AVFilterLink *outlink)
             volume = VOLUME_MAX;
         }
 
+        if (s->fullhd || !(k & 1)) {
+            int fontcolor = av_expr_eval(fontcolor_expr, expr_vars_val, NULL);
+            fontcolor_value[0] = (fontcolor >> 16) & 0xFF;
+            fontcolor_value[1] = (fontcolor >> 8) & 0xFF;
+            fontcolor_value[2] = fontcolor & 0xFF;
+            fontcolor_value += 3;
+        }
+
         tlen = tlength * rate;
         s->fft_data[0].re = 0;
         s->fft_data[0].im = 0;
@@ -445,6 +480,7 @@ static int config_output(AVFilterLink *outlink)
                 num_coeffs += s->coeffs_len[k];
                 s->coeffs[k] = av_malloc_array(s->coeffs_len[k], sizeof(*s->coeffs[k]));
                 if (!s->coeffs[k]) {
+                    av_expr_free(fontcolor_expr);
                     av_expr_free(volume_expr);
                     av_expr_free(tlength_expr);
                     return AVERROR(ENOMEM);
@@ -455,6 +491,7 @@ static int config_output(AVFilterLink *outlink)
             }
         }
     }
+    av_expr_free(fontcolor_expr);
     av_expr_free(volume_expr);
     av_expr_free(tlength_expr);
     end_time = av_gettime_relative();
@@ -597,12 +634,13 @@ static int plot_cqt(AVFilterLink *inlink)
             for (y = 0; y < font_height; y++) {
                 uint8_t *lineptr = data + (spectogram_height + y) * linesize;
                 uint8_t *spectogram_src = s->spectogram + s->spectogram_index * linesize;
+                uint8_t *fontcolor_value = s->fontcolor_value;
                 for (x = 0; x < video_width; x++) {
                     uint8_t alpha = s->font_alpha[y*video_width+x];
-                    uint8_t color = s->font_color[x];
-                    lineptr[3*x] = (spectogram_src[3*x] * (255-alpha) + (255-color) * alpha + 255) >> 8;
-                    lineptr[3*x+1] = (spectogram_src[3*x+1] * (255-alpha) + 255) >> 8;
-                    lineptr[3*x+2] = (spectogram_src[3*x+2] * (255-alpha) + color * alpha + 255) >> 8;
+                    lineptr[3*x] = (spectogram_src[3*x] * (255-alpha) + fontcolor_value[0] * alpha + 255) >> 8;
+                    lineptr[3*x+1] = (spectogram_src[3*x+1] * (255-alpha) + fontcolor_value[1] * alpha + 255) >> 8;
+                    lineptr[3*x+2] = (spectogram_src[3*x+2] * (255-alpha) + fontcolor_value[2] * alpha + 255) >> 8;
+                    fontcolor_value += 3;
                 }
             }
         } else {
@@ -622,16 +660,16 @@ static int plot_cqt(AVFilterLink *inlink)
                         int mask;
                         for (mask = 0x80; mask; mask >>= 1) {
                             if (mask & avpriv_vga16_font[str[u] * 16 + v]) {
-                                p[0] = 255 - s->font_color[ux];
-                                p[1] = 0;
-                                p[2] = s->font_color[ux];
+                                p[0] = s->fontcolor_value[3*ux];
+                                p[1] = s->fontcolor_value[3*ux+1];
+                                p[2] = s->fontcolor_value[3*ux+2];
                                 if (video_scale == 2) {
                                     p[linesize] = p[0];
                                     p[linesize+1] = p[1];
                                     p[linesize+2] = p[2];
-                                    p[3] = p[linesize+3] = 255 - s->font_color[ux+1];
-                                    p[4] = p[linesize+4] = 0;
-                                    p[5] = p[linesize+5] = s->font_color[ux+1];
+                                    p[3] = p[linesize+3] = s->fontcolor_value[3*ux+3];
+                                    p[4] = p[linesize+4] = s->fontcolor_value[3*ux+4];
+                                    p[5] = p[linesize+5] = s->fontcolor_value[3*ux+5];
                                 }
                             }
                             p  += 3 * video_scale;
-- 
1.8.3.1


More information about the ffmpeg-devel mailing list