Add execute mechanism to receiver nodes.
[paraslash.git] / amp_filter.c
1 /*
2  * Copyright (C) 2009-2012 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file amp_filter.c Paraslash's amplify filter. */
8
9 #include <regex.h>
10
11 #include "para.h"
12 #include "amp_filter.cmdline.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "buffer_tree.h"
17 #include "filter.h"
18 #include "string.h"
19 #include "error.h"
20
21 extern char *stat_item_values[NUM_STAT_ITEMS];
22
23 /** Data specific to the amplify filter. */
24 struct private_amp_data {
25         /** Points to the configuration data for this instance of this filter. */
26         /** Amplification factor. */
27         unsigned amp;
28 };
29
30 static void amp_close(struct filter_node *fn)
31 {
32         free(fn->private_data);
33 }
34
35 static int amp_parse_config(int argc, char **argv, void **config)
36 {
37         struct amp_filter_args_info *conf = para_calloc(sizeof(*conf));
38         int ret = -E_AMP_SYNTAX;
39
40         amp_filter_cmdline_parser(argc, argv, conf);
41         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
42         if (conf->amp_arg < 0)
43                 goto err;
44         *config = conf;
45         return 1;
46 err:
47         free(conf);
48         return ret;
49 }
50
51 static void amp_open(struct filter_node *fn)
52 {
53         struct private_amp_data *pad = para_calloc(sizeof(*pad));
54         struct amp_filter_args_info *conf = fn->conf;
55
56         fn->private_data = pad;
57         fn->min_iqs = 2;
58         if (!conf->amp_given && stat_item_values[SI_AMPLIFICATION])
59                 sscanf(stat_item_values[SI_AMPLIFICATION], "%u", &pad->amp);
60         else
61                 pad->amp = conf->amp_arg;
62         PARA_NOTICE_LOG("amplification: %u (scaling factor: %1.2f)\n",
63                 pad->amp, pad->amp / 64.0 + 1.0);
64 }
65
66 static void amp_post_select(__a_unused struct sched *s, struct task *t)
67 {
68         struct filter_node *fn = container_of(t, struct filter_node, task);
69         struct private_amp_data *pad = fn->private_data;
70         struct btr_node *btrn = fn->btrn;
71         int ret, factor = 64 + pad->amp;
72         size_t i, in_bytes, len;
73         int16_t *in, *out;
74         bool inplace = btr_inplace_ok(btrn);
75
76         if (pad->amp == 0) { /* no amplification */
77                 t->error = -E_AMP_ZERO_AMP;
78                 btr_splice_out_node(btrn);
79                 return;
80         }
81 next_buffer:
82         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
83         if (ret < 0)
84                 goto err;
85         if (ret == 0)
86                 return;
87         btr_merge(btrn, fn->min_iqs);
88         in_bytes = btr_next_buffer(btrn, (char **)&in);
89         len = in_bytes / 2;
90         if (len == 0) { /* eof and in_bytes == 1 */
91                 ret = -E_AMP_EOF;
92                 goto err;
93         }
94
95         if (inplace)
96                 out = in;
97         else
98                 out = para_malloc(len * 2);
99
100         for (i = 0; i < len; i++) {
101                 int x = (in[i] * factor) >> 6;
102
103                 out[i] = x;
104                 if (out[i] != x) /* overflow, clip */
105                         out[i] = (x >= 0)? 32767 : -32768;
106         }
107
108         if (inplace)
109                 btr_pushdown_one(btrn);
110         else {
111                 btr_consume(btrn, len * 2);
112                 btr_add_output((char *)out, len * 2, btrn);
113         }
114         t->error = 0;
115         goto next_buffer;
116 err:
117         assert(ret < 0);
118         t->error = ret;
119         btr_remove_node(&fn->btrn);
120 }
121
122 static void amp_free_config(void *conf)
123 {
124         amp_filter_cmdline_parser_free(conf);
125 }
126
127 /**
128  * The init function of the amplify filter.
129  *
130  * \param f Pointer to the struct to initialize.
131  */
132 void amp_filter_init(struct filter *f)
133 {
134         struct amp_filter_args_info dummy;
135
136         amp_filter_cmdline_parser_init(&dummy);
137         f->open = amp_open;
138         f->close = amp_close;
139         f->pre_select = generic_filter_pre_select;
140         f->post_select = amp_post_select;
141         f->parse_config = amp_parse_config;
142         f->free_config = amp_free_config;
143         f->help = (struct ggo_help) {
144                 .short_help = amp_filter_args_info_help,
145                 .detailed_help = amp_filter_args_info_detailed_help
146         };
147 }