server: Don't crash on blank moods.
[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 mask = (1U << U32_OPTVAL(BLOCKSIZE, fn->lpr)) - 1U;
52         //inplace = false;
53 next_buffer:
54         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
55         if (ret < 0)
56                 goto err;
57         if (ret == 0)
58                 return 0;
59         btr_merge(btrn, fn->min_iqs);
60         length = btr_next_buffer(btrn, &inbuf) & ~(size_t)1;
61         if (length == 0) { /* eof and 1 byte available */
62                 ret = -E_COMPRESS_EOF;
63                 goto err;
64         }
65         ip = (int16_t *)inbuf;
66         if (inplace)
67                 op = ip;
68         else
69                 op = para_malloc(length);
70         for (i = 0; i < length / 2; i++) {
71                 /* be careful in that heat, my dear */
72                 int sample = *ip++;
73                 bool neg = false;
74
75                 if (sample < 0) {
76                         sample = -sample;
77                         neg = true;
78                 }
79                 sample *= pcd->current_gain;
80                 sample >>= inertia + 1;
81                 if (sample > 32767) { /* clip */
82                         PARA_WARNING_LOG("clip: %d\n", sample);
83                         sample = 32767;
84                         pcd->current_gain = (3 * pcd->current_gain +
85                                 (1 << inertia)) / 4;
86                         pcd->peak = 0;
87                 } else if (sample > pcd->peak)
88                         pcd->peak = sample;
89                 sample >>= U32_OPTVAL(DAMP, fn->lpr);
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
96                 if (pcd->peak < U32_OPTVAL(TARGET_LEVEL, fn->lpr)) {
97                         if (pcd->current_gain < pcd->max_gain)
98                                 pcd->current_gain++;
99                 } else
100                         pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
101                                 1U << inertia);
102                 pcd->peak = 0;
103         }
104         if (inplace)
105                 btr_pushdown_one(btrn);
106         else {
107                 btr_consume(btrn, length);
108                 btr_add_output((char *)op, length, btrn);
109         }
110         goto next_buffer;
111 err:
112         assert(ret < 0);
113         btr_remove_node(&fn->btrn);
114         return ret;
115 }
116
117 static void compress_open(struct filter_node *fn)
118 {
119         struct private_compress_data *pcd = para_calloc(sizeof(*pcd));
120         uint32_t inertia = U32_OPTVAL(INERTIA, fn->lpr);
121         uint32_t aggressiveness = U32_OPTVAL(AGGRESSIVENESS, fn->lpr);
122
123         fn->private_data = pcd;
124         fn->min_iqs = 2; /* 16 bit audio */
125         pcd->current_gain = 1U << inertia;
126         pcd->max_gain = (1U << inertia) * (1.0 + 3.0 * aggressiveness / 10.0);
127 }
128
129 static void *compress_setup(const struct lls_parse_result *lpr)
130 {
131         uint32_t val;
132
133         val = U32_OPTVAL(BLOCKSIZE, lpr);
134         if (val == 0 || val > 31) {
135                 PARA_EMERG_LOG("blocksize (%u) out of range\n", val);
136                 exit(EXIT_FAILURE);
137         }
138         val = U32_OPTVAL(AGGRESSIVENESS, lpr);
139         if (val > 10) {
140                 PARA_EMERG_LOG("aggressiveness (%u) out of range\n", val);
141                 exit(EXIT_FAILURE);
142         }
143         val = U32_OPTVAL(INERTIA, lpr);
144         if (val == 0 || val > 14) {
145                 PARA_EMERG_LOG("inertia (%u) out of range\n", val);
146                 exit(EXIT_FAILURE);
147         }
148         val = U32_OPTVAL(TARGET_LEVEL, lpr);
149         if (val > 32767) {
150                 PARA_EMERG_LOG("target-level (%u) out of range\n", val);
151                 exit(EXIT_FAILURE);
152         }
153         val = U32_OPTVAL(DAMP, lpr);
154         if (val > 16) {
155                 PARA_EMERG_LOG("damp (%u) out of range\n", val);
156                 exit(EXIT_FAILURE);
157         }
158         return NULL; /* no need for a config structure */
159 }
160
161 const struct filter lsg_filter_cmd_com_compress_user_data = {
162         .setup = compress_setup,
163         .open = compress_open,
164         .close = compress_close,
165         .pre_select = generic_filter_pre_select,
166         .post_select = compress_post_select,
167 };