History

Update FIP implementation and add fip_best.h.

Erik de Castro Lopo committed 22 years ago

Commit d981d311 parent 66946a62 files · +177 −32Browse files

Unified · Split

2 files
  1. src/fip_best.h +43 −0
  2. src/src_fir_iir_poly.c +134 −32
src/fip_best.h+43−0
@@ -0,0 +1,43 @@
1/*
2** Copyright (C) 2004 Erik de Castro Lopo <erikd@mega-nerd.com>
3**
4** This program is free software; you can redistribute it and/or modify
5** it under the terms of the GNU General Public License as published by
6** the Free Software Foundation; either version 2 of the License, or
7** (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12** GNU General Public License for more details.
13**
14** You should have received a copy of the GNU General Public License
15** along with this program; if not, write to the Free Software
16** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19
20static const float fip_best_coeffs [250] =
21{ 0.0, 1.2, 2.3
22} ;
23
24/* FIP_COEFF_SET needs to be defined before this header is included. */
25static const FIP_COEFF_SET fip_best =
26{
27 /* count */
28 ARRAY_LEN (fip_best_coeffs),
29
30 /* increment */
31 1,
32
33 /* c */
34 fip_best_coeffs
35} ;
36
37/*
38** Do not edit or modify anything in this comment block.
39** The following line is a file identity tag for the GNU Arch
40** revision control system.
41**
42** arch-tag: 86739f0c-c446-45f9-8ad0-35b6903c6135
43*/
src/src_fir_iir_poly.c+134−32
@@ -1,5 +1,5 @@
1/*1/*
2** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>2** Copyright (C) 2004 Erik de Castro Lopo <erikd@mega-nerd.com>
3**3**
4** This program is free software; you can redistribute it and/or modify4** This program is free software; you can redistribute it and/or modify
5** it under the terms of the GNU General Public License as published by5** it under the terms of the GNU General Public License as published by
@@ -24,9 +24,17 @@ · 18 unmodified lines
24#include "float_cast.h"24#include "float_cast.h"
25#include "common.h"25#include "common.h"
26 26
27#define FIP_MAGIC_MARKER MAKE_MAGIC ('f', 'i', 'r', 'p', 'l', 'y')27#define FIP_MAGIC_MARKER MAKE_MAGIC ('F', '/', 'I', '/', 'P', ' ')
28 28
29#define IN_BUF_LEN 409629#define BUFFER_LEN 1024
30
31typedef struct
32{ int count ;
33 int increment ;
34 const float *c ;
35} FIP_COEFF_SET ;
36
37#include "fip_best.h"
30 38
31typedef struct39typedef struct
32{ int fip_magic_marker ;40{ int fip_magic_marker ;
@@ -37,8 +45,16 @@ typedef struct · 4 unmodified lines
37 45
38 double src_ratio, input_index ;46 double src_ratio, input_index ;
39 47
48 /* The raw coeff set for this converter. */
49 const FIP_COEFF_SET *coeff_set ;
50
51 /* The current FIR coefficents for the times 2 upsampler. */
52 float *coeffs0, * coeffs1 ;
53 int max_half_coeff_len, half_coeff_len ;
54
55 /* Buffer definitions. */
40 float *input_buffer ;56 float *input_buffer ;
41 int in_buf_len, in_start, in_end ;57 int in_buf_len, in_current ;
42 58
43 float *fir_out ;59 float *fir_out ;
44 int fir_out_len, fir_start, fir_end ;60 int fir_out_len, fir_start, fir_end ;
@@ -58,8 +74,6 @@ static int fip_process_var_up (FIR_IIR_POLY * fip, SRC_DATA * data) ; · 13 unmodified lines
58static int fip_process_const_up (FIR_IIR_POLY * fip, SRC_DATA * data) ;74static int fip_process_const_up (FIR_IIR_POLY * fip, SRC_DATA * data) ;
59 75
60 76
61static float best_coeffs [250] ;
62
63const char*77const char*
64fip_get_name (int src_enum)78fip_get_name (int src_enum)
65{79{
@@ -98,7 +112,7 @@ fip_get_description (int src_enum) · 32 unmodified lines
98 112
99int113int
100fip_set_converter (SRC_PRIVATE *psrc, int src_enum)114fip_set_converter (SRC_PRIVATE *psrc, int src_enum)
101{ FIR_IIR_POLY *fip, temp_fip ;115{ FIR_IIR_POLY *fip = NULL, temp_fip ;
102 int buffer_total ;116 int buffer_total ;
103 117
104 if (psrc->private_data != NULL)118 if (psrc->private_data != NULL)
@@ -111,6 +125,11 @@ fip_set_converter (SRC_PRIVATE *psrc, int src_enum) · 6 unmodified lines
111 125
112 memset (&temp_fip, 0, sizeof (temp_fip)) ;126 memset (&temp_fip, 0, sizeof (temp_fip)) ;
113 127
128 if ((OFFSETOF (FIR_IIR_POLY, dummy) & 0xF) != 0)
129 { printf ("(OFFSETOF (FIR_IIR_POLY, dummy) & 0xF) != 0\n") ;
130 exit (1) ;
131 } ;
132
114 temp_fip.fip_magic_marker = FIP_MAGIC_MARKER ;133 temp_fip.fip_magic_marker = FIP_MAGIC_MARKER ;
115 temp_fip.channels = psrc->channels ;134 temp_fip.channels = psrc->channels ;
116 135
@@ -119,23 +138,28 @@ fip_set_converter (SRC_PRIVATE *psrc, int src_enum) · 2 unmodified lines
119 138
120 switch (src_enum)139 switch (src_enum)
121 { case SRC_FIR_IIR_POLY_BEST :140 { case SRC_FIR_IIR_POLY_BEST :
122 temp_fip.in_buf_len = IN_BUF_LEN + 2 * ARRAY_LEN (best_coeffs) ;141 temp_fip.coeff_set = &fip_best ;
123 break ;142 break ;
124#if 0143#if 0
125 case SRC_FIR_IIR_POLY_MEDIUM :144 case SRC_FIR_IIR_POLY_MEDIUM :
145 temp_fip.coeff_set = &fip_medium ;
126 break ;146 break ;
127 147
128 case SRC_FIR_IIR_POLY_FASTEST :148 case SRC_FIR_IIR_POLY_FASTEST :
149 temp_fip.coeff_set = &fip_fastest ;
129 break ;150 break ;
130#endif151#endif
131 default :152 default :
132 return SRC_ERR_BAD_CONVERTER ;153 return SRC_ERR_BAD_CONVERTER ;
133 } ;154 } ;
134 155
135 temp_fip.fir_out_len = 2 * IN_BUF_LEN ;156 temp_fip.max_half_coeff_len = SRC_MAX_RATIO * temp_fip.coeff_set->count / temp_fip.coeff_set->increment ;
136 temp_fip.iir_out_len = 4 * IN_BUF_LEN ;157 temp_fip.in_buf_len = temp_fip.channels * 2 * temp_fip.max_half_coeff_len ;
158
159 temp_fip.fir_out_len = BUFFER_LEN ;
160 temp_fip.iir_out_len = 2 * BUFFER_LEN ;
137 161
138 buffer_total = psrc->channels * (temp_fip.in_buf_len + temp_fip.fir_out_len + temp_fip.iir_out_len) ;162 buffer_total = 4 + temp_fip.max_half_coeff_len + psrc->channels * (temp_fip.in_buf_len + temp_fip.fir_out_len + temp_fip.iir_out_len) ;
139 163
140 if ((fip = calloc (1, sizeof (FIR_IIR_POLY) + sizeof (fip->dummy [0]) * buffer_total)) == NULL)164 if ((fip = calloc (1, sizeof (FIR_IIR_POLY) + sizeof (fip->dummy [0]) * buffer_total)) == NULL)
141 return SRC_ERR_MALLOC_FAILED ;165 return SRC_ERR_MALLOC_FAILED ;
@@ -145,9 +169,18 @@ fip_set_converter (SRC_PRIVATE *psrc, int src_enum) · 3 unmodified lines
145 169
146 psrc->private_data = fip ;170 psrc->private_data = fip ;
147 171
148 fip->input_buffer = fip->dummy ;172 /* Allocate buffers. */
149 fip->fir_out = fip->dummy + fip->channels * fip->in_buf_len ;173 fip->coeffs0 = fip->dummy ;
150 fip->iir_out = fip->dummy + fip->channels * (fip->in_buf_len + fip->fir_out_len) ;174 while ((((unsigned long) fip->coeffs0) & 0xF) > 0)
175 fip->coeffs0 ++ ;
176
177 fip->coeffs1 = fip->coeffs0 + fip->max_half_coeff_len ;
178 fip->input_buffer = fip->coeffs1 + fip->max_half_coeff_len ;
179
180 fip->fir_out = fip->input_buffer + fip->channels * fip->in_buf_len ;
181 fip->iir_out = fip->fir_out + fip->channels * fip->fir_out_len ;
182
183 printf ("total : %d / %d\n", (fip->iir_out + fip->iir_out_len) - fip->dummy, buffer_total) ;
151 184
152 fip_reset (psrc) ;185 fip_reset (psrc) ;
153 186
@@ -166,12 +199,14 @@ fip_reset (SRC_PRIVATE *psrc) · 12 unmodified lines
166 memset (fip->fir_out, 0, fip->channels * fip->fir_out_len * sizeof (fip->dummy [0])) ;199 memset (fip->fir_out, 0, fip->channels * fip->fir_out_len * sizeof (fip->dummy [0])) ;
167 memset (fip->iir_out, 0, fip->channels * fip->iir_out_len * sizeof (fip->dummy [0])) ;200 memset (fip->iir_out, 0, fip->channels * fip->iir_out_len * sizeof (fip->dummy [0])) ;
168 201
169 fip->in_start = fip->in_end = ARRAY_LEN (best_coeffs) ;202 fip->in_current = 0 ;
170 fip->fir_start = fip->fir_end = 0 ;203 fip->fir_start = 0 ;
171 fip->iir_start = fip->iir_end = 0 ;204 fip->iir_start = fip->iir_end = 0 ;
172 205
173 fip->src_ratio = fip->input_index = 0.0 ;206 fip->src_ratio = fip->input_index = 0.0 ;
174 207
208 fip->half_coeff_len = 0 ;
209
175 return ;210 return ;
176} /* fip_reset */211} /* fip_reset */
177 212
@@ -181,6 +216,7 @@ fip_reset (SRC_PRIVATE *psrc) · 3 unmodified lines
181static int216static int
182fip_process (SRC_PRIVATE *psrc, SRC_DATA *data)217fip_process (SRC_PRIVATE *psrc, SRC_DATA *data)
183{ FIR_IIR_POLY *fip ;218{ FIR_IIR_POLY *fip ;
219 int error ;
184 220
185 if (psrc->private_data == NULL)221 if (psrc->private_data == NULL)
186 return SRC_ERR_NO_PRIVATE ;222 return SRC_ERR_NO_PRIVATE ;
@@ -195,23 +231,57 @@ fip_process (SRC_PRIVATE *psrc, SRC_DATA *data) · 8 unmodified lines
195 if (fip->src_ratio < 1.0 / SRC_MAX_RATIO)231 if (fip->src_ratio < 1.0 / SRC_MAX_RATIO)
196 fip->src_ratio = data->src_ratio ;232 fip->src_ratio = data->src_ratio ;
197 233
198 if (fip->src_ratio < 1.0 || data->src_ratio < 1.0)234 fip->in_count = data->input_frames * fip->channels ;
199 { /* Special case code for constant and varying src_ratio downsampling. */235 fip->out_count = data->output_frames * fip->channels ;
200 if (fabs (fip->src_ratio - data->src_ratio) > 1e-20)236 fip->in_used = fip->out_gen = 0 ;
201 return fip_process_var_down (fip, data) ;
202
203 return fip_process_const_down (fip, data) ;
204 } ;
205
206 if (fabs (fip->src_ratio - data->src_ratio) > 1e-20)
207 return fip_process_var_up (fip, data) ;
208 237
209 return fip_process_const_up (fip, data) ;238 /* Choose a more specialised process() function. */
239 if (fip->src_ratio < 1.0 || data->src_ratio < 1.0)
240 { if (fabs (fip->src_ratio - data->src_ratio) > 1e-20)
241 error = fip_process_var_down (fip, data) ;
242 else
243 error = fip_process_const_down (fip, data) ;
244 }
245 else if (fabs (fip->src_ratio - data->src_ratio) > 1e-20)
246 error = fip_process_var_up (fip, data) ;
247 else
248 error = fip_process_const_up (fip, data) ;
249
250 data->input_frames_used = fip->in_used / fip->channels ;
251 data->output_frames_gen = fip->out_gen / fip->channels ;
252
253 return error ;
210} /* fip_process */254} /* fip_process */
211 255
212/*----------------------------------------------------------------------------------------256/*----------------------------------------------------------------------------------------
213*/257*/
214 258
259static void
260fip_generate_fir_current_coeffs (FIR_IIR_POLY * fip)
261{ int k ;
262
263 if (fip->src_ratio >= 1.0)
264 { fip->half_coeff_len = fip->coeff_set->count / fip->coeff_set->increment / 2 ;
265
266 for (k = 0 ; k < fip->half_coeff_len ; k++)
267 { fip->coeffs0 [k] = fip->coeff_set->c [fip->coeff_set->increment * k] ;
268 fip->coeffs1 [k] = fip->coeff_set->c [fip->coeff_set->increment * k + fip->coeff_set->increment / 2] ;
269 } ;
270
271 /* Round it up to a multiple of 4. */
272 while (fip->half_coeff_len & 3)
273 { fip->coeffs0 [fip->half_coeff_len] = 0.0 ;
274 fip->coeffs1 [fip->half_coeff_len] = 0.0 ;
275 fip->half_coeff_len ++ ;
276 } ;
277
278 return ;
279 } ;
280
281 printf ("%s : not implemented yet.\n", __func__) ;
282 exit (1) ;
283} /* fip_generate_fir_current_coeffs */
284
215static int285static int
216fip_process_var_down (FIR_IIR_POLY * fip, SRC_DATA * data)286fip_process_var_down (FIR_IIR_POLY * fip, SRC_DATA * data)
217{ fip = NULL ;287{ fip = NULL ;
@@ -222,7 +292,10 @@ fip_process_var_down (FIR_IIR_POLY * fip, SRC_DATA * data) · 4 unmodified lines
222 292
223static int293static int
224fip_process_const_down (FIR_IIR_POLY * fip, SRC_DATA * data)294fip_process_const_down (FIR_IIR_POLY * fip, SRC_DATA * data)
225{ fip = NULL ;295{
296 if (fip->half_coeff_len == 0)
297 fip_generate_fir_current_coeffs (fip) ;
298
226 data = NULL ;299 data = NULL ;
227 printf ("%s : not implemented yet.\n", __func__) ;300 printf ("%s : not implemented yet.\n", __func__) ;
228 return SRC_ERR_NO_ERROR ;301 return SRC_ERR_NO_ERROR ;
@@ -230,7 +303,10 @@ fip_process_const_down (FIR_IIR_POLY * fip, SRC_DATA * data) · 1 unmodified line
230 303
231static int304static int
232fip_process_var_up (FIR_IIR_POLY * fip, SRC_DATA * data)305fip_process_var_up (FIR_IIR_POLY * fip, SRC_DATA * data)
233{ fip = NULL ;306{
307 if (fip->half_coeff_len == 0)
308 fip_generate_fir_current_coeffs (fip) ;
309
234 data = NULL ;310 data = NULL ;
235 printf ("%s : not implemented yet.\n", __func__) ;311 printf ("%s : not implemented yet.\n", __func__) ;
236 return SRC_ERR_NO_ERROR ;312 return SRC_ERR_NO_ERROR ;
@@ -238,9 +314,35 @@ fip_process_var_up (FIR_IIR_POLY * fip, SRC_DATA * data) · 1 unmodified line
238 314
239static int315static int
240fip_process_const_up (FIR_IIR_POLY * fip, SRC_DATA * data)316fip_process_const_up (FIR_IIR_POLY * fip, SRC_DATA * data)
241{ fip = NULL ;317{ long max_in_used ;
242 data = NULL ;318 int ch, indx ;
243 printf ("%s : not implemented yet.\n", __func__) ;319
320 /* If we don't yet have the coeffs, generate them. */
321 if (fip->half_coeff_len == 0)
322 fip_generate_fir_current_coeffs (fip) ;
323
324 max_in_used = lrintf (fip->in_count / fip->src_ratio - 0.5) ;
325 max_in_used -= max_in_used & 1 ;
326
327 if (fip->channels != 1)
328 { printf ("%s : fip->channels != 1\n", __func__) ;
329 exit (1) ;
330 }
331
332 /* Main processing loop. */
333 while (fip->out_gen < fip->out_count && fip->in_used < max_in_used)
334 {
335 for (ch = 0 ; ch < fip->channels ; ch ++)
336 { indx = (fip->in_current + fip->half_coeff_len + ch) % fip->in_buf_len ;
337 fip->input_buffer [indx] = data->data_in [fip->in_used ++] ;
338 } ;
339
340
341
342
343 fip->in_current = (fip->in_current + 1) % fip->in_buf_len ;
344 } ;
345
244 return SRC_ERR_NO_ERROR ;346 return SRC_ERR_NO_ERROR ;
245} /* fip_process_const_up */347} /* fip_process_const_up */
246 348

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t