annotate libaf/af_resample.c @ 7820:125515a0c8ed

XviD needs some more header files. Hint provided by Falk Richter and Anders Rune Jensen <anders@gnulinux.dk>.
author diego
date Mon, 21 Oct 2002 01:57:23 +0000
parents 41e8d0916c60
children 3aba91eb5c1f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
1 /*=============================================================================
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
2 //
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
3 // This software has been released under the terms of the GNU Public
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
4 // license. See http://www.gnu.org/copyleft/gpl.html for details.
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
5 //
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
6 // Copyright 2002 Anders Johansson ajh@atri.curtin.edu.au
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
7 //
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
8 //=============================================================================
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
9 */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
10
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
11 /* This audio filter changes the sample rate. */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
12
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
13 #define PLUGIN
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
14
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
15 #include <stdio.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
16 #include <stdlib.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
17 #include <unistd.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
18 #include <inttypes.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
19
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
20 #include "../config.h"
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
21 #include "../mp_msg.h"
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
22 #include "../libao2/afmt.h"
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
23
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
24 #include "af.h"
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
25 #include "dsp.h"
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
26
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
27 /* Below definition selects the length of each poly phase component.
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
28 Valid definitions are L8 and L16, where the number denotes the
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
29 length of the filter. This definition affects the computational
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
30 complexity (see play()), the performance (see filter.h) and the
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
31 memory usage. The filterlenght is choosen to 8 if the machine is
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
32 slow and to 16 if the machine is fast and has MMX.
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
33 */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
34
7588
arpi
parents: 7587
diff changeset
35 #if !defined(HAVE_SSE) && !defined(HAVE_3DNOW) // This machine is slow
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
36
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
37 #define L 8 // Filter length
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
38 // Unrolled loop to speed up execution
7602
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
39 #define FIR(x,w,y) \
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
40 (y[0]) = ( w[0]*x[0]+w[1]*x[1]+w[2]*x[2]+w[3]*x[3] \
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
41 + w[4]*x[4]+w[5]*x[5]+w[6]*x[6]+w[7]*x[7] ) >> 16
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
42
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
43 #else /* Fast machine */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
44
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
45 #define L 16
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
46 // Unrolled loop to speed up execution
7602
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
47 #define FIR(x,w,y) \
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
48 y[0] = ( w[0] *x[0] +w[1] *x[1] +w[2] *x[2] +w[3] *x[3] \
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
49 + w[4] *x[4] +w[5] *x[5] +w[6] *x[6] +w[7] *x[7] \
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
50 + w[8] *x[8] +w[9] *x[9] +w[10]*x[10]+w[11]*x[11] \
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
51 + w[12]*x[12]+w[13]*x[13]+w[14]*x[14]+w[15]*x[15] ) >> 16
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
52
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
53 #endif /* Fast machine */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
54
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
55 // Macro to add data to circular que
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
56 #define ADDQUE(xi,xq,in)\
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
57 xq[xi]=xq[xi+L]=(*in);\
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
58 xi=(--xi)&(L-1);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
59
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
60
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
61
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
62 // local data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
63 typedef struct af_resample_s
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
64 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
65 int16_t* w; // Current filter weights
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
66 int16_t** xq; // Circular buffers
7580
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
67 uint32_t xi; // Index for circular buffers
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
68 uint32_t wi; // Index for w
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
69 uint32_t i; // Number of new samples to put in x queue
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
70 uint32_t dn; // Down sampling factor
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
71 uint32_t up; // Up sampling factor
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
72 } af_resample_t;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
73
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
74 // Euclids algorithm for calculating Greatest Common Divisor GCD(a,b)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
75 inline int gcd(register int a, register int b)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
76 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
77 register int r = min(a,b);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
78 a=max(a,b);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
79 b=r;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
80
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
81 r=a%b;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
82 while(r!=0){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
83 a=b;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
84 b=r;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
85 r=a%b;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
86 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
87 return b;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
88 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
89
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
90 static int upsample(af_data_t* c,af_data_t* l, af_resample_t* s)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
91 {
7580
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
92 uint32_t ci = l->nch; // Index for channels
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
93 uint32_t len = 0; // Number of input samples
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
94 uint32_t nch = l->nch; // Number of channels
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
95 uint32_t inc = s->up/s->dn;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
96 uint32_t level = s->up%s->dn;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
97 uint32_t up = s->up;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
98 uint32_t dn = s->dn;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
99
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
100 register int16_t* w = s->w;
7580
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
101 register uint32_t wi = 0;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
102 register uint32_t xi = 0;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
103
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
104 // Index current channel
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
105 while(ci--){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
106 // Temporary pointers
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
107 register int16_t* x = s->xq[ci];
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
108 register int16_t* in = ((int16_t*)c->audio)+ci;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
109 register int16_t* out = ((int16_t*)l->audio)+ci;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
110 int16_t* end = in+c->len/2; // Block loop end
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
111 wi = s->wi; xi = s->xi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
112
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
113 while(in < end){
7580
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
114 register uint32_t i = inc;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
115 if(wi<level) i++;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
116
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
117 ADDQUE(xi,x,in);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
118 in+=nch;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
119 while(i--){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
120 // Run the FIR filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
121 FIR((&x[xi]),(&w[wi*L]),out);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
122 len++; out+=nch;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
123 // Update wi to point at the correct polyphase component
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
124 wi=(wi+dn)%up;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
125 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
126 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
127 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
128 // Save values that needs to be kept for next time
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
129 s->wi = wi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
130 s->xi = xi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
131 return len;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
132 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
133
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
134 static int downsample(af_data_t* c,af_data_t* l, af_resample_t* s)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
135 {
7580
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
136 uint32_t ci = l->nch; // Index for channels
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
137 uint32_t len = 0; // Number of output samples
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
138 uint32_t nch = l->nch; // Number of channels
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
139 uint32_t inc = s->dn/s->up;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
140 uint32_t level = s->dn%s->up;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
141 uint32_t up = s->up;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
142 uint32_t dn = s->dn;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
143
7587
arpi
parents: 7580
diff changeset
144 register int32_t i = 0;
7580
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
145 register uint32_t wi = 0;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
146 register uint32_t xi = 0;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
147
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
148 // Index current channel
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
149 while(ci--){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
150 // Temporary pointers
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
151 register int16_t* x = s->xq[ci];
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
152 register int16_t* in = ((int16_t*)c->audio)+ci;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
153 register int16_t* out = ((int16_t*)l->audio)+ci;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
154 register int16_t* end = in+c->len/2; // Block loop end
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
155 i = s->i; wi = s->wi; xi = s->xi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
156
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
157 while(in < end){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
158
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
159 ADDQUE(xi,x,in);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
160 in+=nch;
7587
arpi
parents: 7580
diff changeset
161 if((--i)<=0){
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
162 // Run the FIR filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
163 FIR((&x[xi]),(&s->w[wi*L]),out);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
164 len++; out+=nch;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
165
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
166 // Update wi to point at the correct polyphase component
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
167 wi=(wi+dn)%up;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
168
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
169 // Insert i number of new samples in queue
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
170 i = inc;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
171 if(wi<level) i++;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
172 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
173 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
174 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
175 // Save values that needs to be kept for next time
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
176 s->wi = wi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
177 s->xi = xi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
178 s->i = i;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
179
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
180 return len;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
181 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
182
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
183 // Initialization and runtime control
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
184 static int control(struct af_instance_s* af, int cmd, void* arg)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
185 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
186 switch(cmd){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
187 case AF_CONTROL_REINIT:{
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
188 af_resample_t* s = (af_resample_t*)af->setup;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
189 af_data_t* n = (af_data_t*)arg; // New configureation
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
190 int i,d = 0;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
191 int rv = AF_OK;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
192
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
193 // Make sure this filter isn't redundant
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
194 if(af->data->rate == n->rate)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
195 return AF_DETACH;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
196
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
197 // Create space for circular bufers (if nesessary)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
198 if(af->data->nch != n->nch){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
199 // First free the old ones
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
200 if(s->xq){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
201 for(i=1;i<af->data->nch;i++)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
202 if(s->xq[i])
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
203 free(s->xq[i]);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
204 free(s->xq);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
205 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
206 // ... then create new
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
207 s->xq = malloc(n->nch*sizeof(int16_t*));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
208 for(i=0;i<n->nch;i++)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
209 s->xq[i] = malloc(2*L*sizeof(int16_t));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
210 s->xi = 0;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
211 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
212
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
213 // Set parameters
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
214 af->data->nch = n->nch;
7719
41e8d0916c60 Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents: 7665
diff changeset
215 af->data->format = AFMT_S16_NE;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
216 af->data->bps = 2;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
217 if(af->data->format != n->format || af->data->bps != n->bps)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
218 rv = AF_FALSE;
7719
41e8d0916c60 Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents: 7665
diff changeset
219 n->format = AFMT_S16_NE;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
220 n->bps = 2;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
221
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
222 // Calculate up and down sampling factors
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
223 d=gcd(af->data->rate,n->rate);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
224
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
225 // Check if the the design needs to be redone
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
226 if(s->up != af->data->rate/d || s->dn != n->rate/d){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
227 float* w;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
228 float* wt;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
229 float fc;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
230 int j;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
231 s->up = af->data->rate/d;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
232 s->dn = n->rate/d;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
233
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
234 // Calculate cuttof frequency for filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
235 fc = 1/(float)(max(s->up,s->dn));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
236 // Allocate space for polyphase filter bank and protptype filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
237 w = malloc(sizeof(float) * s->up *L);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
238 if(NULL != s->w)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
239 free(s->w);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
240 s->w = malloc(L*s->up*sizeof(int16_t));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
241
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
242 // Design prototype filter type using Kaiser window with beta = 10
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
243 if(NULL == w || NULL == s->w ||
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
244 -1 == design_fir(s->up*L, w, &fc, LP|KAISER , 10.0)){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
245 mp_msg(MSGT_AFILTER,MSGL_ERR,"[resample] Unable to design prototype filter.\n");
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
246 return AF_ERROR;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
247 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
248 // Copy data from prototype to polyphase filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
249 wt=w;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
250 for(j=0;j<L;j++){//Columns
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
251 for(i=0;i<s->up;i++){//Rows
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
252 float t=(float)s->up*32767.0*(*wt);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
253 s->w[i*L+j] = (int16_t)((t>=0.0)?(t+0.5):(t-0.5));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
254 wt++;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
255 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
256 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
257 free(w);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
258 mp_msg(MSGT_AFILTER,MSGL_V,"[resample] New filter designed up: %i down: %i\n", s->up, s->dn);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
259 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
260
7665
fbd5445cc853 Adding function for calculating the delay caused by the filters
anders
parents: 7616
diff changeset
261 // Set multiplier and delay
fbd5445cc853 Adding function for calculating the delay caused by the filters
anders
parents: 7616
diff changeset
262 af->delay = (double)(1000*L/2)/((double)n->rate);
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
263 af->mul.n = s->up;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
264 af->mul.d = s->dn;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
265 return rv;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
266 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
267 case AF_CONTROL_RESAMPLE:
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
268 // Reinit must be called after this function has been called
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
269
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
270 // Sanity check
7616
d09c125a88fa Changing frequency limit for resampling
anders
parents: 7615
diff changeset
271 if(((int*)arg)[0] < 8000 || ((int*)arg)[0] > 192000){
d09c125a88fa Changing frequency limit for resampling
anders
parents: 7615
diff changeset
272 mp_msg(MSGT_AFILTER,MSGL_ERR,"[resample] The output sample frequency must be between 8kHz and 192kHz. Current value is %i \n",((int*)arg)[0]);
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
273 return AF_ERROR;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
274 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
275
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
276 af->data->rate=((int*)arg)[0];
7571
8819fdf88b5d Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents: 7568
diff changeset
277 mp_msg(MSGT_AFILTER,MSGL_V,"[resample] Changing sample rate to %iHz\n",af->data->rate);
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
278 return AF_OK;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
279 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
280 return AF_UNKNOWN;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
281 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
282
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
283 // Deallocate memory
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
284 static void uninit(struct af_instance_s* af)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
285 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
286 if(af->data)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
287 free(af->data);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
288 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
289
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
290 // Filter data through filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
291 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
292 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
293 int len = 0; // Length of output data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
294 af_data_t* c = data; // Current working data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
295 af_data_t* l = af->data; // Local data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
296 af_resample_t* s = (af_resample_t*)af->setup;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
297
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
298 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
299 return NULL;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
300
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
301 // Run resampling
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
302 if(s->up>s->dn)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
303 len = upsample(c,l,s);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
304 else
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
305 len = downsample(c,l,s);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
306
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
307 // Set output data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
308 c->audio = l->audio;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
309 c->len = len*2;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
310 c->rate = l->rate;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
311
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
312 return c;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
313 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
314
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
315 // Allocate memory and set function pointers
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
316 static int open(af_instance_t* af){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
317 af->control=control;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
318 af->uninit=uninit;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
319 af->play=play;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
320 af->mul.n=1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
321 af->mul.d=1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
322 af->data=calloc(1,sizeof(af_data_t));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
323 af->setup=calloc(1,sizeof(af_resample_t));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
324 if(af->data == NULL || af->setup == NULL)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
325 return AF_ERROR;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
326 return AF_OK;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
327 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
328
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
329 // Description of this plugin
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
330 af_info_t af_info_resample = {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
331 "Sample frequency conversion",
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
332 "resample",
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
333 "Anders",
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
334 "",
7615
c67328dd459a Adding Support for non-reentrant audio filters
anders
parents: 7602
diff changeset
335 AF_FLAGS_REENTRANT,
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
336 open
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
337 };
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
338