Fix return value of dccp_recv pre_select()
[paraslash.git] / compress.c
1 /*
2  * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file compress.c paraslash's dynamic audio range compressor */
20
21 /*
22  * Used ideas of AudioCompress, (C) 2002-2004  M. Hari Nezumi <magenta@trikuare.cx>
23  */
24
25 #include "para.h"
26 #include "compress_filter.cmdline.h"
27 #include "list.h"
28 #include "filter.h"
29 #include "string.h"
30
31 /** the size of the output data buffer */
32 #define COMPRESS_CHUNK_SIZE 40960
33
34 /** data specific to the compress filter */
35 struct private_compress_data {
36         /** the current multiplier */
37         unsigned current_gain;
38         /** points to the configuration data for this instance of the compress filter */
39         struct gengetopt_args_info *conf;
40         /** minimal admissible gain */
41         unsigned min_gain;
42         /** maximal admissible gain */
43         unsigned max_gain;
44         /** number of samples already seen */
45         unsigned num_samples;
46         /** absolute value of the maximal sample in the current block */
47         unsigned peak;
48 };
49
50 static ssize_t compress(char *inbuf, size_t inbuf_len, struct filter_node *fn)
51 {
52         size_t i, length = MIN((inbuf_len / 2) * 2, (fn->bufsize - fn->loaded) / 2 * 2);
53         struct private_compress_data *pcd = fn->private_data;
54         int16_t *ip = (int16_t *)inbuf, *op = (int16_t *)(fn->buf + fn->loaded);
55         unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
56                 mask = (1 << pcd->conf->blocksize_arg) - 1;
57
58         if (!length)
59                 return 0;
60         for (i = 0; i < length / 2; i++) {
61                 /* be careful in that heat, my dear */
62                 int sample = *ip++, adjusted_sample;
63
64                 if (sample > 0) {
65                         adjusted_sample = (sample * pcd->current_gain)
66                                 >> gain_shift;
67                         if (unlikely(adjusted_sample > 32767)) {
68                                 adjusted_sample = 32767;
69                                 pcd->current_gain = (pcd->current_gain +
70                                         (1 << pcd->conf->inertia_arg)) / 2;
71                                 pcd->peak = 0;
72                         } else
73                                 if (adjusted_sample > pcd->peak)
74                                         pcd->peak = sample;
75                 } else {
76                         adjusted_sample = -((-sample * pcd->current_gain)
77                                 >> gain_shift);
78                         if (unlikely(adjusted_sample < -32768)) {
79                                 adjusted_sample = -32768;
80                                 pcd->current_gain = (pcd->current_gain +
81                                         (1 << pcd->conf->inertia_arg)) / 2;
82                                 pcd->peak = 0;
83                         } else
84                                 if (-adjusted_sample > pcd->peak)
85                                         pcd->peak = -adjusted_sample;
86                 }
87                 *op++ = adjusted_sample;
88                 if (likely(++pcd->num_samples & mask))
89                         continue;
90                 if (pcd->peak < pcd->conf->target_level_arg) {
91                         if (pcd->current_gain < pcd->max_gain)
92                                 pcd->current_gain++;
93                 } else {
94                         if (pcd->current_gain > pcd->min_gain + 1)
95                                 pcd->current_gain -= 2;
96                 }
97 //              PARA_DEBUG_LOG("gain: %lu, peak: %d\n", pcd->current_gain,
98 //                      pcd->peak);
99                 pcd->peak = 0;
100 //              PARA_INFO_LOG("sample: %lu\n", ABS(sample));
101         }
102         fn->loaded += length;
103         return length;
104 }
105
106 static void close_compress(struct filter_node *fn)
107 {
108         free(fn->private_data);
109         free(fn->buf);
110 }
111
112 static void *compress_parse_config(int argc, char **argv)
113 {
114         struct gengetopt_args_info *ret = para_calloc(sizeof(struct gengetopt_args_info));
115         if (!compress_cmdline_parser(argc, argv, ret))
116                 return ret;
117         free(ret);
118         return NULL;
119 }
120
121 static void open_compress(struct filter_node *fn)
122 {
123         struct private_compress_data *pcd = para_calloc(
124                 sizeof(struct private_compress_data));
125         pcd->conf = fn->conf;
126         fn->private_data = pcd;
127         fn->bufsize = COMPRESS_CHUNK_SIZE;
128         fn->buf = para_malloc(fn->bufsize);
129         pcd->current_gain = 1 << pcd->conf->inertia_arg;
130         pcd->min_gain = 1 << (pcd->conf->inertia_arg - pcd->conf->aggressiveness_arg);
131         pcd->max_gain = 1 << (pcd->conf->inertia_arg + pcd->conf->aggressiveness_arg);
132 }
133
134 /** the init function of the compress filter */
135 void compress_init(struct filter *f)
136 {
137         f->open = open_compress;
138         f->close = close_compress;
139         f->convert = compress;
140         f->print_help = compress_cmdline_parser_print_help;
141         f->parse_config = compress_parse_config;
142 }