| @@ -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 | 4 | ** This program is free software; you can redistribute it and/or modify |
| 5 | 5 | ** it under the terms of the GNU General Public License as published by |
| @@ -24,9 +24,17 @@ · 18 unmodified lines |
| 24 | 24 | #include "float_cast.h" |
| 25 | 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 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" |
| 30 | 38 | |
| 31 | 39 | typedef struct |
| 32 | 40 | { int fip_magic_marker ; |
| @@ -37,8 +45,16 @@ typedef struct · 4 unmodified lines |
| 37 | 45 | |
| 38 | 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 | 56 | float *input_buffer ; |
| 41 | | − int in_buf_len, in_start, in_end ; |
| 57 | + int in_buf_len, in_current ; |
| 42 | 58 | |
| 43 | 59 | float *fir_out ; |
| 44 | 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 |
| 58 | 74 | static int fip_process_const_up (FIR_IIR_POLY * fip, SRC_DATA * data) ; |
| 59 | 75 | |
| 60 | 76 | |
| 61 | | −static float best_coeffs [250] ; |
| 62 | | − |
| 63 | 77 | const char* |
| 64 | 78 | fip_get_name (int src_enum) |
| 65 | 79 | { |
| @@ -98,7 +112,7 @@ fip_get_description (int src_enum) · 32 unmodified lines |
| 98 | 112 | |
| 99 | 113 | int |
| 100 | 114 | fip_set_converter (SRC_PRIVATE *psrc, int src_enum) |
| 101 | | −{ FIR_IIR_POLY *fip, temp_fip ; |
| 115 | +{ FIR_IIR_POLY *fip = NULL, temp_fip ; |
| 102 | 116 | int buffer_total ; |
| 103 | 117 | |
| 104 | 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 | 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 | 133 | temp_fip.fip_magic_marker = FIP_MAGIC_MARKER ; |
| 115 | 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 | 139 | switch (src_enum) |
| 121 | 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 | 142 | break ; |
| 124 | 143 | #if 0 |
| 125 | 144 | case SRC_FIR_IIR_POLY_MEDIUM : |
| 145 | + temp_fip.coeff_set = &fip_medium ; |
| 126 | 146 | break ; |
| 127 | 147 | |
| 128 | 148 | case SRC_FIR_IIR_POLY_FASTEST : |
| 149 | + temp_fip.coeff_set = &fip_fastest ; |
| 129 | 150 | break ; |
| 130 | 151 | #endif |
| 131 | 152 | default : |
| 132 | 153 | return SRC_ERR_BAD_CONVERTER ; |
| 133 | 154 | } ; |
| 134 | 155 | |
| 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 ; |
| 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 | 164 | if ((fip = calloc (1, sizeof (FIR_IIR_POLY) + sizeof (fip->dummy [0]) * buffer_total)) == NULL) |
| 141 | 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 | 170 | psrc->private_data = fip ; |
| 147 | 171 | |
| 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) ; |
| 151 | 184 | |
| 152 | 185 | fip_reset (psrc) ; |
| 153 | 186 | |
| @@ -166,12 +199,14 @@ fip_reset (SRC_PRIVATE *psrc) · 12 unmodified lines |
| 166 | 199 | memset (fip->fir_out, 0, fip->channels * fip->fir_out_len * sizeof (fip->dummy [0])) ; |
| 167 | 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) ; |
| 170 | | − fip->fir_start = fip->fir_end = 0 ; |
| 202 | + fip->in_current = 0 ; |
| 203 | + fip->fir_start = 0 ; |
| 171 | 204 | fip->iir_start = fip->iir_end = 0 ; |
| 172 | 205 | |
| 173 | 206 | fip->src_ratio = fip->input_index = 0.0 ; |
| 174 | 207 | |
| 208 | + fip->half_coeff_len = 0 ; |
| 209 | + |
| 175 | 210 | return ; |
| 176 | 211 | } /* fip_reset */ |
| 177 | 212 | |
| @@ -181,6 +216,7 @@ fip_reset (SRC_PRIVATE *psrc) · 3 unmodified lines |
| 181 | 216 | static int |
| 182 | 217 | fip_process (SRC_PRIVATE *psrc, SRC_DATA *data) |
| 183 | 218 | { FIR_IIR_POLY *fip ; |
| 219 | + int error ; |
| 184 | 220 | |
| 185 | 221 | if (psrc->private_data == NULL) |
| 186 | 222 | return SRC_ERR_NO_PRIVATE ; |
| @@ -195,23 +231,57 @@ fip_process (SRC_PRIVATE *psrc, SRC_DATA *data) · 8 unmodified lines |
| 195 | 231 | if (fip->src_ratio < 1.0 / SRC_MAX_RATIO) |
| 196 | 232 | fip->src_ratio = data->src_ratio ; |
| 197 | 233 | |
| 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 ; |
| 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 | 254 | } /* fip_process */ |
| 211 | 255 | |
| 212 | 256 | /*---------------------------------------------------------------------------------------- |
| 213 | 257 | */ |
| 214 | 258 | |
| 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 | + |
| 215 | 285 | static int |
| 216 | 286 | fip_process_var_down (FIR_IIR_POLY * fip, SRC_DATA * data) |
| 217 | 287 | { fip = NULL ; |
| @@ -222,7 +292,10 @@ fip_process_var_down (FIR_IIR_POLY * fip, SRC_DATA * data) · 4 unmodified lines |
| 222 | 292 | |
| 223 | 293 | static int |
| 224 | 294 | 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 | + |
| 226 | 299 | data = NULL ; |
| 227 | 300 | printf ("%s : not implemented yet.\n", __func__) ; |
| 228 | 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 | |
| 231 | 304 | static int |
| 232 | 305 | 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 | + |
| 234 | 310 | data = NULL ; |
| 235 | 311 | printf ("%s : not implemented yet.\n", __func__) ; |
| 236 | 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 | |
| 239 | 315 | static int |
| 240 | 316 | 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 | + |
| 244 | 346 | return SRC_ERR_NO_ERROR ; |
| 245 | 347 | } /* fip_process_const_up */ |
| 246 | 348 | |