f98fb413b2498311d25299fe9dfabfcad87af823
[paraslash.git] / compress_filter.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file compress_filter.c Paraslash's dynamic audio range compressor. */
4
5 /*
6 * Uses ideas of AudioCompress, (C) 2002-2004 M. Hari Nezumi <magenta@trikuare.cx>
7 */
8
9 #include <regex.h>
10 #include <lopsub.h>
11
12 #include "filter_cmd.lsg.h"
13 #include "para.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "buffer_tree.h"
17 #include "filter.h"
18 #include "string.h"
19 #include "error.h"
20
21 #define U32_OPTVAL(_opt, _lpr) (FILTER_CMD_OPT_UINT32_VAL(COMPRESS, _opt, _lpr))
22
23 /** Data specific to the compress filter. */
24 struct private_compress_data {
25 /** The current multiplier. */
26 unsigned current_gain;
27 /** Maximal admissible gain. */
28 unsigned max_gain;
29 /** Number of samples already seen. */
30 unsigned num_samples;
31 /** Absolute value of the maximal sample in the current block. */
32 int peak;
33 };
34
35 static void compress_close(struct filter_node *fn)
36 {
37 free(fn->private_data);
38 }
39
40 static int compress_post_select(__a_unused struct sched *s, void *context)
41 {
42 struct filter_node *fn = context;
43 struct private_compress_data *pcd = fn->private_data;
44 struct btr_node *btrn = fn->btrn;
45 bool inplace = btr_inplace_ok(btrn);
46 int ret;
47 char *inbuf;
48 size_t length, i;
49 int16_t *ip, *op;
50 uint32_t inertia = U32_OPTVAL(INERTIA, fn->lpr);
51 unsigned gain_shift = inertia + U32_OPTVAL(DAMP, fn->lpr),
52 mask = (1U << U32_OPTVAL(BLOCKSIZE, fn->lpr)) - 1U;
53 //inplace = false;
54 next_buffer:
55 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
56 if (ret < 0)
57 goto err;
58 if (ret == 0)
59 return 0;
60 btr_merge(btrn, fn->min_iqs);
61 length = btr_next_buffer(btrn, &inbuf) & ~(size_t)1;
62 if (length == 0) { /* eof and 1 byte available */
63 ret = -E_COMPRESS_EOF;
64 goto err;
65 }
66 ip = (int16_t *)inbuf;
67 if (inplace)
68 op = ip;
69 else
70 op = para_malloc(length);
71 for (i = 0; i < length / 2; i++) {
72 /* be careful in that heat, my dear */
73 int sample = *ip++;
74 bool neg = false;
75
76 if (sample < 0) {
77 sample = -sample;
78 neg = true;
79 }
80 sample *= pcd->current_gain;
81 sample >>= gain_shift;
82 if (sample > 32767) { /* clip */
83 PARA_WARNING_LOG("clip: %d\n", sample);
84 sample = 32767;
85 pcd->current_gain = (3 * pcd->current_gain +
86 (1 << inertia)) / 4;
87 pcd->peak = 0;
88 } else if (sample > pcd->peak)
89 pcd->peak = sample;
90 op[i] = neg? -sample : 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 < U32_OPTVAL(TARGET_LEVEL, fn->lpr)) {
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 << inertia);
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 btr_remove_node(&fn->btrn);
113 return ret;
114 }
115
116 static void compress_open(struct filter_node *fn)
117 {
118 struct private_compress_data *pcd = para_calloc(sizeof(*pcd));
119 uint32_t inertia = U32_OPTVAL(INERTIA, fn->lpr);
120 uint32_t aggressiveness = U32_OPTVAL(AGGRESSIVENESS, fn->lpr);
121
122 fn->private_data = pcd;
123 fn->min_iqs = 2; /* 16 bit audio */
124 pcd->current_gain = 1U << inertia;
125 pcd->max_gain = 1U << (inertia + aggressiveness);
126 }
127
128 static void *compress_setup(const struct lls_parse_result *lpr)
129 {
130 uint32_t val;
131
132 val = U32_OPTVAL(BLOCKSIZE, lpr);
133 if (val == 0 || val > 31) {
134 PARA_EMERG_LOG("blocksize (%u) out of range\n", val);
135 exit(EXIT_FAILURE);
136 }
137 val = U32_OPTVAL(AGGRESSIVENESS, lpr);
138 if (val == 0 || val > 15) {
139 PARA_EMERG_LOG("aggressiveness (%u) out of range\n", val);
140 exit(EXIT_FAILURE);
141 }
142 val = U32_OPTVAL(INERTIA, lpr);
143 if (val == 0 || val > 15) {
144 PARA_EMERG_LOG("inertia (%u) out of range\n", val);
145 exit(EXIT_FAILURE);
146 }
147 val = U32_OPTVAL(AGGRESSIVENESS, lpr) + U32_OPTVAL(INERTIA, lpr);
148 if (val > 16) {
149 PARA_EMERG_LOG("inertia + aggressiveness (%u) > 16\n", val);
150 exit(EXIT_FAILURE);
151 }
152 val = U32_OPTVAL(TARGET_LEVEL, lpr);
153 if (val > 32767) {
154 PARA_EMERG_LOG("target-level (%u) out of range\n", val);
155 exit(EXIT_FAILURE);
156 }
157 val = U32_OPTVAL(DAMP, lpr);
158 if (val > 16) {
159 PARA_EMERG_LOG("damp (%u) out of range\n", val);
160 exit(EXIT_FAILURE);
161 }
162 return NULL; /* no need for a config structure */
163 }
164
165 const struct filter lsg_filter_cmd_com_compress_user_data = {
166 .setup = compress_setup,
167 .open = compress_open,
168 .close = compress_close,
169 .pre_select = generic_filter_pre_select,
170 .post_select = compress_post_select,
171 };