Merge branch 't/mood_fix'
[paraslash.git] / compress_filter.c
1 /*
2 * Copyright (C) 2005-2013 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
15 #include "para.h"
16 #include "compress_filter.cmdline.h"
17 #include "list.h"
18 #include "sched.h"
19 #include "ggo.h"
20 #include "buffer_tree.h"
21 #include "filter.h"
22 #include "string.h"
23 #include "error.h"
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 void compress_close(struct filter_node *fn)
40 {
41 free(fn->private_data);
42 }
43
44 static void compress_post_select(__a_unused struct sched *s, struct task *t)
45 {
46 struct filter_node *fn = container_of(t, struct filter_node, task);
47 struct private_compress_data *pcd = fn->private_data;
48 struct btr_node *btrn = fn->btrn;
49 bool inplace = btr_inplace_ok(btrn);
50 int ret;
51 char *inbuf;
52 size_t length, i;
53 int16_t *ip, *op;
54 unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
55 mask = (1 << pcd->conf->blocksize_arg) - 1;
56
57 //inplace = false;
58 next_buffer:
59 t->error = 0;
60 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
61 if (ret < 0)
62 goto err;
63 if (ret == 0)
64 return;
65 btr_merge(btrn, fn->min_iqs);
66 length = btr_next_buffer(btrn, &inbuf) & ~(size_t)1;
67 if (length == 0) { /* eof and 1 byte available */
68 ret = -E_COMPRESS_EOF;
69 goto err;
70 }
71 ip = (int16_t *)inbuf;
72 if (inplace)
73 op = ip;
74 else
75 op = para_malloc(length);
76 for (i = 0; i < length / 2; i++) {
77 /* be careful in that heat, my dear */
78 int sample = *ip++, adjusted_sample = (PARA_ABS(sample) *
79 pcd->current_gain) >> gain_shift;
80 if (adjusted_sample > 32767) { /* clip */
81 PARA_NOTICE_LOG("clip: sample: %d, adjusted sample: %d\n",
82 sample, adjusted_sample);
83 adjusted_sample = 32767;
84 pcd->current_gain = (3 * pcd->current_gain +
85 (1 << pcd->conf->inertia_arg)) / 4;
86 pcd->peak = 0;
87 } else
88 pcd->peak = PARA_MAX(pcd->peak, adjusted_sample);
89 op[i] = sample >= 0? adjusted_sample : -adjusted_sample;
90 if (++pcd->num_samples & mask)
91 continue;
92 // PARA_DEBUG_LOG("gain: %u, peak: %u\n", pcd->current_gain,
93 // pcd->peak);
94 if (pcd->peak < pcd->conf->target_level_arg) {
95 if (pcd->current_gain < pcd->max_gain)
96 pcd->current_gain++;
97 } else
98 pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
99 1U << pcd->conf->inertia_arg);
100 pcd->peak = 0;
101 }
102 if (inplace)
103 btr_pushdown_one(btrn);
104 else {
105 btr_consume(btrn, length);
106 btr_add_output((char *)op, length, btrn);
107 }
108 goto next_buffer;
109 err:
110 assert(ret < 0);
111 t->error = ret;
112 btr_remove_node(&fn->btrn);
113 }
114
115 /** TODO: Add sanity checks */
116 static int compress_parse_config(int argc, char **argv, void **config)
117 {
118 struct compress_filter_args_info *conf = para_calloc(sizeof(*conf));
119
120 compress_filter_cmdline_parser(argc, argv, conf);
121 *config = conf;
122 return 1;
123 }
124
125 static void compress_open(struct filter_node *fn)
126 {
127 struct private_compress_data *pcd = para_calloc(
128 sizeof(struct private_compress_data));
129 pcd->conf = fn->conf;
130 fn->private_data = pcd;
131 fn->min_iqs = 2; /* 16 bit audio */
132 pcd->current_gain = 1 << pcd->conf->inertia_arg;
133 pcd->max_gain = 1 << (pcd->conf->inertia_arg + pcd->conf->aggressiveness_arg);
134 }
135
136 static void compress_free_config(void *conf)
137 {
138 compress_filter_cmdline_parser_free(conf);
139 }
140
141 /**
142 * The init function of the compress filter.
143 *
144 * \param f Pointer to the struct to initialize.
145 */
146 void compress_filter_init(struct filter *f)
147 {
148 struct compress_filter_args_info dummy;
149
150 compress_filter_cmdline_parser_init(&dummy);
151 f->open = compress_open;
152 f->close = compress_close;
153 f->pre_select = generic_filter_pre_select;
154 f->post_select = compress_post_select;
155 f->parse_config = compress_parse_config;
156 f->free_config = compress_free_config;
157 f->help = (struct ggo_help) {
158 .short_help = compress_filter_args_info_help,
159 .detailed_help = compress_filter_args_info_detailed_help
160 };
161 }