Mercurial > mplayer.hg
annotate libaf/af_channels.c @ 17478:7d60fe925f87
vglobal so the user can tell the encoder that she wants global headers ... one step closer to correct mov/mp4 muxing
author | michael |
---|---|
date | Tue, 24 Jan 2006 23:50:41 +0000 |
parents | 95bb94a930a3 |
children | ecf562795caf |
rev | line source |
---|---|
7568 | 1 /* Audio filter that adds and removes channels, according to the |
2 command line parameter channels. It is stupid and can only add | |
3 silence or copy channels not mix or filter. | |
4 */ | |
5 #include <stdio.h> | |
6 #include <stdlib.h> | |
7 #include <string.h> | |
7570 | 8 #include <unistd.h> |
7975 | 9 #include <inttypes.h> |
7568 | 10 |
11 #include "af.h" | |
12 | |
8607 | 13 #define FR 0 |
14 #define TO 1 | |
15 | |
16 typedef struct af_channels_s{ | |
17 int route[AF_NCH][2]; | |
18 int nr; | |
19 int router; | |
20 }af_channels_t; | |
21 | |
7568 | 22 // Local function for copying data |
8711
906f7a2dc085
sig 11 fix in reinit and resample + spelling error fixes
anders
parents:
8607
diff
changeset
|
23 static void copy(void* in, void* out, int ins, int inos,int outs, int outos, int len, int bps) |
7568 | 24 { |
25 switch(bps){ | |
26 case 1:{ | |
27 int8_t* tin = (int8_t*)in; | |
28 int8_t* tout = (int8_t*)out; | |
29 tin += inos; | |
30 tout += outos; | |
31 len = len/ins; | |
32 while(len--){ | |
33 *tout=*tin; | |
34 tin +=ins; | |
35 tout+=outs; | |
36 } | |
37 break; | |
38 } | |
39 case 2:{ | |
40 int16_t* tin = (int16_t*)in; | |
41 int16_t* tout = (int16_t*)out; | |
42 tin += inos; | |
43 tout += outos; | |
44 len = len/(2*ins); | |
45 while(len--){ | |
46 *tout=*tin; | |
47 tin +=ins; | |
48 tout+=outs; | |
49 } | |
50 break; | |
51 } | |
12913 | 52 case 3:{ |
53 int8_t* tin = (int8_t*)in; | |
54 int8_t* tout = (int8_t*)out; | |
55 tin += 3 * inos; | |
56 tout += 3 * outos; | |
57 len = len / ( 3 * ins); | |
58 while (len--) { | |
59 tout[0] = tin[0]; | |
60 tout[1] = tin[1]; | |
61 tout[2] = tin[2]; | |
62 tin += 3 * ins; | |
63 tout += 3 * outs; | |
64 } | |
65 break; | |
66 } | |
7568 | 67 case 4:{ |
68 int32_t* tin = (int32_t*)in; | |
69 int32_t* tout = (int32_t*)out; | |
70 tin += inos; | |
71 tout += outos; | |
72 len = len/(4*ins); | |
73 while(len--){ | |
74 *tout=*tin; | |
75 tin +=ins; | |
76 tout+=outs; | |
77 } | |
78 break; | |
79 } | |
80 case 8:{ | |
81 int64_t* tin = (int64_t*)in; | |
82 int64_t* tout = (int64_t*)out; | |
83 tin += inos; | |
84 tout += outos; | |
85 len = len/(8*ins); | |
86 while(len--){ | |
87 *tout=*tin; | |
88 tin +=ins; | |
89 tout+=outs; | |
90 } | |
91 break; | |
92 } | |
93 default: | |
8607 | 94 af_msg(AF_MSG_ERROR,"[channels] Unsupported number of bytes/sample: %i" |
95 " please report this error on the MPlayer mailing list. \n",bps); | |
7568 | 96 } |
97 } | |
98 | |
8607 | 99 // Make sure the routes are sane |
100 static int check_routes(af_channels_t* s, int nin, int nout) | |
101 { | |
102 int i; | |
103 if((s->nr < 1) || (s->nr > AF_NCH)){ | |
8711
906f7a2dc085
sig 11 fix in reinit and resample + spelling error fixes
anders
parents:
8607
diff
changeset
|
104 af_msg(AF_MSG_ERROR,"[channels] The number of routing pairs must be" |
8607 | 105 " between 1 and %i. Current value is %i\n",AF_NCH,s->nr); |
106 return AF_ERROR; | |
107 } | |
108 | |
109 for(i=0;i<s->nr;i++){ | |
110 if((s->route[i][FR] >= nin) || (s->route[i][TO] >= nout)){ | |
111 af_msg(AF_MSG_ERROR,"[channels] Invalid routing in pair nr. %i.\n", i); | |
112 return AF_ERROR; | |
113 } | |
114 } | |
115 return AF_OK; | |
116 } | |
117 | |
7568 | 118 // Initialization and runtime control |
119 static int control(struct af_instance_s* af, int cmd, void* arg) | |
120 { | |
8607 | 121 af_channels_t* s = af->setup; |
7568 | 122 switch(cmd){ |
123 case AF_CONTROL_REINIT: | |
8607 | 124 |
125 // Set default channel assignment | |
126 if(!s->router){ | |
127 int i; | |
128 // Make sure this filter isn't redundant | |
129 if(af->data->nch == ((af_data_t*)arg)->nch) | |
130 return AF_DETACH; | |
131 | |
132 // If mono: fake stereo | |
133 if(((af_data_t*)arg)->nch == 1){ | |
134 s->nr = min(af->data->nch,2); | |
135 for(i=0;i<s->nr;i++){ | |
136 s->route[i][FR] = 0; | |
137 s->route[i][TO] = i; | |
138 } | |
139 } | |
140 else{ | |
141 s->nr = min(af->data->nch, ((af_data_t*)arg)->nch); | |
142 for(i=0;i<s->nr;i++){ | |
143 s->route[i][FR] = i; | |
144 s->route[i][TO] = i; | |
145 } | |
146 } | |
147 } | |
7568 | 148 |
149 af->data->rate = ((af_data_t*)arg)->rate; | |
150 af->data->format = ((af_data_t*)arg)->format; | |
151 af->data->bps = ((af_data_t*)arg)->bps; | |
152 af->mul.n = af->data->nch; | |
153 af->mul.d = ((af_data_t*)arg)->nch; | |
14433
95bb94a930a3
always cancel down fractions (frac_t) to avoid overflows and playback
reimar
parents:
12913
diff
changeset
|
154 af_frac_cancel(&af->mul); |
8607 | 155 return check_routes(s,((af_data_t*)arg)->nch,af->data->nch); |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7975
diff
changeset
|
156 case AF_CONTROL_COMMAND_LINE:{ |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7975
diff
changeset
|
157 int nch = 0; |
8607 | 158 int n = 0; |
159 // Check number of channels and number of routing pairs | |
160 sscanf(arg, "%i:%i%n", &nch, &s->nr, &n); | |
161 | |
162 // If router scan commandline for routing pairs | |
163 if(s->nr){ | |
164 char* cp = &((char*)arg)[n]; | |
165 int ch = 0; | |
166 // Sanity check | |
167 if((s->nr < 1) || (s->nr > AF_NCH)){ | |
8711
906f7a2dc085
sig 11 fix in reinit and resample + spelling error fixes
anders
parents:
8607
diff
changeset
|
168 af_msg(AF_MSG_ERROR,"[channels] The number of routing pairs must be" |
8607 | 169 " between 1 and %i. Current value is %i\n",AF_NCH,s->nr); |
170 } | |
171 s->router = 1; | |
172 // Scan for pairs on commandline | |
173 while((*cp == ':') && (ch < s->nr)){ | |
174 sscanf(cp, ":%i:%i%n" ,&s->route[ch][FR], &s->route[ch][TO], &n); | |
8711
906f7a2dc085
sig 11 fix in reinit and resample + spelling error fixes
anders
parents:
8607
diff
changeset
|
175 af_msg(AF_MSG_VERBOSE,"[channels] Routing from channel %i to" |
8607 | 176 " channel %i\n",s->route[ch][FR],s->route[ch][TO]); |
177 cp = &cp[n]; | |
178 ch++; | |
179 } | |
180 } | |
181 | |
182 if(AF_OK != af->control(af,AF_CONTROL_CHANNELS | AF_CONTROL_SET ,&nch)) | |
183 return AF_ERROR; | |
184 return AF_OK; | |
185 } | |
186 case AF_CONTROL_CHANNELS | AF_CONTROL_SET: | |
7568 | 187 // Reinit must be called after this function has been called |
188 | |
189 // Sanity check | |
12008 | 190 if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > AF_NCH){ |
8607 | 191 af_msg(AF_MSG_ERROR,"[channels] The number of output channels must be" |
192 " between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]); | |
7568 | 193 return AF_ERROR; |
194 } | |
195 | |
196 af->data->nch=((int*)arg)[0]; | |
8607 | 197 if(!s->router) |
198 af_msg(AF_MSG_VERBOSE,"[channels] Changing number of channels" | |
199 " to %i\n",af->data->nch); | |
200 return AF_OK; | |
201 case AF_CONTROL_CHANNELS | AF_CONTROL_GET: | |
202 *(int*)arg = af->data->nch; | |
203 return AF_OK; | |
204 case AF_CONTROL_CHANNELS_ROUTING | AF_CONTROL_SET:{ | |
205 int ch = ((af_control_ext_t*)arg)->ch; | |
206 int* route = ((af_control_ext_t*)arg)->arg; | |
207 s->route[ch][FR] = route[FR]; | |
208 s->route[ch][TO] = route[TO]; | |
209 return AF_OK; | |
210 } | |
211 case AF_CONTROL_CHANNELS_ROUTING | AF_CONTROL_GET:{ | |
212 int ch = ((af_control_ext_t*)arg)->ch; | |
213 int* route = ((af_control_ext_t*)arg)->arg; | |
214 route[FR] = s->route[ch][FR]; | |
215 route[TO] = s->route[ch][TO]; | |
216 return AF_OK; | |
217 } | |
218 case AF_CONTROL_CHANNELS_NR | AF_CONTROL_SET: | |
219 s->nr = *(int*)arg; | |
220 return AF_OK; | |
221 case AF_CONTROL_CHANNELS_NR | AF_CONTROL_GET: | |
222 *(int*)arg = s->nr; | |
223 return AF_OK; | |
224 case AF_CONTROL_CHANNELS_ROUTER | AF_CONTROL_SET: | |
225 s->router = *(int*)arg; | |
226 return AF_OK; | |
227 case AF_CONTROL_CHANNELS_ROUTER | AF_CONTROL_GET: | |
228 *(int*)arg = s->router; | |
7568 | 229 return AF_OK; |
230 } | |
231 return AF_UNKNOWN; | |
232 } | |
233 | |
234 // Deallocate memory | |
235 static void uninit(struct af_instance_s* af) | |
236 { | |
8607 | 237 if(af->setup) |
238 free(af->setup); | |
7568 | 239 if(af->data) |
240 free(af->data); | |
241 } | |
242 | |
243 // Filter data through filter | |
244 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
245 { | |
8607 | 246 af_data_t* c = data; // Current working data |
247 af_data_t* l = af->data; // Local data | |
248 af_channels_t* s = af->setup; | |
249 int i; | |
250 | |
7568 | 251 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) |
252 return NULL; | |
253 | |
8607 | 254 // Reset unused channels |
255 memset(l->audio,0,(c->len*af->mul.n)/af->mul.d); | |
7568 | 256 |
8607 | 257 if(AF_OK == check_routes(s,c->nch,l->nch)) |
258 for(i=0;i<s->nr;i++) | |
259 copy(c->audio,l->audio,c->nch,s->route[i][FR], | |
260 l->nch,s->route[i][TO],c->len,c->bps); | |
7568 | 261 |
262 // Set output data | |
263 c->audio = l->audio; | |
7590 | 264 c->len = (c->len*af->mul.n)/af->mul.d; |
7568 | 265 c->nch = l->nch; |
266 | |
267 return c; | |
268 } | |
269 | |
270 // Allocate memory and set function pointers | |
271 static int open(af_instance_t* af){ | |
272 af->control=control; | |
273 af->uninit=uninit; | |
274 af->play=play; | |
275 af->mul.n=1; | |
276 af->mul.d=1; | |
277 af->data=calloc(1,sizeof(af_data_t)); | |
8607 | 278 af->setup=calloc(1,sizeof(af_channels_t)); |
279 if((af->data == NULL) || (af->setup == NULL)) | |
7568 | 280 return AF_ERROR; |
281 return AF_OK; | |
282 } | |
283 | |
284 // Description of this filter | |
285 af_info_t af_info_channels = { | |
286 "Insert or remove channels", | |
287 "channels", | |
288 "Anders", | |
289 "", | |
7615 | 290 AF_FLAGS_REENTRANT, |
7568 | 291 open |
292 }; |