Mercurial > mplayer.hg
annotate libao2/ao_plugin.c @ 5730:466c7adbb3ce
mpegaudio->mp2+mp3 renamed in ffmpeg
author | arpi |
---|---|
date | Sat, 20 Apr 2002 13:34:54 +0000 |
parents | b6d83ac1e3e0 |
children | 4c6b5ab80de2 |
rev | line source |
---|---|
3096 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 | |
4 #include "../config.h" | |
5 | |
3194
1648d11fc36c
commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents:
3107
diff
changeset
|
6 #include "afmt.h" |
3096 | 7 #include "audio_out.h" |
8 #include "audio_out_internal.h" | |
9 | |
10 #include "audio_plugin.h" | |
11 | |
12 static ao_info_t info = | |
13 { | |
14 "Plugin audio output", | |
15 "plugin", | |
16 "Anders", | |
17 "" | |
18 }; | |
19 | |
20 LIBAO_EXTERN(plugin) | |
21 | |
3107 | 22 #define plugin(i) (ao_plugin_local_data.plugins[i]) |
23 #define driver() (ao_plugin_local_data.driver) | |
3096 | 24 |
3107 | 25 // local data |
3096 | 26 typedef struct ao_plugin_local_data_s |
27 { | |
4313 | 28 void* buf; // Output data buffer |
29 int len; // Amount of data in buffer | |
30 float bps; // Bytes per second out | |
31 ao_functions_t* driver; // Output driver | |
3107 | 32 ao_plugin_functions_t** plugins; // List of used plugins |
3194
1648d11fc36c
commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents:
3107
diff
changeset
|
33 ao_plugin_functions_t* available_plugins[NPL]; // List of available plugins |
3096 | 34 } ao_plugin_local_data_t; |
35 | |
4313 | 36 static ao_plugin_local_data_t ao_plugin_local_data={NULL,0,0.0,NULL,NULL,AO_PLUGINS}; |
3096 | 37 |
4313 | 38 // global data |
4896 | 39 volatile ao_plugin_data_t ao_plugin_data; // Data used by the plugins |
4859 | 40 ao_plugin_cfg_t ao_plugin_cfg=CFG_DEFAULTS; // Set in cfg-mplayer.h |
3096 | 41 |
42 // to set/get/query special features/parameters | |
43 static int control(int cmd,int arg){ | |
3107 | 44 switch(cmd){ |
45 case AOCONTROL_SET_PLUGIN_DRIVER: | |
46 ao_plugin_local_data.driver=(ao_functions_t*)arg; | |
47 return CONTROL_OK; | |
4859 | 48 case AOCONTROL_GET_VOLUME: |
49 case AOCONTROL_SET_VOLUME: | |
50 { | |
51 int r=audio_plugin_volume.control(cmd,arg); | |
52 if(CONTROL_OK != r) | |
53 return driver()->control(cmd,arg); | |
54 else | |
55 return r; | |
56 } | |
3107 | 57 default: |
58 return driver()->control(cmd,arg); | |
59 } | |
60 return CONTROL_UNKNOWN; | |
61 } | |
62 | |
63 // Recursive function for adding plugins | |
64 // return 1 for success and 0 for error | |
65 int add_plugin(int i,char* cfg){ | |
66 int cnt=0; | |
67 // Find end of plugin name | |
68 while((cfg[cnt]!=',')&&(cfg[cnt]!='\0')&&(cnt<100)) cnt++; | |
69 if(cnt >= 100) | |
70 return 0; | |
71 | |
3680 | 72 // Is this the last iteration or just another plugin |
3107 | 73 if(cfg[cnt]=='\0'){ |
74 ao_plugin_local_data.plugins=malloc((i+1)*sizeof(ao_plugin_functions_t*)); | |
75 if(ao_plugin_local_data.plugins){ | |
76 ao_plugin_local_data.plugins[i+1]=NULL; | |
77 // Find the plugin matching the cfg string name | |
78 cnt=0; | |
79 while(ao_plugin_local_data.available_plugins[cnt] && cnt<20){ | |
80 if(0==strcmp(ao_plugin_local_data.available_plugins[cnt]->info->short_name,cfg)){ | |
81 ao_plugin_local_data.plugins[i]=ao_plugin_local_data.available_plugins[cnt]; | |
82 return 1; | |
83 } | |
84 cnt++; | |
85 } | |
86 printf("[plugin]: Invalid plugin: %s \n",cfg); | |
87 return 0; | |
88 } | |
89 else | |
90 return 0; | |
91 } else { | |
92 cfg[cnt]='\0'; | |
93 if(add_plugin(i+1,&cfg[cnt+1])){ | |
94 cnt=0; | |
95 // Find the plugin matching the cfg string name | |
96 while(ao_plugin_local_data.available_plugins[cnt] && cnt < 20){ | |
97 if(0==strcmp(ao_plugin_local_data.available_plugins[cnt]->info->short_name,cfg)){ | |
98 ao_plugin_local_data.plugins[i]=ao_plugin_local_data.available_plugins[cnt]; | |
99 return 1; | |
100 } | |
101 cnt++; | |
102 } | |
103 printf("[plugin]: Invalid plugin: %s \n",cfg); | |
104 return 0; | |
105 } | |
106 else | |
107 return 0; | |
108 } | |
109 return 0; // Will never happen... | |
3096 | 110 } |
111 | |
112 // open & setup audio device and plugins | |
113 // return: 1=success 0=fail | |
114 static int init(int rate,int channels,int format,int flags){ | |
115 int ok=1; | |
116 | |
4313 | 117 // Create list of plugins from cfg option |
3096 | 118 int i=0; |
3194
1648d11fc36c
commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents:
3107
diff
changeset
|
119 if(ao_plugin_cfg.plugin_list){ |
1648d11fc36c
commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents:
3107
diff
changeset
|
120 if(!add_plugin(i,ao_plugin_cfg.plugin_list)) |
3107 | 121 return 0; |
122 } | |
3096 | 123 |
124 /* Set input parameters and itterate through plugins each plugin | |
125 changes the parameters according to its output */ | |
126 ao_plugin_data.rate=rate; | |
127 ao_plugin_data.channels=channels; | |
128 ao_plugin_data.format=format; | |
129 ao_plugin_data.sz_mult=1; | |
130 ao_plugin_data.sz_fix=0; | |
131 ao_plugin_data.delay_mult=1; | |
132 ao_plugin_data.delay_fix=0; | |
133 i=0; | |
134 while(plugin(i)&&ok) | |
135 ok=plugin(i++)->init(); | |
136 | |
137 if(!ok) return 0; | |
138 | |
4313 | 139 // Calculate bps |
140 ao_plugin_local_data.bps=(float)(ao_plugin_data.rate * | |
141 ao_plugin_data.channels); | |
142 | |
143 if(ao_plugin_data.format == AFMT_S16_LE || | |
144 ao_plugin_data.format == AFMT_S16_BE || | |
145 ao_plugin_data.format == AFMT_U16_LE || | |
146 ao_plugin_data.format == AFMT_U16_BE) | |
147 ao_plugin_local_data.bps *= 2; | |
148 | |
149 if(ao_plugin_data.format == AFMT_S32_LE || | |
150 ao_plugin_data.format == AFMT_S32_BE) | |
151 ao_plugin_local_data.bps *= 4; | |
152 | |
3107 | 153 // This should never happen but check anyway |
154 if(NULL==ao_plugin_local_data.driver) | |
155 return 0; | |
156 | |
3096 | 157 ok = driver()->init(ao_plugin_data.rate, |
3107 | 158 ao_plugin_data.channels, |
159 ao_plugin_data.format, | |
160 flags); | |
3096 | 161 if(!ok) return 0; |
162 | |
163 /* Now that the driver is initialized we can calculate and set the | |
164 input and output buffers for each plugin */ | |
165 ao_plugin_data.len=driver()->get_space(); | |
3107 | 166 while((i>0) && ok) |
3194
1648d11fc36c
commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents:
3107
diff
changeset
|
167 ok=plugin(--i)->control(AOCONTROL_PLUGIN_SET_LEN,0); |
3107 | 168 |
169 if(!ok) return 0; | |
3096 | 170 |
4374
0a95c5074c50
Fixed sig 11 caused by resampling plugin, some cosmetic changes and speed improvements
anders
parents:
4313
diff
changeset
|
171 // Allocate output buffer |
4313 | 172 if(ao_plugin_local_data.buf) |
173 free(ao_plugin_local_data.buf); | |
174 ao_plugin_local_data.buf=malloc(MAX_OUTBURST); | |
175 | |
176 if(!ao_plugin_local_data.buf) return 0; | |
177 | |
3096 | 178 return 1; |
179 } | |
180 | |
181 // close audio device | |
182 static void uninit(){ | |
183 int i=0; | |
184 driver()->uninit(); | |
185 while(plugin(i)) | |
186 plugin(i++)->uninit(); | |
3107 | 187 if(ao_plugin_local_data.plugins) |
188 free(ao_plugin_local_data.plugins); | |
5050 | 189 ao_plugin_local_data.plugins=NULL; |
4313 | 190 if(ao_plugin_local_data.buf) |
191 free(ao_plugin_local_data.buf); | |
5050 | 192 ao_plugin_local_data.buf=NULL; |
3096 | 193 } |
194 | |
195 // stop playing and empty buffers (for seeking/pause) | |
196 static void reset(){ | |
197 int i=0; | |
198 driver()->reset(); | |
199 while(plugin(i)) | |
200 plugin(i++)->reset(); | |
4313 | 201 ao_plugin_local_data.len=0; |
3096 | 202 } |
203 | |
204 // stop playing, keep buffers (for pause) | |
205 static void audio_pause(){ | |
206 driver()->pause(); | |
207 } | |
208 | |
209 // resume playing, after audio_pause() | |
210 static void audio_resume(){ | |
211 driver()->resume(); | |
212 } | |
213 | |
214 // return: how many bytes can be played without blocking | |
215 static int get_space(){ | |
216 double sz=(double)(driver()->get_space()); | |
4313 | 217 if(sz+(double)ao_plugin_local_data.len > (double)MAX_OUTBURST) |
218 sz=(double)MAX_OUTBURST-(double)ao_plugin_local_data.len; | |
3096 | 219 sz*=ao_plugin_data.sz_mult; |
220 sz+=ao_plugin_data.sz_fix; | |
221 return (int)(sz); | |
222 } | |
223 | |
224 // plays 'len' bytes of 'data' | |
225 // return: number of bytes played | |
226 static int play(void* data,int len,int flags){ | |
4313 | 227 int l,i=0; |
228 // Limit length to avoid over flow in plugins | |
4922 | 229 int tmp = get_space(); |
3096 | 230 int ret_len =(tmp<len)?tmp:len; |
4375 | 231 if(ret_len){ |
232 // Filter data | |
233 ao_plugin_data.len=ret_len; | |
234 ao_plugin_data.data=data; | |
4376 | 235 while(plugin(i)) |
4375 | 236 plugin(i++)->play(); |
237 // Copy data to output buffer | |
238 memcpy(ao_plugin_local_data.buf+ao_plugin_local_data.len, | |
239 ao_plugin_data.data,ao_plugin_data.len); | |
240 // Send data to output | |
241 l=driver()->play(ao_plugin_local_data.buf, | |
242 ao_plugin_data.len+ao_plugin_local_data.len,flags); | |
243 // Save away unsent data | |
244 ao_plugin_local_data.len=ao_plugin_data.len+ao_plugin_local_data.len-l; | |
245 memcpy(ao_plugin_local_data.buf,ao_plugin_local_data.buf+l, | |
246 ao_plugin_local_data.len); | |
247 } | |
3096 | 248 return ret_len; |
249 } | |
250 | |
251 // return: delay in seconds between first and last sample in buffer | |
252 static float get_delay(){ | |
253 float delay=driver()->get_delay(); | |
4313 | 254 delay+=(float)ao_plugin_local_data.len/ao_plugin_local_data.bps; |
3096 | 255 delay*=ao_plugin_data.delay_mult; |
256 delay+=ao_plugin_data.delay_fix; | |
257 return delay; | |
258 } | |
259 | |
260 | |
3279
d6ea11bed983
Commandline interface to ao_plugin updated according to mplayers complex parameter format and plugin pl_format finished (alpha code needs testing)
anders
parents:
3194
diff
changeset
|
261 |
d6ea11bed983
Commandline interface to ao_plugin updated according to mplayers complex parameter format and plugin pl_format finished (alpha code needs testing)
anders
parents:
3194
diff
changeset
|
262 |