summaryrefslogtreecommitdiff
path: root/compress_filter.c
blob: 5ccbc4b2554644ef4b8feefd283b82249d8d5cff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* SPDX-License-Identifier: GPL-2.0 */

/** \file compress_filter.c Paraslash's dynamic audio range compressor. */

/*
 * Uses ideas of AudioCompress, (C) 2002-2004  M. Hari Nezumi <magenta@trikuare.cx>
 */

#include <lopsub.h>

#include "filter_cmd.lsg.h"
#include "para.h"
#include "list.h"
#include "sched.h"
#include "buffer_tree.h"
#include "filter.h"
#include "string.h"
#include "error.h"

#define U32_OPTVAL(_opt, _lpr) (FILTER_CMD_OPT_UINT32_VAL(COMPRESS, _opt, _lpr))

/** Data specific to the compress filter. */
struct private_compress_data {
	/** The current multiplier. */
	unsigned current_gain;
	/** Maximal admissible gain. */
	unsigned max_gain;
	/** Number of samples already seen. */
	unsigned num_samples;
	/** Absolute value of the maximal sample in the current block. */
	int peak;
};

static void compress_close(struct filter_node *fn)
{
	free(fn->private_data);
}

static int compress_post_monitor(__a_unused struct sched *s, void *context)
{
	struct filter_node *fn = context;
	struct private_compress_data *pcd = fn->private_data;
	struct btr_node *btrn = fn->btrn;
	bool inplace = btr_inplace_ok(btrn);
	int ret;
	char *inbuf;
	size_t length, i;
	int16_t *ip, *op;
	uint32_t inertia = U32_OPTVAL(INERTIA, fn->lpr);
	unsigned mask = (1U << U32_OPTVAL(BLOCKSIZE, fn->lpr)) - 1U;
	//inplace = false;
next_buffer:
	ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
	if (ret < 0)
		goto err;
	if (ret == 0)
		return 0;
	btr_merge(btrn, fn->min_iqs);
	length = btr_next_buffer(btrn, &inbuf) & ~(size_t)1;
	if (length == 0) { /* eof and 1 byte available */
		ret = -E_EOF;
		goto err;
	}
	ip = (int16_t *)inbuf;
	if (inplace)
		op = ip;
	else
		op = alloc(length);
	for (i = 0; i < length / 2; i++) {
		/* be careful in that heat, my dear */
		int sample = *ip++;
		bool neg = false;

		if (sample < 0) {
			sample = -sample;
			neg = true;
		}
		sample *= pcd->current_gain;
		sample >>= inertia + 1;
		if (sample > 32767) { /* clip */
			PARA_INFO_LOG("clip: %d\n", sample);
			sample = 32767;
			pcd->current_gain = (3 * pcd->current_gain +
				(1 << inertia)) / 4;
			pcd->peak = 0;
		} else if (sample > pcd->peak)
			pcd->peak = sample;
		sample >>= U32_OPTVAL(DAMP, fn->lpr);
		op[i] = neg? -sample : sample;
		if (++pcd->num_samples & mask)
			continue;
//		PARA_DEBUG_LOG("gain: %u, peak: %u\n", pcd->current_gain,
//			pcd->peak);

		if (pcd->peak < U32_OPTVAL(TARGET_LEVEL, fn->lpr)) {
			if (pcd->current_gain < pcd->max_gain)
				pcd->current_gain++;
		} else
			pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
				1U << inertia);
		pcd->peak = 0;
	}
	if (inplace)
		btr_pushdown_one(btrn);
	else {
		btr_consume(btrn, length);
		btr_add_output((char *)op, length, btrn);
	}
	goto next_buffer;
err:
	assert(ret < 0);
	btr_remove_node(&fn->btrn);
	return ret;
}

static void compress_open(struct filter_node *fn)
{
	struct private_compress_data *pcd = zalloc(sizeof(*pcd));
	uint32_t inertia = U32_OPTVAL(INERTIA, fn->lpr);
	uint32_t aggressiveness = U32_OPTVAL(AGGRESSIVENESS, fn->lpr);

	fn->private_data = pcd;
	fn->min_iqs = 2; /* 16 bit audio */
	pcd->current_gain = 1U << inertia;
	pcd->max_gain = (1U << inertia) * (1.0 + 3.0 * aggressiveness / 10.0);
}

static void *compress_setup(const struct lls_parse_result *lpr)
{
	uint32_t val;

	val = U32_OPTVAL(BLOCKSIZE, lpr);
	if (val == 0 || val > 31) {
		PARA_EMERG_LOG("blocksize (%u) out of range\n", val);
		exit(EXIT_FAILURE);
	}
	val = U32_OPTVAL(AGGRESSIVENESS, lpr);
	if (val > 10) {
		PARA_EMERG_LOG("aggressiveness (%u) out of range\n", val);
		exit(EXIT_FAILURE);
	}
	val = U32_OPTVAL(INERTIA, lpr);
	if (val == 0 || val > 14) {
		PARA_EMERG_LOG("inertia (%u) out of range\n", val);
		exit(EXIT_FAILURE);
	}
	val = U32_OPTVAL(TARGET_LEVEL, lpr);
	if (val > 32767) {
		PARA_EMERG_LOG("target-level (%u) out of range\n", val);
		exit(EXIT_FAILURE);
	}
	val = U32_OPTVAL(DAMP, lpr);
	if (val > 16) {
		PARA_EMERG_LOG("damp (%u) out of range\n", val);
		exit(EXIT_FAILURE);
	}
	return NULL; /* no need for a config structure */
}

/** \cond doxygen_ignore */
const struct filter lsg_filter_cmd_com_compress_user_data = {
	.setup = compress_setup,
	.open = compress_open,
	.close = compress_close,
	.pre_monitor = generic_filter_pre_monitor,
	.post_monitor = compress_post_monitor,
};
/** \endcond */