compress: Further optimize inner loop.
[paraslash.git] / compress_filter.c
1 /*
2 * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
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
15 #include "para.h"
16 #include "compress_filter.cmdline.h"
17 #include "list.h"
18 #include "sched.h"
19 #include "ggo.h"
20 #include "buffer_tree.h"
21 #include "filter.h"
22 #include "string.h"
23 #include "error.h"
24
25 /** Data specific to the compress filter. */
26 struct private_compress_data {
27 /** The current multiplier. */
28 unsigned current_gain;
29 /** Points to the configuration data for this instance of the compress filter. */
30 struct compress_filter_args_info *conf;
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 void compress_post_select(__a_unused struct sched *s, struct task *t)
45 {
46 struct filter_node *fn = container_of(t, struct filter_node, task);
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 unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
55 mask = (1 << pcd->conf->blocksize_arg) - 1;
56
57 //inplace = false;
58 next_buffer:
59 t->error = 0;
60 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
61 if (ret < 0)
62 goto err;
63 if (ret == 0)
64 return;
65 btr_merge(btrn, fn->min_iqs);
66 length = btr_next_buffer(btrn, &inbuf) & ~(size_t)1;
67 if (length == 0) { /* eof and 1 byte available */
68 ret = -E_COMPRESS_EOF;
69 goto err;
70 }
71 ip = (int16_t *)inbuf;
72 if (inplace)
73 op = ip;
74 else
75 op = para_malloc(length);
76 for (i = 0; i < length / 2; i++) {
77 /* be careful in that heat, my dear */
78 int sample = *ip++;
79 bool neg = false;
80
81 if (sample < 0) {
82 sample = -sample;
83 neg = true;
84 }
85 sample *= pcd->current_gain;
86 sample >>= gain_shift;
87 if (sample > 32767) { /* clip */
88 sample = 32767;
89 pcd->current_gain = (3 * pcd->current_gain +
90 (1 << pcd->conf->inertia_arg)) / 4;
91 pcd->peak = 0;
92 } else if (sample > pcd->peak)
93 pcd->peak = sample;
94 op[i] = neg? -sample : sample;
95 if (++pcd->num_samples & mask)
96 continue;
97 // PARA_DEBUG_LOG("gain: %u, peak: %u\n", pcd->current_gain,
98 // pcd->peak);
99 if (pcd->peak < pcd->conf->target_level_arg) {
100 if (pcd->current_gain < pcd->max_gain)
101 pcd->current_gain++;
102 } else
103 pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
104 1U << pcd->conf->inertia_arg);
105 pcd->peak = 0;
106 }
107 if (inplace)
108 btr_pushdown_one(btrn);
109 else {
110 btr_consume(btrn, length);
111 btr_add_output((char *)op, length, btrn);
112 }
113 goto next_buffer;
114 err:
115 assert(ret < 0);
116 t->error = ret;
117 btr_remove_node(&fn->btrn);
118 }
119
120 /** TODO: Add sanity checks */
121 static int compress_parse_config(int argc, char **argv, void **config)
122 {
123 struct compress_filter_args_info *conf = para_calloc(sizeof(*conf));
124
125 compress_filter_cmdline_parser(argc, argv, conf);
126 *config = conf;
127 return 1;
128 }
129
130 static void compress_open(struct filter_node *fn)
131 {
132 struct private_compress_data *pcd = para_calloc(
133 sizeof(struct private_compress_data));
134 pcd->conf = fn->conf;
135 fn->private_data = pcd;
136 fn->min_iqs = 2; /* 16 bit audio */
137 pcd->current_gain = 1 << pcd->conf->inertia_arg;
138 pcd->max_gain = 1 << (pcd->conf->inertia_arg + pcd->conf->aggressiveness_arg);
139 }
140
141 static void compress_free_config(void *conf)
142 {
143 compress_filter_cmdline_parser_free(conf);
144 }
145
146 /**
147 * The init function of the compress filter.
148 *
149 * \param f Pointer to the struct to initialize.
150 */
151 void compress_filter_init(struct filter *f)
152 {
153 struct compress_filter_args_info dummy;
154
155 compress_filter_cmdline_parser_init(&dummy);
156 f->open = compress_open;
157 f->close = compress_close;
158 f->pre_select = generic_filter_pre_select;
159 f->post_select = compress_post_select;
160 f->parse_config = compress_parse_config;
161 f->free_config = compress_free_config;
162 f->help = (struct ggo_help) {
163 .short_help = compress_filter_args_info_help,
164 .detailed_help = compress_filter_args_info_detailed_help
165 };
166 }