mirror of
				https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
				synced 2025-11-03 09:01:50 -05:00 
			
		
		
		
	resampler: Introduce a implementation specific struct
This struct holds all the implementation specific data in one place.
This commit is contained in:
		
							parent
							
								
									c5cd65d81e
								
							
						
					
					
						commit
						4bdfebee5c
					
				
					 2 changed files with 56 additions and 51 deletions
				
			
		| 
						 | 
				
			
			@ -74,11 +74,7 @@ struct pa_resampler {
 | 
			
		|||
    pa_remap_t remap;
 | 
			
		||||
    bool map_required;
 | 
			
		||||
 | 
			
		||||
    void (*impl_free)(pa_resampler *r);
 | 
			
		||||
    void (*impl_update_rates)(pa_resampler *r);
 | 
			
		||||
    void (*impl_resample)(pa_resampler *r, const pa_memchunk *in, unsigned in_samples, pa_memchunk *out, unsigned *out_samples);
 | 
			
		||||
    void (*impl_reset)(pa_resampler *r);
 | 
			
		||||
    void *impl_data;
 | 
			
		||||
    pa_resampler_impl impl;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct trivial_data { /* data specific to the trivial resampler */
 | 
			
		||||
| 
						 | 
				
			
			@ -445,8 +441,8 @@ fail:
 | 
			
		|||
void pa_resampler_free(pa_resampler *r) {
 | 
			
		||||
    pa_assert(r);
 | 
			
		||||
 | 
			
		||||
    if (r->impl_free)
 | 
			
		||||
        r->impl_free(r);
 | 
			
		||||
    if (r->impl.free)
 | 
			
		||||
        r->impl.free(r);
 | 
			
		||||
 | 
			
		||||
    if (r->to_work_format_buf.memblock)
 | 
			
		||||
        pa_memblock_unref(r->to_work_format_buf.memblock);
 | 
			
		||||
| 
						 | 
				
			
			@ -457,7 +453,7 @@ void pa_resampler_free(pa_resampler *r) {
 | 
			
		|||
    if (r->from_work_format_buf.memblock)
 | 
			
		||||
        pa_memblock_unref(r->from_work_format_buf.memblock);
 | 
			
		||||
 | 
			
		||||
    pa_xfree(r->impl_data);
 | 
			
		||||
    pa_xfree(r->impl.data);
 | 
			
		||||
    pa_xfree(r);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -470,7 +466,7 @@ void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
 | 
			
		|||
 | 
			
		||||
    r->i_ss.rate = rate;
 | 
			
		||||
 | 
			
		||||
    r->impl_update_rates(r);
 | 
			
		||||
    r->impl.update_rates(r);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
 | 
			
		||||
| 
						 | 
				
			
			@ -482,7 +478,7 @@ void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
 | 
			
		|||
 | 
			
		||||
    r->o_ss.rate = rate;
 | 
			
		||||
 | 
			
		||||
    r->impl_update_rates(r);
 | 
			
		||||
    r->impl.update_rates(r);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
 | 
			
		||||
| 
						 | 
				
			
			@ -548,8 +544,8 @@ size_t pa_resampler_max_block_size(pa_resampler *r) {
 | 
			
		|||
void pa_resampler_reset(pa_resampler *r) {
 | 
			
		||||
    pa_assert(r);
 | 
			
		||||
 | 
			
		||||
    if (r->impl_reset)
 | 
			
		||||
        r->impl_reset(r);
 | 
			
		||||
    if (r->impl.reset)
 | 
			
		||||
        r->impl.reset(r);
 | 
			
		||||
 | 
			
		||||
    r->remap_buf_contains_leftover_data = false;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1239,7 +1235,7 @@ static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
 | 
			
		|||
 | 
			
		||||
    /* Resample the data and place the result in resample_buf. */
 | 
			
		||||
 | 
			
		||||
    if (!r->impl_resample || !input->length)
 | 
			
		||||
    if (!r->impl.resample || !input->length)
 | 
			
		||||
        return input;
 | 
			
		||||
 | 
			
		||||
    in_n_samples = (unsigned) (input->length / r->w_sz);
 | 
			
		||||
| 
						 | 
				
			
			@ -1259,7 +1255,7 @@ static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
 | 
			
		|||
        r->resample_buf.memblock = pa_memblock_new(r->mempool, r->resample_buf.length);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    r->impl_resample(r, input, in_n_frames, &r->resample_buf, &out_n_frames);
 | 
			
		||||
    r->impl.resample(r, input, in_n_frames, &r->resample_buf, &out_n_frames);
 | 
			
		||||
    r->resample_buf.length = out_n_frames * r->w_sz * r->o_ss.channels;
 | 
			
		||||
 | 
			
		||||
    return &r->resample_buf;
 | 
			
		||||
| 
						 | 
				
			
			@ -1366,7 +1362,7 @@ static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, un
 | 
			
		|||
    pa_assert(output);
 | 
			
		||||
    pa_assert(out_n_frames);
 | 
			
		||||
 | 
			
		||||
    libsamplerate_data = r->impl_data;
 | 
			
		||||
    libsamplerate_data = r->impl.data;
 | 
			
		||||
    memset(&data, 0, sizeof(data));
 | 
			
		||||
 | 
			
		||||
    data.data_in = pa_memblock_acquire_chunk(input);
 | 
			
		||||
| 
						 | 
				
			
			@ -1397,7 +1393,7 @@ static void libsamplerate_update_rates(pa_resampler *r) {
 | 
			
		|||
    struct src_data *libsamplerate_data;
 | 
			
		||||
    pa_assert(r);
 | 
			
		||||
 | 
			
		||||
    libsamplerate_data = r->impl_data;
 | 
			
		||||
    libsamplerate_data = r->impl.data;
 | 
			
		||||
    pa_assert_se(src_set_ratio(libsamplerate_data->state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1405,7 +1401,7 @@ static void libsamplerate_reset(pa_resampler *r) {
 | 
			
		|||
    struct src_data *libsamplerate_data;
 | 
			
		||||
    pa_assert(r);
 | 
			
		||||
 | 
			
		||||
    libsamplerate_data = r->impl_data;
 | 
			
		||||
    libsamplerate_data = r->impl.data;
 | 
			
		||||
    pa_assert_se(src_reset(libsamplerate_data->state) == 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1413,7 +1409,7 @@ static void libsamplerate_free(pa_resampler *r) {
 | 
			
		|||
    struct src_data *libsamplerate_data;
 | 
			
		||||
    pa_assert(r);
 | 
			
		||||
 | 
			
		||||
    libsamplerate_data = r->impl_data;
 | 
			
		||||
    libsamplerate_data = r->impl.data;
 | 
			
		||||
    if (libsamplerate_data->state)
 | 
			
		||||
        src_delete(libsamplerate_data->state);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1429,11 +1425,11 @@ static int libsamplerate_init(pa_resampler *r) {
 | 
			
		|||
    if (!(libsamplerate_data->state = src_new(r->method, r->o_ss.channels, &err)))
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    r->impl_free = libsamplerate_free;
 | 
			
		||||
    r->impl_update_rates = libsamplerate_update_rates;
 | 
			
		||||
    r->impl_resample = libsamplerate_resample;
 | 
			
		||||
    r->impl_reset = libsamplerate_reset;
 | 
			
		||||
    r->impl_data = libsamplerate_data;
 | 
			
		||||
    r->impl.free = libsamplerate_free;
 | 
			
		||||
    r->impl.update_rates = libsamplerate_update_rates;
 | 
			
		||||
    r->impl.resample = libsamplerate_resample;
 | 
			
		||||
    r->impl.reset = libsamplerate_reset;
 | 
			
		||||
    r->impl.data = libsamplerate_data;
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1452,7 +1448,7 @@ static void speex_resample_float(pa_resampler *r, const pa_memchunk *input, unsi
 | 
			
		|||
    pa_assert(output);
 | 
			
		||||
    pa_assert(out_n_frames);
 | 
			
		||||
 | 
			
		||||
    speex_data = r->impl_data;
 | 
			
		||||
    speex_data = r->impl.data;
 | 
			
		||||
 | 
			
		||||
    in = pa_memblock_acquire_chunk(input);
 | 
			
		||||
    out = pa_memblock_acquire_chunk(output);
 | 
			
		||||
| 
						 | 
				
			
			@ -1476,7 +1472,7 @@ static void speex_resample_int(pa_resampler *r, const pa_memchunk *input, unsign
 | 
			
		|||
    pa_assert(output);
 | 
			
		||||
    pa_assert(out_n_frames);
 | 
			
		||||
 | 
			
		||||
    speex_data = r->impl_data;
 | 
			
		||||
    speex_data = r->impl.data;
 | 
			
		||||
 | 
			
		||||
    in = pa_memblock_acquire_chunk(input);
 | 
			
		||||
    out = pa_memblock_acquire_chunk(output);
 | 
			
		||||
| 
						 | 
				
			
			@ -1494,7 +1490,7 @@ static void speex_update_rates(pa_resampler *r) {
 | 
			
		|||
    struct speex_data *speex_data;
 | 
			
		||||
    pa_assert(r);
 | 
			
		||||
 | 
			
		||||
    speex_data = r->impl_data;
 | 
			
		||||
    speex_data = r->impl.data;
 | 
			
		||||
 | 
			
		||||
    pa_assert_se(speex_resampler_set_rate(speex_data->state, r->i_ss.rate, r->o_ss.rate) == 0);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1503,7 +1499,7 @@ static void speex_reset(pa_resampler *r) {
 | 
			
		|||
    struct speex_data *speex_data;
 | 
			
		||||
    pa_assert(r);
 | 
			
		||||
 | 
			
		||||
    speex_data = r->impl_data;
 | 
			
		||||
    speex_data = r->impl.data;
 | 
			
		||||
 | 
			
		||||
    pa_assert_se(speex_resampler_reset_mem(speex_data->state) == 0);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1512,7 +1508,7 @@ static void speex_free(pa_resampler *r) {
 | 
			
		|||
    struct speex_data *speex_data;
 | 
			
		||||
    pa_assert(r);
 | 
			
		||||
 | 
			
		||||
    speex_data = r->impl_data;
 | 
			
		||||
    speex_data = r->impl.data;
 | 
			
		||||
    if (!speex_data->state)
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1527,21 +1523,21 @@ static int speex_init(pa_resampler *r) {
 | 
			
		|||
 | 
			
		||||
    speex_data = pa_xnew(struct speex_data, 1);
 | 
			
		||||
 | 
			
		||||
    r->impl_free = speex_free;
 | 
			
		||||
    r->impl_update_rates = speex_update_rates;
 | 
			
		||||
    r->impl_reset = speex_reset;
 | 
			
		||||
    r->impl_data = speex_data;
 | 
			
		||||
    r->impl.free = speex_free;
 | 
			
		||||
    r->impl.update_rates = speex_update_rates;
 | 
			
		||||
    r->impl.reset = speex_reset;
 | 
			
		||||
    r->impl.data = speex_data;
 | 
			
		||||
 | 
			
		||||
    if (r->method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->method <= PA_RESAMPLER_SPEEX_FIXED_MAX) {
 | 
			
		||||
 | 
			
		||||
        q = r->method - PA_RESAMPLER_SPEEX_FIXED_BASE;
 | 
			
		||||
        r->impl_resample = speex_resample_int;
 | 
			
		||||
        r->impl.resample = speex_resample_int;
 | 
			
		||||
 | 
			
		||||
    } else {
 | 
			
		||||
        pa_assert(r->method >= PA_RESAMPLER_SPEEX_FLOAT_BASE && r->method <= PA_RESAMPLER_SPEEX_FLOAT_MAX);
 | 
			
		||||
 | 
			
		||||
        q = r->method - PA_RESAMPLER_SPEEX_FLOAT_BASE;
 | 
			
		||||
        r->impl_resample = speex_resample_float;
 | 
			
		||||
        r->impl.resample = speex_resample_float;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pa_log_info("Choosing speex quality setting %i.", q);
 | 
			
		||||
| 
						 | 
				
			
			@ -1566,7 +1562,7 @@ static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned
 | 
			
		|||
    pa_assert(output);
 | 
			
		||||
    pa_assert(out_n_frames);
 | 
			
		||||
 | 
			
		||||
    trivial_data = r->impl_data;
 | 
			
		||||
    trivial_data = r->impl.data;
 | 
			
		||||
    fz = r->w_sz * r->o_ss.channels;
 | 
			
		||||
 | 
			
		||||
    src = pa_memblock_acquire_chunk(input);
 | 
			
		||||
| 
						 | 
				
			
			@ -1604,7 +1600,7 @@ static void trivial_update_rates_or_reset(pa_resampler *r) {
 | 
			
		|||
    struct trivial_data *trivial_data;
 | 
			
		||||
    pa_assert(r);
 | 
			
		||||
 | 
			
		||||
    trivial_data = r->impl_data;
 | 
			
		||||
    trivial_data = r->impl.data;
 | 
			
		||||
 | 
			
		||||
    trivial_data->i_counter = 0;
 | 
			
		||||
    trivial_data->o_counter = 0;
 | 
			
		||||
| 
						 | 
				
			
			@ -1616,10 +1612,10 @@ static int trivial_init(pa_resampler*r) {
 | 
			
		|||
 | 
			
		||||
    trivial_data = pa_xnew0(struct trivial_data, 1);
 | 
			
		||||
 | 
			
		||||
    r->impl_resample = trivial_resample;
 | 
			
		||||
    r->impl_update_rates = trivial_update_rates_or_reset;
 | 
			
		||||
    r->impl_reset = trivial_update_rates_or_reset;
 | 
			
		||||
    r->impl_data = trivial_data;
 | 
			
		||||
    r->impl.resample = trivial_resample;
 | 
			
		||||
    r->impl.update_rates = trivial_update_rates_or_reset;
 | 
			
		||||
    r->impl.reset = trivial_update_rates_or_reset;
 | 
			
		||||
    r->impl.data = trivial_data;
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1637,7 +1633,7 @@ static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned i
 | 
			
		|||
    pa_assert(output);
 | 
			
		||||
    pa_assert(out_n_frames);
 | 
			
		||||
 | 
			
		||||
    peaks_data = r->impl_data;
 | 
			
		||||
    peaks_data = r->impl.data;
 | 
			
		||||
    src = pa_memblock_acquire_chunk(input);
 | 
			
		||||
    dst = pa_memblock_acquire_chunk(output);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1728,7 +1724,7 @@ static void peaks_update_rates_or_reset(pa_resampler *r) {
 | 
			
		|||
    struct peaks_data *peaks_data;
 | 
			
		||||
    pa_assert(r);
 | 
			
		||||
 | 
			
		||||
    peaks_data = r->impl_data;
 | 
			
		||||
    peaks_data = r->impl.data;
 | 
			
		||||
 | 
			
		||||
    peaks_data->i_counter = 0;
 | 
			
		||||
    peaks_data->o_counter = 0;
 | 
			
		||||
| 
						 | 
				
			
			@ -1742,10 +1738,10 @@ static int peaks_init(pa_resampler*r) {
 | 
			
		|||
 | 
			
		||||
    peaks_data = pa_xnew0(struct peaks_data, 1);
 | 
			
		||||
 | 
			
		||||
    r->impl_resample = peaks_resample;
 | 
			
		||||
    r->impl_update_rates = peaks_update_rates_or_reset;
 | 
			
		||||
    r->impl_reset = peaks_update_rates_or_reset;
 | 
			
		||||
    r->impl_data = peaks_data;
 | 
			
		||||
    r->impl.resample = peaks_resample;
 | 
			
		||||
    r->impl.update_rates = peaks_update_rates_or_reset;
 | 
			
		||||
    r->impl.reset = peaks_update_rates_or_reset;
 | 
			
		||||
    r->impl.data = peaks_data;
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1762,7 +1758,7 @@ static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned
 | 
			
		|||
    pa_assert(output);
 | 
			
		||||
    pa_assert(out_n_frames);
 | 
			
		||||
 | 
			
		||||
    ffmpeg_data = r->impl_data;
 | 
			
		||||
    ffmpeg_data = r->impl.data;
 | 
			
		||||
 | 
			
		||||
    for (c = 0; c < r->o_ss.channels; c++) {
 | 
			
		||||
        unsigned u;
 | 
			
		||||
| 
						 | 
				
			
			@ -1831,7 +1827,7 @@ static void ffmpeg_free(pa_resampler *r) {
 | 
			
		|||
 | 
			
		||||
    pa_assert(r);
 | 
			
		||||
 | 
			
		||||
    ffmpeg_data = r->impl_data;
 | 
			
		||||
    ffmpeg_data = r->impl.data;
 | 
			
		||||
    if (ffmpeg_data->state)
 | 
			
		||||
        av_resample_close(ffmpeg_data->state);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1856,9 +1852,9 @@ static int ffmpeg_init(pa_resampler *r) {
 | 
			
		|||
    if (!(ffmpeg_data->state = av_resample_init((int) r->o_ss.rate, (int) r->i_ss.rate, 16, 10, 0, 0.8)))
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    r->impl_free = ffmpeg_free;
 | 
			
		||||
    r->impl_resample = ffmpeg_resample;
 | 
			
		||||
    r->impl_data = (void *) ffmpeg_data;
 | 
			
		||||
    r->impl.free = ffmpeg_free;
 | 
			
		||||
    r->impl.resample = ffmpeg_resample;
 | 
			
		||||
    r->impl.data = (void *) ffmpeg_data;
 | 
			
		||||
 | 
			
		||||
    for (c = 0; c < PA_ELEMENTSOF(ffmpeg_data->buf); c++)
 | 
			
		||||
        pa_memchunk_reset(&ffmpeg_data->buf[c]);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -28,6 +28,15 @@
 | 
			
		|||
#include <pulsecore/memchunk.h>
 | 
			
		||||
 | 
			
		||||
typedef struct pa_resampler pa_resampler;
 | 
			
		||||
typedef struct pa_resampler_impl pa_resampler_impl;
 | 
			
		||||
 | 
			
		||||
struct pa_resampler_impl {
 | 
			
		||||
    void (*free)(pa_resampler *r);
 | 
			
		||||
    void (*update_rates)(pa_resampler *r);
 | 
			
		||||
    void (*resample)(pa_resampler *r, const pa_memchunk *in, unsigned in_samples, pa_memchunk *out, unsigned *out_samples);
 | 
			
		||||
    void (*reset)(pa_resampler *r);
 | 
			
		||||
    void *data;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef enum pa_resample_method {
 | 
			
		||||
    PA_RESAMPLER_INVALID                 = -1,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue