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