summaryrefslogtreecommitdiff
path: root/lib/rbcodec/dsp/compressor.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/dsp/compressor.c')
-rw-r--r--lib/rbcodec/dsp/compressor.c70
1 files changed, 56 insertions, 14 deletions
diff --git a/lib/rbcodec/dsp/compressor.c b/lib/rbcodec/dsp/compressor.c
index a6c1ac1018..1816bfef9c 100644
--- a/lib/rbcodec/dsp/compressor.c
+++ b/lib/rbcodec/dsp/compressor.c
@@ -19,16 +19,18 @@
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21#include "config.h" 21#include "config.h"
22#include "system.h"
22#include "fixedpoint.h" 23#include "fixedpoint.h"
23#include "fracmul.h" 24#include "fracmul.h"
24#include "settings.h"
25#include "dsp.h" 25#include "dsp.h"
26#include "compressor.h" 26#include <string.h>
27 27
28/* Define LOGF_ENABLE to enable logf output in this file */ 28/* Define LOGF_ENABLE to enable logf output in this file */
29/*#define LOGF_ENABLE*/ 29/*#define LOGF_ENABLE*/
30#include "logf.h" 30#include "logf.h"
31 31
32#include "dsp_proc_entry.h"
33
32static struct compressor_settings curr_set; /* Cached settings */ 34static struct compressor_settings curr_set; /* Cached settings */
33 35
34static int32_t comp_rel_slope IBSS_ATTR; /* S7.24 format */ 36static int32_t comp_rel_slope IBSS_ATTR; /* S7.24 format */
@@ -251,10 +253,10 @@ bool compressor_update(const struct compressor_settings *settings)
251 * Returns the required gain factor in S7.24 format in order to compress the 253 * Returns the required gain factor in S7.24 format in order to compress the
252 * sample in accordance with the compression curve. Always 1 or less. 254 * sample in accordance with the compression curve. Always 1 or less.
253 */ 255 */
254static inline int32_t get_compression_gain(struct dsp_data *data, 256static inline int32_t get_compression_gain(struct sample_format *format,
255 int32_t sample) 257 int32_t sample)
256{ 258{
257 const int frac_bits_offset = data->frac_bits - 15; 259 const int frac_bits_offset = format->frac_bits - 15;
258 260
259 /* sample must be positive */ 261 /* sample must be positive */
260 if (sample < 0) 262 if (sample < 0)
@@ -292,24 +294,40 @@ static inline int32_t get_compression_gain(struct dsp_data *data,
292 return -1; 294 return -1;
293} 295}
294 296
297/** DSP interface **/
298
299/** SET COMPRESSOR
300 * Enable or disable the compressor based upon the settings
301 */
302void dsp_set_compressor(const struct compressor_settings *settings)
303{
304 /* enable/disable the compressor depending upon settings */
305 bool enable = compressor_update(settings);
306 struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO);
307 dsp_proc_enable(dsp, DSP_PROC_COMPRESSOR, enable);
308 dsp_proc_activate(dsp, DSP_PROC_COMPRESSOR, true);
309}
310
295/** COMPRESSOR PROCESS 311/** COMPRESSOR PROCESS
296 * Changes the gain of the samples according to the compressor curve 312 * Changes the gain of the samples according to the compressor curve
297 */ 313 */
298void compressor_process(int count, struct dsp_data *data, int32_t *buf[]) 314static void compressor_process(struct dsp_proc_entry *this,
315 struct dsp_buffer **buf_p)
299{ 316{
300 const int num_chan = data->num_channels; 317 struct dsp_buffer *buf = *buf_p;
301 int32_t *in_buf[2] = {buf[0], buf[1]}; 318 int count = buf->remcount;
302 319 int32_t *in_buf[2] = { buf->p32[0], buf->p32[1] };
320 const int num_chan = buf->format.num_channels;
321
303 while (count-- > 0) 322 while (count-- > 0)
304 { 323 {
305 int ch;
306 /* use lowest (most compressed) gain factor of the output buffer 324 /* use lowest (most compressed) gain factor of the output buffer
307 sample pair for both samples (mono is also handled correctly here) 325 sample pair for both samples (mono is also handled correctly here)
308 */ 326 */
309 int32_t sample_gain = UNITY; 327 int32_t sample_gain = UNITY;
310 for (ch = 0; ch < num_chan; ch++) 328 for (int ch = 0; ch < num_chan; ch++)
311 { 329 {
312 int32_t this_gain = get_compression_gain(data, *in_buf[ch]); 330 int32_t this_gain = get_compression_gain(&buf->format, *in_buf[ch]);
313 if (this_gain < sample_gain) 331 if (this_gain < sample_gain)
314 sample_gain = this_gain; 332 sample_gain = this_gain;
315 } 333 }
@@ -345,7 +363,7 @@ void compressor_process(int count, struct dsp_data *data, int32_t *buf[])
345 output buffer sample pair/mono sample */ 363 output buffer sample pair/mono sample */
346 if (total_gain != UNITY) 364 if (total_gain != UNITY)
347 { 365 {
348 for (ch = 0; ch < num_chan; ch++) 366 for (int ch = 0; ch < num_chan; ch++)
349 { 367 {
350 *in_buf[ch] = FRACMUL_SHL(total_gain, *in_buf[ch], 7); 368 *in_buf[ch] = FRACMUL_SHL(total_gain, *in_buf[ch], 7);
351 } 369 }
@@ -353,9 +371,33 @@ void compressor_process(int count, struct dsp_data *data, int32_t *buf[])
353 in_buf[0]++; 371 in_buf[0]++;
354 in_buf[1]++; 372 in_buf[1]++;
355 } 373 }
374
375 (void)this;
356} 376}
357 377
358void compressor_reset(void) 378/* DSP message hook */
379static intptr_t compressor_configure(struct dsp_proc_entry *this,
380 struct dsp_config *dsp,
381 unsigned int setting,
382 intptr_t value)
359{ 383{
360 release_gain = UNITY; 384 switch (setting)
385 {
386 case DSP_PROC_INIT:
387 if (value != 0)
388 break; /* Already enabled */
389 this->process[0] = compressor_process;
390 case DSP_RESET:
391 case DSP_FLUSH:
392 release_gain = UNITY;
393 break;
394 }
395
396 return 1;
397 (void)dsp;
361} 398}
399
400/* Database entry */
401DSP_PROC_DB_ENTRY(
402 COMPRESSOR,
403 compressor_configure);