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+
20+static 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. */
25+static 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 @@
11 /*
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>
33 **
44 ** This program is free software; you can redistribute it and/or modify
55 ** it under the terms of the GNU General Public License as published by
@@ -24,9 +24,17 @@ · 18 unmodified lines
2424 #include "float_cast.h"
2525 #include "common.h"
2626
27−#define FIP_MAGIC_MARKER MAKE_MAGIC ('f', 'i', 'r', 'p', 'l', 'y')
27+#define FIP_MAGIC_MARKER MAKE_MAGIC ('F', '/', 'I', '/', 'P', ' ')
2828
29−#define IN_BUF_LEN 4096
29+#define BUFFER_LEN 1024
30+
31+typedef struct
32+{ int count ;
33+ int increment ;
34+ const float *c ;
35+} FIP_COEFF_SET ;
36+
37+#include "fip_best.h"
3038
3139 typedef struct
3240 { int fip_magic_marker ;
@@ -37,8 +45,16 @@ typedef struct · 4 unmodified lines
3745
3846 double src_ratio, input_index ;
3947
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. */
4056 float *input_buffer ;
41− int in_buf_len, in_start, in_end ;
57+ int in_buf_len, in_current ;
4258
4359 float *fir_out ;
4460 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
5874 static int fip_process_const_up (FIR_IIR_POLY * fip, SRC_DATA * data) ;
5975
6076
61−static float best_coeffs [250] ;
62−
6377 const char*
6478 fip_get_name (int src_enum)
6579 {
@@ -98,7 +112,7 @@ fip_get_description (int src_enum) · 32 unmodified lines
98112
99113 int
100114 fip_set_converter (SRC_PRIVATE *psrc, int src_enum)
101−{ FIR_IIR_POLY *fip, temp_fip ;
115+{ FIR_IIR_POLY *fip = NULL, temp_fip ;
102116 int buffer_total ;
103117
104118 if (psrc->private_data != NULL)
@@ -111,6 +125,11 @@ fip_set_converter (SRC_PRIVATE *psrc, int src_enum) · 6 unmodified lines
111125
112126 memset (&temp_fip, 0, sizeof (temp_fip)) ;
113127
128+ if ((OFFSETOF (FIR_IIR_POLY, dummy) & 0xF) != 0)
129+ { printf ("(OFFSETOF (FIR_IIR_POLY, dummy) & 0xF) != 0\n") ;
130+ exit (1) ;
131+ } ;
132+
114133 temp_fip.fip_magic_marker = FIP_MAGIC_MARKER ;
115134 temp_fip.channels = psrc->channels ;
116135
@@ -119,23 +138,28 @@ fip_set_converter (SRC_PRIVATE *psrc, int src_enum) · 2 unmodified lines
119138
120139 switch (src_enum)
121140 { 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 ;
123142 break ;
124143 #if 0
125144 case SRC_FIR_IIR_POLY_MEDIUM :
145+ temp_fip.coeff_set = &fip_medium ;
126146 break ;
127147
128148 case SRC_FIR_IIR_POLY_FASTEST :
149+ temp_fip.coeff_set = &fip_fastest ;
129150 break ;
130151 #endif
131152 default :
132153 return SRC_ERR_BAD_CONVERTER ;
133154 } ;
134155
135− temp_fip.fir_out_len = 2 * IN_BUF_LEN ;
136− temp_fip.iir_out_len = 4 * IN_BUF_LEN ;
156+ temp_fip.max_half_coeff_len = SRC_MAX_RATIO * temp_fip.coeff_set->count / temp_fip.coeff_set->increment ;
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 ;
137161
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) ;
139163
140164 if ((fip = calloc (1, sizeof (FIR_IIR_POLY) + sizeof (fip->dummy [0]) * buffer_total)) == NULL)
141165 return SRC_ERR_MALLOC_FAILED ;
@@ -145,9 +169,18 @@ fip_set_converter (SRC_PRIVATE *psrc, int src_enum) · 3 unmodified lines
145169
146170 psrc->private_data = fip ;
147171
148− fip->input_buffer = fip->dummy ;
149− fip->fir_out = fip->dummy + fip->channels * fip->in_buf_len ;
150− fip->iir_out = fip->dummy + fip->channels * (fip->in_buf_len + fip->fir_out_len) ;
172+ /* Allocate buffers. */
173+ fip->coeffs0 = fip->dummy ;
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) ;
151184
152185 fip_reset (psrc) ;
153186
@@ -166,12 +199,14 @@ fip_reset (SRC_PRIVATE *psrc) · 12 unmodified lines
166199 memset (fip->fir_out, 0, fip->channels * fip->fir_out_len * sizeof (fip->dummy [0])) ;
167200 memset (fip->iir_out, 0, fip->channels * fip->iir_out_len * sizeof (fip->dummy [0])) ;
168201
169− fip->in_start = fip->in_end = ARRAY_LEN (best_coeffs) ;
170− fip->fir_start = fip->fir_end = 0 ;
202+ fip->in_current = 0 ;
203+ fip->fir_start = 0 ;
171204 fip->iir_start = fip->iir_end = 0 ;
172205
173206 fip->src_ratio = fip->input_index = 0.0 ;
174207
208+ fip->half_coeff_len = 0 ;
209+
175210 return ;
176211 } /* fip_reset */
177212
@@ -181,6 +216,7 @@ fip_reset (SRC_PRIVATE *psrc) · 3 unmodified lines
181216 static int
182217 fip_process (SRC_PRIVATE *psrc, SRC_DATA *data)
183218 { FIR_IIR_POLY *fip ;
219+ int error ;
184220
185221 if (psrc->private_data == NULL)
186222 return SRC_ERR_NO_PRIVATE ;
@@ -195,23 +231,57 @@ fip_process (SRC_PRIVATE *psrc, SRC_DATA *data) · 8 unmodified lines
195231 if (fip->src_ratio < 1.0 / SRC_MAX_RATIO)
196232 fip->src_ratio = data->src_ratio ;
197233
198− if (fip->src_ratio < 1.0 || data->src_ratio < 1.0)
199− { /* Special case code for constant and varying src_ratio downsampling. */
200− if (fabs (fip->src_ratio - data->src_ratio) > 1e-20)
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) ;
234+ fip->in_count = data->input_frames * fip->channels ;
235+ fip->out_count = data->output_frames * fip->channels ;
236+ fip->in_used = fip->out_gen = 0 ;
208237
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 ;
210254 } /* fip_process */
211255
212256 /*----------------------------------------------------------------------------------------
213257 */
214258
259+static void
260+fip_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+
215285 static int
216286 fip_process_var_down (FIR_IIR_POLY * fip, SRC_DATA * data)
217287 { fip = NULL ;
@@ -222,7 +292,10 @@ fip_process_var_down (FIR_IIR_POLY * fip, SRC_DATA * data) · 4 unmodified lines
222292
223293 static int
224294 fip_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+
226299 data = NULL ;
227300 printf ("%s : not implemented yet.\n", __func__) ;
228301 return SRC_ERR_NO_ERROR ;
@@ -230,7 +303,10 @@ fip_process_const_down (FIR_IIR_POLY * fip, SRC_DATA * data) · 1 unmodified line
230303
231304 static int
232305 fip_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+
234310 data = NULL ;
235311 printf ("%s : not implemented yet.\n", __func__) ;
236312 return SRC_ERR_NO_ERROR ;
@@ -238,9 +314,35 @@ fip_process_var_up (FIR_IIR_POLY * fip, SRC_DATA * data) · 1 unmodified line
238314
239315 static int
240316 fip_process_const_up (FIR_IIR_POLY * fip, SRC_DATA * data)
241−{ fip = NULL ;
242− data = NULL ;
243− printf ("%s : not implemented yet.\n", __func__) ;
317+{ long max_in_used ;
318+ int ch, indx ;
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+
244346 return SRC_ERR_NO_ERROR ;
245347 } /* fip_process_const_up */
246348

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