Add btr support for the compress filter.
[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         PARA_NOTICE_LOG("amplification: %u (scaling factor: %1.2f)\n",
79                 amp_conf->amp_arg, amp_conf->amp_arg / 64.0 + 1.0);
80         *config = amp_conf;
81         return 1;
82 err:
83         free(amp_conf);
84         return ret;
85 }
86
87 static void amp_open(struct filter_node *fn)
88 {
89         struct private_amp_data *pad = para_calloc(sizeof(*pad));
90
91         pad->conf = fn->conf;
92         fn->private_data = pad;
93         if (!pad->conf->amp_given && stat_item_values[SI_AMPLIFICATION])
94                 sscanf(stat_item_values[SI_AMPLIFICATION], "%u", &pad->amp);
95         else
96                 pad->amp = pad->conf->amp_arg;
97         fn->bufsize = AMP_CHUNK_SIZE;
98         fn->buf = para_malloc(fn->bufsize);
99 }
100
101 static void amp_post_select(__a_unused struct sched *s, struct task *t)
102 {
103         struct filter_node *fn = container_of(t, struct filter_node, task);
104         struct private_amp_data *pad = fn->private_data;
105         struct btr_node *btrn = fn->btrn;
106         int ret, factor = 64 + pad->amp;
107         char *in;
108         size_t in_len = btr_next_buffer(btrn, &in);
109         size_t i, length = in_len / 2;
110         int16_t *ip = (int16_t *)in, *op;
111         bool inplace = btr_inplace_ok(btrn);
112
113         ret = prepare_filter_node(fn);
114         if (ret < 0)
115                 goto err;
116         if (ret == 0)
117                 return;
118
119         if (inplace)
120                 op = ip;
121         else
122                 op = para_malloc(length * 2);
123         if (pad->amp == 0) {
124                 memcpy(op, ip, length * 2);
125                 goto success;
126         }
127         for (i = 0; i < length; i++) {
128                 int x = (ip[i] * factor) >> 6;
129
130                 op[i] = x;
131                 if (op[i] != x)
132                         op[i] = (x >= 32768)? 32767 : -32768;
133         }
134 success:
135         t->error = 0;
136         if (inplace)
137                 btr_pushdown_one(btrn);
138         else {
139                 btr_consume(btrn, length * 2);
140                 btr_add_output((char *)op, length * 2, btrn);
141         }
142         return;
143 err:
144         assert(ret < 0);
145         amp_close(fn);
146         t->error = ret;
147         btr_del_node(btrn);
148 }
149
150 /**
151  * The init function of the amplify filter.
152  *
153  * \param f Pointer to the struct to initialize.
154  */
155 void amp_filter_init(struct filter *f)
156 {
157         struct amp_filter_args_info dummy;
158
159         amp_cmdline_parser_init(&dummy);
160         f->open = amp_open;
161         f->close = amp_close;
162         f->convert = amp_convert;
163         f->pre_select = generic_filter_pre_select;
164         f->post_select = amp_post_select;
165         f->parse_config = amp_parse_config;
166         f->help = (struct ggo_help) {
167                 .short_help = amp_filter_args_info_help,
168                 .detailed_help = amp_filter_args_info_detailed_help
169         };
170 }