amp filter fixes.
[paraslash.git] / amp_filter.c
1 /*
2 * Copyright (C) 2009 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 /** The size of the output data buffer. */
23 #define AMP_CHUNK_SIZE 40960
24
25 extern char *stat_item_values[NUM_STAT_ITEMS];
26
27 /** Data specific to the amplify filter. */
28 struct private_amp_data {
29 /** Points to the configuration data for this instance of this filter. */
30 struct amp_filter_args_info *conf;
31 /** Amplification factor. */
32 unsigned amp;
33 };
34
35 static ssize_t amp_convert(char *inbuf, size_t inbuf_len, struct filter_node *fn)
36 {
37 size_t i, length = PARA_MIN((inbuf_len / 2),
38 (fn->bufsize - fn->loaded) / 2);
39 struct private_amp_data *pad = fn->private_data;
40 int16_t *ip = (int16_t *)inbuf, *op = (int16_t *)(fn->buf + fn->loaded);
41 int factor = 64 + pad->amp;
42
43 if (!length)
44 return 0;
45
46 if (pad->amp == 0) {
47 memcpy(op, ip, length * 2);
48 goto out;
49 }
50 for (i = 0; i < length; i++) {
51 int x = (ip[i] * factor) >> 6;
52
53 op[i] = x;
54 if (op[i] != x)
55 op[i] = (x >= 32768)? 32767 : -32768;
56 }
57 out:
58 fn->loaded += length * 2;
59 return length * 2;
60 }
61
62 static void amp_close(struct filter_node *fn)
63 {
64 free(fn->private_data);
65 free(fn->buf);
66 }
67
68 static int amp_parse_config(int argc, char **argv, void **config)
69 {
70 struct amp_filter_args_info *amp_conf = para_calloc(sizeof(*amp_conf));
71 int ret = -E_AMP_SYNTAX;
72
73 if (amp_cmdline_parser(argc, argv, amp_conf))
74 goto err;
75 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
76 if (amp_conf->amp_arg < 0)
77 goto err;
78 *config = amp_conf;
79 return 1;
80 err:
81 free(amp_conf);
82 return ret;
83 }
84
85 static void amp_open(struct filter_node *fn)
86 {
87 struct private_amp_data *pad = para_calloc(sizeof(*pad));
88
89 pad->conf = fn->conf;
90 fn->private_data = pad;
91 fn->min_iqs = 2;
92 if (!pad->conf->amp_given && stat_item_values[SI_AMPLIFICATION])
93 sscanf(stat_item_values[SI_AMPLIFICATION], "%u", &pad->amp);
94 else
95 pad->amp = pad->conf->amp_arg;
96 fn->bufsize = AMP_CHUNK_SIZE;
97 fn->buf = para_malloc(fn->bufsize);
98 PARA_NOTICE_LOG("amplification: %u (scaling factor: %1.2f)\n",
99 pad->amp, pad->amp / 64.0 + 1.0);
100 }
101
102 static void amp_post_select(__a_unused struct sched *s, struct task *t)
103 {
104 struct filter_node *fn = container_of(t, struct filter_node, task);
105 struct private_amp_data *pad = fn->private_data;
106 struct btr_node *btrn = fn->btrn;
107 int ret, factor = 64 + pad->amp;
108 size_t i, in_bytes, len;
109 int16_t *in, *out;
110 bool inplace = btr_inplace_ok(btrn);
111
112 if (pad->amp == 0) { /* no amplification */
113 t->error = -E_AMP_ZERO_AMP;
114 btr_splice_out_node(btrn);
115 return;
116 }
117 next_buffer:
118 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
119 if (ret < 0)
120 goto err;
121 if (ret == 0)
122 return;
123 btr_merge(btrn, fn->min_iqs);
124 in_bytes = btr_next_buffer(btrn, (char **)&in);
125 len = in_bytes / 2;
126 if (len == 0) { /* eof and in_bytes == 1 */
127 ret = -E_AMP_EOF;
128 goto err;
129 }
130
131 if (inplace)
132 out = in;
133 else
134 out = para_malloc(len * 2);
135
136 for (i = 0; i < len; i++) {
137 int x = (in[i] * factor) >> 6;
138
139 out[i] = x;
140 if (out[i] != x) /* overflow, clip */
141 out[i] = (x >= 0)? 32767 : -32768;
142 }
143
144 if (inplace)
145 btr_pushdown_one(btrn);
146 else {
147 btr_consume(btrn, len * 2);
148 btr_add_output((char *)out, len * 2, btrn);
149 }
150 t->error = 0;
151 goto next_buffer;
152 err:
153 assert(ret < 0);
154 t->error = ret;
155 btr_remove_node(btrn);
156 }
157
158 static void amp_free_config(void *conf)
159 {
160 amp_cmdline_parser_free(conf);
161 }
162
163 /**
164 * The init function of the amplify filter.
165 *
166 * \param f Pointer to the struct to initialize.
167 */
168 void amp_filter_init(struct filter *f)
169 {
170 struct amp_filter_args_info dummy;
171
172 amp_cmdline_parser_init(&dummy);
173 f->open = amp_open;
174 f->close = amp_close;
175 f->convert = amp_convert;
176 f->pre_select = generic_filter_pre_select;
177 f->post_select = amp_post_select;
178 f->parse_config = amp_parse_config;
179 f->free_config = amp_free_config;
180 f->help = (struct ggo_help) {
181 .short_help = amp_filter_args_info_help,
182 .detailed_help = amp_filter_args_info_detailed_help
183 };
184 }