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