History

avfilter/af_amix: make weights option runtime configured

Paul B Mahol committed 6 years ago

Commit d8147c41 parent a124f4e2 files · +57 −20Browse files

Unified · Split

2 files
  1. doc/filters.texi +8 −0
  2. libavfilter/af_amix.c +49 −20
doc/filters.texi+8−0
@@ -1703,6 +1703,14 @@ Specify weight of each input audio stream as sequence. · 1702 unmodified lines
1703Each weight is separated by space. By default all inputs have same weight.1703Each weight is separated by space. By default all inputs have same weight.
1704@end table1704@end table
1705 1705
1706@subsection Commands
1707
1708This filter supports the following commands:
1709@table @option
1710@item weights
1711Syntax is same as option with same name.
1712@end table
1713
1706@section amultiply1714@section amultiply
1707 1715
1708Multiply first audio stream with second audio stream and store result1716Multiply first audio stream with second audio stream and store result
libavfilter/af_amix.c+49−20
@@ -181,6 +181,7 @@ typedef struct MixContext { · 180 unmodified lines
181#define OFFSET(x) offsetof(MixContext, x)181#define OFFSET(x) offsetof(MixContext, x)
182#define A AV_OPT_FLAG_AUDIO_PARAM182#define A AV_OPT_FLAG_AUDIO_PARAM
183#define F AV_OPT_FLAG_FILTERING_PARAM183#define F AV_OPT_FLAG_FILTERING_PARAM
184#define T AV_OPT_FLAG_RUNTIME_PARAM
184static const AVOption amix_options[] = {185static const AVOption amix_options[] = {
185 { "inputs", "Number of inputs.",186 { "inputs", "Number of inputs.",
186 OFFSET(nb_inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT16_MAX, A|F },187 OFFSET(nb_inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT16_MAX, A|F },
@@ -193,7 +194,7 @@ static const AVOption amix_options[] = { · 6 unmodified lines
193 "renormalization when an input stream ends.",194 "renormalization when an input stream ends.",
194 OFFSET(dropout_transition), AV_OPT_TYPE_FLOAT, { .dbl = 2.0 }, 0, INT_MAX, A|F },195 OFFSET(dropout_transition), AV_OPT_TYPE_FLOAT, { .dbl = 2.0 }, 0, INT_MAX, A|F },
195 { "weights", "Set weight for each input.",196 { "weights", "Set weight for each input.",
196 OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, A|F },197 OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, A|F|T },
197 { NULL }198 { NULL }
198};199};
199 200
@@ -504,12 +505,37 @@ static int activate(AVFilterContext *ctx) · 304 unmodified lines
504 return 0;505 return 0;
505}506}
506 507
507static av_cold int init(AVFilterContext *ctx)508static void parse_weights(AVFilterContext *ctx)
508{509{
509 MixContext *s = ctx->priv;510 MixContext *s = ctx->priv;
510 float last_weight = 1.f;511 float last_weight = 1.f;
511 int i, ret;
512 char *p;512 char *p;
513 int i;
514
515 s->weight_sum = 0.f;
516 p = s->weights_str;
517 for (i = 0; i < s->nb_inputs; i++) {
518 last_weight = av_strtod(p, &p);
519 s->weights[i] = last_weight;
520 s->weight_sum += FFABS(last_weight);
521 if (p && *p) {
522 p++;
523 } else {
524 i++;
525 break;
526 }
527 }
528
529 for (; i < s->nb_inputs; i++) {
530 s->weights[i] = last_weight;
531 s->weight_sum += FFABS(last_weight);
532 }
533}
534
535static av_cold int init(AVFilterContext *ctx)
536{
537 MixContext *s = ctx->priv;
538 int i, ret;
513 539
514 for (i = 0; i < s->nb_inputs; i++) {540 for (i = 0; i < s->nb_inputs; i++) {
515 AVFilterPad pad = { 0 };541 AVFilterPad pad = { 0 };
@@ -533,23 +559,7 @@ static av_cold int init(AVFilterContext *ctx) · 17 unmodified lines
533 if (!s->weights)559 if (!s->weights)
534 return AVERROR(ENOMEM);560 return AVERROR(ENOMEM);
535 561
536 p = s->weights_str;562 parse_weights(ctx);
537 for (i = 0; i < s->nb_inputs; i++) {
538 last_weight = av_strtod(p, &p);
539 s->weights[i] = last_weight;
540 s->weight_sum += FFABS(last_weight);
541 if (p && *p) {
542 p++;
543 } else {
544 i++;
545 break;
546 }
547 }
548
549 for (; i < s->nb_inputs; i++) {
550 s->weights[i] = last_weight;
551 s->weight_sum += FFABS(last_weight);
552 }
553 563
554 return 0;564 return 0;
555}565}
@@ -604,6 +614,24 @@ fail: · 48 unmodified lines
604 return ret;614 return ret;
605}615}
606 616
617static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
618 char *res, int res_len, int flags)
619{
620 MixContext *s = ctx->priv;
621 int ret;
622
623 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
624 if (ret < 0)
625 return ret;
626
627 parse_weights(ctx);
628 for (int i = 0; i < s->nb_inputs; i++)
629 s->scale_norm[i] = s->weight_sum / FFABS(s->weights[i]);
630 calculate_scales(s, 0);
631
632 return 0;
633}
634
607static const AVFilterPad avfilter_af_amix_outputs[] = {635static const AVFilterPad avfilter_af_amix_outputs[] = {
608 {636 {
609 .name = "default",637 .name = "default",
@@ -624,5 +652,6 @@ AVFilter ff_af_amix = { · 14 unmodified lines
624 .query_formats = query_formats,652 .query_formats = query_formats,
625 .inputs = NULL,653 .inputs = NULL,
626 .outputs = avfilter_af_amix_outputs,654 .outputs = avfilter_af_amix_outputs,
655 .process_command = process_command,
627 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,656 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
628};657};

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