/***************************************************************************** * DoWork: convert a buffer *****************************************************************************/ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter, aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf ) { filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys; float *p_out = (float *)p_out_buf->p_buffer; int i_nb_channels = aout_FormatNbChannels( &p_filter->input ); int i_in_nb = p_in_buf->i_nb_samples; int i_in, i_out = 0; unsigned int i_out_rate; double d_factor, d_scale_factor, d_old_scale_factor; int i_filter_wing; if( p_sys->b_filter2 ) i_out_rate = p_filter->output.i_rate; else i_out_rate = p_aout->mixer_format.i_rate; /* Check if we really need to run the resampler */ if( i_out_rate == p_filter->input.i_rate ) { if( /*p_filter->b_continuity && /--* What difference does it make ? :) */ p_sys->i_old_wing && p_in_buf->i_size >= p_in_buf->i_nb_bytes + p_sys->i_old_wing * p_filter->input.i_bytes_per_frame ) { /* output the whole thing with the samples from last time */ memmove( ((float *)(p_in_buf->p_buffer)) + i_nb_channels * p_sys->i_old_wing, p_in_buf->p_buffer, p_in_buf->i_nb_bytes ); memcpy( p_in_buf->p_buffer, p_sys->p_buf + i_nb_channels * p_sys->i_old_wing, p_sys->i_old_wing * p_filter->input.i_bytes_per_frame ); p_out_buf->i_nb_samples = p_in_buf->i_nb_samples + p_sys->i_old_wing; p_out_buf->start_date = date_Get( &p_sys->end_date ); p_out_buf->end_date = date_Increment( &p_sys->end_date, p_out_buf->i_nb_samples ); p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples * p_filter->input.i_bytes_per_frame; } p_filter->b_continuity = false; p_sys->i_old_wing = 0; return; } if( !p_filter->b_continuity ) { /* Continuity in sound samples has been broken, we'd better reset * everything. */ p_filter->b_continuity = true; p_sys->i_remainder = 0; date_Init( &p_sys->end_date, i_out_rate, 1 ); date_Set( &p_sys->end_date, p_in_buf->start_date ); p_sys->i_old_rate = p_filter->input.i_rate; p_sys->d_old_factor = 1; p_sys->i_old_wing = 0; } #if 0 msg_Err( p_filter, "old rate: %i, old factor: %f, old wing: %i, i_in: %i", p_sys->i_old_rate, p_sys->d_old_factor, p_sys->i_old_wing, i_in_nb ); #endif /* Prepare the source buffer */ i_in_nb += (p_sys->i_old_wing * 2); float p_in_orig[i_in_nb * p_filter->input.i_bytes_per_frame / 4], *p_in = p_in_orig; /* Copy all our samples in p_in */ if( p_sys->i_old_wing ) { vlc_memcpy( p_in, p_sys->p_buf, p_sys->i_old_wing * 2 * p_filter->input.i_bytes_per_frame ); } vlc_memcpy( p_in + p_sys->i_old_wing * 2 * i_nb_channels, p_in_buf->p_buffer, p_in_buf->i_nb_samples * p_filter->input.i_bytes_per_frame ); /* Make sure the output buffer is reset */ memset( p_out, 0, p_out_buf->i_size ); /* Calculate the new length of the filter wing */ d_factor = (double)i_out_rate / p_filter->input.i_rate; i_filter_wing = ((SMALL_FILTER_NMULT+1)/2.0) * __MAX(1.0,1.0/d_factor) + 1; /* Account for increased filter gain when using factors less than 1 */ d_old_scale_factor = SMALL_FILTER_SCALE * p_sys->d_old_factor + 0.5; d_scale_factor = SMALL_FILTER_SCALE * d_factor + 0.5; /* Apply the old rate until we have enough samples for the new one */ i_in = p_sys->i_old_wing; p_in += p_sys->i_old_wing * i_nb_channels; for( ; i_in < i_filter_wing && (i_in + p_sys->i_old_wing) < i_in_nb; i_in++ ) { if( p_sys->d_old_factor == 1 ) { /* Just copy the samples */ memcpy( p_out, p_in, p_filter->input.i_bytes_per_frame ); p_in += i_nb_channels; p_out += i_nb_channels; i_out++; continue; } while( p_sys->i_remainder < p_filter->output.i_rate ) { if( p_sys->d_old_factor >= 1 ) { /* FilterFloatUP() is faster if we can use it */ /* Perform left-wing inner product */ FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in, p_out, p_sys->i_remainder, p_filter->output.i_rate, -1, i_nb_channels ); /* Perform right-wing inner product */ FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in + i_nb_channels, p_out, p_filter->output.i_rate - p_sys->i_remainder, p_filter->output.i_rate, 1, i_nb_channels ); #if 0 /* Normalize for unity filter gain */ for( i = 0; i < i_nb_channels; i++ ) { *(p_out+i) *= d_old_scale_factor; } #endif /* Sanity check */ if( p_out_buf->i_size/p_filter->input.i_bytes_per_frame <= (unsigned int)i_out+1 ) { p_out += i_nb_channels; i_out++; p_sys->i_remainder += p_filter->input.i_rate; break; } } else { /* Perform left-wing inner product */ FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in, p_out, p_sys->i_remainder, p_filter->output.i_rate, p_filter->input.i_rate, -1, i_nb_channels ); /* Perform right-wing inner product */ FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in + i_nb_channels, p_out, p_filter->output.i_rate - p_sys->i_remainder, p_filter->output.i_rate, p_filter->input.i_rate, 1, i_nb_channels ); } p_out += i_nb_channels; i_out++; p_sys->i_remainder += p_filter->input.i_rate; } p_in += i_nb_channels; p_sys->i_remainder -= p_filter->output.i_rate; } /* Apply the new rate for the rest of the samples */ if( i_in < i_in_nb - i_filter_wing ) { p_sys->i_old_rate = p_filter->input.i_rate; p_sys->d_old_factor = d_factor; p_sys->i_old_wing = i_filter_wing; } for( ; i_in < i_in_nb - i_filter_wing; i_in++ ) { while( p_sys->i_remainder < p_filter->output.i_rate ) { if( d_factor >= 1 ) { /* FilterFloatUP() is faster if we can use it */ /* Perform left-wing inner product */ FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in, p_out, p_sys->i_remainder, p_filter->output.i_rate, -1, i_nb_channels ); /* Perform right-wing inner product */ FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in + i_nb_channels, p_out, p_filter->output.i_rate - p_sys->i_remainder, p_filter->output.i_rate, 1, i_nb_channels ); #if 0 /* Normalize for unity filter gain */ for( int i = 0; i < i_nb_channels; i++ ) { *(p_out+i) *= d_old_scale_factor; } #endif /* Sanity check */ if( p_out_buf->i_size/p_filter->input.i_bytes_per_frame <= (unsigned int)i_out+1 ) { p_out += i_nb_channels; i_out++; p_sys->i_remainder += p_filter->input.i_rate; break; } } else { /* Perform left-wing inner product */ FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in, p_out, p_sys->i_remainder, p_filter->output.i_rate, p_filter->input.i_rate, -1, i_nb_channels ); /* Perform right-wing inner product */ FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in + i_nb_channels, p_out, p_filter->output.i_rate - p_sys->i_remainder, p_filter->output.i_rate, p_filter->input.i_rate, 1, i_nb_channels ); } p_out += i_nb_channels; i_out++; p_sys->i_remainder += p_filter->input.i_rate; } p_in += i_nb_channels; p_sys->i_remainder -= p_filter->output.i_rate; } /* Buffer i_filter_wing * 2 samples for next time */ if( p_sys->i_old_wing ) { memcpy( p_sys->p_buf, p_in_orig + (i_in_nb - 2 * p_sys->i_old_wing) * i_nb_channels, (2 * p_sys->i_old_wing) * p_filter->input.i_bytes_per_frame ); } #if 0 msg_Err( p_filter, "p_out size: %i, nb bytes out: %i", p_out_buf->i_size, i_out * p_filter->input.i_bytes_per_frame ); #endif /* Finalize aout buffer */ p_out_buf->i_nb_samples = i_out; p_out_buf->start_date = date_Get( &p_sys->end_date ); p_out_buf->end_date = date_Increment( &p_sys->end_date, p_out_buf->i_nb_samples ); p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples * i_nb_channels * sizeof(int32_t); }
static void ResampleFloat( filter_t *p_filter, block_t **pp_out_buf, size_t *pi_out, float **pp_in, int i_in, int i_in_end, double d_factor, bool b_factor_old, int i_nb_channels, int i_bytes_per_frame ) { filter_sys_t *p_sys = p_filter->p_sys; float *p_in = *pp_in; size_t i_out = *pi_out; float *p_out = (float*)(*pp_out_buf)->p_buffer + i_out * i_nb_channels; for( ; i_in < i_in_end; i_in++ ) { if( b_factor_old && d_factor == 1 ) { if( ReallocBuffer( pp_out_buf, &p_out, i_out, i_nb_channels, i_bytes_per_frame ) ) return; /* Just copy the samples */ memcpy( p_out, p_in, i_bytes_per_frame ); p_in += i_nb_channels; p_out += i_nb_channels; i_out++; continue; } while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate ) { if( ReallocBuffer( pp_out_buf, &p_out, i_out, i_nb_channels, i_bytes_per_frame ) ) return; if( d_factor >= 1 ) { /* FilterFloatUP() is faster if we can use it */ /* Perform left-wing inner product */ FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in, p_out, p_sys->i_remainder, p_filter->fmt_out.audio.i_rate, -1, i_nb_channels ); /* Perform right-wing inner product */ FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in + i_nb_channels, p_out, p_filter->fmt_out.audio.i_rate - p_sys->i_remainder, p_filter->fmt_out.audio.i_rate, 1, i_nb_channels ); #if 0 /* Normalize for unity filter gain */ for( i = 0; i < i_nb_channels; i++ ) { *(p_out+i) *= d_old_scale_factor; } #endif } else { /* Perform left-wing inner product */ FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in, p_out, p_sys->i_remainder, p_filter->fmt_out.audio.i_rate, p_filter->fmt_in.audio.i_rate, -1, i_nb_channels ); /* Perform right-wing inner product */ FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in + i_nb_channels, p_out, p_filter->fmt_out.audio.i_rate - p_sys->i_remainder, p_filter->fmt_out.audio.i_rate, p_filter->fmt_in.audio.i_rate, 1, i_nb_channels ); } p_out += i_nb_channels; i_out++; p_sys->i_remainder += p_filter->fmt_in.audio.i_rate; } p_in += i_nb_channels; p_sys->i_remainder -= p_filter->fmt_out.audio.i_rate; } *pp_in = p_in; *pi_out = i_out; }
/***************************************************************************** * Resample: convert a buffer *****************************************************************************/ static block_t *Resample( filter_t * p_filter, block_t * p_in_buf ) { if( !p_in_buf || !p_in_buf->i_nb_samples ) { if( p_in_buf ) block_Release( p_in_buf ); return NULL; } filter_sys_t *p_sys = p_filter->p_sys; unsigned int i_out_rate = p_filter->fmt_out.audio.i_rate; int i_nb_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio ); /* Check if we really need to run the resampler */ if( i_out_rate == p_filter->fmt_in.audio.i_rate ) { if( !(p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY) && p_sys->i_old_wing ) { /* output the whole thing with the samples from last time */ p_in_buf = block_Realloc( p_in_buf, p_sys->i_old_wing * p_filter->fmt_in.audio.i_bytes_per_frame, p_in_buf->i_buffer ); if( !p_in_buf ) return NULL; memcpy( p_in_buf->p_buffer, p_sys->p_buf + i_nb_channels * p_sys->i_old_wing, p_sys->i_old_wing * p_filter->fmt_in.audio.i_bytes_per_frame ); p_in_buf->i_nb_samples += p_sys->i_old_wing; p_in_buf->i_pts = date_Get( &p_sys->end_date ); p_in_buf->i_length = date_Increment( &p_sys->end_date, p_in_buf->i_nb_samples ) - p_in_buf->i_pts; } p_sys->i_old_wing = 0; p_sys->b_first = true; return p_in_buf; } unsigned i_bytes_per_frame = p_filter->fmt_out.audio.i_channels * p_filter->fmt_out.audio.i_bitspersample / 8; size_t i_out_size = i_bytes_per_frame * ( 1 + ( p_in_buf->i_nb_samples * p_filter->fmt_out.audio.i_rate / p_filter->fmt_in.audio.i_rate) ) + p_filter->p_sys->i_buf_size; block_t *p_out_buf = filter_NewAudioBuffer( p_filter, i_out_size ); if( !p_out_buf ) return NULL; float *p_out = (float *)p_out_buf->p_buffer; if( (p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY) || p_sys->b_first ) { /* Continuity in sound samples has been broken, we'd better reset * everything. */ p_out_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY; p_sys->i_remainder = 0; date_Init( &p_sys->end_date, i_out_rate, 1 ); date_Set( &p_sys->end_date, p_in_buf->i_pts ); p_sys->d_old_factor = 1; p_sys->i_old_wing = 0; p_sys->b_first = false; } int i_in_nb = p_in_buf->i_nb_samples; int i_in, i_out = 0; double d_factor, d_scale_factor, d_old_scale_factor; int i_filter_wing; #if 0 msg_Err( p_filter, "old rate: %i, old factor: %f, old wing: %i, i_in: %i", p_sys->i_old_rate, p_sys->d_old_factor, p_sys->i_old_wing, i_in_nb ); #endif /* Prepare the source buffer */ i_in_nb += (p_sys->i_old_wing * 2); float p_in_orig[i_in_nb * p_filter->fmt_in.audio.i_bytes_per_frame / 4], *p_in = p_in_orig; /* Copy all our samples in p_in */ if( p_sys->i_old_wing ) { vlc_memcpy( p_in, p_sys->p_buf, p_sys->i_old_wing * 2 * p_filter->fmt_in.audio.i_bytes_per_frame ); } /* XXX: why i_nb_channels instead of i_bytes_per_frame??? */ vlc_memcpy( p_in + p_sys->i_old_wing * 2 * i_nb_channels, p_in_buf->p_buffer, p_in_buf->i_nb_samples * p_filter->fmt_in.audio.i_bytes_per_frame ); block_Release( p_in_buf ); /* Make sure the output buffer is reset */ memset( p_out, 0, p_out_buf->i_buffer ); /* Calculate the new length of the filter wing */ d_factor = (double)i_out_rate / p_filter->fmt_in.audio.i_rate; i_filter_wing = ((SMALL_FILTER_NMULT+1)/2.0) * __MAX(1.0,1.0/d_factor) + 1; /* Account for increased filter gain when using factors less than 1 */ d_old_scale_factor = SMALL_FILTER_SCALE * p_sys->d_old_factor + 0.5; d_scale_factor = SMALL_FILTER_SCALE * d_factor + 0.5; /* Apply the old rate until we have enough samples for the new one */ i_in = p_sys->i_old_wing; p_in += p_sys->i_old_wing * i_nb_channels; for( ; i_in < i_filter_wing && (i_in + p_sys->i_old_wing) < i_in_nb; i_in++ ) { if( p_sys->d_old_factor == 1 ) { /* Just copy the samples */ memcpy( p_out, p_in, p_filter->fmt_in.audio.i_bytes_per_frame ); p_in += i_nb_channels; p_out += i_nb_channels; i_out++; continue; } while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate ) { if( p_sys->d_old_factor >= 1 ) { /* FilterFloatUP() is faster if we can use it */ /* Perform left-wing inner product */ FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in, p_out, p_sys->i_remainder, p_filter->fmt_out.audio.i_rate, -1, i_nb_channels ); /* Perform right-wing inner product */ FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in + i_nb_channels, p_out, p_filter->fmt_out.audio.i_rate - p_sys->i_remainder, p_filter->fmt_out.audio.i_rate, 1, i_nb_channels ); #if 0 /* Normalize for unity filter gain */ for( i = 0; i < i_nb_channels; i++ ) { *(p_out+i) *= d_old_scale_factor; } #endif /* Sanity check */ if( p_out_buf->i_buffer/p_filter->fmt_in.audio.i_bytes_per_frame <= (unsigned int)i_out+1 ) { p_out += i_nb_channels; i_out++; p_sys->i_remainder += p_filter->fmt_in.audio.i_rate; break; } } else { /* Perform left-wing inner product */ FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in, p_out, p_sys->i_remainder, p_filter->fmt_out.audio.i_rate, p_filter->fmt_in.audio.i_rate, -1, i_nb_channels ); /* Perform right-wing inner product */ FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in + i_nb_channels, p_out, p_filter->fmt_out.audio.i_rate - p_sys->i_remainder, p_filter->fmt_out.audio.i_rate, p_filter->fmt_in.audio.i_rate, 1, i_nb_channels ); } p_out += i_nb_channels; i_out++; p_sys->i_remainder += p_filter->fmt_in.audio.i_rate; } p_in += i_nb_channels; p_sys->i_remainder -= p_filter->fmt_out.audio.i_rate; } /* Apply the new rate for the rest of the samples */ if( i_in < i_in_nb - i_filter_wing ) { p_sys->d_old_factor = d_factor; p_sys->i_old_wing = i_filter_wing; } for( ; i_in < i_in_nb - i_filter_wing; i_in++ ) { while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate ) { if( d_factor >= 1 ) { /* FilterFloatUP() is faster if we can use it */ /* Perform left-wing inner product */ FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in, p_out, p_sys->i_remainder, p_filter->fmt_out.audio.i_rate, -1, i_nb_channels ); /* Perform right-wing inner product */ FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in + i_nb_channels, p_out, p_filter->fmt_out.audio.i_rate - p_sys->i_remainder, p_filter->fmt_out.audio.i_rate, 1, i_nb_channels ); #if 0 /* Normalize for unity filter gain */ for( int i = 0; i < i_nb_channels; i++ ) { *(p_out+i) *= d_old_scale_factor; } #endif /* Sanity check */ if( p_out_buf->i_buffer/p_filter->fmt_in.audio.i_bytes_per_frame <= (unsigned int)i_out+1 ) { p_out += i_nb_channels; i_out++; p_sys->i_remainder += p_filter->fmt_in.audio.i_rate; break; } } else { /* Perform left-wing inner product */ FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in, p_out, p_sys->i_remainder, p_filter->fmt_out.audio.i_rate, p_filter->fmt_in.audio.i_rate, -1, i_nb_channels ); /* Perform right-wing inner product */ FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD, SMALL_FILTER_NWING, p_in + i_nb_channels, p_out, p_filter->fmt_out.audio.i_rate - p_sys->i_remainder, p_filter->fmt_out.audio.i_rate, p_filter->fmt_in.audio.i_rate, 1, i_nb_channels ); } p_out += i_nb_channels; i_out++; p_sys->i_remainder += p_filter->fmt_in.audio.i_rate; } p_in += i_nb_channels; p_sys->i_remainder -= p_filter->fmt_out.audio.i_rate; } /* Finalize aout buffer */ p_out_buf->i_nb_samples = i_out; p_out_buf->i_pts = date_Get( &p_sys->end_date ); p_out_buf->i_length = date_Increment( &p_sys->end_date, p_out_buf->i_nb_samples ) - p_out_buf->i_pts; p_out_buf->i_buffer = p_out_buf->i_nb_samples * i_nb_channels * sizeof(int32_t); /* Buffer i_filter_wing * 2 samples for next time */ if( p_sys->i_old_wing ) { size_t newsize = p_sys->i_old_wing * 2 * p_filter->fmt_in.audio.i_bytes_per_frame; if( newsize > p_sys->i_buf_size ) { free( p_sys->p_buf ); p_sys->p_buf = malloc( newsize ); if( p_sys->p_buf != NULL ) p_sys->i_buf_size = newsize; else { p_sys->i_buf_size = p_sys->i_old_wing = 0; /* oops! */ return p_out_buf; } } memcpy( p_sys->p_buf, p_in_orig + (i_in_nb - 2 * p_sys->i_old_wing) * i_nb_channels, (2 * p_sys->i_old_wing) * p_filter->fmt_in.audio.i_bytes_per_frame ); } #if 0 msg_Err( p_filter, "p_out size: %i, nb bytes out: %i", p_out_buf->i_buffer, i_out * p_filter->fmt_in.audio.i_bytes_per_frame ); #endif return p_out_buf; }