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