]> git.tuebingen.mpg.de Git - paraslash.git/blob - compress_filter.c
8e6b1c45eca5c65ae712875ab46250fa7a7dca7c
[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 void close_compress(struct filter_node *fn)
44 {
45         free(fn->private_data);
46         free(fn->buf);
47 }
48
49 static void compress_post_select(__a_unused struct sched *s, struct task *t)
50 {
51         struct filter_node *fn = container_of(t, struct filter_node, task);
52         struct private_compress_data *pcd = fn->private_data;
53         struct btr_node *btrn = fn->btrn;
54         bool inplace = btr_inplace_ok(btrn);
55         int ret;
56         char *inbuf;
57         size_t length, i;
58         int16_t *ip, *op;
59         unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
60                 mask = (1 << pcd->conf->blocksize_arg) - 1;
61
62         //inplace = false;
63 next_buffer:
64         t->error = 0;
65         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
66         if (ret < 0)
67                 goto err;
68         if (ret == 0)
69                 return;
70         btr_merge(btrn, fn->min_iqs);
71         length = btr_next_buffer(btrn, &inbuf) & ~(size_t)1;
72         if (length == 0) { /* eof and 1 byte available */
73                 ret = -E_COMPRESS_EOF;
74                 goto err;
75         }
76         ip = (int16_t *)inbuf;
77         if (inplace)
78                 op = ip;
79         else
80                 op = para_malloc(length);
81         for (i = 0; i < length / 2; i++) {
82                 /* be careful in that heat, my dear */
83                 int sample = *ip++, adjusted_sample = (PARA_ABS(sample) *
84                         pcd->current_gain) >> gain_shift;
85                 if (adjusted_sample > 32767) { /* clip */
86                         PARA_NOTICE_LOG("clip: sample: %d, adjusted sample: %d\n",
87                                 sample, adjusted_sample);
88                         adjusted_sample = 32767;
89                         pcd->current_gain = (3 * pcd->current_gain +
90                                 (1 << pcd->conf->inertia_arg)) / 4;
91                         pcd->peak = 0;
92                 } else
93                         pcd->peak = PARA_MAX(pcd->peak, adjusted_sample);
94                 op[i] = sample >= 0? adjusted_sample : -adjusted_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(btrn);
118 }
119
120 /** TODO: Add sanity checks */
121 static int compress_parse_config(int argc, char **argv, void **config)
122 {
123         int ret;
124         struct compress_filter_args_info *compress_conf
125                 = para_calloc(sizeof(*compress_conf));
126
127         ret = -E_COMPRESS_SYNTAX;
128         if (compress_cmdline_parser(argc, argv, compress_conf))
129                 goto err;
130         *config = compress_conf;
131         return 1;
132 err:
133         free(compress_conf);
134         return  ret;
135 }
136
137 static void open_compress(struct filter_node *fn)
138 {
139         struct private_compress_data *pcd = para_calloc(
140                 sizeof(struct private_compress_data));
141         pcd->conf = fn->conf;
142         fn->private_data = pcd;
143         fn->min_iqs = 2; /* 16 bit audio */
144         fn->bufsize = COMPRESS_CHUNK_SIZE;
145         fn->buf = para_malloc(fn->bufsize);
146         pcd->current_gain = 1 << pcd->conf->inertia_arg;
147         pcd->max_gain = 1 << (pcd->conf->inertia_arg + pcd->conf->aggressiveness_arg);
148 }
149
150 static void compress_free_config(void *conf)
151 {
152         compress_cmdline_parser_free(conf);
153 }
154
155 /**
156  * The init function of the compress filter.
157  *
158  * \param f Pointer to the struct to initialize.
159  */
160 void compress_filter_init(struct filter *f)
161 {
162         struct compress_filter_args_info dummy;
163
164         compress_cmdline_parser_init(&dummy);
165         f->open = open_compress;
166         f->close = close_compress;
167         f->pre_select = generic_filter_pre_select;
168         f->post_select = compress_post_select;
169         f->parse_config = compress_parse_config;
170         f->free_config = compress_free_config;
171         f->help = (struct ggo_help) {
172                 .short_help = compress_filter_args_info_help,
173                 .detailed_help = compress_filter_args_info_detailed_help
174         };
175 }