Mercurial > mplayer.hg
annotate libaf/af_resample.c @ 29127:d22a57af2835
some updates about translation maintenance
author | diego |
---|---|
date | Fri, 10 Apr 2009 15:55:38 +0000 |
parents | 8c706ce21c6f |
children | 0f1b5b68af32 |
rev | line source |
---|---|
28229
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
1 /* |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
2 * This audio filter changes the sample rate. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
3 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
4 * Copyright (C) 2002 Anders Johansson ajh@atri.curtin.edu.au |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
5 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
6 * This file is part of MPlayer. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
7 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
8 * MPlayer is free software; you can redistribute it and/or modify |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
11 * (at your option) any later version. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
12 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
13 * MPlayer is distributed in the hope that it will be useful, |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
16 * GNU General Public License for more details. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
17 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
18 * You should have received a copy of the GNU General Public License along |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
19 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
28201
diff
changeset
|
21 */ |
7568 | 22 |
23 #include <stdio.h> | |
24 #include <stdlib.h> | |
25 #include <inttypes.h> | |
26 | |
24889 | 27 #include "libavutil/common.h" |
28322 | 28 #include "libavutil/mathematics.h" |
7568 | 29 #include "af.h" |
30 #include "dsp.h" | |
31 | |
32 /* Below definition selects the length of each poly phase component. | |
33 Valid definitions are L8 and L16, where the number denotes the | |
34 length of the filter. This definition affects the computational | |
35 complexity (see play()), the performance (see filter.h) and the | |
28809 | 36 memory usage. The filter length is chosen to 8 if the machine is |
7568 | 37 slow and to 16 if the machine is fast and has MMX. |
38 */ | |
39 | |
28292
d6001126678f
More #ifdef HAVE_MMX etc. missed by earlier search.
reimar
parents:
28229
diff
changeset
|
40 #if !HAVE_MMX // This machine is slow |
8607 | 41 #define L8 |
42 #else | |
43 #define L16 | |
44 #endif | |
7568 | 45 |
28201
4ff973912251
Rename libaf/af_resample.h to libaf/af_resample_template.c, it is used as
diego
parents:
24900
diff
changeset
|
46 #include "af_resample_template.c" |
7568 | 47 |
8607 | 48 // Filtering types |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
49 #define RSMP_LIN (0<<0) // Linear interpolation |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
50 #define RSMP_INT (1<<0) // 16 bit integer |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
51 #define RSMP_FLOAT (2<<0) // 32 bit floating point |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
52 #define RSMP_MASK (3<<0) |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
53 |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
54 // Defines for sloppy or exact resampling |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
55 #define FREQ_SLOPPY (0<<2) |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
56 #define FREQ_EXACT (1<<2) |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
57 #define FREQ_MASK (1<<2) |
7568 | 58 |
8607 | 59 // Accuracy for linear interpolation |
60 #define STEPACCURACY 32 | |
7568 | 61 |
62 // local data | |
63 typedef struct af_resample_s | |
64 { | |
8607 | 65 void* w; // Current filter weights |
66 void** xq; // Circular buffers | |
7580 | 67 uint32_t xi; // Index for circular buffers |
68 uint32_t wi; // Index for w | |
8607 | 69 uint32_t i; // Number of new samples to put in x queue |
7580 | 70 uint32_t dn; // Down sampling factor |
71 uint32_t up; // Up sampling factor | |
8607 | 72 uint64_t step; // Step size for linear interpolation |
73 uint64_t pt; // Pointer remainder for linear interpolation | |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
74 int setup; // Setup parameters cmdline or through postcreate |
7568 | 75 } af_resample_t; |
76 | |
8607 | 77 // Fast linear interpolation resample with modest audio quality |
78 static int linint(af_data_t* c,af_data_t* l, af_resample_t* s) | |
7568 | 79 { |
8607 | 80 uint32_t len = 0; // Number of input samples |
81 uint32_t nch = l->nch; // Words pre transfer | |
82 uint64_t step = s->step; | |
83 int16_t* in16 = ((int16_t*)c->audio); | |
84 int16_t* out16 = ((int16_t*)l->audio); | |
85 int32_t* in32 = ((int32_t*)c->audio); | |
86 int32_t* out32 = ((int32_t*)l->audio); | |
87 uint64_t end = ((((uint64_t)c->len)/2LL)<<STEPACCURACY); | |
88 uint64_t pt = s->pt; | |
89 uint16_t tmp; | |
90 | |
91 switch (nch){ | |
92 case 1: | |
93 while(pt < end){ | |
94 out16[len++]=in16[pt>>STEPACCURACY]; | |
95 pt+=step; | |
7568 | 96 } |
8607 | 97 s->pt=pt & ((1LL<<STEPACCURACY)-1); |
98 break; | |
99 case 2: | |
100 end/=2; | |
101 while(pt < end){ | |
102 out32[len++]=in32[pt>>STEPACCURACY]; | |
103 pt+=step; | |
104 } | |
105 len=(len<<1); | |
106 s->pt=pt & ((1LL<<STEPACCURACY)-1); | |
107 break; | |
108 default: | |
109 end /=nch; | |
110 while(pt < end){ | |
111 tmp=nch; | |
112 do { | |
113 tmp--; | |
114 out16[len+tmp]=in16[tmp+(pt>>STEPACCURACY)*nch]; | |
115 } while (tmp); | |
116 len+=nch; | |
117 pt+=step; | |
118 } | |
119 s->pt=pt & ((1LL<<STEPACCURACY)-1); | |
7568 | 120 } |
121 return len; | |
122 } | |
123 | |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
124 /* Determine resampling type and format */ |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
125 static int set_types(struct af_instance_s* af, af_data_t* data) |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
126 { |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
127 af_resample_t* s = af->setup; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
128 int rv = AF_OK; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
129 float rd = 0; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
130 |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
131 // Make sure this filter isn't redundant |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
132 if((af->data->rate == data->rate) || (af->data->rate == 0)) |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
133 return AF_DETACH; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
134 /* If sloppy and small resampling difference (2%) */ |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
135 rd = abs((float)af->data->rate - (float)data->rate)/(float)data->rate; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
136 if((((s->setup & FREQ_MASK) == FREQ_SLOPPY) && (rd < 0.02) && |
14245 | 137 (data->format != (AF_FORMAT_FLOAT_NE))) || |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
138 ((s->setup & RSMP_MASK) == RSMP_LIN)){ |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
139 s->setup = (s->setup & ~RSMP_MASK) | RSMP_LIN; |
14245 | 140 af->data->format = AF_FORMAT_S16_NE; |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
141 af->data->bps = 2; |
29049 | 142 mp_msg(MSGT_AFILTER, MSGL_V, "[resample] Using linear interpolation. \n"); |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
143 } |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
144 else{ |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
145 /* If the input format is float or if float is explicitly selected |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
146 use float, otherwise use int */ |
14245 | 147 if((data->format == (AF_FORMAT_FLOAT_NE)) || |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
148 ((s->setup & RSMP_MASK) == RSMP_FLOAT)){ |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
149 s->setup = (s->setup & ~RSMP_MASK) | RSMP_FLOAT; |
14245 | 150 af->data->format = AF_FORMAT_FLOAT_NE; |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
151 af->data->bps = 4; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
152 } |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
153 else{ |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
154 s->setup = (s->setup & ~RSMP_MASK) | RSMP_INT; |
14245 | 155 af->data->format = AF_FORMAT_S16_NE; |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
156 af->data->bps = 2; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
157 } |
29049 | 158 mp_msg(MSGT_AFILTER, MSGL_V, "[resample] Using %s processing and %s frequecy" |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
159 " conversion.\n", |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
160 ((s->setup & RSMP_MASK) == RSMP_FLOAT)?"floating point":"integer", |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
161 ((s->setup & FREQ_MASK) == FREQ_SLOPPY)?"inexact":"exact"); |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
162 } |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
163 |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
164 if(af->data->format != data->format || af->data->bps != data->bps) |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
165 rv = AF_FALSE; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
166 data->format = af->data->format; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
167 data->bps = af->data->bps; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
168 af->data->nch = data->nch; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
169 return rv; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
170 } |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
171 |
7568 | 172 // Initialization and runtime control |
173 static int control(struct af_instance_s* af, int cmd, void* arg) | |
174 { | |
175 switch(cmd){ | |
176 case AF_CONTROL_REINIT:{ | |
28813 | 177 af_resample_t* s = af->setup; |
178 af_data_t* n = arg; // New configuration | |
7568 | 179 int i,d = 0; |
180 int rv = AF_OK; | |
181 | |
28809 | 182 // Free space for circular buffers |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
183 if(s->xq){ |
28810
54230bed6a34
Use a single malloc to allocate space for the circular buffers.
reimar
parents:
28809
diff
changeset
|
184 free(s->xq[0]); |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
185 free(s->xq); |
21017
32d611b59079
Fix double free in af_resample when reinited with suitable parameters
uau
parents:
17366
diff
changeset
|
186 s->xq = NULL; |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
187 } |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
188 |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
189 if(AF_DETACH == (rv = set_types(af,n))) |
7568 | 190 return AF_DETACH; |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
191 |
8607 | 192 // If linear interpolation |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
193 if((s->setup & RSMP_MASK) == RSMP_LIN){ |
8607 | 194 s->pt=0LL; |
195 s->step=((uint64_t)n->rate<<STEPACCURACY)/(uint64_t)af->data->rate+1LL; | |
29049 | 196 mp_msg(MSGT_AFILTER, MSGL_DBG2, "[resample] Linear interpolation step: 0x%016"PRIX64".\n", |
8607 | 197 s->step); |
24888 | 198 af->mul = (double)af->data->rate / n->rate; |
8905 | 199 return rv; |
7568 | 200 } |
201 | |
202 // Calculate up and down sampling factors | |
28303 | 203 d=av_gcd(af->data->rate,n->rate); |
7568 | 204 |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
205 // If sloppy resampling is enabled limit the upsampling factor |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
206 if(((s->setup & FREQ_MASK) == FREQ_SLOPPY) && (af->data->rate/d > 5000)){ |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
207 int up=af->data->rate/2; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
208 int dn=n->rate/2; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
209 int m=2; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
210 while(af->data->rate/(d*m) > 5000){ |
28303 | 211 d=av_gcd(up,dn); |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
212 up/=2; dn/=2; m*=2; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
213 } |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
214 d*=m; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
215 } |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
216 |
28809 | 217 // Create space for circular buffers |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
218 s->xq = malloc(n->nch*sizeof(void*)); |
28811
7c0b23a811bd
Use calloc to allocate the af_resample ring buffers, reportedly using
reimar
parents:
28810
diff
changeset
|
219 s->xq[0] = calloc(n->nch, 2*L*af->data->bps); |
28810
54230bed6a34
Use a single malloc to allocate space for the circular buffers.
reimar
parents:
28809
diff
changeset
|
220 for(i=1;i<n->nch;i++) |
54230bed6a34
Use a single malloc to allocate space for the circular buffers.
reimar
parents:
28809
diff
changeset
|
221 s->xq[i] = (uint8_t *)s->xq[i-1] + 2*L*af->data->bps; |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
222 s->xi = 0; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
223 |
28809 | 224 // Check if the design needs to be redone |
7568 | 225 if(s->up != af->data->rate/d || s->dn != n->rate/d){ |
226 float* w; | |
227 float* wt; | |
228 float fc; | |
229 int j; | |
230 s->up = af->data->rate/d; | |
231 s->dn = n->rate/d; | |
21058
1ed61a0494c4
Reinitialize some variables on af_resample reinit, fixes crash
uau
parents:
21017
diff
changeset
|
232 s->wi = 0; |
1ed61a0494c4
Reinitialize some variables on af_resample reinit, fixes crash
uau
parents:
21017
diff
changeset
|
233 s->i = 0; |
7568 | 234 |
28809 | 235 // Calculate cutoff frequency for filter |
7568 | 236 fc = 1/(float)(max(s->up,s->dn)); |
28809 | 237 // Allocate space for polyphase filter bank and prototype filter |
7568 | 238 w = malloc(sizeof(float) * s->up *L); |
239 if(NULL != s->w) | |
240 free(s->w); | |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
241 s->w = malloc(L*s->up*af->data->bps); |
7568 | 242 |
243 // Design prototype filter type using Kaiser window with beta = 10 | |
244 if(NULL == w || NULL == s->w || | |
14275
de13fd557440
less namespace pollution #2 (prefixed globals in filter.c with af_filter_)
alex
parents:
14245
diff
changeset
|
245 -1 == af_filter_design_fir(s->up*L, w, &fc, LP|KAISER , 10.0)){ |
29049 | 246 mp_msg(MSGT_AFILTER, MSGL_ERR, "[resample] Unable to design prototype filter.\n"); |
7568 | 247 return AF_ERROR; |
248 } | |
249 // Copy data from prototype to polyphase filter | |
250 wt=w; | |
251 for(j=0;j<L;j++){//Columns | |
252 for(i=0;i<s->up;i++){//Rows | |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
253 if((s->setup & RSMP_MASK) == RSMP_INT){ |
8607 | 254 float t=(float)s->up*32767.0*(*wt); |
255 ((int16_t*)s->w)[i*L+j] = (int16_t)((t>=0.0)?(t+0.5):(t-0.5)); | |
256 } | |
257 else | |
258 ((float*)s->w)[i*L+j] = (float)s->up*(*wt); | |
7568 | 259 wt++; |
260 } | |
261 } | |
262 free(w); | |
29049 | 263 mp_msg(MSGT_AFILTER, MSGL_V, "[resample] New filter designed up: %i " |
8607 | 264 "down: %i\n", s->up, s->dn); |
7568 | 265 } |
266 | |
7665
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7616
diff
changeset
|
267 // Set multiplier and delay |
24900 | 268 af->delay = 0; // not set correctly, but shouldn't be too large anyway |
24888 | 269 af->mul = (double)s->up / s->dn; |
7568 | 270 return rv; |
271 } | |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
272 case AF_CONTROL_COMMAND_LINE:{ |
28813 | 273 af_resample_t* s = af->setup; |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
274 int rate=0; |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
275 int type=RSMP_INT; |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
276 int sloppy=1; |
8868
398e3fb7c103
10l bug for float conversion control + feature fix in volume control
anders
parents:
8867
diff
changeset
|
277 sscanf((char*)arg,"%i:%i:%i", &rate, &sloppy, &type); |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
278 s->setup = (sloppy?FREQ_SLOPPY:FREQ_EXACT) | |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
279 (clamp(type,RSMP_LIN,RSMP_FLOAT)); |
8607 | 280 return af->control(af,AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET, &rate); |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
281 } |
8607 | 282 case AF_CONTROL_POST_CREATE: |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
283 if((((af_cfg_t*)arg)->force & AF_INIT_FORMAT_MASK) == AF_INIT_FLOAT) |
8868
398e3fb7c103
10l bug for float conversion control + feature fix in volume control
anders
parents:
8867
diff
changeset
|
284 ((af_resample_t*)af->setup)->setup = RSMP_FLOAT; |
8607 | 285 return AF_OK; |
286 case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET: | |
7568 | 287 // Reinit must be called after this function has been called |
288 | |
289 // Sanity check | |
7616 | 290 if(((int*)arg)[0] < 8000 || ((int*)arg)[0] > 192000){ |
29049 | 291 mp_msg(MSGT_AFILTER, MSGL_ERR, "[resample] The output sample frequency " |
8607 | 292 "must be between 8kHz and 192kHz. Current value is %i \n", |
293 ((int*)arg)[0]); | |
7568 | 294 return AF_ERROR; |
295 } | |
296 | |
297 af->data->rate=((int*)arg)[0]; | |
29049 | 298 mp_msg(MSGT_AFILTER, MSGL_V, "[resample] Changing sample rate " |
8607 | 299 "to %iHz\n",af->data->rate); |
7568 | 300 return AF_OK; |
301 } | |
302 return AF_UNKNOWN; | |
303 } | |
304 | |
305 // Deallocate memory | |
306 static void uninit(struct af_instance_s* af) | |
307 { | |
28812
9dd2e47e5a06
Free af->setup and contents in af_resample uninit function.
reimar
parents:
28811
diff
changeset
|
308 af_resample_t *s = af->setup; |
9dd2e47e5a06
Free af->setup and contents in af_resample uninit function.
reimar
parents:
28811
diff
changeset
|
309 if (s) { |
9dd2e47e5a06
Free af->setup and contents in af_resample uninit function.
reimar
parents:
28811
diff
changeset
|
310 if (s->xq) free(s->xq[0]); |
9dd2e47e5a06
Free af->setup and contents in af_resample uninit function.
reimar
parents:
28811
diff
changeset
|
311 free(s->xq); |
9dd2e47e5a06
Free af->setup and contents in af_resample uninit function.
reimar
parents:
28811
diff
changeset
|
312 free(s->w); |
9dd2e47e5a06
Free af->setup and contents in af_resample uninit function.
reimar
parents:
28811
diff
changeset
|
313 free(s); |
9dd2e47e5a06
Free af->setup and contents in af_resample uninit function.
reimar
parents:
28811
diff
changeset
|
314 } |
7568 | 315 if(af->data) |
22179 | 316 free(af->data->audio); |
317 free(af->data); | |
7568 | 318 } |
319 | |
320 // Filter data through filter | |
321 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
322 { | |
323 int len = 0; // Length of output data | |
324 af_data_t* c = data; // Current working data | |
325 af_data_t* l = af->data; // Local data | |
28813 | 326 af_resample_t* s = af->setup; |
7568 | 327 |
328 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
329 return NULL; | |
330 | |
331 // Run resampling | |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
332 switch(s->setup & RSMP_MASK){ |
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
333 case(RSMP_INT): |
8607 | 334 # define FORMAT_I 1 |
335 if(s->up>s->dn){ | |
336 # define UP | |
28201
4ff973912251
Rename libaf/af_resample.h to libaf/af_resample_template.c, it is used as
diego
parents:
24900
diff
changeset
|
337 # include "af_resample_template.c" |
8607 | 338 # undef UP |
339 } | |
340 else{ | |
341 # define DN | |
28201
4ff973912251
Rename libaf/af_resample.h to libaf/af_resample_template.c, it is used as
diego
parents:
24900
diff
changeset
|
342 # include "af_resample_template.c" |
8607 | 343 # undef DN |
344 } | |
345 break; | |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
346 case(RSMP_FLOAT): |
8607 | 347 # undef FORMAT_I |
348 # define FORMAT_F 1 | |
349 if(s->up>s->dn){ | |
350 # define UP | |
28201
4ff973912251
Rename libaf/af_resample.h to libaf/af_resample_template.c, it is used as
diego
parents:
24900
diff
changeset
|
351 # include "af_resample_template.c" |
8607 | 352 # undef UP |
353 } | |
354 else{ | |
355 # define DN | |
28201
4ff973912251
Rename libaf/af_resample.h to libaf/af_resample_template.c, it is used as
diego
parents:
24900
diff
changeset
|
356 # include "af_resample_template.c" |
8607 | 357 # undef DN |
358 } | |
359 break; | |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
360 case(RSMP_LIN): |
8607 | 361 len = linint(c, l, s); |
362 break; | |
363 } | |
7568 | 364 |
365 // Set output data | |
366 c->audio = l->audio; | |
8607 | 367 c->len = len*l->bps; |
7568 | 368 c->rate = l->rate; |
369 | |
370 return c; | |
371 } | |
372 | |
373 // Allocate memory and set function pointers | |
22746
fd6f824ef894
Rename open to af_open so as not to conflict with a previous header definition.
diego
parents:
22179
diff
changeset
|
374 static int af_open(af_instance_t* af){ |
7568 | 375 af->control=control; |
376 af->uninit=uninit; | |
377 af->play=play; | |
24888 | 378 af->mul=1; |
7568 | 379 af->data=calloc(1,sizeof(af_data_t)); |
380 af->setup=calloc(1,sizeof(af_resample_t)); | |
381 if(af->data == NULL || af->setup == NULL) | |
382 return AF_ERROR; | |
8867
558f0b1f45ee
New auto config for volume and resample and added support for float flag in configuration
anders
parents:
8711
diff
changeset
|
383 ((af_resample_t*)af->setup)->setup = RSMP_INT | FREQ_SLOPPY; |
7568 | 384 return AF_OK; |
385 } | |
386 | |
387 // Description of this plugin | |
388 af_info_t af_info_resample = { | |
389 "Sample frequency conversion", | |
390 "resample", | |
391 "Anders", | |
392 "", | |
7615 | 393 AF_FLAGS_REENTRANT, |
22746
fd6f824ef894
Rename open to af_open so as not to conflict with a previous header definition.
diego
parents:
22179
diff
changeset
|
394 af_open |
7568 | 395 }; |
396 |