Rockbox open source high quality audio player as a Music Player Daemon
mpris rockbox mpd libadwaita audio rust zig deno
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

compressor remove static delay line buffer

core_alloc works well for this and we can allow the alloc to be moved

bugfix compressor_process() possible buffer overflow

Change-Id: I701c6306c39cc1c7aa72b0dbe402b9b0d1a3fcda

+29 -8
+29 -8
lib/rbcodec/dsp/compressor.c
··· 22 22 #include "fixedpoint.h" 23 23 #include "fracmul.h" 24 24 #include <string.h> 25 + #include "core_alloc.h" 25 26 26 27 /* Define LOGF_ENABLE to enable logf output in this file 27 28 * #define LOGF_ENABLE ··· 66 67 static int32_t hp2y1 IBSS_ATTR; /* hpf2 y[n-1] */ 67 68 68 69 /* Delay Line for look-ahead compression */ 69 - static int32_t labuf[MAX_CH][MAX_DLY]; /* look-ahead buffer */ 70 + static int labuf_handle = -1; 71 + //static int32_t labuf[MAX_CH][MAX_DLY]; /* look-ahead buffer */ 70 72 static int32_t delay_time; 71 73 static int32_t delay_write; 72 74 static int32_t delay_read; ··· 150 152 attcb = 0; 151 153 } 152 154 153 - 155 + if (threshold < 0 && labuf_handle <= 0) 156 + { 157 + labuf_handle = core_alloc(sizeof(int32_t[MAX_CH][MAX_DLY])); 158 + if (labuf_handle < 0) 159 + { 160 + logf("%s Failed to allocate %d bytes", 161 + __func__, (int)sizeof(int32_t[MAX_CH][MAX_DLY])); 162 + return false; 163 + } 164 + logf(" Compressor Allocated %d bytes", (int)sizeof(int32_t[MAX_CH][MAX_DLY])); 165 + } 166 + #if 0 /* don't really need this */ 167 + if (threshold >= 0 && labuf_handle > 0) 168 + { 169 + labuf_handle = core_free(labuf_handle); 170 + logf(" Compressor Freed %d bytes", (int)sizeof(int32_t[MAX_CH][MAX_DLY])); 171 + } 172 + #endif 154 173 /* Sidechain pre-emphasis filter coefficients */ 155 174 hp1ca = fs + 0x003C1; /** The "magic" constant is 1/RC. This filter 156 175 * cut-off is approximately 237 Hz ··· 437 456 struct dsp_buffer *buf = *buf_p; 438 457 int count = buf->remcount; 439 458 int32_t *in_buf[2] = { buf->p32[0], buf->p32[1] }; 440 - const int num_chan = buf->format.num_channels; 459 + const int num_chan = MIN(buf->format.num_channels, MAX_CH); 460 + int32_t (*labufp)[MAX_CH][MAX_DLY] = core_get_data(labuf_handle); 441 461 442 462 while (count-- > 0) 443 463 { ··· 452 472 { 453 473 tmpx = *in_buf[ch]; 454 474 x += tmpx; 455 - labuf[ch][delay_write] = tmpx; 475 + (*labufp)[ch][delay_write] = tmpx; 456 476 /* Limiter detection */ 457 477 if(tmpx < 0) tmpx = -(tmpx + 1); 458 478 if(tmpx > in_buf_max_level) in_buf_max_level = tmpx; ··· 541 561 { 542 562 for (int ch = 0; ch < num_chan; ch++) 543 563 { 544 - *in_buf[ch] = FRACMUL_SHL(total_gain, labuf[ch][delay_read], 7); 564 + *in_buf[ch] = FRACMUL_SHL(total_gain, (*labufp)[ch][delay_read], 7); 545 565 } 546 566 } 547 567 in_buf[0]++; ··· 575 595 /* Fall-through */ 576 596 case DSP_RESET: 577 597 case DSP_FLUSH: 578 - 598 + { 599 + int32_t (*labufp)[MAX_CH][MAX_DLY] = core_get_data(labuf_handle); 579 600 release_gain = UNITY; 580 601 for(i=0; i<MAX_CH; i++) 581 602 { 582 603 for(j=0; j<MAX_DLY; j++) 583 604 { 584 - labuf[i][j] = 0; /* All Silence */ 605 + (*labufp)[i][j] = 0; /* All Silence */ 585 606 } 586 607 } 587 608 ··· 599 620 * look-ahead limiter 600 621 */ 601 622 break; 602 - 623 + } 603 624 case DSP_SET_OUT_FREQUENCY: 604 625 compressor_update(dsp, &curr_set); 605 626 break;