Skip to content

Commit

Permalink
Patch by Case: Fixes for resampling to lower sample rates
Browse files Browse the repository at this point in the history
When downsampling, more input samples are needed than output samples are generated. Thus allocate and use a bigger input sample buffer.
  • Loading branch information
maikmerten committed May 19, 2024
1 parent b2a13dd commit d581e89
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
13 changes: 5 additions & 8 deletions hmp3/src/pub/srcc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* ***** BEGIN LICENSE BLOCK *****
* Source last modified: 2024-03-16, Case
* Source last modified: 2024-05-19, Case
*
* Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
*
Expand Down Expand Up @@ -86,11 +86,8 @@ class Csrc
{

public:
Csrc ( );

~Csrc ( )
{
}
Csrc();
~Csrc();

int sr_convert_init ( int source, int channels, int bits, int is_float,
int target, int target_channels,
Expand All @@ -103,8 +100,8 @@ class Csrc

unsigned int src_bytes_out;
int src_filter;
int m_channels, m_bits, m_is_float;
float itof_buf[1152 * 2];
int m_channels, m_bits, m_is_float, m_frames_to_convert;
float *itof_buf;

//-functions------------------------------------------------
int gen_src_filter ( int source0, int target );
Expand Down
20 changes: 14 additions & 6 deletions hmp3/src/srcc.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* ***** BEGIN LICENSE BLOCK *****
* Source last modified: 2024-03-16, Case
* Source last modified: 2024-05-19, Case
*
* Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
*
Expand Down Expand Up @@ -63,10 +63,15 @@

/*==================================================================*/
Csrc::Csrc ( ):
src_bytes_out ( 0 ), src_filter ( 0 )
src_bytes_out ( 0 ), src_filter ( 0 ), itof_buf ( NULL )
{
}

Csrc::~Csrc()
{
delete[] itof_buf;
}

/*==================================================================*/

static int common_factor ( int s, int t );
Expand Down Expand Up @@ -781,6 +786,9 @@ Csrc::sr_convert_init ( int source, int channels, int bits, int is_float,
m_channels = channels;
m_bits = bits;
m_is_float = is_float;
m_frames_to_convert = 1152;
if (source > target) m_frames_to_convert *= ((source / target) + 1);
itof_buf = new float[m_frames_to_convert * 2];

return min_inbuf_bytes;
}
Expand All @@ -799,26 +807,26 @@ Csrc::sr_convert ( unsigned char xin[], float yout[] )
if (m_bits == 32) {
if (m_is_float) {
float *src = (float *)xin;
for ( int i = 0; i < 1152 * m_channels; ++i ) {
for ( int i = 0; i < m_frames_to_convert * m_channels; ++i ) {
*dst++ = (float)(*src++) * 32768.0f;
}
} else {
int *src = (int *)xin;

for ( int i = 0; i < 1152 * m_channels; ++i ) {
for ( int i = 0; i < m_frames_to_convert * m_channels; ++i ) {
*dst++ = (float)( (*src++) / 65536.0f );
}
}
} else if (m_bits == 24) {
unsigned char *src = (unsigned char *)xin;
for (int i = 0; i < 1152 * m_channels; ++i) {
for (int i = 0; i < m_frames_to_convert * m_channels; ++i) {
int s = ( ( src[2] << 24 ) | ( src[1] << 16 ) | ( src[0] << 8 ) ) >> 8;
*dst++ = (float)( (float)s / 256.0f );
src += 3;
}
} else if (m_bits == 16) {
short *src = (short *)xin;
for ( int i = 0; i < 1152 * m_channels; ++i ) {
for ( int i = 0; i < m_frames_to_convert * m_channels; ++i ) {
*dst++ = (float)*src++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions hmp3/src/test/tomp3.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* ***** BEGIN LICENSE BLOCK *****
* Source last modified: 2024-05-13, Maik Merten
* Source last modified: 2024-05-19, Case
*
* Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
*
Expand Down Expand Up @@ -1019,7 +1019,7 @@ ff_encode ( const fn_char *filename, const fn_char *fileout, E_CONTROL *ec0 )
// write zero samples into the pcm buffer
memset(pcm_buffer,0,bytes_in_init*4);

if(fi.rate < 32000) // adjust for MPEG2
if(ec.samprate < 32000) // adjust for MPEG2
frames_expected *= 2;

while(Encode.L3_audio_encode_get_frames() < frames_expected) {
Expand Down

0 comments on commit d581e89

Please sign in to comment.