]> git.tuebingen.mpg.de Git - paraslash.git/blob - compress.c
Make filter config parsers return int.
[paraslash.git] / compress.c
1 /*
2  * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file compress.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 "para.h"
14 #include "compress_filter.cmdline.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "filter.h"
18 #include "string.h"
19 #include "error.h"
20
21 /** The size of the output data buffer. */
22 #define COMPRESS_CHUNK_SIZE 40960
23
24 extern char *stat_item_values[NUM_STAT_ITEMS];
25
26 /** Data specific to the compress filter. */
27 struct private_compress_data {
28         /** The current multiplier. */
29         unsigned current_gain;
30         /** Points to the configuration data for this instance of the compress filter. */
31         struct compress_filter_args_info *conf;
32         /** Maximal admissible gain. */
33         unsigned max_gain;
34         /** Number of samples already seen. */
35         unsigned num_samples;
36         /** Absolute value of the maximal sample in the current block. */
37         unsigned peak;
38 };
39
40 static ssize_t compress(char *inbuf, size_t inbuf_len, struct filter_node *fn)
41 {
42         size_t i, length = PARA_MIN((inbuf_len / 2) * 2,
43                 (fn->bufsize - fn->loaded) / 2 * 2);
44         struct private_compress_data *pcd = fn->private_data;
45         int16_t *ip = (int16_t *)inbuf, *op = (int16_t *)(fn->buf + fn->loaded);
46         unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
47                 mask = (1 << pcd->conf->blocksize_arg) - 1;
48
49         if (!length)
50                 return 0;
51         for (i = 0; i < length / 2; i++) {
52                 /* be careful in that heat, my dear */
53                 int sample = *ip++, adjusted_sample = (PARA_ABS(sample) *
54                         pcd->current_gain) >> gain_shift;
55                 if (unlikely(adjusted_sample > 32767)) { /* clip */
56                         PARA_NOTICE_LOG("clip: sample: %d, adjusted sample: %d\n",
57                                 sample, adjusted_sample);
58                         adjusted_sample = 32767;
59                         pcd->current_gain = (3 * pcd->current_gain +
60                                 (1 << pcd->conf->inertia_arg)) / 4;
61                         pcd->peak = 0;
62                 } else
63                         pcd->peak = PARA_MAX(pcd->peak, adjusted_sample);
64                 *op++ = sample >= 0? adjusted_sample : -adjusted_sample;
65                 if (likely(++pcd->num_samples & mask))
66                         continue;
67 //              PARA_DEBUG_LOG("gain: %u, peak: %u\n", pcd->current_gain,
68 //                      pcd->peak);
69                 if (pcd->peak < pcd->conf->target_level_arg) {
70                         if (pcd->current_gain < pcd->max_gain)
71                                 pcd->current_gain++;
72                 } else
73                         pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
74                                 1 << pcd->conf->inertia_arg);
75                 pcd->peak = 0;
76         }
77         fn->loaded += length;
78         return length;
79 }
80
81 static void close_compress(struct filter_node *fn)
82 {
83         free(fn->private_data);
84         free(fn->buf);
85 }
86
87 /** TODO: Add sanity checks */
88 static int compress_parse_config(int argc, char **argv, void **config)
89 {
90         int ret;
91         struct compress_filter_args_info *compress_conf
92                 = para_calloc(sizeof(*compress_conf));
93
94         ret = -E_COMPRESS_SYNTAX;
95         if (compress_cmdline_parser(argc, argv, compress_conf))
96                 goto err;
97         *config = compress_conf;
98         return 1;
99 err:
100         free(compress_conf);
101         return  ret;
102 }
103
104 static void open_compress(struct filter_node *fn)
105 {
106         struct private_compress_data *pcd = para_calloc(
107                 sizeof(struct private_compress_data));
108         pcd->conf = fn->conf;
109         fn->private_data = pcd;
110         fn->bufsize = COMPRESS_CHUNK_SIZE;
111         fn->buf = para_malloc(fn->bufsize);
112         pcd->current_gain = 1 << pcd->conf->inertia_arg;
113         pcd->max_gain = 1 << (pcd->conf->inertia_arg + pcd->conf->aggressiveness_arg);
114 }
115
116 /**
117  * The init function of the compress filter.
118  *
119  * \param f Pointer to the struct to initialize.
120  */
121 void compress_init(struct filter *f)
122 {
123         f->open = open_compress;
124         f->close = close_compress;
125         f->convert = compress;
126         f->print_help = compress_cmdline_parser_print_help;
127         f->parse_config = compress_parse_config;
128 }