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