From 5eee28e37d78e0679fc5aa8488e5408429153e4c Mon Sep 17 00:00:00 2001 From: Michael Sevakis Date: Thu, 12 Oct 2017 03:12:58 -0400 Subject: Nitpick configuration code in a few DSP filters to fix some bugs Most importantly is surround shouldn't operate in mono mode. Have it watch and (de)activate itself on relevant format changes as it should. Other changes to better handle buffer allocation failure. PBE was set internally at 100 by default; SBZ. Change-Id: I328e0b674e56751a255eae817d7892d685796b06 --- lib/rbcodec/dsp/afr.c | 4 +- lib/rbcodec/dsp/pbe.c | 77 +++++++++++++-------------- lib/rbcodec/dsp/surround.c | 128 ++++++++++++++++++++++++++++++--------------- 3 files changed, 125 insertions(+), 84 deletions(-) diff --git a/lib/rbcodec/dsp/afr.c b/lib/rbcodec/dsp/afr.c index b266e4b549..d9b6ef97a4 100644 --- a/lib/rbcodec/dsp/afr.c +++ b/lib/rbcodec/dsp/afr.c @@ -77,9 +77,10 @@ void dsp_afr_enable(int var) if (var == afr_strength) return; /* No setting change */ - bool was_enabled = afr_strength > 0; afr_strength = var; + struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO); + bool was_enabled = dsp_proc_enabled(dsp, DSP_PROC_AFR); bool now_enabled = var > 0; if (was_enabled == now_enabled && !now_enabled) @@ -88,7 +89,6 @@ void dsp_afr_enable(int var) /* If changing status, enable or disable it; if already enabled push additional DSP_PROC_INIT messages with value = 1 to force-update the filters */ - struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO); dsp_proc_enable(dsp, DSP_PROC_AFR, now_enabled); } diff --git a/lib/rbcodec/dsp/pbe.c b/lib/rbcodec/dsp/pbe.c index 3656f687fd..3e2183a302 100644 --- a/lib/rbcodec/dsp/pbe.c +++ b/lib/rbcodec/dsp/pbe.c @@ -37,7 +37,7 @@ #define B2_SIZE (B2_DLY+1) #define B0_SIZE (B0_DLY+1) -static int pbe_strength = 100; +static int pbe_strength = 0; static int pbe_precut = 0; static int32_t tcoef1, tcoef2, tcoef3; static int32_t *b0[2], *b2[2], *b3[2]; @@ -46,20 +46,21 @@ int32_t temp_buffer; static struct dsp_filter pbe_filter[5]; static int handle = -1; -static void pbe_buffer_alloc(void) -{ - if (handle > 0) - return; /* already-allocated */ +#define PBE_BUFSIZE ((B0_SIZE + B2_SIZE + B3_SIZE)*2*sizeof(int32_t)) - unsigned int total_len = (B0_SIZE + B2_SIZE + B3_SIZE) * 2; - handle = core_alloc("dsp_pbe_buffer",sizeof(int32_t) * total_len); +static int pbe_buffer_alloc(void) +{ + handle = core_alloc("dsp_pbe_buffer", PBE_BUFSIZE); + return handle; +} +static void pbe_buffer_free(void) +{ if (handle < 0) - { - pbe_strength = 0; return; - } - memset(core_get_data(handle),0,sizeof(int32_t) * total_len); + + core_free(handle); + handle = -1; } static void pbe_buffer_get_data(void) @@ -76,12 +77,8 @@ static void pbe_buffer_get_data(void) static void dsp_pbe_flush(void) { - if (pbe_strength == 0) - return; /* Not currently enabled */ + memset(core_get_data(handle), 0, PBE_BUFSIZE); - unsigned int total_len = (B0_SIZE + B2_SIZE + B3_SIZE) * 2; - if (handle > 0) - memset(core_get_data(handle),0,sizeof(int32_t) * total_len); b0_r[0] = 0; b0_w[0] = 0; b0_r[1] = 0; b0_w[1] = 0; b2_r[0] = 0; b2_w[0] = 0; @@ -121,35 +118,28 @@ void dsp_pbe_precut(int var) pbe_precut = var; - if (pbe_strength == 0) + struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO); + + if (!dsp_proc_enabled(dsp, DSP_PROC_PBE)) return; /* Not currently enabled */ - /* Push more DSP_PROC_INIT messages to force filter updates - (with value = 1) */ - struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO); - dsp_proc_enable(dsp, DSP_PROC_PBE, true); + pbe_update_filter(dsp_get_output_frequency(dsp)); } - void dsp_pbe_enable(int var) { if (var == pbe_strength) return; /* No change */ - bool was_enabled = pbe_strength > 0; + pbe_strength = var; + struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO); + bool was_enabled = dsp_proc_enabled(dsp, DSP_PROC_PBE); bool now_enabled = var > 0; if (now_enabled == was_enabled) return; /* No change in enabled status */ - if (now_enabled == false && handle > 0) - { - core_free(handle); - handle = -1; - } - - struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO); dsp_proc_enable(dsp, DSP_PROC_PBE, now_enabled); } @@ -209,21 +199,28 @@ static intptr_t pbe_configure(struct dsp_proc_entry *this, intptr_t value) { /* This only attaches to the audio (codec) DSP */ + intptr_t retval = 0; switch (setting) { case DSP_PROC_INIT: - if (value == 0) - { - /* Coming online; was disabled */ - this->process = pbe_process; - pbe_buffer_alloc(); - dsp_pbe_flush(); - dsp_proc_activate(dsp, DSP_PROC_PBE, true); - } - /* else additional forced messages */ + /* Coming online; was disabled */ + retval = pbe_buffer_alloc(); + if (retval < 0) + break; + + this->process = pbe_process; + + dsp_pbe_flush(); + /* Wouldn't have been getting frequency updates */ pbe_update_filter(dsp_get_output_frequency(dsp)); + dsp_proc_activate(dsp, DSP_PROC_PBE, true); + break; + + case DSP_PROC_CLOSE: + /* Being disabled (called also if init fails) */ + pbe_buffer_free(); break; case DSP_FLUSH: @@ -237,7 +234,7 @@ static intptr_t pbe_configure(struct dsp_proc_entry *this, break; } - return 1; + return retval; } /* Database entry */ diff --git a/lib/rbcodec/dsp/surround.c b/lib/rbcodec/dsp/surround.c index b2995de49f..f3349b34e5 100644 --- a/lib/rbcodec/dsp/surround.c +++ b/lib/rbcodec/dsp/surround.c @@ -27,7 +27,6 @@ #include "dsp_filter.h" #include "core_alloc.h" -static bool surround_enabled = false; static int surround_balance = 0; static bool surround_side_only = false; static int surround_mix = 100; @@ -64,26 +63,25 @@ static int b0_r=0,b0_w=0, cl_r=0,cl_w=0; static int handle = -1; -static void surround_buffer_alloc(void) -{ - if (handle > 0) - return; /* already-allocated */ +#define SURROUND_BUFSIZE ((B0_DLY + B2_DLY + BB_DLY + HH_DLY + CL_DLY)*sizeof (int32_t)) - unsigned int total_len = B0_DLY + B2_DLY + BB_DLY + HH_DLY + CL_DLY; - handle = core_alloc("dsp_surround_buffer",sizeof(int32_t) * total_len); +static int surround_buffer_alloc(void) +{ + handle = core_alloc("dsp_surround_buffer", SURROUND_BUFSIZE); + return handle; +} +static void surround_buffer_free(void) +{ if (handle < 0) - { - surround_enabled = false; return; - } - memset(core_get_data(handle),0,sizeof(int32_t) * total_len); + + core_free(handle); + handle = -1; } static void surround_buffer_get_data(void) { - if (handle < 0) - return; b0 = core_get_data(handle); b2 = b0 + B0_DLY; bb = b2 + B2_DLY; @@ -93,12 +91,7 @@ static void surround_buffer_get_data(void) static void dsp_surround_flush(void) { - if (!surround_enabled) - return; - - unsigned int total_len = B0_DLY + B2_DLY + BB_DLY + HH_DLY + CL_DLY; - if (handle > 0) - memset(core_get_data(handle),0,sizeof(int32_t) * total_len); + memset(core_get_data(handle), 0, SURROUND_BUFSIZE); } static void surround_update_filter(unsigned int fout) @@ -116,8 +109,16 @@ void dsp_surround_set_balance(int var) void dsp_surround_side_only(bool var) { - dsp_surround_flush(); + if (var == surround_side_only) + return; /* No setting change */ + surround_side_only = var; + + struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO); + if (!dsp_proc_enabled(dsp, DSP_PROC_SURROUND)) + return; + + dsp_surround_flush(); } void dsp_surround_mix(int var) @@ -127,12 +128,17 @@ void dsp_surround_mix(int var) void dsp_surround_set_cutoff(int frq_l, int frq_h) { + if (cutoff_l == frq_l && cutoff_h == frq_h) + return; /* No settings change */ + cutoff_l = frq_l;/*fx2*/ cutoff_h = frq_h;/*fx1*/ struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO); - unsigned int fout = dsp_get_output_frequency(dsp); - surround_update_filter(fout); + if (!dsp_proc_enabled(dsp, DSP_PROC_SURROUND)) + return; + + surround_update_filter(dsp_get_output_frequency(dsp)); } static void surround_set_stepsize(int surround_strength) @@ -163,23 +169,21 @@ void dsp_surround_enable(int var) if (var == surround_strength) return; /* No setting change */ - bool was_enabled = surround_strength > 0; surround_strength = var; - surround_set_stepsize(surround_strength); + struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO); + bool was_enabled = dsp_proc_enabled(dsp, DSP_PROC_SURROUND); bool now_enabled = var > 0; if (was_enabled == now_enabled && !now_enabled) return; /* No change in enabled status */ - if (now_enabled == false && handle > 0) - { - core_free(handle); - handle = -1; - } - surround_enabled = now_enabled; + if (now_enabled) + surround_set_stepsize(var); - struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO); + /* If changing status, enable or disable it; if already enabled push + additional DSP_PROC_INIT messages with value = 1 to force-update the + filters */ dsp_proc_enable(dsp, DSP_PROC_SURROUND, now_enabled); } @@ -284,42 +288,82 @@ static void surround_process(struct dsp_proc_entry *this, (void)this; } +/* Handle format changes and verify the format compatibility */ +static intptr_t surround_new_format(struct dsp_proc_entry *this, + struct dsp_config *dsp, + struct sample_format *format) +{ + DSP_PRINT_FORMAT(DSP_PROC_SURROUND, *format); + + /* Stereo mode only */ + bool was_active = dsp_proc_active(dsp, DSP_PROC_SURROUND); + bool now_active = format->num_channels > 1; + dsp_proc_activate(dsp, DSP_PROC_SURROUND, now_active); + + if (now_active) + { + if (!was_active) + dsp_surround_flush(); /* Going online */ + + return PROC_NEW_FORMAT_OK; + } + + /* Can't do this. Sleep until next change. */ + DEBUGF(" DSP_PROC_SURROUND- deactivated\n"); + return PROC_NEW_FORMAT_DEACTIVATED; + + (void)this; +} + /* DSP message hook */ static intptr_t surround_configure(struct dsp_proc_entry *this, struct dsp_config *dsp, unsigned int setting, intptr_t value) { - unsigned int fout = dsp_get_output_frequency(dsp); + intptr_t retval = 0; + switch (setting) { case DSP_PROC_INIT: if (value == 0) { + retval = surround_buffer_alloc(); + if (retval < 0) + break; + this->process = surround_process; - surround_buffer_alloc(); - dsp_surround_flush(); - dsp_proc_activate(dsp, DSP_PROC_SURROUND, true); } - else - surround_update_filter(fout); + /* else additional forced messages */ + + surround_update_filter(dsp_get_output_frequency(dsp)); break; + + case DSP_PROC_CLOSE: + /* Being disabled (called also if init fails) */ + surround_buffer_free(); + break; + case DSP_FLUSH: + /* Discontinuity; clear filters */ dsp_surround_flush(); break; - case DSP_SET_OUT_FREQUENCY: + + case DSP_SET_OUT_FREQUENCY: + /* New output frequency */ surround_update_filter(value); break; - case DSP_PROC_CLOSE: + + case DSP_PROC_NEW_FORMAT: + /* Source buffer format is changing (also sent when first enabled) */ + retval = surround_new_format(this, dsp, (struct sample_format *)value); break; } - return 1; - (void)dsp; + return retval; } /* Database entry */ DSP_PROC_DB_ENTRY( SURROUND, surround_configure); - -- cgit v1.2.3