Browse code

avfilter/firequalizer: add dumpfile and dumpscale option

Signed-off-by: Muhammad Faiz <mfcc64@gmail.com>

Muhammad Faiz authored on 2016/10/18 03:13:19
Showing 2 changed files
... ...
@@ -2576,6 +2576,13 @@ logarithmic (in octave scale where 20 Hz is 0) frequency, linear gain
2576 2576
 @item loglog
2577 2577
 logarithmic frequency, logarithmic gain
2578 2578
 @end table
2579
+
2580
+@item dumpfile
2581
+Set file for dumping, suitable for gnuplot.
2582
+
2583
+@item dumpscale
2584
+Set scale for dumpfile. Acceptable values are same with scale option.
2585
+Default is linlog.
2579 2586
 @end table
2580 2587
 
2581 2588
 @subsection Examples
... ...
@@ -65,6 +65,7 @@ typedef struct {
65 65
 typedef struct {
66 66
     const AVClass *class;
67 67
 
68
+    RDFTContext   *analysis_rdft;
68 69
     RDFTContext   *analysis_irdft;
69 70
     RDFTContext   *rdft;
70 71
     RDFTContext   *irdft;
... ...
@@ -72,6 +73,7 @@ typedef struct {
72 72
     int           rdft_len;
73 73
 
74 74
     float         *analysis_buf;
75
+    float         *dump_buf;
75 76
     float         *kernel_tmp_buf;
76 77
     float         *kernel_buf;
77 78
     float         *conv_buf;
... ...
@@ -93,6 +95,8 @@ typedef struct {
93 93
     int           multi;
94 94
     int           zero_phase;
95 95
     int           scale;
96
+    char          *dumpfile;
97
+    int           dumpscale;
96 98
 
97 99
     int           nb_gain_entry;
98 100
     int           gain_entry_err;
... ...
@@ -126,6 +130,8 @@ static const AVOption firequalizer_options[] = {
126 126
         { "linlog", "linear-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLOG }, 0, 0, FLAGS, "scale" },
127 127
         { "loglin", "logarithmic-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLIN }, 0, 0, FLAGS, "scale" },
128 128
         { "loglog", "logarithmic-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLOG }, 0, 0, FLAGS, "scale" },
129
+    { "dumpfile", "set dump file", OFFSET(dumpfile), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
130
+    { "dumpscale", "set dump scale", OFFSET(dumpscale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, "scale" },
129 131
     { NULL }
130 132
 };
131 133
 
... ...
@@ -133,12 +139,14 @@ AVFILTER_DEFINE_CLASS(firequalizer);
133 133
 
134 134
 static void common_uninit(FIREqualizerContext *s)
135 135
 {
136
+    av_rdft_end(s->analysis_rdft);
136 137
     av_rdft_end(s->analysis_irdft);
137 138
     av_rdft_end(s->rdft);
138 139
     av_rdft_end(s->irdft);
139
-    s->analysis_irdft = s->rdft = s->irdft = NULL;
140
+    s->analysis_rdft = s->analysis_irdft = s->rdft = s->irdft = NULL;
140 141
 
141 142
     av_freep(&s->analysis_buf);
143
+    av_freep(&s->dump_buf);
142 144
     av_freep(&s->kernel_tmp_buf);
143 145
     av_freep(&s->kernel_buf);
144 146
     av_freep(&s->conv_buf);
... ...
@@ -223,6 +231,53 @@ static void fast_convolute(FIREqualizerContext *s, const float *kernel_buf, floa
223 223
     }
224 224
 }
225 225
 
226
+static void dump_fir(AVFilterContext *ctx, FILE *fp, int ch)
227
+{
228
+    FIREqualizerContext *s = ctx->priv;
229
+    int rate = ctx->inputs[0]->sample_rate;
230
+    int xlog = s->dumpscale == SCALE_LOGLIN || s->dumpscale == SCALE_LOGLOG;
231
+    int ylog = s->dumpscale == SCALE_LINLOG || s->dumpscale == SCALE_LOGLOG;
232
+    int x;
233
+    int center = s->fir_len / 2;
234
+    double delay = s->zero_phase ? 0.0 : (double) center / rate;
235
+    double vx, ya, yb;
236
+
237
+    s->analysis_buf[0] *= s->rdft_len/2;
238
+    for (x = 1; x <= center; x++) {
239
+        s->analysis_buf[x] *= s->rdft_len/2;
240
+        s->analysis_buf[s->analysis_rdft_len - x] *= s->rdft_len/2;
241
+    }
242
+
243
+    if (ch)
244
+        fprintf(fp, "\n\n");
245
+
246
+    fprintf(fp, "# time[%d] (time amplitude)\n", ch);
247
+
248
+    for (x = center; x > 0; x--)
249
+        fprintf(fp, "%15.10f %15.10f\n", delay - (double) x / rate, (double) s->analysis_buf[s->analysis_rdft_len - x]);
250
+
251
+    for (x = 0; x <= center; x++)
252
+        fprintf(fp, "%15.10f %15.10f\n", delay + (double)x / rate , (double) s->analysis_buf[x]);
253
+
254
+    av_rdft_calc(s->analysis_rdft, s->analysis_buf);
255
+
256
+    fprintf(fp, "\n\n# freq[%d] (frequency desired_gain actual_gain)\n", ch);
257
+
258
+    for (x = 0; x <= s->analysis_rdft_len/2; x++) {
259
+        int i = (x == s->analysis_rdft_len/2) ? 1 : 2 * x;
260
+        vx = (double)x * rate / s->analysis_rdft_len;
261
+        if (xlog)
262
+            vx = log2(0.05*vx);
263
+        ya = s->dump_buf[i];
264
+        yb = s->analysis_buf[i];
265
+        if (ylog) {
266
+            ya = 20.0 * log10(fabs(ya));
267
+            yb = 20.0 * log10(fabs(yb));
268
+        }
269
+        fprintf(fp, "%17.10f %17.10f %17.10f\n", vx, ya, yb);
270
+    }
271
+}
272
+
226 273
 static double entry_func(void *p, double freq, double gain)
227 274
 {
228 275
     AVFilterContext *ctx = p;
... ...
@@ -332,6 +387,7 @@ static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *g
332 332
     int ret, k, center, ch;
333 333
     int xlog = s->scale == SCALE_LOGLIN || s->scale == SCALE_LOGLOG;
334 334
     int ylog = s->scale == SCALE_LINLOG || s->scale == SCALE_LOGLOG;
335
+    FILE *dump_fp = NULL;
335 336
 
336 337
     s->nb_gain_entry = 0;
337 338
     s->gain_entry_err = 0;
... ...
@@ -352,10 +408,14 @@ static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *g
352 352
     if (ret < 0)
353 353
         return ret;
354 354
 
355
+    if (s->dumpfile && (!s->dump_buf || !s->analysis_rdft || !(dump_fp = fopen(s->dumpfile, "w"))))
356
+        av_log(ctx, AV_LOG_WARNING, "dumping failed.\n");
357
+
355 358
     vars[VAR_CHS] = inlink->channels;
356 359
     vars[VAR_CHLAYOUT] = inlink->channel_layout;
357 360
     vars[VAR_SR] = inlink->sample_rate;
358 361
     for (ch = 0; ch < inlink->channels; ch++) {
362
+        float *rdft_buf = s->kernel_tmp_buf + ch * s->rdft_len;
359 363
         double result;
360 364
         vars[VAR_CH] = ch;
361 365
         vars[VAR_CHID] = av_channel_layout_extract_channel(inlink->channel_layout, ch);
... ...
@@ -380,6 +440,9 @@ static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *g
380 380
             s->analysis_buf[2*k+1] = 0.0;
381 381
         }
382 382
 
383
+        if (s->dump_buf)
384
+            memcpy(s->dump_buf, s->analysis_buf, s->analysis_rdft_len * sizeof(*s->analysis_buf));
385
+
383 386
         av_rdft_calc(s->analysis_irdft, s->analysis_buf);
384 387
         center = s->fir_len / 2;
385 388
 
... ...
@@ -421,35 +484,36 @@ static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *g
421 421
                 av_assert0(0);
422 422
             }
423 423
             s->analysis_buf[k] *= (2.0/s->analysis_rdft_len) * (2.0/s->rdft_len) * win;
424
+            if (k)
425
+                s->analysis_buf[s->analysis_rdft_len - k] = s->analysis_buf[k];
424 426
         }
425 427
 
426
-        for (k = 0; k < center - k; k++) {
427
-            float tmp = s->analysis_buf[k];
428
-            s->analysis_buf[k] = s->analysis_buf[center - k];
429
-            s->analysis_buf[center - k] = tmp;
430
-        }
431
-
432
-        for (k = 1; k <= center; k++)
433
-            s->analysis_buf[center + k] = s->analysis_buf[center - k];
434
-
435
-        memset(s->analysis_buf + s->fir_len, 0, (s->rdft_len - s->fir_len) * sizeof(*s->analysis_buf));
436
-        av_rdft_calc(s->rdft, s->analysis_buf);
428
+        memset(s->analysis_buf + center + 1, 0, (s->analysis_rdft_len - s->fir_len) * sizeof(*s->analysis_buf));
429
+        memcpy(rdft_buf, s->analysis_buf + s->analysis_rdft_len - center, center * sizeof(*s->analysis_buf));
430
+        memcpy(rdft_buf + center, s->analysis_buf, (s->rdft_len - center) * sizeof(*s->analysis_buf));
431
+        av_rdft_calc(s->rdft, rdft_buf);
437 432
 
438 433
         for (k = 0; k < s->rdft_len; k++) {
439
-            if (isnan(s->analysis_buf[k]) || isinf(s->analysis_buf[k])) {
434
+            if (isnan(rdft_buf[k]) || isinf(rdft_buf[k])) {
440 435
                 av_log(ctx, AV_LOG_ERROR, "filter kernel contains nan or infinity.\n");
441 436
                 av_expr_free(gain_expr);
437
+                if (dump_fp)
438
+                    fclose(dump_fp);
442 439
                 return AVERROR(EINVAL);
443 440
             }
444 441
         }
445 442
 
446
-        memcpy(s->kernel_tmp_buf + ch * s->rdft_len, s->analysis_buf, s->rdft_len * sizeof(*s->analysis_buf));
443
+        if (dump_fp)
444
+            dump_fir(ctx, dump_fp, ch);
445
+
447 446
         if (!s->multi)
448 447
             break;
449 448
     }
450 449
 
451 450
     memcpy(s->kernel_buf, s->kernel_tmp_buf, (s->multi ? inlink->channels : 1) * s->rdft_len * sizeof(*s->kernel_buf));
452 451
     av_expr_free(gain_expr);
452
+    if (dump_fp)
453
+        fclose(dump_fp);
453 454
     return 0;
454 455
 }
455 456
 
... ...
@@ -499,6 +563,11 @@ static int config_input(AVFilterLink *inlink)
499 499
     if (!(s->analysis_irdft = av_rdft_init(rdft_bits, IDFT_C2R)))
500 500
         return AVERROR(ENOMEM);
501 501
 
502
+    if (s->dumpfile) {
503
+        s->analysis_rdft = av_rdft_init(rdft_bits, DFT_R2C);
504
+        s->dump_buf = av_malloc_array(s->analysis_rdft_len, sizeof(*s->dump_buf));
505
+    }
506
+
502 507
     s->analysis_buf = av_malloc_array(s->analysis_rdft_len, sizeof(*s->analysis_buf));
503 508
     s->kernel_tmp_buf = av_malloc_array(s->rdft_len * (s->multi ? inlink->channels : 1), sizeof(*s->kernel_tmp_buf));
504 509
     s->kernel_buf = av_malloc_array(s->rdft_len * (s->multi ? inlink->channels : 1), sizeof(*s->kernel_buf));