2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file compress_filter.c Paraslash's dynamic audio range compressor. */
10 * Uses ideas of AudioCompress, (C) 2002-2004 M. Hari Nezumi <magenta@trikuare.cx>
17 #include "compress_filter.cmdline.h"
21 #include "buffer_tree.h"
26 /** The size of the output data buffer. */
27 #define COMPRESS_CHUNK_SIZE 40960
29 /** Data specific to the compress filter. */
30 struct private_compress_data {
31 /** The current multiplier. */
32 unsigned current_gain;
33 /** Points to the configuration data for this instance of the compress filter. */
34 struct compress_filter_args_info *conf;
35 /** Maximal admissible gain. */
37 /** Number of samples already seen. */
39 /** Absolute value of the maximal sample in the current block. */
43 static ssize_t compress(char *inbuf, size_t inbuf_len, struct filter_node *fn)
45 size_t i, length = PARA_MIN((inbuf_len / 2) * 2,
46 (fn->bufsize - fn->loaded) / 2 * 2);
47 struct private_compress_data *pcd = fn->private_data;
48 int16_t *ip = (int16_t *)inbuf, *op = (int16_t *)(fn->buf + fn->loaded);
49 unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
50 mask = (1 << pcd->conf->blocksize_arg) - 1;
54 for (i = 0; i < length / 2; i++) {
55 /* be careful in that heat, my dear */
56 int sample = *ip++, adjusted_sample = (PARA_ABS(sample) *
57 pcd->current_gain) >> gain_shift;
58 if (adjusted_sample > 32767) { /* clip */
59 PARA_NOTICE_LOG("clip: sample: %d, adjusted sample: %d\n",
60 sample, adjusted_sample);
61 adjusted_sample = 32767;
62 pcd->current_gain = (3 * pcd->current_gain +
63 (1 << pcd->conf->inertia_arg)) / 4;
66 pcd->peak = PARA_MAX(pcd->peak, adjusted_sample);
67 *op++ = sample >= 0? adjusted_sample : -adjusted_sample;
68 if (++pcd->num_samples & mask)
70 // PARA_DEBUG_LOG("gain: %u, peak: %u\n", pcd->current_gain,
72 if (pcd->peak < pcd->conf->target_level_arg) {
73 if (pcd->current_gain < pcd->max_gain)
76 pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
77 1U << pcd->conf->inertia_arg);
84 static void close_compress(struct filter_node *fn)
86 free(fn->private_data);
90 static void compress_post_select(__a_unused struct sched *s, struct task *t)
92 struct filter_node *fn = container_of(t, struct filter_node, task);
93 struct private_compress_data *pcd = fn->private_data;
94 struct btr_node *btrn = fn->btrn;
95 bool inplace = btr_inplace_ok(btrn);
100 unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
101 mask = (1 << pcd->conf->blocksize_arg) - 1;
106 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
111 btr_merge(btrn, fn->min_iqs);
112 length = btr_next_buffer(btrn, &inbuf) & ~(size_t)1;
113 if (length == 0) { /* eof and 1 byte available */
114 ret = -E_COMPRESS_EOF;
117 ip = (int16_t *)inbuf;
121 op = para_malloc(length);
122 for (i = 0; i < length / 2; i++) {
123 /* be careful in that heat, my dear */
124 int sample = *ip++, adjusted_sample = (PARA_ABS(sample) *
125 pcd->current_gain) >> gain_shift;
126 if (adjusted_sample > 32767) { /* clip */
127 PARA_NOTICE_LOG("clip: sample: %d, adjusted sample: %d\n",
128 sample, adjusted_sample);
129 adjusted_sample = 32767;
130 pcd->current_gain = (3 * pcd->current_gain +
131 (1 << pcd->conf->inertia_arg)) / 4;
134 pcd->peak = PARA_MAX(pcd->peak, adjusted_sample);
135 op[i] = sample >= 0? adjusted_sample : -adjusted_sample;
136 if (++pcd->num_samples & mask)
138 // PARA_DEBUG_LOG("gain: %u, peak: %u\n", pcd->current_gain,
140 if (pcd->peak < pcd->conf->target_level_arg) {
141 if (pcd->current_gain < pcd->max_gain)
144 pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
145 1U << pcd->conf->inertia_arg);
149 btr_pushdown_one(btrn);
151 btr_consume(btrn, length);
152 btr_add_output((char *)op, length, btrn);
158 btr_remove_node(btrn);
161 /** TODO: Add sanity checks */
162 static int compress_parse_config(int argc, char **argv, void **config)
165 struct compress_filter_args_info *compress_conf
166 = para_calloc(sizeof(*compress_conf));
168 ret = -E_COMPRESS_SYNTAX;
169 if (compress_cmdline_parser(argc, argv, compress_conf))
171 *config = compress_conf;
178 static void open_compress(struct filter_node *fn)
180 struct private_compress_data *pcd = para_calloc(
181 sizeof(struct private_compress_data));
182 pcd->conf = fn->conf;
183 fn->private_data = pcd;
184 fn->min_iqs = 2; /* 16 bit audio */
185 fn->bufsize = COMPRESS_CHUNK_SIZE;
186 fn->buf = para_malloc(fn->bufsize);
187 pcd->current_gain = 1 << pcd->conf->inertia_arg;
188 pcd->max_gain = 1 << (pcd->conf->inertia_arg + pcd->conf->aggressiveness_arg);
192 * The init function of the compress filter.
194 * \param f Pointer to the struct to initialize.
196 void compress_filter_init(struct filter *f)
198 struct compress_filter_args_info dummy;
200 compress_cmdline_parser_init(&dummy);
201 f->open = open_compress;
202 f->close = close_compress;
203 f->convert = compress;
204 f->pre_select = generic_filter_pre_select;
205 f->post_select = compress_post_select;
206 f->parse_config = compress_parse_config;
207 f->help = (struct ggo_help) {
208 .short_help = compress_filter_args_info_help,
209 .detailed_help = compress_filter_args_info_detailed_help