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