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