Rework para_accept().
[paraslash.git] / compress_filter.c
1 /*
2  * Copyright (C) 2005-2010 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file compress_filter.c Paraslash's dynamic audio range compressor. */
8
9 /*
10  * Uses ideas of AudioCompress, (C) 2002-2004  M. Hari Nezumi <magenta@trikuare.cx>
11  */
12
13 #include <regex.h>
14 #include <stdbool.h>
15
16 #include "para.h"
17 #include "compress_filter.cmdline.h"
18 #include "list.h"
19 #include "sched.h"
20 #include "ggo.h"
21 #include "buffer_tree.h"
22 #include "filter.h"
23 #include "string.h"
24 #include "error.h"
25
26 /** Data specific to the compress filter. */
27 struct private_compress_data {
28         /** The current multiplier. */
29         unsigned current_gain;
30         /** Points to the configuration data for this instance of the compress filter. */
31         struct compress_filter_args_info *conf;
32         /** Maximal admissible gain. */
33         unsigned max_gain;
34         /** Number of samples already seen. */
35         unsigned num_samples;
36         /** Absolute value of the maximal sample in the current block. */
37         int peak;
38 };
39
40 static void compress_close(struct filter_node *fn)
41 {
42         free(fn->private_data);
43 }
44
45 static void compress_post_select(__a_unused struct sched *s, struct task *t)
46 {
47         struct filter_node *fn = container_of(t, struct filter_node, task);
48         struct private_compress_data *pcd = fn->private_data;
49         struct btr_node *btrn = fn->btrn;
50         bool inplace = btr_inplace_ok(btrn);
51         int ret;
52         char *inbuf;
53         size_t length, i;
54         int16_t *ip, *op;
55         unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
56                 mask = (1 << pcd->conf->blocksize_arg) - 1;
57
58         //inplace = false;
59 next_buffer:
60         t->error = 0;
61         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
62         if (ret < 0)
63                 goto err;
64         if (ret == 0)
65                 return;
66         btr_merge(btrn, fn->min_iqs);
67         length = btr_next_buffer(btrn, &inbuf) & ~(size_t)1;
68         if (length == 0) { /* eof and 1 byte available */
69                 ret = -E_COMPRESS_EOF;
70                 goto err;
71         }
72         ip = (int16_t *)inbuf;
73         if (inplace)
74                 op = ip;
75         else
76                 op = para_malloc(length);
77         for (i = 0; i < length / 2; i++) {
78                 /* be careful in that heat, my dear */
79                 int sample = *ip++, adjusted_sample = (PARA_ABS(sample) *
80                         pcd->current_gain) >> gain_shift;
81                 if (adjusted_sample > 32767) { /* clip */
82                         PARA_NOTICE_LOG("clip: sample: %d, adjusted sample: %d\n",
83                                 sample, adjusted_sample);
84                         adjusted_sample = 32767;
85                         pcd->current_gain = (3 * pcd->current_gain +
86                                 (1 << pcd->conf->inertia_arg)) / 4;
87                         pcd->peak = 0;
88                 } else
89                         pcd->peak = PARA_MAX(pcd->peak, adjusted_sample);
90                 op[i] = sample >= 0? adjusted_sample : -adjusted_sample;
91                 if (++pcd->num_samples & mask)
92                         continue;
93 //              PARA_DEBUG_LOG("gain: %u, peak: %u\n", pcd->current_gain,
94 //                      pcd->peak);
95                 if (pcd->peak < pcd->conf->target_level_arg) {
96                         if (pcd->current_gain < pcd->max_gain)
97                                 pcd->current_gain++;
98                 } else
99                         pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
100                                 1U << pcd->conf->inertia_arg);
101                 pcd->peak = 0;
102         }
103         if (inplace)
104                 btr_pushdown_one(btrn);
105         else {
106                 btr_consume(btrn, length);
107                 btr_add_output((char *)op, length, btrn);
108         }
109         goto next_buffer;
110 err:
111         assert(ret < 0);
112         t->error = ret;
113         btr_remove_node(btrn);
114 }
115
116 /** TODO: Add sanity checks */
117 static int compress_parse_config(int argc, char **argv, void **config)
118 {
119         int ret;
120         struct compress_filter_args_info *compress_conf
121                 = para_calloc(sizeof(*compress_conf));
122
123         ret = -E_COMPRESS_SYNTAX;
124         if (compress_cmdline_parser(argc, argv, compress_conf))
125                 goto err;
126         *config = compress_conf;
127         return 1;
128 err:
129         free(compress_conf);
130         return  ret;
131 }
132
133 static void compress_open(struct filter_node *fn)
134 {
135         struct private_compress_data *pcd = para_calloc(
136                 sizeof(struct private_compress_data));
137         pcd->conf = fn->conf;
138         fn->private_data = pcd;
139         fn->min_iqs = 2; /* 16 bit audio */
140         pcd->current_gain = 1 << pcd->conf->inertia_arg;
141         pcd->max_gain = 1 << (pcd->conf->inertia_arg + pcd->conf->aggressiveness_arg);
142 }
143
144 static void compress_free_config(void *conf)
145 {
146         compress_cmdline_parser_free(conf);
147 }
148
149 /**
150  * The init function of the compress filter.
151  *
152  * \param f Pointer to the struct to initialize.
153  */
154 void compress_filter_init(struct filter *f)
155 {
156         struct compress_filter_args_info dummy;
157
158         compress_cmdline_parser_init(&dummy);
159         f->open = compress_open;
160         f->close = compress_close;
161         f->pre_select = generic_filter_pre_select;
162         f->post_select = compress_post_select;
163         f->parse_config = compress_parse_config;
164         f->free_config = compress_free_config;
165         f->help = (struct ggo_help) {
166                 .short_help = compress_filter_args_info_help,
167                 .detailed_help = compress_filter_args_info_detailed_help
168         };
169 }