| 1 |
/* |
| 2 |
filters: parse filter specifications |
| 3 |
|
| 4 |
copyright 2020-2023 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 5 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 6 |
initially written by Thomas Orgis |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "filters.h" |
| 10 |
#include <ctype.h> |
| 11 |
|
| 12 |
#include "common/debug.h" |
| 13 |
|
| 14 |
// Validate syntax of filter specification, returning the number |
| 15 |
// of configured filters. |
| 16 |
// b_0,...,b_N,a_0,...,a_N:b_0,...,b_N,a_0,...,a_N |
| 17 |
// 1. An even number, at least two of coefficients for each |
| 18 |
// 2. Numbers (decimal, scientific notation ...) separated by comma. |
| 19 |
// 3. Spacing optional. |
| 20 |
// If there's a non-empty spec, getting zero filters out of it is the error. |
| 21 |
// Total number of coefficients of successfully parsed filters is optionally |
| 22 |
// returned, too. |
| 23 |
static size_t validate_filterspec(const char *spec, size_t *coeff_total) |
| 24 |
{ |
| 25 |
size_t count = 0; |
| 26 |
size_t ncoeff = 0; |
| 27 |
size_t totcoeff = 0; |
| 28 |
while(*spec) |
| 29 |
{ |
| 30 |
char *nspec = (char*)spec;; |
| 31 |
errno = 0; |
| 32 |
strtod(spec, &nspec); |
| 33 |
if(errno) |
| 34 |
{ |
| 35 |
merror("Bad number in filter spec, here: %s", spec); |
| 36 |
return 0; |
| 37 |
} |
| 38 |
if(nspec == spec) |
| 39 |
{ |
| 40 |
merror("Parser did not advance on: %s", spec); |
| 41 |
return 0; |
| 42 |
} |
| 43 |
spec += nspec-spec; |
| 44 |
++ncoeff; |
| 45 |
++totcoeff; |
| 46 |
while(isspace(*spec)) ++spec; |
| 47 |
// Increment number of coefficients if one shall follow, |
| 48 |
// increment number of filters if one shall follow. |
| 49 |
if(*spec == ',') |
| 50 |
++spec; |
| 51 |
else if(*spec == ':') |
| 52 |
{ |
| 53 |
++spec; |
| 54 |
if(!ncoeff || ncoeff % 2) |
| 55 |
break; |
| 56 |
ncoeff = 0; |
| 57 |
++count; |
| 58 |
} |
| 59 |
} |
| 60 |
if(coeff_total) |
| 61 |
*coeff_total = totcoeff; |
| 62 |
// If we collected enough coefficients until end of string, we got |
| 63 |
// another filter. |
| 64 |
if(!ncoeff || ncoeff%2) |
| 65 |
{ |
| 66 |
merror("Bad number of coefficients in final filter spec: %zu", ncoeff); |
| 67 |
return 0; |
| 68 |
} else |
| 69 |
return ++count; |
| 70 |
} |
| 71 |
|
| 72 |
// Storage of the filters must succeed as we validated the spec. |
| 73 |
// Still playing safe where things would massively go wrong. |
| 74 |
static int store_filters(struct filterlist *fl, const char *spec) |
| 75 |
{ |
| 76 |
size_t ci = 0; // offset in global coefficient list |
| 77 |
errno = 0; |
| 78 |
for(size_t fi=0; fi<fl->count; ++fi) |
| 79 |
{ |
| 80 |
size_t fcoeffs = 0; // coefficients for this filter, 2*(order+1) |
| 81 |
while(!errno && *spec && *spec != ':') |
| 82 |
{ |
| 83 |
char *nspec; |
| 84 |
fl->coeff[ci+fcoeffs] = strtod(spec, &nspec); |
| 85 |
fcoeffs++; |
| 86 |
spec += nspec-spec; |
| 87 |
if(*spec == ',') |
| 88 |
++spec; |
| 89 |
while(isspace(*spec)) |
| 90 |
++spec; |
| 91 |
} |
| 92 |
if(errno) |
| 93 |
{ |
| 94 |
merror("Number parsing error on validated spec: %s", INT123_strerror(errno)); |
| 95 |
return -1; |
| 96 |
} |
| 97 |
if(*spec) // Skip ":" |
| 98 |
++spec; |
| 99 |
if(fcoeffs < 2) |
| 100 |
{ |
| 101 |
merror("Bad coefficient count for filter %zu on validated spec.", fi); |
| 102 |
fl->f[fi].order = 0; |
| 103 |
fl->f[fi].b = fl->f[fi].a = NULL; |
| 104 |
return -1; |
| 105 |
} |
| 106 |
fl->f[fi].order = fcoeffs/2-1; |
| 107 |
fl->f[fi].b = fl->coeff + ci; |
| 108 |
fl->f[fi].a = fl->coeff + ci+fl->f[fi].order + 1; |
| 109 |
ci+=fcoeffs; |
| 110 |
} |
| 111 |
if(ci != fl->coeff_total) |
| 112 |
{ |
| 113 |
error("Mismatch in total coefficient count."); |
| 114 |
return -1; |
| 115 |
} |
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
void free_filterlist(struct filterlist *fl) |
| 120 |
{ |
| 121 |
if(!fl) |
| 122 |
return; |
| 123 |
if(fl->coeff) |
| 124 |
free(fl->coeff); |
| 125 |
if(fl->f) |
| 126 |
free(fl->f); |
| 127 |
} |
| 128 |
|
| 129 |
struct filterlist* parse_filterspec(const char *spec) |
| 130 |
{ |
| 131 |
struct filterlist *fl = NULL; |
| 132 |
size_t count, coeffs; |
| 133 |
count = validate_filterspec(spec, &coeffs); |
| 134 |
if(count) |
| 135 |
{ |
| 136 |
fl = malloc(sizeof(*fl)); |
| 137 |
if(coeffs > UINT_MAX) |
| 138 |
{ |
| 139 |
merror("Too many filter coefficients: %zu", coeffs); |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
} |
| 143 |
mdebug("%zu filters, %zu coefficients, fl=%p", count, count ? coeffs : 0, (void*)fl); |
| 144 |
if(fl) |
| 145 |
{ |
| 146 |
fl->count = count; |
| 147 |
fl->coeff_total = coeffs; |
| 148 |
fl->coeff = malloc(sizeof(*fl->coeff)*coeffs); |
| 149 |
fl->f = malloc(sizeof(*fl->f)*count); |
| 150 |
if(!fl->coeff || !fl->f) |
| 151 |
{ |
| 152 |
if(fl->coeff) |
| 153 |
free(fl->coeff); |
| 154 |
if(fl->f) |
| 155 |
free(fl->f); |
| 156 |
free(fl); |
| 157 |
fl = NULL; |
| 158 |
} |
| 159 |
} |
| 160 |
if(fl) |
| 161 |
{ |
| 162 |
if(store_filters(fl, spec)) |
| 163 |
{ |
| 164 |
error("Storage failed after validaton (impossible!)"); |
| 165 |
free_filterlist(fl); |
| 166 |
fl = NULL; |
| 167 |
} |
| 168 |
} |
| 169 |
return fl; |
| 170 |
} |
| 171 |
|