audio: add af_lavrresample, remove old resampling filters
[mplayer2.git] / libaf / af_channels.c
blobb42cde380a93db62795df532f4b281a70190fafe
1 /*
2 * Audio filter that adds and removes channels, according to the
3 * command line parameter channels. It is stupid and can only add
4 * silence or copy channels, not mix or filter.
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
28 #include "af.h"
30 #define FR 0
31 #define TO 1
33 typedef struct af_channels_s{
34 int route[AF_NCH][2];
35 int nr;
36 int router;
37 }af_channels_t;
39 // Local function for copying data
40 static void copy(void* in, void* out, int ins, int inos,int outs, int outos, int len, int bps)
42 switch(bps){
43 case 1:{
44 int8_t* tin = (int8_t*)in;
45 int8_t* tout = (int8_t*)out;
46 tin += inos;
47 tout += outos;
48 len = len/ins;
49 while(len--){
50 *tout=*tin;
51 tin +=ins;
52 tout+=outs;
54 break;
56 case 2:{
57 int16_t* tin = (int16_t*)in;
58 int16_t* tout = (int16_t*)out;
59 tin += inos;
60 tout += outos;
61 len = len/(2*ins);
62 while(len--){
63 *tout=*tin;
64 tin +=ins;
65 tout+=outs;
67 break;
69 case 3:{
70 int8_t* tin = (int8_t*)in;
71 int8_t* tout = (int8_t*)out;
72 tin += 3 * inos;
73 tout += 3 * outos;
74 len = len / ( 3 * ins);
75 while (len--) {
76 tout[0] = tin[0];
77 tout[1] = tin[1];
78 tout[2] = tin[2];
79 tin += 3 * ins;
80 tout += 3 * outs;
82 break;
84 case 4:{
85 int32_t* tin = (int32_t*)in;
86 int32_t* tout = (int32_t*)out;
87 tin += inos;
88 tout += outos;
89 len = len/(4*ins);
90 while(len--){
91 *tout=*tin;
92 tin +=ins;
93 tout+=outs;
95 break;
97 case 8:{
98 int64_t* tin = (int64_t*)in;
99 int64_t* tout = (int64_t*)out;
100 tin += inos;
101 tout += outos;
102 len = len/(8*ins);
103 while(len--){
104 *tout=*tin;
105 tin +=ins;
106 tout+=outs;
108 break;
110 default:
111 mp_msg(MSGT_AFILTER, MSGL_ERR, "[channels] Unsupported number of bytes/sample: %i"
112 " please report this error on the MPlayer mailing list. \n",bps);
116 // Make sure the routes are sane
117 static int check_routes(af_channels_t* s, int nin, int nout)
119 int i;
120 if((s->nr < 1) || (s->nr > AF_NCH)){
121 mp_msg(MSGT_AFILTER, MSGL_ERR, "[channels] The number of routing pairs must be"
122 " between 1 and %i. Current value is %i\n",AF_NCH,s->nr);
123 return AF_ERROR;
126 for(i=0;i<s->nr;i++){
127 if((s->route[i][FR] >= nin) || (s->route[i][TO] >= nout)){
128 mp_msg(MSGT_AFILTER, MSGL_ERR, "[channels] Invalid routing in pair nr. %i.\n", i);
129 return AF_ERROR;
132 return AF_OK;
135 // Initialization and runtime control
136 static int control(struct af_instance_s* af, int cmd, void* arg)
138 af_channels_t* s = af->setup;
139 switch(cmd){
140 case AF_CONTROL_REINIT:
142 // Set default channel assignment
143 if(!s->router){
144 int i;
145 // Make sure this filter isn't redundant
146 if(af->data->nch == ((af_data_t*)arg)->nch)
147 return AF_DETACH;
149 // If mono: fake stereo
150 if(((af_data_t*)arg)->nch == 1){
151 s->nr = min(af->data->nch,2);
152 for(i=0;i<s->nr;i++){
153 s->route[i][FR] = 0;
154 s->route[i][TO] = i;
157 else{
158 s->nr = min(af->data->nch, ((af_data_t*)arg)->nch);
159 for(i=0;i<s->nr;i++){
160 s->route[i][FR] = i;
161 s->route[i][TO] = i;
166 af->data->rate = ((af_data_t*)arg)->rate;
167 af->data->format = ((af_data_t*)arg)->format;
168 af->data->bps = ((af_data_t*)arg)->bps;
169 af->mul = (double)af->data->nch / ((af_data_t*)arg)->nch;
170 return check_routes(s,((af_data_t*)arg)->nch,af->data->nch);
171 case AF_CONTROL_COMMAND_LINE:{
172 int nch = 0;
173 int n = 0;
174 // Check number of channels and number of routing pairs
175 sscanf(arg, "%i:%i%n", &nch, &s->nr, &n);
177 // If router scan commandline for routing pairs
178 if(s->nr){
179 char* cp = &((char*)arg)[n];
180 int ch = 0;
181 // Sanity check
182 if((s->nr < 1) || (s->nr > AF_NCH)){
183 mp_msg(MSGT_AFILTER, MSGL_ERR, "[channels] The number of routing pairs must be"
184 " between 1 and %i. Current value is %i\n",AF_NCH,s->nr);
186 s->router = 1;
187 // Scan for pairs on commandline
188 while((*cp == ':') && (ch < s->nr)){
189 sscanf(cp, ":%i:%i%n" ,&s->route[ch][FR], &s->route[ch][TO], &n);
190 mp_msg(MSGT_AFILTER, MSGL_V, "[channels] Routing from channel %i to"
191 " channel %i\n",s->route[ch][FR],s->route[ch][TO]);
192 cp = &cp[n];
193 ch++;
197 if(AF_OK != af->control(af,AF_CONTROL_CHANNELS | AF_CONTROL_SET ,&nch))
198 return AF_ERROR;
199 return AF_OK;
201 case AF_CONTROL_CHANNELS | AF_CONTROL_SET:
202 // Reinit must be called after this function has been called
204 // Sanity check
205 if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > AF_NCH){
206 mp_msg(MSGT_AFILTER, MSGL_ERR, "[channels] The number of output channels must be"
207 " between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]);
208 return AF_ERROR;
211 af->data->nch=((int*)arg)[0];
212 if(!s->router)
213 mp_msg(MSGT_AFILTER, MSGL_V, "[channels] Changing number of channels"
214 " to %i\n",af->data->nch);
215 return AF_OK;
216 case AF_CONTROL_CHANNELS | AF_CONTROL_GET:
217 *(int*)arg = af->data->nch;
218 return AF_OK;
219 case AF_CONTROL_CHANNELS_ROUTING | AF_CONTROL_SET:{
220 int ch = ((af_control_ext_t*)arg)->ch;
221 int* route = ((af_control_ext_t*)arg)->arg;
222 s->route[ch][FR] = route[FR];
223 s->route[ch][TO] = route[TO];
224 return AF_OK;
226 case AF_CONTROL_CHANNELS_ROUTING | AF_CONTROL_GET:{
227 int ch = ((af_control_ext_t*)arg)->ch;
228 int* route = ((af_control_ext_t*)arg)->arg;
229 route[FR] = s->route[ch][FR];
230 route[TO] = s->route[ch][TO];
231 return AF_OK;
233 case AF_CONTROL_CHANNELS_NR | AF_CONTROL_SET:
234 s->nr = *(int*)arg;
235 return AF_OK;
236 case AF_CONTROL_CHANNELS_NR | AF_CONTROL_GET:
237 *(int*)arg = s->nr;
238 return AF_OK;
239 case AF_CONTROL_CHANNELS_ROUTER | AF_CONTROL_SET:
240 s->router = *(int*)arg;
241 return AF_OK;
242 case AF_CONTROL_CHANNELS_ROUTER | AF_CONTROL_GET:
243 *(int*)arg = s->router;
244 return AF_OK;
246 return AF_UNKNOWN;
249 // Deallocate memory
250 static void uninit(struct af_instance_s* af)
252 free(af->setup);
253 if (af->data)
254 free(af->data->audio);
255 free(af->data);
258 // Filter data through filter
259 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
261 af_data_t* c = data; // Current working data
262 af_data_t* l = af->data; // Local data
263 af_channels_t* s = af->setup;
264 int i;
266 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
267 return NULL;
269 // Reset unused channels
270 memset(l->audio,0,c->len / c->nch * l->nch);
272 if(AF_OK == check_routes(s,c->nch,l->nch))
273 for(i=0;i<s->nr;i++)
274 copy(c->audio,l->audio,c->nch,s->route[i][FR],
275 l->nch,s->route[i][TO],c->len,c->bps);
277 // Set output data
278 c->audio = l->audio;
279 c->len = c->len / c->nch * l->nch;
280 c->nch = l->nch;
282 return c;
285 // Allocate memory and set function pointers
286 static int af_open(af_instance_t* af){
287 af->control=control;
288 af->uninit=uninit;
289 af->play=play;
290 af->mul=1;
291 af->data=calloc(1,sizeof(af_data_t));
292 af->setup=calloc(1,sizeof(af_channels_t));
293 if((af->data == NULL) || (af->setup == NULL))
294 return AF_ERROR;
295 return AF_OK;
298 // Description of this filter
299 af_info_t af_info_channels = {
300 "Insert or remove channels",
301 "channels",
302 "Anders",
304 AF_FLAGS_REENTRANT,
305 af_open