Convert para_audiod to lopsub.
[paraslash.git] / compress_filter.c
1 /*
2  * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
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 <lopsub.h>
15
16 #include "filter_cmd.lsg.h"
17 #include "para.h"
18 #include "list.h"
19 #include "sched.h"
20 #include "buffer_tree.h"
21 #include "filter.h"
22 #include "string.h"
23 #include "error.h"
24
25 #define U32_OPTVAL(_opt, _lpr) (FILTER_CMD_OPT_UINT32_VAL(COMPRESS, _opt, _lpr))
26
27 /** Data specific to the compress filter. */
28 struct private_compress_data {
29         /** The current multiplier. */
30         unsigned current_gain;
31         /** Maximal admissible gain. */
32         unsigned max_gain;
33         /** Number of samples already seen. */
34         unsigned num_samples;
35         /** Absolute value of the maximal sample in the current block. */
36         int peak;
37 };
38
39 static void compress_close(struct filter_node *fn)
40 {
41         free(fn->private_data);
42 }
43
44 static int compress_post_select(__a_unused struct sched *s, void *context)
45 {
46         struct filter_node *fn = context;
47         struct private_compress_data *pcd = fn->private_data;
48         struct btr_node *btrn = fn->btrn;
49         bool inplace = btr_inplace_ok(btrn);
50         int ret;
51         char *inbuf;
52         size_t length, i;
53         int16_t *ip, *op;
54         uint32_t inertia = U32_OPTVAL(INERTIA, fn->lpr);
55         unsigned gain_shift = inertia + U32_OPTVAL(DAMP, fn->lpr),
56                 mask = (1 << U32_OPTVAL(BLOCKSIZE, fn->lpr)) - 1;
57         //inplace = false;
58 next_buffer:
59         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
60         if (ret < 0)
61                 goto err;
62         if (ret == 0)
63                 return 0;
64         btr_merge(btrn, fn->min_iqs);
65         length = btr_next_buffer(btrn, &inbuf) & ~(size_t)1;
66         if (length == 0) { /* eof and 1 byte available */
67                 ret = -E_COMPRESS_EOF;
68                 goto err;
69         }
70         ip = (int16_t *)inbuf;
71         if (inplace)
72                 op = ip;
73         else
74                 op = para_malloc(length);
75         for (i = 0; i < length / 2; i++) {
76                 /* be careful in that heat, my dear */
77                 int sample = *ip++;
78                 bool neg = false;
79
80                 if (sample < 0) {
81                         sample = -sample;
82                         neg = true;
83                 }
84                 sample *= pcd->current_gain;
85                 sample >>= gain_shift;
86                 if (sample > 32767) { /* clip */
87                         sample = 32767;
88                         pcd->current_gain = (3 * pcd->current_gain +
89                                 (1 << inertia)) / 4;
90                         pcd->peak = 0;
91                 } else if (sample > pcd->peak)
92                         pcd->peak = sample;
93                 op[i] = neg? -sample : sample;
94                 if (++pcd->num_samples & mask)
95                         continue;
96 //              PARA_DEBUG_LOG("gain: %u, peak: %u\n", pcd->current_gain,
97 //                      pcd->peak);
98                 if (pcd->peak < U32_OPTVAL(TARGET_LEVEL, fn->lpr)) {
99                         if (pcd->current_gain < pcd->max_gain)
100                                 pcd->current_gain++;
101                 } else
102                         pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
103                                 1U << inertia);
104                 pcd->peak = 0;
105         }
106         if (inplace)
107                 btr_pushdown_one(btrn);
108         else {
109                 btr_consume(btrn, length);
110                 btr_add_output((char *)op, length, btrn);
111         }
112         goto next_buffer;
113 err:
114         assert(ret < 0);
115         btr_remove_node(&fn->btrn);
116         return ret;
117 }
118
119 static void compress_open(struct filter_node *fn)
120 {
121         struct private_compress_data *pcd = para_calloc(sizeof(*pcd));
122         uint32_t inertia = U32_OPTVAL(INERTIA, fn->lpr);
123         uint32_t aggressiveness = U32_OPTVAL(AGGRESSIVENESS, fn->lpr);
124
125         fn->private_data = pcd;
126         fn->min_iqs = 2; /* 16 bit audio */
127         pcd->current_gain = 1U << inertia;
128         pcd->max_gain = 1U << (inertia + aggressiveness);
129 }
130
131 const struct filter lsg_filter_cmd_com_compress_user_data = {
132         .open = compress_open,
133         .close = compress_close,
134         .pre_select = generic_filter_pre_select,
135         .post_select = compress_post_select,
136 };