]> git.tuebingen.mpg.de Git - paraslash.git/blob - compress_filter.c
filter: Fix a memory leak in non-btr mode.
[paraslash.git] / compress_filter.c
1 /*
2  * Copyright (C) 2005-2009 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 #include <stdbool.h>
15
16 #include "para.h"
17 #include "compress_filter.cmdline.h"
18 #include "list.h"
19 #include "sched.h"
20 #include "ggo.h"
21 #include "buffer_tree.h"
22 #include "filter.h"
23 #include "string.h"
24 #include "error.h"
25
26 /** The size of the output data buffer. */
27 #define COMPRESS_CHUNK_SIZE 40960
28
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. */
36         unsigned max_gain;
37         /** Number of samples already seen. */
38         unsigned num_samples;
39         /** Absolute value of the maximal sample in the current block. */
40         int peak;
41 };
42
43 static ssize_t compress(char *inbuf, size_t inbuf_len, struct filter_node *fn)
44 {
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;
51
52         if (!length)
53                 return 0;
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;
64                         pcd->peak = 0;
65                 } else
66                         pcd->peak = PARA_MAX(pcd->peak, adjusted_sample);
67                 *op++ = sample >= 0? adjusted_sample : -adjusted_sample;
68                 if (++pcd->num_samples & mask)
69                         continue;
70 //              PARA_DEBUG_LOG("gain: %u, peak: %u\n", pcd->current_gain,
71 //                      pcd->peak);
72                 if (pcd->peak < pcd->conf->target_level_arg) {
73                         if (pcd->current_gain < pcd->max_gain)
74                                 pcd->current_gain++;
75                 } else
76                         pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
77                                 1U << pcd->conf->inertia_arg);
78                 pcd->peak = 0;
79         }
80         fn->loaded += length;
81         return length;
82 }
83
84 static void close_compress(struct filter_node *fn)
85 {
86         free(fn->private_data);
87         free(fn->buf);
88 }
89
90 static void compress_post_select(__a_unused struct sched *s, struct task *t)
91 {
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);
96         int ret;
97         char *inbuf;
98         size_t length, i;
99         int16_t *ip, *op;
100         unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
101                 mask = (1 << pcd->conf->blocksize_arg) - 1;
102
103         //inplace = false;
104 next_buffer:
105         t->error = 0;
106         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
107         if (ret < 0)
108                 goto err;
109         if (ret == 0)
110                 return;
111         btr_merge(btrn, fn->min_iqs);
112         length = btr_next_buffer(btrn, &inbuf) & ~(size_t)1;
113         ip = (int16_t *)inbuf;
114         if (inplace)
115                 op = ip;
116         else
117                 op = para_malloc(length);
118         for (i = 0; i < length / 2; i++) {
119                 /* be careful in that heat, my dear */
120                 int sample = *ip++, adjusted_sample = (PARA_ABS(sample) *
121                         pcd->current_gain) >> gain_shift;
122                 if (adjusted_sample > 32767) { /* clip */
123                         PARA_NOTICE_LOG("clip: sample: %d, adjusted sample: %d\n",
124                                 sample, adjusted_sample);
125                         adjusted_sample = 32767;
126                         pcd->current_gain = (3 * pcd->current_gain +
127                                 (1 << pcd->conf->inertia_arg)) / 4;
128                         pcd->peak = 0;
129                 } else
130                         pcd->peak = PARA_MAX(pcd->peak, adjusted_sample);
131                 op[i] = sample >= 0? adjusted_sample : -adjusted_sample;
132                 if (++pcd->num_samples & mask)
133                         continue;
134 //              PARA_DEBUG_LOG("gain: %u, peak: %u\n", pcd->current_gain,
135 //                      pcd->peak);
136                 if (pcd->peak < pcd->conf->target_level_arg) {
137                         if (pcd->current_gain < pcd->max_gain)
138                                 pcd->current_gain++;
139                 } else
140                         pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
141                                 1U << pcd->conf->inertia_arg);
142                 pcd->peak = 0;
143         }
144         if (inplace)
145                 btr_pushdown_one(btrn);
146         else {
147                 btr_consume(btrn, length);
148                 btr_add_output((char *)op, length, btrn);
149         }
150         goto next_buffer;
151 err:
152         assert(ret < 0);
153         t->error = ret;
154         btr_remove_node(btrn);
155 }
156
157 /** TODO: Add sanity checks */
158 static int compress_parse_config(int argc, char **argv, void **config)
159 {
160         int ret;
161         struct compress_filter_args_info *compress_conf
162                 = para_calloc(sizeof(*compress_conf));
163
164         ret = -E_COMPRESS_SYNTAX;
165         if (compress_cmdline_parser(argc, argv, compress_conf))
166                 goto err;
167         *config = compress_conf;
168         return 1;
169 err:
170         free(compress_conf);
171         return  ret;
172 }
173
174 static void open_compress(struct filter_node *fn)
175 {
176         struct private_compress_data *pcd = para_calloc(
177                 sizeof(struct private_compress_data));
178         pcd->conf = fn->conf;
179         fn->private_data = pcd;
180         fn->min_iqs = 2; /* 16 bit audio */
181         fn->bufsize = COMPRESS_CHUNK_SIZE;
182         fn->buf = para_malloc(fn->bufsize);
183         pcd->current_gain = 1 << pcd->conf->inertia_arg;
184         pcd->max_gain = 1 << (pcd->conf->inertia_arg + pcd->conf->aggressiveness_arg);
185 }
186
187 /**
188  * The init function of the compress filter.
189  *
190  * \param f Pointer to the struct to initialize.
191  */
192 void compress_filter_init(struct filter *f)
193 {
194         struct compress_filter_args_info dummy;
195
196         compress_cmdline_parser_init(&dummy);
197         f->open = open_compress;
198         f->close = close_compress;
199         f->convert = compress;
200         f->pre_select = generic_filter_pre_select;
201         f->post_select = compress_post_select;
202         f->parse_config = compress_parse_config;
203         f->help = (struct ggo_help) {
204                 .short_help = compress_filter_args_info_help,
205                 .detailed_help = compress_filter_args_info_detailed_help
206         };
207 }