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